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