irsb-sdk 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/dist/index.mjs ADDED
@@ -0,0 +1,436 @@
1
+ // src/client.ts
2
+ import { ethers, Contract } from "ethers";
3
+
4
+ // src/types.ts
5
+ var SolverStatus = /* @__PURE__ */ ((SolverStatus3) => {
6
+ SolverStatus3[SolverStatus3["Inactive"] = 0] = "Inactive";
7
+ SolverStatus3[SolverStatus3["Active"] = 1] = "Active";
8
+ SolverStatus3[SolverStatus3["Jailed"] = 2] = "Jailed";
9
+ SolverStatus3[SolverStatus3["Banned"] = 3] = "Banned";
10
+ return SolverStatus3;
11
+ })(SolverStatus || {});
12
+ var ReceiptStatus = /* @__PURE__ */ ((ReceiptStatus3) => {
13
+ ReceiptStatus3[ReceiptStatus3["None"] = 0] = "None";
14
+ ReceiptStatus3[ReceiptStatus3["Posted"] = 1] = "Posted";
15
+ ReceiptStatus3[ReceiptStatus3["Challenged"] = 2] = "Challenged";
16
+ ReceiptStatus3[ReceiptStatus3["Finalized"] = 3] = "Finalized";
17
+ ReceiptStatus3[ReceiptStatus3["Slashed"] = 4] = "Slashed";
18
+ return ReceiptStatus3;
19
+ })(ReceiptStatus || {});
20
+ var DisputeReason = /* @__PURE__ */ ((DisputeReason3) => {
21
+ DisputeReason3[DisputeReason3["None"] = 0] = "None";
22
+ DisputeReason3[DisputeReason3["Timeout"] = 1] = "Timeout";
23
+ DisputeReason3[DisputeReason3["MinOutViolation"] = 2] = "MinOutViolation";
24
+ DisputeReason3[DisputeReason3["WrongToken"] = 3] = "WrongToken";
25
+ DisputeReason3[DisputeReason3["WrongChain"] = 4] = "WrongChain";
26
+ DisputeReason3[DisputeReason3["WrongRecipient"] = 5] = "WrongRecipient";
27
+ DisputeReason3[DisputeReason3["ReceiptForgery"] = 6] = "ReceiptForgery";
28
+ DisputeReason3[DisputeReason3["InvalidSignature"] = 7] = "InvalidSignature";
29
+ return DisputeReason3;
30
+ })(DisputeReason || {});
31
+ var DisputeStatus = /* @__PURE__ */ ((DisputeStatus2) => {
32
+ DisputeStatus2[DisputeStatus2["None"] = 0] = "None";
33
+ DisputeStatus2[DisputeStatus2["Open"] = 1] = "Open";
34
+ DisputeStatus2[DisputeStatus2["EvidenceSubmitted"] = 2] = "EvidenceSubmitted";
35
+ DisputeStatus2[DisputeStatus2["Escalated"] = 3] = "Escalated";
36
+ DisputeStatus2[DisputeStatus2["Resolved"] = 4] = "Resolved";
37
+ return DisputeStatus2;
38
+ })(DisputeStatus || {});
39
+ var DisputeResolution = /* @__PURE__ */ ((DisputeResolution2) => {
40
+ DisputeResolution2[DisputeResolution2["None"] = 0] = "None";
41
+ DisputeResolution2[DisputeResolution2["SolverWins"] = 1] = "SolverWins";
42
+ DisputeResolution2[DisputeResolution2["ChallengerWins"] = 2] = "ChallengerWins";
43
+ DisputeResolution2[DisputeResolution2["Split"] = 3] = "Split";
44
+ DisputeResolution2[DisputeResolution2["Timeout"] = 4] = "Timeout";
45
+ return DisputeResolution2;
46
+ })(DisputeResolution || {});
47
+ var CHAIN_CONFIGS = {
48
+ sepolia: {
49
+ chainId: 11155111,
50
+ rpcUrl: "https://rpc.sepolia.org",
51
+ solverRegistry: "0xB6ab964832808E49635fF82D1996D6a888ecB745",
52
+ intentReceiptHub: "0xD66A1e880AA3939CA066a9EA1dD37ad3d01D977c",
53
+ disputeModule: "0x144DfEcB57B08471e2A75E78fc0d2A74A89DB79D"
54
+ }
55
+ // mainnet: { ... } // Add after mainnet deployment
56
+ };
57
+ var CONSTANTS = {
58
+ MINIMUM_BOND: BigInt("100000000000000000"),
59
+ // 0.1 ETH
60
+ WITHDRAWAL_COOLDOWN: BigInt(7 * 24 * 60 * 60),
61
+ // 7 days
62
+ MAX_JAILS: 3,
63
+ CHALLENGE_WINDOW: BigInt(60 * 60),
64
+ // 1 hour
65
+ CHALLENGER_BOND_BPS: 1e3,
66
+ // 10%
67
+ EVIDENCE_WINDOW: BigInt(24 * 60 * 60),
68
+ // 24 hours
69
+ ARBITRATION_TIMEOUT: BigInt(7 * 24 * 60 * 60)
70
+ // 7 days
71
+ };
72
+
73
+ // src/contracts/abis.ts
74
+ var SOLVER_REGISTRY_ABI = [
75
+ // Read functions
76
+ "function MINIMUM_BOND() view returns (uint256)",
77
+ "function WITHDRAWAL_COOLDOWN() view returns (uint64)",
78
+ "function MAX_JAILS() view returns (uint8)",
79
+ "function solvers(address) view returns (uint256 bondAmount, uint256 lockedAmount, uint256 reputation, uint64 registrationTime, uint64 lastActiveTime, uint64 totalIntents, uint64 successfulIntents, uint8 jailCount, uint8 status, uint256 pendingWithdrawal, uint64 withdrawalRequestTime)",
80
+ "function isActiveSolver(address solver) view returns (bool)",
81
+ "function getSolverBond(address solver) view returns (uint256)",
82
+ "function getAvailableBond(address solver) view returns (uint256)",
83
+ "function owner() view returns (address)",
84
+ // Write functions
85
+ "function register() payable",
86
+ "function depositBond() payable",
87
+ "function requestWithdrawal(uint256 amount)",
88
+ "function cancelWithdrawal()",
89
+ "function executeWithdrawal()",
90
+ "function unjail() payable",
91
+ // Events
92
+ "event SolverRegistered(address indexed solver, uint256 bondAmount)",
93
+ "event BondDeposited(address indexed solver, uint256 amount, uint256 newTotal)",
94
+ "event WithdrawalRequested(address indexed solver, uint256 amount)",
95
+ "event WithdrawalExecuted(address indexed solver, uint256 amount)",
96
+ "event WithdrawalCancelled(address indexed solver)",
97
+ "event SolverSlashed(address indexed solver, uint256 amount, bytes32 reason)",
98
+ "event SolverJailed(address indexed solver, uint8 jailCount)",
99
+ "event SolverUnjailed(address indexed solver)",
100
+ "event SolverBanned(address indexed solver)"
101
+ ];
102
+ var INTENT_RECEIPT_HUB_ABI = [
103
+ // Read functions
104
+ "function challengeWindow() view returns (uint64)",
105
+ "function challengerBondBps() view returns (uint16)",
106
+ "function receipts(bytes32) view returns (address solver, bytes32 intentHash, bytes32 constraintsHash, bytes32 outcomeHash, bytes32 evidenceHash, uint64 postedAt, uint64 deadline, bytes solverSig, uint8 status)",
107
+ "function challenges(bytes32) view returns (address challenger, uint8 reason, uint256 bond, uint64 timestamp)",
108
+ "function getReceipt(bytes32 intentHash) view returns (tuple(address solver, bytes32 intentHash, bytes32 constraintsHash, bytes32 outcomeHash, bytes32 evidenceHash, uint64 postedAt, uint64 deadline, bytes solverSig, uint8 status))",
109
+ "function getChallenge(bytes32 intentHash) view returns (tuple(address challenger, uint8 reason, uint256 bond, uint64 timestamp))",
110
+ "function solverRegistry() view returns (address)",
111
+ // Write functions
112
+ "function postReceipt(bytes32 intentHash, bytes32 constraintsHash, bytes32 outcomeHash, bytes32 evidenceHash, uint64 deadline, bytes solverSig)",
113
+ "function challengeReceipt(bytes32 intentHash, uint8 reason) payable",
114
+ "function finalizeReceipt(bytes32 intentHash)",
115
+ "function resolveDeterministic(bytes32 intentHash, bytes settlementProof)",
116
+ // Events
117
+ "event ReceiptPosted(bytes32 indexed intentHash, address indexed solver, uint64 deadline)",
118
+ "event ReceiptChallenged(bytes32 indexed intentHash, address indexed challenger, uint8 reason, uint256 bond)",
119
+ "event ReceiptFinalized(bytes32 indexed intentHash)",
120
+ "event ReceiptSlashed(bytes32 indexed intentHash, address indexed solver, uint256 slashAmount)",
121
+ "event ChallengeResolved(bytes32 indexed intentHash, bool solverWins)"
122
+ ];
123
+ var DISPUTE_MODULE_ABI = [
124
+ // Read functions
125
+ "function evidenceWindow() view returns (uint64)",
126
+ "function arbitrationTimeout() view returns (uint64)",
127
+ "function arbitrator() view returns (address)",
128
+ "function disputes(bytes32) view returns (bytes32 intentHash, address challenger, address solver, uint8 reason, bytes32 solverEvidence, bytes32 challengerEvidence, uint64 createdAt, uint64 evidenceDeadline, uint8 status, uint8 resolution)",
129
+ "function getDispute(bytes32 intentHash) view returns (tuple(bytes32 intentHash, address challenger, address solver, uint8 reason, bytes32 solverEvidence, bytes32 challengerEvidence, uint64 createdAt, uint64 evidenceDeadline, uint8 status, uint8 resolution))",
130
+ // Write functions
131
+ "function submitEvidence(bytes32 intentHash, bytes32 evidenceHash)",
132
+ "function escalateToArbitration(bytes32 intentHash)",
133
+ "function resolveArbitration(bytes32 intentHash, uint8 resolution, uint16 solverShareBps)",
134
+ "function resolveByTimeout(bytes32 intentHash)",
135
+ // Events
136
+ "event DisputeCreated(bytes32 indexed intentHash, address indexed challenger, address indexed solver, uint8 reason)",
137
+ "event EvidenceSubmitted(bytes32 indexed intentHash, address indexed submitter, bytes32 evidenceHash)",
138
+ "event DisputeEscalated(bytes32 indexed intentHash)",
139
+ "event DisputeResolved(bytes32 indexed intentHash, uint8 resolution)"
140
+ ];
141
+
142
+ // src/client.ts
143
+ var IRSBClient = class {
144
+ config;
145
+ provider;
146
+ signer;
147
+ solverRegistry;
148
+ intentReceiptHub;
149
+ disputeModule;
150
+ constructor(options) {
151
+ if (typeof options.chain === "string") {
152
+ const config = CHAIN_CONFIGS[options.chain];
153
+ if (!config) {
154
+ throw new Error(`Unknown chain: ${options.chain}. Available: ${Object.keys(CHAIN_CONFIGS).join(", ")}`);
155
+ }
156
+ this.config = config;
157
+ } else {
158
+ this.config = options.chain;
159
+ }
160
+ if (options.signer) {
161
+ this.signer = options.signer;
162
+ this.provider = options.signer.provider;
163
+ } else if (options.provider) {
164
+ this.provider = options.provider;
165
+ } else {
166
+ this.provider = new ethers.JsonRpcProvider(this.config.rpcUrl);
167
+ }
168
+ const signerOrProvider = this.signer || this.provider;
169
+ this.solverRegistry = new Contract(
170
+ this.config.solverRegistry,
171
+ SOLVER_REGISTRY_ABI,
172
+ signerOrProvider
173
+ );
174
+ this.intentReceiptHub = new Contract(
175
+ this.config.intentReceiptHub,
176
+ INTENT_RECEIPT_HUB_ABI,
177
+ signerOrProvider
178
+ );
179
+ this.disputeModule = new Contract(
180
+ this.config.disputeModule,
181
+ DISPUTE_MODULE_ABI,
182
+ signerOrProvider
183
+ );
184
+ }
185
+ // ============ Solver Registry - Read ============
186
+ /**
187
+ * Get solver information
188
+ */
189
+ async getSolver(address) {
190
+ const data = await this.solverRegistry.solvers(address);
191
+ return {
192
+ bondAmount: data.bondAmount,
193
+ lockedAmount: data.lockedAmount,
194
+ reputation: data.reputation,
195
+ registrationTime: data.registrationTime,
196
+ lastActiveTime: data.lastActiveTime,
197
+ totalIntents: data.totalIntents,
198
+ successfulIntents: data.successfulIntents,
199
+ jailCount: Number(data.jailCount),
200
+ status: Number(data.status),
201
+ pendingWithdrawal: data.pendingWithdrawal,
202
+ withdrawalRequestTime: data.withdrawalRequestTime
203
+ };
204
+ }
205
+ /**
206
+ * Check if address is an active solver
207
+ */
208
+ async isActiveSolver(address) {
209
+ return this.solverRegistry.isActiveSolver(address);
210
+ }
211
+ /**
212
+ * Get solver's total bond amount
213
+ */
214
+ async getSolverBond(address) {
215
+ return this.solverRegistry.getSolverBond(address);
216
+ }
217
+ /**
218
+ * Get solver's available (unlocked) bond
219
+ */
220
+ async getAvailableBond(address) {
221
+ return this.solverRegistry.getAvailableBond(address);
222
+ }
223
+ /**
224
+ * Get minimum bond required for registration
225
+ */
226
+ async getMinimumBond() {
227
+ return this.solverRegistry.MINIMUM_BOND();
228
+ }
229
+ // ============ Solver Registry - Write ============
230
+ /**
231
+ * Register as a solver with initial bond
232
+ * @param options.value - ETH to deposit as bond (must be >= MINIMUM_BOND)
233
+ */
234
+ async register(options) {
235
+ this.requireSigner();
236
+ return this.solverRegistry.register({ value: options.value });
237
+ }
238
+ /**
239
+ * Deposit additional bond
240
+ * @param options.value - ETH to deposit
241
+ */
242
+ async depositBond(options) {
243
+ this.requireSigner();
244
+ return this.solverRegistry.depositBond({ value: options.value });
245
+ }
246
+ /**
247
+ * Request withdrawal of bond (starts cooldown)
248
+ * @param amount - Amount to withdraw
249
+ */
250
+ async requestWithdrawal(amount) {
251
+ this.requireSigner();
252
+ return this.solverRegistry.requestWithdrawal(amount);
253
+ }
254
+ /**
255
+ * Cancel pending withdrawal
256
+ */
257
+ async cancelWithdrawal() {
258
+ this.requireSigner();
259
+ return this.solverRegistry.cancelWithdrawal();
260
+ }
261
+ /**
262
+ * Execute withdrawal after cooldown
263
+ */
264
+ async executeWithdrawal() {
265
+ this.requireSigner();
266
+ return this.solverRegistry.executeWithdrawal();
267
+ }
268
+ /**
269
+ * Unjail solver by paying penalty
270
+ * @param options.value - Unjail penalty amount
271
+ */
272
+ async unjail(options) {
273
+ this.requireSigner();
274
+ return this.solverRegistry.unjail({ value: options.value });
275
+ }
276
+ // ============ Intent Receipt Hub - Read ============
277
+ /**
278
+ * Get receipt by intent hash
279
+ */
280
+ async getReceipt(intentHash) {
281
+ try {
282
+ const data = await this.intentReceiptHub.getReceipt(intentHash);
283
+ if (data.solver === ethers.ZeroAddress) return null;
284
+ return {
285
+ solver: data.solver,
286
+ intentHash: data.intentHash,
287
+ constraintsHash: data.constraintsHash,
288
+ outcomeHash: data.outcomeHash,
289
+ evidenceHash: data.evidenceHash,
290
+ postedAt: data.postedAt,
291
+ deadline: data.deadline,
292
+ solverSig: data.solverSig,
293
+ status: Number(data.status)
294
+ };
295
+ } catch {
296
+ return null;
297
+ }
298
+ }
299
+ /**
300
+ * Get challenge for an intent
301
+ */
302
+ async getChallenge(intentHash) {
303
+ try {
304
+ const data = await this.intentReceiptHub.getChallenge(intentHash);
305
+ if (data.challenger === ethers.ZeroAddress) return null;
306
+ return {
307
+ challenger: data.challenger,
308
+ reason: Number(data.reason),
309
+ bond: data.bond,
310
+ timestamp: data.timestamp
311
+ };
312
+ } catch {
313
+ return null;
314
+ }
315
+ }
316
+ /**
317
+ * Get challenge window duration
318
+ */
319
+ async getChallengeWindow() {
320
+ return this.intentReceiptHub.challengeWindow();
321
+ }
322
+ // ============ Intent Receipt Hub - Write ============
323
+ /**
324
+ * Post a receipt for an executed intent
325
+ */
326
+ async postReceipt(params) {
327
+ this.requireSigner();
328
+ return this.intentReceiptHub.postReceipt(
329
+ params.intentHash,
330
+ params.constraintsHash,
331
+ params.outcomeHash,
332
+ params.evidenceHash,
333
+ params.deadline,
334
+ params.solverSig
335
+ );
336
+ }
337
+ /**
338
+ * Challenge a receipt
339
+ * @param intentHash - Hash of the intent to challenge
340
+ * @param reason - Dispute reason code
341
+ * @param options.value - Challenger bond (must meet minimum)
342
+ */
343
+ async challengeReceipt(intentHash, reason, options) {
344
+ this.requireSigner();
345
+ return this.intentReceiptHub.challengeReceipt(intentHash, reason, {
346
+ value: options.value
347
+ });
348
+ }
349
+ /**
350
+ * Finalize a receipt after challenge window
351
+ */
352
+ async finalizeReceipt(intentHash) {
353
+ this.requireSigner();
354
+ return this.intentReceiptHub.finalizeReceipt(intentHash);
355
+ }
356
+ // ============ Dispute Module - Read ============
357
+ /**
358
+ * Get dispute details
359
+ */
360
+ async getDispute(intentHash) {
361
+ try {
362
+ const data = await this.disputeModule.getDispute(intentHash);
363
+ if (data.challenger === ethers.ZeroAddress) return null;
364
+ return data;
365
+ } catch {
366
+ return null;
367
+ }
368
+ }
369
+ // ============ Dispute Module - Write ============
370
+ /**
371
+ * Submit evidence for a dispute
372
+ */
373
+ async submitEvidence(intentHash, evidenceHash) {
374
+ this.requireSigner();
375
+ return this.disputeModule.submitEvidence(intentHash, evidenceHash);
376
+ }
377
+ /**
378
+ * Escalate dispute to arbitration
379
+ */
380
+ async escalateToArbitration(intentHash) {
381
+ this.requireSigner();
382
+ return this.disputeModule.escalateToArbitration(intentHash);
383
+ }
384
+ // ============ Utilities ============
385
+ /**
386
+ * Create a signed receipt for posting
387
+ */
388
+ async signReceipt(params) {
389
+ this.requireSigner();
390
+ const message = ethers.solidityPackedKeccak256(
391
+ ["bytes32", "bytes32", "bytes32", "bytes32", "uint64"],
392
+ [
393
+ params.intentHash,
394
+ params.constraintsHash,
395
+ params.outcomeHash,
396
+ params.evidenceHash,
397
+ params.deadline
398
+ ]
399
+ );
400
+ return this.signer.signMessage(ethers.getBytes(message));
401
+ }
402
+ /**
403
+ * Calculate required challenger bond for a slash amount
404
+ */
405
+ calculateChallengerBond(slashAmount) {
406
+ return slashAmount * BigInt(CONSTANTS.CHALLENGER_BOND_BPS) / BigInt(1e4);
407
+ }
408
+ /**
409
+ * Get contract addresses
410
+ */
411
+ getAddresses() {
412
+ return {
413
+ solverRegistry: this.config.solverRegistry,
414
+ intentReceiptHub: this.config.intentReceiptHub,
415
+ disputeModule: this.config.disputeModule
416
+ };
417
+ }
418
+ requireSigner() {
419
+ if (!this.signer) {
420
+ throw new Error("Signer required for write operations");
421
+ }
422
+ }
423
+ };
424
+ export {
425
+ CHAIN_CONFIGS,
426
+ CONSTANTS,
427
+ DISPUTE_MODULE_ABI,
428
+ DisputeReason,
429
+ DisputeResolution,
430
+ DisputeStatus,
431
+ INTENT_RECEIPT_HUB_ABI,
432
+ IRSBClient,
433
+ ReceiptStatus,
434
+ SOLVER_REGISTRY_ABI,
435
+ SolverStatus
436
+ };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "irsb-sdk",
3
+ "version": "0.1.0",
4
+ "description": "SDK for IRSB Protocol - Intent Receipts & Solver Bonds",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "src"
11
+ ],
12
+ "scripts": {
13
+ "build": "tsup src/index.ts --format cjs,esm --dts",
14
+ "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
15
+ "lint": "eslint src/",
16
+ "test": "vitest",
17
+ "prepublishOnly": "npm run build"
18
+ },
19
+ "keywords": [
20
+ "ethereum",
21
+ "intent",
22
+ "solver",
23
+ "erc-7683",
24
+ "defi",
25
+ "accountability"
26
+ ],
27
+ "author": "Intent Solutions IO <jeremy@intentsolutions.io>",
28
+ "license": "MIT",
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://github.com/intent-solutions-io/irsb-protocol.git",
32
+ "directory": "sdk"
33
+ },
34
+ "homepage": "https://irsb-protocol.web.app",
35
+ "dependencies": {
36
+ "ethers": "^6.9.0"
37
+ },
38
+ "devDependencies": {
39
+ "@types/node": "^20.10.0",
40
+ "tsup": "^8.0.1",
41
+ "typescript": "^5.3.0",
42
+ "vitest": "^1.0.0"
43
+ },
44
+ "engines": {
45
+ "node": ">=18.0.0"
46
+ }
47
+ }