arl-protocol-sdk 0.0.0-stage → 0.2.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 +79 -2
- package/package.json +32 -4
- package/protocol/src/canonical.d.ts +7 -0
- package/protocol/src/canonical.js +35 -0
- package/protocol/src/deployments.d.ts +17 -0
- package/protocol/src/deployments.js +24 -0
- package/protocol/src/evidence.d.ts +72 -0
- package/protocol/src/evidence.js +107 -0
- package/protocol/src/index.d.ts +14 -0
- package/protocol/src/index.js +9 -0
- package/protocol/src/policies.d.ts +5 -0
- package/protocol/src/policies.js +56 -0
- package/protocol/src/policy.d.ts +65 -0
- package/protocol/src/policy.js +202 -0
- package/protocol/src/prepare.d.ts +176 -0
- package/protocol/src/prepare.js +104 -0
- package/protocol/src/proof.d.ts +70 -0
- package/protocol/src/proof.js +239 -0
- package/protocol/src/reputation.d.ts +47 -0
- package/protocol/src/reputation.js +74 -0
- package/protocol/src/task.d.ts +121 -0
- package/protocol/src/task.js +322 -0
- package/sdk/src/abi.d.ts +358 -0
- package/sdk/src/abi.js +465 -0
- package/sdk/src/acquireArl.d.ts +34 -0
- package/sdk/src/acquireArl.js +126 -0
- package/sdk/src/escrowProvider.d.ts +73 -0
- package/sdk/src/escrowProvider.js +169 -0
- package/sdk/src/index.d.ts +9 -0
- package/sdk/src/index.js +10 -0
- package/sdk/src/jobs.d.ts +278 -0
- package/sdk/src/jobs.js +220 -0
- package/sdk/src/openTask.d.ts +23 -0
- package/sdk/src/openTask.js +116 -0
- package/sdk/src/payVerified.d.ts +45 -0
- package/sdk/src/payVerified.js +147 -0
- package/sdk/src/streaming.d.ts +80 -0
- package/sdk/src/streaming.js +229 -0
- package/sdk/src/verified.d.ts +229 -0
- package/sdk/src/verified.js +220 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { type Account, type Address, type Chain, type Hash, type Hex, type PublicClient, type WalletClient } from "viem";
|
|
2
|
+
/**
|
|
3
|
+
* Base Mainnet deployment as prepared in docs/mainnet-services.md: the escrow paid in ARL with the
|
|
4
|
+
* deadline fix (security report 2026-10-25, audit 2026-10-28; deployer nonce 25) and its evaluator
|
|
5
|
+
* with the acceptance fix (security report 2026-10-31; nonce 27). The evaluator at nonce 26 closed
|
|
6
|
+
* acceptance when voting started and is retired. The addresses follow from the nonces; calls check
|
|
7
|
+
* that they hold code before using them.
|
|
8
|
+
*/
|
|
9
|
+
export declare const BASE_MAINNET_VERIFIED: {
|
|
10
|
+
readonly chainId: 8453;
|
|
11
|
+
readonly token: "0xde91E6f39D50bBd875f831dcc1134A3D7f3aaDb4";
|
|
12
|
+
readonly jobs: "0x9aE99bF9a59aF47E81346B7c0b93Cd2B610455fC";
|
|
13
|
+
readonly evaluator: "0x6648e141cb7473822ddBd8bC039b91e5af3259B4";
|
|
14
|
+
readonly arbiterSafe: "0x8FF6c3aD0991Ac848BFf5d0acD78BBc5e190466f";
|
|
15
|
+
};
|
|
16
|
+
/** The jobs escrow and evaluator of a chain, or undefined on the local chain. */
|
|
17
|
+
export declare function verifiedDeployment(chainId: number): {
|
|
18
|
+
jobs: Address;
|
|
19
|
+
evaluator: Address;
|
|
20
|
+
} | undefined;
|
|
21
|
+
export declare const verifiedJobsAbi: readonly [{
|
|
22
|
+
readonly name: "createJob";
|
|
23
|
+
readonly type: "function";
|
|
24
|
+
readonly stateMutability: "nonpayable";
|
|
25
|
+
readonly inputs: readonly [{
|
|
26
|
+
readonly type: "address";
|
|
27
|
+
readonly name: "provider";
|
|
28
|
+
}, {
|
|
29
|
+
readonly type: "address";
|
|
30
|
+
readonly name: "evaluator";
|
|
31
|
+
}, {
|
|
32
|
+
readonly type: "uint256";
|
|
33
|
+
readonly name: "expiredAt";
|
|
34
|
+
}, {
|
|
35
|
+
readonly type: "string";
|
|
36
|
+
readonly name: "description";
|
|
37
|
+
}, {
|
|
38
|
+
readonly type: "address";
|
|
39
|
+
readonly name: "hook";
|
|
40
|
+
}];
|
|
41
|
+
readonly outputs: readonly [{
|
|
42
|
+
readonly type: "uint256";
|
|
43
|
+
}];
|
|
44
|
+
}, {
|
|
45
|
+
readonly name: "JobCreated";
|
|
46
|
+
readonly type: "event";
|
|
47
|
+
readonly inputs: readonly [{
|
|
48
|
+
readonly type: "uint256";
|
|
49
|
+
readonly name: "jobId";
|
|
50
|
+
readonly indexed: true;
|
|
51
|
+
}, {
|
|
52
|
+
readonly type: "address";
|
|
53
|
+
readonly name: "client";
|
|
54
|
+
readonly indexed: true;
|
|
55
|
+
}, {
|
|
56
|
+
readonly type: "address";
|
|
57
|
+
readonly name: "provider";
|
|
58
|
+
readonly indexed: true;
|
|
59
|
+
}, {
|
|
60
|
+
readonly type: "address";
|
|
61
|
+
readonly name: "evaluator";
|
|
62
|
+
}, {
|
|
63
|
+
readonly type: "uint256";
|
|
64
|
+
readonly name: "expiredAt";
|
|
65
|
+
}, {
|
|
66
|
+
readonly type: "address";
|
|
67
|
+
readonly name: "hook";
|
|
68
|
+
}];
|
|
69
|
+
}];
|
|
70
|
+
export declare const verifiedEvaluatorAbi: readonly [{
|
|
71
|
+
readonly name: "configure";
|
|
72
|
+
readonly type: "function";
|
|
73
|
+
readonly stateMutability: "nonpayable";
|
|
74
|
+
readonly inputs: readonly [{
|
|
75
|
+
readonly type: "uint256";
|
|
76
|
+
readonly name: "jobId";
|
|
77
|
+
}, {
|
|
78
|
+
readonly type: "tuple";
|
|
79
|
+
readonly components: readonly [{
|
|
80
|
+
readonly type: "uint8";
|
|
81
|
+
readonly name: "tier";
|
|
82
|
+
}, {
|
|
83
|
+
readonly type: "uint8";
|
|
84
|
+
readonly name: "threshold";
|
|
85
|
+
}, {
|
|
86
|
+
readonly type: "uint64";
|
|
87
|
+
readonly name: "voteWindow";
|
|
88
|
+
}, {
|
|
89
|
+
readonly type: "uint64";
|
|
90
|
+
readonly name: "challengeWindow";
|
|
91
|
+
}, {
|
|
92
|
+
readonly type: "uint96";
|
|
93
|
+
readonly name: "bond";
|
|
94
|
+
}, {
|
|
95
|
+
readonly type: "uint256";
|
|
96
|
+
readonly name: "providerAgentId";
|
|
97
|
+
}, {
|
|
98
|
+
readonly type: "bytes32";
|
|
99
|
+
readonly name: "specHash";
|
|
100
|
+
}, {
|
|
101
|
+
readonly type: "address[]";
|
|
102
|
+
readonly name: "committee";
|
|
103
|
+
}];
|
|
104
|
+
readonly name: "cfg";
|
|
105
|
+
}];
|
|
106
|
+
readonly outputs: readonly [];
|
|
107
|
+
}, {
|
|
108
|
+
readonly name: "getVerdict";
|
|
109
|
+
readonly type: "function";
|
|
110
|
+
readonly stateMutability: "view";
|
|
111
|
+
readonly inputs: readonly [{
|
|
112
|
+
readonly type: "uint256";
|
|
113
|
+
readonly name: "jobId";
|
|
114
|
+
}];
|
|
115
|
+
readonly outputs: readonly [{
|
|
116
|
+
readonly type: "tuple";
|
|
117
|
+
readonly components: readonly [{
|
|
118
|
+
readonly type: "uint8";
|
|
119
|
+
readonly name: "phase";
|
|
120
|
+
}, {
|
|
121
|
+
readonly type: "bool";
|
|
122
|
+
readonly name: "providerApproved";
|
|
123
|
+
}, {
|
|
124
|
+
readonly type: "uint8";
|
|
125
|
+
readonly name: "approvals";
|
|
126
|
+
}, {
|
|
127
|
+
readonly type: "uint8";
|
|
128
|
+
readonly name: "rejections";
|
|
129
|
+
}, {
|
|
130
|
+
readonly type: "uint8";
|
|
131
|
+
readonly name: "topApprovals";
|
|
132
|
+
}, {
|
|
133
|
+
readonly type: "uint64";
|
|
134
|
+
readonly name: "voteEnds";
|
|
135
|
+
}, {
|
|
136
|
+
readonly type: "uint64";
|
|
137
|
+
readonly name: "challengeEnds";
|
|
138
|
+
}, {
|
|
139
|
+
readonly type: "address";
|
|
140
|
+
readonly name: "challenger";
|
|
141
|
+
}, {
|
|
142
|
+
readonly type: "uint256";
|
|
143
|
+
readonly name: "bond";
|
|
144
|
+
}, {
|
|
145
|
+
readonly type: "bytes32";
|
|
146
|
+
readonly name: "deliverable";
|
|
147
|
+
}, {
|
|
148
|
+
readonly type: "bytes32";
|
|
149
|
+
readonly name: "voteHash";
|
|
150
|
+
}];
|
|
151
|
+
}];
|
|
152
|
+
}, {
|
|
153
|
+
readonly name: "finalize";
|
|
154
|
+
readonly type: "function";
|
|
155
|
+
readonly stateMutability: "nonpayable";
|
|
156
|
+
readonly inputs: readonly [{
|
|
157
|
+
readonly type: "uint256";
|
|
158
|
+
readonly name: "jobId";
|
|
159
|
+
}];
|
|
160
|
+
readonly outputs: readonly [];
|
|
161
|
+
}];
|
|
162
|
+
/** ARLVerifiedEvaluator.Phase, by index. */
|
|
163
|
+
export declare const VERDICT_PHASES: readonly ["None", "Configured", "Approved", "Rejected", "Disputed", "Finalized", "Released", "Failed"];
|
|
164
|
+
export interface VerifiedClients {
|
|
165
|
+
publicClient: PublicClient;
|
|
166
|
+
walletClient: WalletClient<never, Chain, Account> | WalletClient;
|
|
167
|
+
}
|
|
168
|
+
export interface OpenVerifiedJobParams {
|
|
169
|
+
provider: Address;
|
|
170
|
+
/** Where validators fetch the arl-reexec/1 spec (https or ipfs); stored as the job description. */
|
|
171
|
+
specUri: string;
|
|
172
|
+
/** `specHash` of that spec (`arl-verifier hash`); validators refuse any other document. */
|
|
173
|
+
specHash: Hex;
|
|
174
|
+
/** Staked validators; 3 to 7 for re-execution. */
|
|
175
|
+
committee: readonly Address[];
|
|
176
|
+
/** Approvals needed: a strict majority of the committee, at least 2. */
|
|
177
|
+
threshold: number;
|
|
178
|
+
/** Seconds until the job expires; it must leave room for the vote and two challenge windows. */
|
|
179
|
+
expiresIn?: bigint;
|
|
180
|
+
voteWindow?: bigint;
|
|
181
|
+
challengeWindow?: bigint;
|
|
182
|
+
/** Bond floor in wei of the escrow token; the evaluator's minimum applies. */
|
|
183
|
+
bond?: bigint;
|
|
184
|
+
/** ERC-8004 agent id of the provider, or 0. */
|
|
185
|
+
providerAgentId?: bigint;
|
|
186
|
+
jobs?: Address;
|
|
187
|
+
evaluator?: Address;
|
|
188
|
+
}
|
|
189
|
+
export declare class VerifiedJobError extends Error {
|
|
190
|
+
name: string;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Refuses a job whose evaluation can no longer finish before its deadline: the vote window and
|
|
194
|
+
* two challenge windows must still fit. Past that point a decision could be overtaken by the
|
|
195
|
+
* deadline, which returns the escrow to the client (security report 2026-10-25). The evaluator
|
|
196
|
+
* enforces this itself from the fixed release on; this check also protects jobs on evaluators
|
|
197
|
+
* deployed before it. Call it before funding (client) and before doing the work (provider).
|
|
198
|
+
*/
|
|
199
|
+
export declare function checkEvaluationFits(publicClient: PublicClient, jobs: Address, jobId: bigint): Promise<void>;
|
|
200
|
+
export declare function checkVerifiedChain(client: PublicClient): Promise<number>;
|
|
201
|
+
export declare function walletAccount(wallet: VerifiedClients["walletClient"]): Account;
|
|
202
|
+
/** Checks the parameters the evaluator would reject, before any transaction is sent. */
|
|
203
|
+
export declare function checkOpenParams(p: OpenVerifiedJobParams): void;
|
|
204
|
+
/**
|
|
205
|
+
* Creates the job (the caller is its client) and configures the committee in two transactions.
|
|
206
|
+
* The provider then approves the committee and sets the budget; the client funds the job.
|
|
207
|
+
*/
|
|
208
|
+
export declare function openVerifiedJob({ publicClient, walletClient }: VerifiedClients, p: OpenVerifiedJobParams): Promise<{
|
|
209
|
+
jobId: bigint;
|
|
210
|
+
createTx: Hash;
|
|
211
|
+
configureTx: Hash;
|
|
212
|
+
}>;
|
|
213
|
+
/** The job's verdict, with the phase as text. */
|
|
214
|
+
export declare function getVerdict(publicClient: PublicClient, jobId: bigint, evaluator?: Address): Promise<{
|
|
215
|
+
phaseName: "None" | "Configured" | "Approved" | "Rejected" | "Disputed" | "Finalized" | "Released" | "Failed" | `Phase ${string}`;
|
|
216
|
+
phase: number;
|
|
217
|
+
providerApproved: boolean;
|
|
218
|
+
approvals: number;
|
|
219
|
+
rejections: number;
|
|
220
|
+
topApprovals: number;
|
|
221
|
+
voteEnds: bigint;
|
|
222
|
+
challengeEnds: bigint;
|
|
223
|
+
challenger: `0x${string}`;
|
|
224
|
+
bond: bigint;
|
|
225
|
+
deliverable: `0x${string}`;
|
|
226
|
+
voteHash: `0x${string}`;
|
|
227
|
+
}>;
|
|
228
|
+
/** Pays out after the challenge window: anyone may call it. */
|
|
229
|
+
export declare function finalizeJob({ publicClient, walletClient }: VerifiedClients, jobId: bigint, evaluator?: Address): Promise<Hash>;
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
// Verified ERC-8183 jobs: open a job on ARLJobs whose evaluator is ARLVerifiedEvaluator, read its
|
|
2
|
+
// verdict and finalize it. A staked validator committee re-executes the job's spec and votes; after
|
|
3
|
+
// the challenge window anyone can finalize, which pays the provider (approved) or refunds the
|
|
4
|
+
// client (rejected). docs/verifiable-execution.md describes the protocol.
|
|
5
|
+
//
|
|
6
|
+
// const jobId = await openVerifiedJob({ publicClient, walletClient }, {
|
|
7
|
+
// provider, specUri, specHash, committee: [v1, v2, v3], threshold: 2 });
|
|
8
|
+
//
|
|
9
|
+
// Base Mainnet and the local chain (docs/mainnet-services.md).
|
|
10
|
+
import { parseAbi, parseEventLogs, } from "viem";
|
|
11
|
+
/**
|
|
12
|
+
* Base Mainnet deployment as prepared in docs/mainnet-services.md: the escrow paid in ARL with the
|
|
13
|
+
* deadline fix (security report 2026-10-25, audit 2026-10-28; deployer nonce 25) and its evaluator
|
|
14
|
+
* with the acceptance fix (security report 2026-10-31; nonce 27). The evaluator at nonce 26 closed
|
|
15
|
+
* acceptance when voting started and is retired. The addresses follow from the nonces; calls check
|
|
16
|
+
* that they hold code before using them.
|
|
17
|
+
*/
|
|
18
|
+
export const BASE_MAINNET_VERIFIED = {
|
|
19
|
+
chainId: 8453,
|
|
20
|
+
token: "0xde91E6f39D50bBd875f831dcc1134A3D7f3aaDb4",
|
|
21
|
+
jobs: "0x9aE99bF9a59aF47E81346B7c0b93Cd2B610455fC",
|
|
22
|
+
evaluator: "0x6648e141cb7473822ddBd8bC039b91e5af3259B4",
|
|
23
|
+
arbiterSafe: "0x8FF6c3aD0991Ac848BFf5d0acD78BBc5e190466f",
|
|
24
|
+
};
|
|
25
|
+
const ALLOWED = [8453, 31_337];
|
|
26
|
+
/** The jobs escrow and evaluator of a chain, or undefined on the local chain. */
|
|
27
|
+
export function verifiedDeployment(chainId) {
|
|
28
|
+
if (chainId === BASE_MAINNET_VERIFIED.chainId)
|
|
29
|
+
return BASE_MAINNET_VERIFIED;
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
async function requireCode(client, address, what) {
|
|
33
|
+
const code = await client.getCode({ address });
|
|
34
|
+
if (code === undefined || code === "0x") {
|
|
35
|
+
throw new VerifiedJobError(`${what} ${address} has no code on this chain (not deployed yet)`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export const verifiedJobsAbi = parseAbi([
|
|
39
|
+
"function createJob(address provider, address evaluator, uint256 expiredAt, string description, address hook) returns (uint256)",
|
|
40
|
+
"event JobCreated(uint256 indexed jobId, address indexed client, address indexed provider, address evaluator, uint256 expiredAt, address hook)",
|
|
41
|
+
]);
|
|
42
|
+
export const verifiedEvaluatorAbi = parseAbi([
|
|
43
|
+
"struct Config { uint8 tier; uint8 threshold; uint64 voteWindow; uint64 challengeWindow; uint96 bond; uint256 providerAgentId; bytes32 specHash; address[] committee; }",
|
|
44
|
+
"struct Verdict { uint8 phase; bool providerApproved; uint8 approvals; uint8 rejections; uint8 topApprovals; uint64 voteEnds; uint64 challengeEnds; address challenger; uint256 bond; bytes32 deliverable; bytes32 voteHash; }",
|
|
45
|
+
"function configure(uint256 jobId, Config cfg)",
|
|
46
|
+
"function getVerdict(uint256 jobId) view returns (Verdict)",
|
|
47
|
+
"function finalize(uint256 jobId)",
|
|
48
|
+
]);
|
|
49
|
+
/** ARLVerifiedEvaluator.Phase, by index. */
|
|
50
|
+
export const VERDICT_PHASES = [
|
|
51
|
+
"None",
|
|
52
|
+
"Configured",
|
|
53
|
+
"Approved",
|
|
54
|
+
"Rejected",
|
|
55
|
+
"Disputed",
|
|
56
|
+
"Finalized",
|
|
57
|
+
"Released",
|
|
58
|
+
"Failed",
|
|
59
|
+
];
|
|
60
|
+
export class VerifiedJobError extends Error {
|
|
61
|
+
name = "VerifiedJobError";
|
|
62
|
+
}
|
|
63
|
+
const deadlineAbi = parseAbi([
|
|
64
|
+
"struct Job { uint256 id; address client; address provider; address evaluator; string description; uint256 budget; uint256 expiredAt; uint8 status; address hook; }",
|
|
65
|
+
"function getJob(uint256 jobId) view returns (Job)",
|
|
66
|
+
"struct Config { uint8 tier; uint8 threshold; uint64 voteWindow; uint64 challengeWindow; uint96 bond; uint256 providerAgentId; bytes32 specHash; address[] committee; }",
|
|
67
|
+
"function getConfig(uint256 jobId) view returns (Config)",
|
|
68
|
+
]);
|
|
69
|
+
/**
|
|
70
|
+
* Refuses a job whose evaluation can no longer finish before its deadline: the vote window and
|
|
71
|
+
* two challenge windows must still fit. Past that point a decision could be overtaken by the
|
|
72
|
+
* deadline, which returns the escrow to the client (security report 2026-10-25). The evaluator
|
|
73
|
+
* enforces this itself from the fixed release on; this check also protects jobs on evaluators
|
|
74
|
+
* deployed before it. Call it before funding (client) and before doing the work (provider).
|
|
75
|
+
*/
|
|
76
|
+
export async function checkEvaluationFits(publicClient, jobs, jobId) {
|
|
77
|
+
const job = await publicClient.readContract({
|
|
78
|
+
address: jobs,
|
|
79
|
+
abi: deadlineAbi,
|
|
80
|
+
functionName: "getJob",
|
|
81
|
+
args: [jobId],
|
|
82
|
+
});
|
|
83
|
+
const cfg = await publicClient.readContract({
|
|
84
|
+
address: job.evaluator,
|
|
85
|
+
abi: deadlineAbi,
|
|
86
|
+
functionName: "getConfig",
|
|
87
|
+
args: [jobId],
|
|
88
|
+
});
|
|
89
|
+
const { timestamp } = await publicClient.getBlock();
|
|
90
|
+
const needed = cfg.voteWindow + 2n * cfg.challengeWindow;
|
|
91
|
+
if (timestamp + needed > job.expiredAt)
|
|
92
|
+
throw new VerifiedJobError(`job ${jobId.toString()}: too late, the evaluation needs ${needed.toString()} s before the deadline`);
|
|
93
|
+
}
|
|
94
|
+
export async function checkVerifiedChain(client) {
|
|
95
|
+
const id = await client.getChainId();
|
|
96
|
+
if (!ALLOWED.includes(id))
|
|
97
|
+
throw new VerifiedJobError(`chain ${id} is not supported`);
|
|
98
|
+
return id;
|
|
99
|
+
}
|
|
100
|
+
export function walletAccount(wallet) {
|
|
101
|
+
if (!wallet.account)
|
|
102
|
+
throw new VerifiedJobError("the wallet client has no account");
|
|
103
|
+
return wallet.account;
|
|
104
|
+
}
|
|
105
|
+
/** Checks the parameters the evaluator would reject, before any transaction is sent. */
|
|
106
|
+
export function checkOpenParams(p) {
|
|
107
|
+
const n = p.committee.length;
|
|
108
|
+
if (n < 3 || n > 7)
|
|
109
|
+
throw new VerifiedJobError("committee must have 3 to 7 validators");
|
|
110
|
+
if (new Set(p.committee.map((a) => a.toLowerCase())).size !== n)
|
|
111
|
+
throw new VerifiedJobError("committee has duplicates");
|
|
112
|
+
if (p.threshold < 2 || p.threshold > n || 2 * p.threshold <= n)
|
|
113
|
+
throw new VerifiedJobError("threshold must be a strict majority of at least 2");
|
|
114
|
+
if (!/^(https|ipfs):\/\//.test(p.specUri))
|
|
115
|
+
throw new VerifiedJobError("specUri must be https or ipfs");
|
|
116
|
+
if (!/^0x[0-9a-fA-F]{64}$/.test(p.specHash))
|
|
117
|
+
throw new VerifiedJobError("specHash must be 32 bytes");
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Creates the job (the caller is its client) and configures the committee in two transactions.
|
|
121
|
+
* The provider then approves the committee and sets the budget; the client funds the job.
|
|
122
|
+
*/
|
|
123
|
+
export async function openVerifiedJob({ publicClient, walletClient }, p) {
|
|
124
|
+
checkOpenParams(p);
|
|
125
|
+
const chainId = await checkVerifiedChain(publicClient);
|
|
126
|
+
const known = verifiedDeployment(chainId);
|
|
127
|
+
const jobs = p.jobs ?? known?.jobs;
|
|
128
|
+
const evaluator = p.evaluator ?? known?.evaluator;
|
|
129
|
+
if (!jobs || !evaluator)
|
|
130
|
+
throw new VerifiedJobError("jobs and evaluator are required locally");
|
|
131
|
+
await requireCode(publicClient, jobs, "jobs escrow");
|
|
132
|
+
await requireCode(publicClient, evaluator, "evaluator");
|
|
133
|
+
const voteWindow = p.voteWindow ?? 3600n;
|
|
134
|
+
const challengeWindow = p.challengeWindow ?? 3600n;
|
|
135
|
+
const { timestamp } = await publicClient.getBlock();
|
|
136
|
+
const expiredAt = timestamp + (p.expiresIn ?? 86400n);
|
|
137
|
+
const from = walletAccount(walletClient);
|
|
138
|
+
const createTx = await walletClient.writeContract({
|
|
139
|
+
account: from,
|
|
140
|
+
chain: walletClient.chain,
|
|
141
|
+
address: jobs,
|
|
142
|
+
abi: verifiedJobsAbi,
|
|
143
|
+
functionName: "createJob",
|
|
144
|
+
args: [
|
|
145
|
+
p.provider,
|
|
146
|
+
evaluator,
|
|
147
|
+
expiredAt,
|
|
148
|
+
p.specUri,
|
|
149
|
+
"0x0000000000000000000000000000000000000000",
|
|
150
|
+
],
|
|
151
|
+
});
|
|
152
|
+
const receipt = await publicClient.waitForTransactionReceipt({ hash: createTx });
|
|
153
|
+
if (receipt.status !== "success")
|
|
154
|
+
throw new VerifiedJobError("createJob reverted");
|
|
155
|
+
const created = parseEventLogs({
|
|
156
|
+
abi: verifiedJobsAbi,
|
|
157
|
+
eventName: "JobCreated",
|
|
158
|
+
logs: receipt.logs,
|
|
159
|
+
}).find((l) => l.address.toLowerCase() === jobs.toLowerCase());
|
|
160
|
+
if (!created)
|
|
161
|
+
throw new VerifiedJobError("JobCreated event missing");
|
|
162
|
+
const jobId = created.args.jobId;
|
|
163
|
+
const configureTx = await walletClient.writeContract({
|
|
164
|
+
account: from,
|
|
165
|
+
chain: walletClient.chain,
|
|
166
|
+
address: evaluator,
|
|
167
|
+
abi: verifiedEvaluatorAbi,
|
|
168
|
+
functionName: "configure",
|
|
169
|
+
args: [
|
|
170
|
+
jobId,
|
|
171
|
+
{
|
|
172
|
+
tier: 1,
|
|
173
|
+
threshold: p.threshold,
|
|
174
|
+
voteWindow,
|
|
175
|
+
challengeWindow,
|
|
176
|
+
bond: p.bond ?? 10n ** 18n,
|
|
177
|
+
providerAgentId: p.providerAgentId ?? 0n,
|
|
178
|
+
specHash: p.specHash,
|
|
179
|
+
committee: [...p.committee],
|
|
180
|
+
},
|
|
181
|
+
],
|
|
182
|
+
});
|
|
183
|
+
const configured = await publicClient.waitForTransactionReceipt({ hash: configureTx });
|
|
184
|
+
if (configured.status !== "success")
|
|
185
|
+
throw new VerifiedJobError("configure reverted");
|
|
186
|
+
return { jobId, createTx, configureTx };
|
|
187
|
+
}
|
|
188
|
+
/** The job's verdict, with the phase as text. */
|
|
189
|
+
export async function getVerdict(publicClient, jobId, evaluator) {
|
|
190
|
+
const chainId = await checkVerifiedChain(publicClient);
|
|
191
|
+
const address = evaluator ?? verifiedDeployment(chainId)?.evaluator;
|
|
192
|
+
if (!address)
|
|
193
|
+
throw new VerifiedJobError("evaluator is required locally");
|
|
194
|
+
const v = await publicClient.readContract({
|
|
195
|
+
address,
|
|
196
|
+
abi: verifiedEvaluatorAbi,
|
|
197
|
+
functionName: "getVerdict",
|
|
198
|
+
args: [jobId],
|
|
199
|
+
});
|
|
200
|
+
return { ...v, phaseName: VERDICT_PHASES[v.phase] ?? `Phase ${String(v.phase)}` };
|
|
201
|
+
}
|
|
202
|
+
/** Pays out after the challenge window: anyone may call it. */
|
|
203
|
+
export async function finalizeJob({ publicClient, walletClient }, jobId, evaluator) {
|
|
204
|
+
const chainId = await checkVerifiedChain(publicClient);
|
|
205
|
+
const address = evaluator ?? verifiedDeployment(chainId)?.evaluator;
|
|
206
|
+
if (!address)
|
|
207
|
+
throw new VerifiedJobError("evaluator is required locally");
|
|
208
|
+
const hash = await walletClient.writeContract({
|
|
209
|
+
account: walletAccount(walletClient),
|
|
210
|
+
chain: walletClient.chain,
|
|
211
|
+
address,
|
|
212
|
+
abi: verifiedEvaluatorAbi,
|
|
213
|
+
functionName: "finalize",
|
|
214
|
+
args: [jobId],
|
|
215
|
+
});
|
|
216
|
+
const r = await publicClient.waitForTransactionReceipt({ hash });
|
|
217
|
+
if (r.status !== "success")
|
|
218
|
+
throw new VerifiedJobError("finalize reverted");
|
|
219
|
+
return hash;
|
|
220
|
+
}
|