@agentbadge/circle-payments 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +203 -0
- package/dist/balance.d.ts +33 -0
- package/dist/balance.js +72 -0
- package/dist/chains.d.ts +63 -0
- package/dist/chains.js +41 -0
- package/dist/escrow/commission.d.ts +37 -0
- package/dist/escrow/commission.js +52 -0
- package/dist/escrow/erc8183.d.ts +288 -0
- package/dist/escrow/erc8183.js +318 -0
- package/dist/escrow/evaluator.d.ts +40 -0
- package/dist/escrow/evaluator.js +87 -0
- package/dist/history.d.ts +42 -0
- package/dist/history.js +52 -0
- package/dist/identity/erc8004.d.ts +227 -0
- package/dist/identity/erc8004.js +284 -0
- package/dist/identity/verify-passport.d.ts +17 -0
- package/dist/identity/verify-passport.js +64 -0
- package/dist/identity.d.ts +14 -0
- package/dist/identity.js +38 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +19 -0
- package/dist/ledger.d.ts +18 -0
- package/dist/ledger.js +39 -0
- package/dist/memo/memo.d.ts +100 -0
- package/dist/memo/memo.js +106 -0
- package/dist/middleware.d.ts +26 -0
- package/dist/middleware.js +104 -0
- package/dist/pricing.d.ts +9 -0
- package/dist/pricing.js +25 -0
- package/dist/router.d.ts +30 -0
- package/dist/router.js +72 -0
- package/dist/schemes/arc-self-settle.d.ts +29 -0
- package/dist/schemes/arc-self-settle.js +114 -0
- package/dist/schemes/exact.d.ts +22 -0
- package/dist/schemes/exact.js +71 -0
- package/dist/schemes/gateway.d.ts +22 -0
- package/dist/schemes/gateway.js +76 -0
- package/dist/status.d.ts +32 -0
- package/dist/status.js +86 -0
- package/package.json +51 -0
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import type { Hex } from "viem";
|
|
2
|
+
export declare const AGENTIC_COMMERCE_ARC_TESTNET: "0x0747EEf0706327138c69792bF28Cd525089e4583";
|
|
3
|
+
export declare const JOB_STATUS: readonly ["Open", "Funded", "Submitted", "Completed", "Rejected", "Expired"];
|
|
4
|
+
export type JobStatusName = (typeof JOB_STATUS)[number];
|
|
5
|
+
export declare const ZERO_HOOK: "0x0000000000000000000000000000000000000000";
|
|
6
|
+
export declare const ERC8183_ABI: readonly [{
|
|
7
|
+
readonly type: "function";
|
|
8
|
+
readonly name: "createJob";
|
|
9
|
+
readonly stateMutability: "nonpayable";
|
|
10
|
+
readonly inputs: readonly [{
|
|
11
|
+
readonly name: "provider";
|
|
12
|
+
readonly type: "address";
|
|
13
|
+
}, {
|
|
14
|
+
readonly name: "evaluator";
|
|
15
|
+
readonly type: "address";
|
|
16
|
+
}, {
|
|
17
|
+
readonly name: "expiredAt";
|
|
18
|
+
readonly type: "uint256";
|
|
19
|
+
}, {
|
|
20
|
+
readonly name: "description";
|
|
21
|
+
readonly type: "string";
|
|
22
|
+
}, {
|
|
23
|
+
readonly name: "hook";
|
|
24
|
+
readonly type: "address";
|
|
25
|
+
}];
|
|
26
|
+
readonly outputs: readonly [{
|
|
27
|
+
readonly name: "jobId";
|
|
28
|
+
readonly type: "uint256";
|
|
29
|
+
}];
|
|
30
|
+
}, {
|
|
31
|
+
readonly type: "function";
|
|
32
|
+
readonly name: "setBudget";
|
|
33
|
+
readonly stateMutability: "nonpayable";
|
|
34
|
+
readonly inputs: readonly [{
|
|
35
|
+
readonly name: "jobId";
|
|
36
|
+
readonly type: "uint256";
|
|
37
|
+
}, {
|
|
38
|
+
readonly name: "amount";
|
|
39
|
+
readonly type: "uint256";
|
|
40
|
+
}, {
|
|
41
|
+
readonly name: "optParams";
|
|
42
|
+
readonly type: "bytes";
|
|
43
|
+
}];
|
|
44
|
+
readonly outputs: readonly [];
|
|
45
|
+
}, {
|
|
46
|
+
readonly type: "function";
|
|
47
|
+
readonly name: "fund";
|
|
48
|
+
readonly stateMutability: "nonpayable";
|
|
49
|
+
readonly inputs: readonly [{
|
|
50
|
+
readonly name: "jobId";
|
|
51
|
+
readonly type: "uint256";
|
|
52
|
+
}, {
|
|
53
|
+
readonly name: "optParams";
|
|
54
|
+
readonly type: "bytes";
|
|
55
|
+
}];
|
|
56
|
+
readonly outputs: readonly [];
|
|
57
|
+
}, {
|
|
58
|
+
readonly type: "function";
|
|
59
|
+
readonly name: "submit";
|
|
60
|
+
readonly stateMutability: "nonpayable";
|
|
61
|
+
readonly inputs: readonly [{
|
|
62
|
+
readonly name: "jobId";
|
|
63
|
+
readonly type: "uint256";
|
|
64
|
+
}, {
|
|
65
|
+
readonly name: "deliverableHash";
|
|
66
|
+
readonly type: "bytes32";
|
|
67
|
+
}, {
|
|
68
|
+
readonly name: "optParams";
|
|
69
|
+
readonly type: "bytes";
|
|
70
|
+
}];
|
|
71
|
+
readonly outputs: readonly [];
|
|
72
|
+
}, {
|
|
73
|
+
readonly type: "function";
|
|
74
|
+
readonly name: "complete";
|
|
75
|
+
readonly stateMutability: "nonpayable";
|
|
76
|
+
readonly inputs: readonly [{
|
|
77
|
+
readonly name: "jobId";
|
|
78
|
+
readonly type: "uint256";
|
|
79
|
+
}, {
|
|
80
|
+
readonly name: "reason";
|
|
81
|
+
readonly type: "bytes32";
|
|
82
|
+
}, {
|
|
83
|
+
readonly name: "optParams";
|
|
84
|
+
readonly type: "bytes";
|
|
85
|
+
}];
|
|
86
|
+
readonly outputs: readonly [];
|
|
87
|
+
}, {
|
|
88
|
+
readonly type: "function";
|
|
89
|
+
readonly name: "reject";
|
|
90
|
+
readonly stateMutability: "nonpayable";
|
|
91
|
+
readonly inputs: readonly [{
|
|
92
|
+
readonly name: "jobId";
|
|
93
|
+
readonly type: "uint256";
|
|
94
|
+
}, {
|
|
95
|
+
readonly name: "reason";
|
|
96
|
+
readonly type: "bytes32";
|
|
97
|
+
}, {
|
|
98
|
+
readonly name: "optParams";
|
|
99
|
+
readonly type: "bytes";
|
|
100
|
+
}];
|
|
101
|
+
readonly outputs: readonly [];
|
|
102
|
+
}, {
|
|
103
|
+
readonly type: "function";
|
|
104
|
+
readonly name: "claimRefund";
|
|
105
|
+
readonly stateMutability: "nonpayable";
|
|
106
|
+
readonly inputs: readonly [{
|
|
107
|
+
readonly name: "jobId";
|
|
108
|
+
readonly type: "uint256";
|
|
109
|
+
}];
|
|
110
|
+
readonly outputs: readonly [];
|
|
111
|
+
}, {
|
|
112
|
+
readonly type: "function";
|
|
113
|
+
readonly name: "getJob";
|
|
114
|
+
readonly stateMutability: "view";
|
|
115
|
+
readonly inputs: readonly [{
|
|
116
|
+
readonly name: "jobId";
|
|
117
|
+
readonly type: "uint256";
|
|
118
|
+
}];
|
|
119
|
+
readonly outputs: readonly [{
|
|
120
|
+
readonly type: "tuple";
|
|
121
|
+
readonly components: readonly [{
|
|
122
|
+
readonly name: "id";
|
|
123
|
+
readonly type: "uint256";
|
|
124
|
+
}, {
|
|
125
|
+
readonly name: "client";
|
|
126
|
+
readonly type: "address";
|
|
127
|
+
}, {
|
|
128
|
+
readonly name: "provider";
|
|
129
|
+
readonly type: "address";
|
|
130
|
+
}, {
|
|
131
|
+
readonly name: "evaluator";
|
|
132
|
+
readonly type: "address";
|
|
133
|
+
}, {
|
|
134
|
+
readonly name: "description";
|
|
135
|
+
readonly type: "string";
|
|
136
|
+
}, {
|
|
137
|
+
readonly name: "budget";
|
|
138
|
+
readonly type: "uint256";
|
|
139
|
+
}, {
|
|
140
|
+
readonly name: "expiredAt";
|
|
141
|
+
readonly type: "uint256";
|
|
142
|
+
}, {
|
|
143
|
+
readonly name: "status";
|
|
144
|
+
readonly type: "uint8";
|
|
145
|
+
}, {
|
|
146
|
+
readonly name: "hook";
|
|
147
|
+
readonly type: "address";
|
|
148
|
+
}];
|
|
149
|
+
}];
|
|
150
|
+
}, {
|
|
151
|
+
readonly type: "event";
|
|
152
|
+
readonly name: "JobCreated";
|
|
153
|
+
readonly inputs: readonly [{
|
|
154
|
+
readonly indexed: true;
|
|
155
|
+
readonly name: "jobId";
|
|
156
|
+
readonly type: "uint256";
|
|
157
|
+
}, {
|
|
158
|
+
readonly indexed: true;
|
|
159
|
+
readonly name: "client";
|
|
160
|
+
readonly type: "address";
|
|
161
|
+
}, {
|
|
162
|
+
readonly indexed: true;
|
|
163
|
+
readonly name: "provider";
|
|
164
|
+
readonly type: "address";
|
|
165
|
+
}, {
|
|
166
|
+
readonly indexed: false;
|
|
167
|
+
readonly name: "evaluator";
|
|
168
|
+
readonly type: "address";
|
|
169
|
+
}, {
|
|
170
|
+
readonly indexed: false;
|
|
171
|
+
readonly name: "expiredAt";
|
|
172
|
+
readonly type: "uint256";
|
|
173
|
+
}, {
|
|
174
|
+
readonly indexed: false;
|
|
175
|
+
readonly name: "hook";
|
|
176
|
+
readonly type: "address";
|
|
177
|
+
}];
|
|
178
|
+
readonly anonymous: false;
|
|
179
|
+
}];
|
|
180
|
+
export interface WriteClient {
|
|
181
|
+
writeContract(args: {
|
|
182
|
+
address: `0x${string}`;
|
|
183
|
+
abi: unknown;
|
|
184
|
+
functionName: string;
|
|
185
|
+
args: readonly unknown[];
|
|
186
|
+
}): Promise<Hex>;
|
|
187
|
+
}
|
|
188
|
+
export interface ReadClient {
|
|
189
|
+
readContract(args: {
|
|
190
|
+
address: `0x${string}`;
|
|
191
|
+
abi: unknown;
|
|
192
|
+
functionName: string;
|
|
193
|
+
args: readonly unknown[];
|
|
194
|
+
}): Promise<unknown>;
|
|
195
|
+
getTransactionReceipt(args: {
|
|
196
|
+
hash: Hex;
|
|
197
|
+
}): Promise<{
|
|
198
|
+
status: string;
|
|
199
|
+
logs: Array<{
|
|
200
|
+
address: string;
|
|
201
|
+
topics: Hex[];
|
|
202
|
+
data: Hex;
|
|
203
|
+
}>;
|
|
204
|
+
}>;
|
|
205
|
+
waitForTransactionReceipt?(args: {
|
|
206
|
+
hash: Hex;
|
|
207
|
+
}): Promise<{
|
|
208
|
+
status: string;
|
|
209
|
+
logs: Array<{
|
|
210
|
+
address: string;
|
|
211
|
+
topics: Hex[];
|
|
212
|
+
data: Hex;
|
|
213
|
+
}>;
|
|
214
|
+
}>;
|
|
215
|
+
}
|
|
216
|
+
export interface Erc8183Config {
|
|
217
|
+
read: ReadClient;
|
|
218
|
+
contract?: `0x${string}`;
|
|
219
|
+
usdc: `0x${string}`;
|
|
220
|
+
}
|
|
221
|
+
export interface JobView {
|
|
222
|
+
id: bigint;
|
|
223
|
+
client: `0x${string}`;
|
|
224
|
+
provider: `0x${string}`;
|
|
225
|
+
evaluator: `0x${string}`;
|
|
226
|
+
description: string;
|
|
227
|
+
budget: bigint;
|
|
228
|
+
expiredAt: bigint;
|
|
229
|
+
status: JobStatusName;
|
|
230
|
+
hook: `0x${string}`;
|
|
231
|
+
}
|
|
232
|
+
export declare class Erc8183Error extends Error {
|
|
233
|
+
readonly phase: "createJob" | "setBudget" | "fund" | "submit" | "complete" | "reject" | "claimRefund" | "getJob";
|
|
234
|
+
readonly cause?: unknown | undefined;
|
|
235
|
+
constructor(message: string, phase: "createJob" | "setBudget" | "fund" | "submit" | "complete" | "reject" | "claimRefund" | "getJob", cause?: unknown | undefined);
|
|
236
|
+
}
|
|
237
|
+
export declare function createErc8183(cfg: Erc8183Config): {
|
|
238
|
+
contract: `0x${string}`;
|
|
239
|
+
createJob(wallet: WriteClient, args: {
|
|
240
|
+
provider: `0x${string}`;
|
|
241
|
+
evaluator: `0x${string}`;
|
|
242
|
+
expiredAt: bigint;
|
|
243
|
+
description: string;
|
|
244
|
+
hook?: `0x${string}`;
|
|
245
|
+
}): Promise<{
|
|
246
|
+
jobId: bigint;
|
|
247
|
+
txHash: Hex;
|
|
248
|
+
}>;
|
|
249
|
+
setBudget(wallet: WriteClient, args: {
|
|
250
|
+
jobId: bigint;
|
|
251
|
+
amount: bigint;
|
|
252
|
+
optParams?: Hex;
|
|
253
|
+
}): Promise<Hex>;
|
|
254
|
+
fundJob(wallet: WriteClient, args: {
|
|
255
|
+
jobId: bigint;
|
|
256
|
+
amount: bigint;
|
|
257
|
+
optParams?: Hex;
|
|
258
|
+
}): Promise<{
|
|
259
|
+
approveTx: Hex;
|
|
260
|
+
fundTx: Hex;
|
|
261
|
+
}>;
|
|
262
|
+
submitResult(wallet: WriteClient, args: {
|
|
263
|
+
jobId: bigint;
|
|
264
|
+
deliverableHash: Hex;
|
|
265
|
+
optParams?: Hex;
|
|
266
|
+
}): Promise<Hex>;
|
|
267
|
+
completeJob(wallet: WriteClient, args: {
|
|
268
|
+
jobId: bigint;
|
|
269
|
+
reason: Hex;
|
|
270
|
+
optParams?: Hex;
|
|
271
|
+
}): Promise<Hex>;
|
|
272
|
+
rejectJob(wallet: WriteClient, args: {
|
|
273
|
+
jobId: bigint;
|
|
274
|
+
reason: Hex;
|
|
275
|
+
optParams?: Hex;
|
|
276
|
+
}): Promise<Hex>;
|
|
277
|
+
claimRefund(wallet: WriteClient, jobId: bigint): Promise<Hex>;
|
|
278
|
+
getJob(jobId: bigint): Promise<JobView>;
|
|
279
|
+
};
|
|
280
|
+
export type Erc8183 = ReturnType<typeof createErc8183>;
|
|
281
|
+
export declare function jobDescription(orderId: string, text: string): string;
|
|
282
|
+
export declare function createJobRegistry(): {
|
|
283
|
+
link(jobId: bigint, orderId: string): void;
|
|
284
|
+
orderFor(jobId: bigint): string | undefined;
|
|
285
|
+
jobsFor(orderId: string): bigint[];
|
|
286
|
+
size(): number;
|
|
287
|
+
};
|
|
288
|
+
export type JobRegistry = ReturnType<typeof createJobRegistry>;
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { decodeEventLog, parseAbi } from "viem";
|
|
2
|
+
export const AGENTIC_COMMERCE_ARC_TESTNET = "0x0747EEf0706327138c69792bF28Cd525089e4583";
|
|
3
|
+
export const JOB_STATUS = [
|
|
4
|
+
"Open",
|
|
5
|
+
"Funded",
|
|
6
|
+
"Submitted",
|
|
7
|
+
"Completed",
|
|
8
|
+
"Rejected",
|
|
9
|
+
"Expired",
|
|
10
|
+
];
|
|
11
|
+
export const ZERO_HOOK = "0x0000000000000000000000000000000000000000";
|
|
12
|
+
export const ERC8183_ABI = [
|
|
13
|
+
{
|
|
14
|
+
type: "function",
|
|
15
|
+
name: "createJob",
|
|
16
|
+
stateMutability: "nonpayable",
|
|
17
|
+
inputs: [
|
|
18
|
+
{ name: "provider", type: "address" },
|
|
19
|
+
{ name: "evaluator", type: "address" },
|
|
20
|
+
{ name: "expiredAt", type: "uint256" },
|
|
21
|
+
{ name: "description", type: "string" },
|
|
22
|
+
{ name: "hook", type: "address" },
|
|
23
|
+
],
|
|
24
|
+
outputs: [{ name: "jobId", type: "uint256" }],
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
type: "function",
|
|
28
|
+
name: "setBudget",
|
|
29
|
+
stateMutability: "nonpayable",
|
|
30
|
+
inputs: [
|
|
31
|
+
{ name: "jobId", type: "uint256" },
|
|
32
|
+
{ name: "amount", type: "uint256" },
|
|
33
|
+
{ name: "optParams", type: "bytes" },
|
|
34
|
+
],
|
|
35
|
+
outputs: [],
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
type: "function",
|
|
39
|
+
name: "fund",
|
|
40
|
+
stateMutability: "nonpayable",
|
|
41
|
+
inputs: [
|
|
42
|
+
{ name: "jobId", type: "uint256" },
|
|
43
|
+
{ name: "optParams", type: "bytes" },
|
|
44
|
+
],
|
|
45
|
+
outputs: [],
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
type: "function",
|
|
49
|
+
name: "submit",
|
|
50
|
+
stateMutability: "nonpayable",
|
|
51
|
+
inputs: [
|
|
52
|
+
{ name: "jobId", type: "uint256" },
|
|
53
|
+
{ name: "deliverableHash", type: "bytes32" },
|
|
54
|
+
{ name: "optParams", type: "bytes" },
|
|
55
|
+
],
|
|
56
|
+
outputs: [],
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
type: "function",
|
|
60
|
+
name: "complete",
|
|
61
|
+
stateMutability: "nonpayable",
|
|
62
|
+
inputs: [
|
|
63
|
+
{ name: "jobId", type: "uint256" },
|
|
64
|
+
{ name: "reason", type: "bytes32" },
|
|
65
|
+
{ name: "optParams", type: "bytes" },
|
|
66
|
+
],
|
|
67
|
+
outputs: [],
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
type: "function",
|
|
71
|
+
name: "reject",
|
|
72
|
+
stateMutability: "nonpayable",
|
|
73
|
+
inputs: [
|
|
74
|
+
{ name: "jobId", type: "uint256" },
|
|
75
|
+
{ name: "reason", type: "bytes32" },
|
|
76
|
+
{ name: "optParams", type: "bytes" },
|
|
77
|
+
],
|
|
78
|
+
outputs: [],
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
type: "function",
|
|
82
|
+
name: "claimRefund",
|
|
83
|
+
stateMutability: "nonpayable",
|
|
84
|
+
inputs: [{ name: "jobId", type: "uint256" }],
|
|
85
|
+
outputs: [],
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
type: "function",
|
|
89
|
+
name: "getJob",
|
|
90
|
+
stateMutability: "view",
|
|
91
|
+
inputs: [{ name: "jobId", type: "uint256" }],
|
|
92
|
+
outputs: [
|
|
93
|
+
{
|
|
94
|
+
type: "tuple",
|
|
95
|
+
components: [
|
|
96
|
+
{ name: "id", type: "uint256" },
|
|
97
|
+
{ name: "client", type: "address" },
|
|
98
|
+
{ name: "provider", type: "address" },
|
|
99
|
+
{ name: "evaluator", type: "address" },
|
|
100
|
+
{ name: "description", type: "string" },
|
|
101
|
+
{ name: "budget", type: "uint256" },
|
|
102
|
+
{ name: "expiredAt", type: "uint256" },
|
|
103
|
+
{ name: "status", type: "uint8" },
|
|
104
|
+
{ name: "hook", type: "address" },
|
|
105
|
+
],
|
|
106
|
+
},
|
|
107
|
+
],
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
type: "event",
|
|
111
|
+
name: "JobCreated",
|
|
112
|
+
inputs: [
|
|
113
|
+
{ indexed: true, name: "jobId", type: "uint256" },
|
|
114
|
+
{ indexed: true, name: "client", type: "address" },
|
|
115
|
+
{ indexed: true, name: "provider", type: "address" },
|
|
116
|
+
{ indexed: false, name: "evaluator", type: "address" },
|
|
117
|
+
{ indexed: false, name: "expiredAt", type: "uint256" },
|
|
118
|
+
{ indexed: false, name: "hook", type: "address" },
|
|
119
|
+
],
|
|
120
|
+
anonymous: false,
|
|
121
|
+
},
|
|
122
|
+
];
|
|
123
|
+
const ERC20_APPROVE_ABI = parseAbi([
|
|
124
|
+
"function approve(address spender, uint256 amount) returns (bool)",
|
|
125
|
+
]);
|
|
126
|
+
async function awaitReceipt(read, hash) {
|
|
127
|
+
if (read.waitForTransactionReceipt) {
|
|
128
|
+
return read.waitForTransactionReceipt({ hash });
|
|
129
|
+
}
|
|
130
|
+
return read.getTransactionReceipt({ hash });
|
|
131
|
+
}
|
|
132
|
+
export class Erc8183Error extends Error {
|
|
133
|
+
phase;
|
|
134
|
+
cause;
|
|
135
|
+
constructor(message, phase, cause) {
|
|
136
|
+
super(message);
|
|
137
|
+
this.phase = phase;
|
|
138
|
+
this.cause = cause;
|
|
139
|
+
this.name = "Erc8183Error";
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const EMPTY_PARAMS = "0x";
|
|
143
|
+
function extractJobId(receipt) {
|
|
144
|
+
for (const log of receipt.logs) {
|
|
145
|
+
try {
|
|
146
|
+
const decoded = decodeEventLog({
|
|
147
|
+
abi: ERC8183_ABI,
|
|
148
|
+
data: log.data,
|
|
149
|
+
topics: log.topics,
|
|
150
|
+
});
|
|
151
|
+
if (decoded.eventName === "JobCreated") {
|
|
152
|
+
return decoded.args.jobId;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
throw new Erc8183Error("JobCreated event not found in receipt", "createJob");
|
|
160
|
+
}
|
|
161
|
+
export function createErc8183(cfg) {
|
|
162
|
+
const contract = cfg.contract ?? AGENTIC_COMMERCE_ARC_TESTNET;
|
|
163
|
+
return {
|
|
164
|
+
contract,
|
|
165
|
+
async createJob(wallet, args) {
|
|
166
|
+
try {
|
|
167
|
+
const txHash = await wallet.writeContract({
|
|
168
|
+
address: contract,
|
|
169
|
+
abi: ERC8183_ABI,
|
|
170
|
+
functionName: "createJob",
|
|
171
|
+
args: [
|
|
172
|
+
args.provider,
|
|
173
|
+
args.evaluator,
|
|
174
|
+
args.expiredAt,
|
|
175
|
+
args.description,
|
|
176
|
+
args.hook ?? ZERO_HOOK,
|
|
177
|
+
],
|
|
178
|
+
});
|
|
179
|
+
const receipt = await awaitReceipt(cfg.read, txHash);
|
|
180
|
+
return { jobId: extractJobId(receipt), txHash };
|
|
181
|
+
}
|
|
182
|
+
catch (e) {
|
|
183
|
+
if (e instanceof Erc8183Error)
|
|
184
|
+
throw e;
|
|
185
|
+
throw new Erc8183Error("createJob failed", "createJob", e);
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
async setBudget(wallet, args) {
|
|
189
|
+
try {
|
|
190
|
+
return await wallet.writeContract({
|
|
191
|
+
address: contract,
|
|
192
|
+
abi: ERC8183_ABI,
|
|
193
|
+
functionName: "setBudget",
|
|
194
|
+
args: [args.jobId, args.amount, args.optParams ?? EMPTY_PARAMS],
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
catch (e) {
|
|
198
|
+
throw new Erc8183Error("setBudget failed", "setBudget", e);
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
async fundJob(wallet, args) {
|
|
202
|
+
try {
|
|
203
|
+
const approveTx = await wallet.writeContract({
|
|
204
|
+
address: cfg.usdc,
|
|
205
|
+
abi: ERC20_APPROVE_ABI,
|
|
206
|
+
functionName: "approve",
|
|
207
|
+
args: [contract, args.amount],
|
|
208
|
+
});
|
|
209
|
+
await awaitReceipt(cfg.read, approveTx);
|
|
210
|
+
const fundTx = await wallet.writeContract({
|
|
211
|
+
address: contract,
|
|
212
|
+
abi: ERC8183_ABI,
|
|
213
|
+
functionName: "fund",
|
|
214
|
+
args: [args.jobId, args.optParams ?? EMPTY_PARAMS],
|
|
215
|
+
});
|
|
216
|
+
return { approveTx, fundTx };
|
|
217
|
+
}
|
|
218
|
+
catch (e) {
|
|
219
|
+
throw new Erc8183Error("fund failed", "fund", e);
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
async submitResult(wallet, args) {
|
|
223
|
+
try {
|
|
224
|
+
return await wallet.writeContract({
|
|
225
|
+
address: contract,
|
|
226
|
+
abi: ERC8183_ABI,
|
|
227
|
+
functionName: "submit",
|
|
228
|
+
args: [
|
|
229
|
+
args.jobId,
|
|
230
|
+
args.deliverableHash,
|
|
231
|
+
args.optParams ?? EMPTY_PARAMS,
|
|
232
|
+
],
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
catch (e) {
|
|
236
|
+
throw new Erc8183Error("submit failed", "submit", e);
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
async completeJob(wallet, args) {
|
|
240
|
+
try {
|
|
241
|
+
return await wallet.writeContract({
|
|
242
|
+
address: contract,
|
|
243
|
+
abi: ERC8183_ABI,
|
|
244
|
+
functionName: "complete",
|
|
245
|
+
args: [args.jobId, args.reason, args.optParams ?? EMPTY_PARAMS],
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
catch (e) {
|
|
249
|
+
throw new Erc8183Error("complete failed", "complete", e);
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
async rejectJob(wallet, args) {
|
|
253
|
+
try {
|
|
254
|
+
return await wallet.writeContract({
|
|
255
|
+
address: contract,
|
|
256
|
+
abi: ERC8183_ABI,
|
|
257
|
+
functionName: "reject",
|
|
258
|
+
args: [args.jobId, args.reason, args.optParams ?? EMPTY_PARAMS],
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
catch (e) {
|
|
262
|
+
throw new Erc8183Error("reject failed", "reject", e);
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
async claimRefund(wallet, jobId) {
|
|
266
|
+
try {
|
|
267
|
+
return await wallet.writeContract({
|
|
268
|
+
address: contract,
|
|
269
|
+
abi: ERC8183_ABI,
|
|
270
|
+
functionName: "claimRefund",
|
|
271
|
+
args: [jobId],
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
catch (e) {
|
|
275
|
+
throw new Erc8183Error("claimRefund failed", "claimRefund", e);
|
|
276
|
+
}
|
|
277
|
+
},
|
|
278
|
+
async getJob(jobId) {
|
|
279
|
+
try {
|
|
280
|
+
const raw = (await cfg.read.readContract({
|
|
281
|
+
address: contract,
|
|
282
|
+
abi: ERC8183_ABI,
|
|
283
|
+
functionName: "getJob",
|
|
284
|
+
args: [jobId],
|
|
285
|
+
}));
|
|
286
|
+
return { ...raw, status: JOB_STATUS[raw.status] ?? "Open" };
|
|
287
|
+
}
|
|
288
|
+
catch (e) {
|
|
289
|
+
throw new Erc8183Error("getJob failed", "getJob", e);
|
|
290
|
+
}
|
|
291
|
+
},
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
export function jobDescription(orderId, text) {
|
|
295
|
+
return `agentbadge:order:${orderId} ${text}`;
|
|
296
|
+
}
|
|
297
|
+
export function createJobRegistry() {
|
|
298
|
+
const byJob = new Map();
|
|
299
|
+
return {
|
|
300
|
+
link(jobId, orderId) {
|
|
301
|
+
byJob.set(jobId.toString(), {
|
|
302
|
+
orderId,
|
|
303
|
+
createdAt: new Date().toISOString(),
|
|
304
|
+
});
|
|
305
|
+
},
|
|
306
|
+
orderFor(jobId) {
|
|
307
|
+
return byJob.get(jobId.toString())?.orderId;
|
|
308
|
+
},
|
|
309
|
+
jobsFor(orderId) {
|
|
310
|
+
return [...byJob.entries()]
|
|
311
|
+
.filter(([, v]) => v.orderId === orderId)
|
|
312
|
+
.map(([k]) => BigInt(k));
|
|
313
|
+
},
|
|
314
|
+
size() {
|
|
315
|
+
return byJob.size;
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Hex } from "viem";
|
|
2
|
+
import type { Erc8183, JobView, WriteClient } from "./erc8183.js";
|
|
3
|
+
export type VerdictKind = "approve" | "reject" | "expired";
|
|
4
|
+
export interface EvaluationVerdict {
|
|
5
|
+
jobId: string;
|
|
6
|
+
verdict: VerdictKind;
|
|
7
|
+
reason: string;
|
|
8
|
+
reasonHash: Hex;
|
|
9
|
+
evidence?: unknown;
|
|
10
|
+
txHash?: Hex;
|
|
11
|
+
evaluatedAt: string;
|
|
12
|
+
}
|
|
13
|
+
export type VerifyFn = (job: JobView, result: unknown) => Promise<{
|
|
14
|
+
pass: boolean;
|
|
15
|
+
reason: string;
|
|
16
|
+
evidence?: unknown;
|
|
17
|
+
}>;
|
|
18
|
+
export interface EvaluationStore {
|
|
19
|
+
put(v: EvaluationVerdict): void;
|
|
20
|
+
get(jobId: bigint): EvaluationVerdict | undefined;
|
|
21
|
+
list(): EvaluationVerdict[];
|
|
22
|
+
}
|
|
23
|
+
export declare function createEvaluationStore(): EvaluationStore;
|
|
24
|
+
export declare class EvaluatorError extends Error {
|
|
25
|
+
readonly jobId: bigint;
|
|
26
|
+
readonly cause?: unknown | undefined;
|
|
27
|
+
constructor(message: string, jobId: bigint, cause?: unknown | undefined);
|
|
28
|
+
}
|
|
29
|
+
export interface EvaluatorDeps {
|
|
30
|
+
escrow: Erc8183;
|
|
31
|
+
wallet: WriteClient;
|
|
32
|
+
verify?: VerifyFn;
|
|
33
|
+
store?: EvaluationStore;
|
|
34
|
+
now?: () => number;
|
|
35
|
+
}
|
|
36
|
+
export declare function createEvaluator(deps: EvaluatorDeps): {
|
|
37
|
+
store: EvaluationStore;
|
|
38
|
+
evaluate(jobId: bigint, result?: unknown): Promise<EvaluationVerdict>;
|
|
39
|
+
};
|
|
40
|
+
export type Evaluator = ReturnType<typeof createEvaluator>;
|