@oobe-protocol-labs/synapse-sap-sdk 0.6.3 → 0.8.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.
Files changed (132) hide show
  1. package/dist/cjs/constants/seeds.js +9 -0
  2. package/dist/cjs/constants/seeds.js.map +1 -1
  3. package/dist/cjs/core/client.js +55 -0
  4. package/dist/cjs/core/client.js.map +1 -1
  5. package/dist/cjs/idl/synapse_agent_sap.json +8571 -4106
  6. package/dist/cjs/index.js +29 -3
  7. package/dist/cjs/index.js.map +1 -1
  8. package/dist/cjs/modules/escrow-v2.js +220 -0
  9. package/dist/cjs/modules/escrow-v2.js.map +1 -0
  10. package/dist/cjs/modules/escrow.js +4 -0
  11. package/dist/cjs/modules/escrow.js.map +1 -1
  12. package/dist/cjs/modules/index.js +9 -1
  13. package/dist/cjs/modules/index.js.map +1 -1
  14. package/dist/cjs/modules/receipt.js +144 -0
  15. package/dist/cjs/modules/receipt.js.map +1 -0
  16. package/dist/cjs/modules/staking.js +94 -0
  17. package/dist/cjs/modules/staking.js.map +1 -0
  18. package/dist/cjs/modules/subscription.js +96 -0
  19. package/dist/cjs/modules/subscription.js.map +1 -0
  20. package/dist/cjs/pda/index.js +166 -1
  21. package/dist/cjs/pda/index.js.map +1 -1
  22. package/dist/cjs/registries/x402.js +88 -51
  23. package/dist/cjs/registries/x402.js.map +1 -1
  24. package/dist/cjs/types/enums.js +96 -1
  25. package/dist/cjs/types/enums.js.map +1 -1
  26. package/dist/cjs/types/index.js +6 -1
  27. package/dist/cjs/types/index.js.map +1 -1
  28. package/dist/cjs/types/instructions.js.map +1 -1
  29. package/dist/cjs/utils/escrow-validation.js +219 -0
  30. package/dist/cjs/utils/escrow-validation.js.map +1 -0
  31. package/dist/cjs/utils/index.js +12 -1
  32. package/dist/cjs/utils/index.js.map +1 -1
  33. package/dist/cjs/utils/merchant-validator.js +246 -0
  34. package/dist/cjs/utils/merchant-validator.js.map +1 -0
  35. package/dist/cjs/utils/x402-direct.js +231 -0
  36. package/dist/cjs/utils/x402-direct.js.map +1 -0
  37. package/dist/esm/constants/seeds.js +9 -0
  38. package/dist/esm/constants/seeds.js.map +1 -1
  39. package/dist/esm/core/client.js +55 -0
  40. package/dist/esm/core/client.js.map +1 -1
  41. package/dist/esm/idl/synapse_agent_sap.json +8571 -4106
  42. package/dist/esm/index.js +5 -3
  43. package/dist/esm/index.js.map +1 -1
  44. package/dist/esm/modules/escrow-v2.js +216 -0
  45. package/dist/esm/modules/escrow-v2.js.map +1 -0
  46. package/dist/esm/modules/escrow.js +4 -0
  47. package/dist/esm/modules/escrow.js.map +1 -1
  48. package/dist/esm/modules/index.js +4 -0
  49. package/dist/esm/modules/index.js.map +1 -1
  50. package/dist/esm/modules/receipt.js +140 -0
  51. package/dist/esm/modules/receipt.js.map +1 -0
  52. package/dist/esm/modules/staking.js +90 -0
  53. package/dist/esm/modules/staking.js.map +1 -0
  54. package/dist/esm/modules/subscription.js +92 -0
  55. package/dist/esm/modules/subscription.js.map +1 -0
  56. package/dist/esm/pda/index.js +157 -0
  57. package/dist/esm/pda/index.js.map +1 -1
  58. package/dist/esm/registries/x402.js +89 -52
  59. package/dist/esm/registries/x402.js.map +1 -1
  60. package/dist/esm/types/enums.js +95 -0
  61. package/dist/esm/types/enums.js.map +1 -1
  62. package/dist/esm/types/index.js +1 -1
  63. package/dist/esm/types/index.js.map +1 -1
  64. package/dist/esm/types/instructions.js.map +1 -1
  65. package/dist/esm/utils/escrow-validation.js +212 -0
  66. package/dist/esm/utils/escrow-validation.js.map +1 -0
  67. package/dist/esm/utils/index.js +4 -0
  68. package/dist/esm/utils/index.js.map +1 -1
  69. package/dist/esm/utils/merchant-validator.js +241 -0
  70. package/dist/esm/utils/merchant-validator.js.map +1 -0
  71. package/dist/esm/utils/x402-direct.js +228 -0
  72. package/dist/esm/utils/x402-direct.js.map +1 -0
  73. package/dist/types/constants/seeds.d.ts +9 -0
  74. package/dist/types/constants/seeds.d.ts.map +1 -1
  75. package/dist/types/core/client.d.ts +43 -0
  76. package/dist/types/core/client.d.ts.map +1 -1
  77. package/dist/types/index.d.ts +6 -4
  78. package/dist/types/index.d.ts.map +1 -1
  79. package/dist/types/modules/escrow-v2.d.ts +58 -0
  80. package/dist/types/modules/escrow-v2.d.ts.map +1 -0
  81. package/dist/types/modules/escrow.d.ts +4 -0
  82. package/dist/types/modules/escrow.d.ts.map +1 -1
  83. package/dist/types/modules/index.d.ts +4 -0
  84. package/dist/types/modules/index.d.ts.map +1 -1
  85. package/dist/types/modules/receipt.d.ts +77 -0
  86. package/dist/types/modules/receipt.d.ts.map +1 -0
  87. package/dist/types/modules/staking.d.ts +32 -0
  88. package/dist/types/modules/staking.d.ts.map +1 -0
  89. package/dist/types/modules/subscription.d.ts +33 -0
  90. package/dist/types/modules/subscription.d.ts.map +1 -0
  91. package/dist/types/pda/index.d.ts +114 -0
  92. package/dist/types/pda/index.d.ts.map +1 -1
  93. package/dist/types/plugin/schemas.d.ts +2 -2
  94. package/dist/types/registries/x402.d.ts +14 -12
  95. package/dist/types/registries/x402.d.ts.map +1 -1
  96. package/dist/types/types/accounts.d.ts +201 -1
  97. package/dist/types/types/accounts.d.ts.map +1 -1
  98. package/dist/types/types/enums.d.ts +115 -0
  99. package/dist/types/types/enums.d.ts.map +1 -1
  100. package/dist/types/types/index.d.ts +4 -4
  101. package/dist/types/types/index.d.ts.map +1 -1
  102. package/dist/types/types/instructions.d.ts +34 -0
  103. package/dist/types/types/instructions.d.ts.map +1 -1
  104. package/dist/types/utils/escrow-validation.d.ts +145 -0
  105. package/dist/types/utils/escrow-validation.d.ts.map +1 -0
  106. package/dist/types/utils/index.d.ts +6 -0
  107. package/dist/types/utils/index.d.ts.map +1 -1
  108. package/dist/types/utils/merchant-validator.d.ts +176 -0
  109. package/dist/types/utils/merchant-validator.d.ts.map +1 -0
  110. package/dist/types/utils/x402-direct.d.ts +114 -0
  111. package/dist/types/utils/x402-direct.d.ts.map +1 -0
  112. package/package.json +2 -2
  113. package/src/constants/seeds.ts +9 -0
  114. package/src/core/client.ts +59 -0
  115. package/src/idl/synapse_agent_sap.json +8571 -4106
  116. package/src/index.ts +54 -0
  117. package/src/modules/escrow-v2.ts +375 -0
  118. package/src/modules/escrow.ts +4 -0
  119. package/src/modules/index.ts +4 -0
  120. package/src/modules/receipt.ts +207 -0
  121. package/src/modules/staking.ts +122 -0
  122. package/src/modules/subscription.ts +147 -0
  123. package/src/pda/index.ts +228 -0
  124. package/src/registries/x402.ts +108 -69
  125. package/src/types/accounts.ts +241 -1
  126. package/src/types/enums.ts +119 -0
  127. package/src/types/index.ts +20 -0
  128. package/src/types/instructions.ts +40 -0
  129. package/src/utils/escrow-validation.ts +301 -0
  130. package/src/utils/index.ts +28 -0
  131. package/src/utils/merchant-validator.ts +359 -0
  132. package/src/utils/x402-direct.ts +370 -0
package/src/index.ts CHANGED
@@ -59,6 +59,11 @@ export type {
59
59
  DelegatePermissionBit,
60
60
  SchemaTypeValue,
61
61
  CompressionTypeValue,
62
+ SettlementSecurityKind,
63
+ DisputeOutcomeKind,
64
+ BillingIntervalKind,
65
+ DisputeTypeValue,
66
+ ResolutionLayerKind,
62
67
  // Helper structs
63
68
  Capability,
64
69
  VolumeCurveBreakpoint,
@@ -83,6 +88,15 @@ export type {
83
88
  AgentAttestationData,
84
89
  MemoryLedgerData,
85
90
  LedgerPageData,
91
+ // V2.1 account data
92
+ EscrowAccountV2Data,
93
+ PendingSettlementData,
94
+ DisputeRecordData,
95
+ ReceiptBatchData,
96
+ AgentStakeData,
97
+ SubscriptionData,
98
+ CounterShardData,
99
+ IndexPageData,
86
100
  // Instruction args
87
101
  RegisterAgentArgs,
88
102
  UpdateAgentArgs,
@@ -95,6 +109,9 @@ export type {
95
109
  CreateEscrowArgs,
96
110
  CreateAttestationArgs,
97
111
  InscribeToolSchemaArgs,
112
+ // V2.1 instruction args
113
+ CreateEscrowV2Args,
114
+ CreateSubscriptionArgs,
98
115
  // v0.6.0 — Endpoint & Manifest types
99
116
  EndpointDescriptor,
100
117
  HealthCheckDescriptor,
@@ -112,6 +129,11 @@ export {
112
129
  DelegatePermission,
113
130
  SchemaType,
114
131
  CompressionType,
132
+ SettlementSecurity,
133
+ DisputeOutcome,
134
+ BillingInterval,
135
+ DisputeType,
136
+ ResolutionLayer,
115
137
  } from "./types";
116
138
 
117
139
  // ── Constants ────────────────────────────────────────
@@ -151,9 +173,17 @@ export {
151
173
  deriveCheckpoint,
152
174
  deriveTool,
153
175
  deriveEscrow,
176
+ deriveEscrowV2,
177
+ derivePendingSettlement,
178
+ deriveDispute,
179
+ deriveStake,
180
+ deriveSubscription,
181
+ deriveShard,
182
+ deriveIndexPage,
154
183
  deriveAttestation,
155
184
  deriveLedger,
156
185
  deriveLedgerPage,
186
+ deriveReceiptBatch,
157
187
  } from "./pda";
158
188
 
159
189
  // ── Utilities ────────────────────────────────────────
@@ -216,6 +246,26 @@ export type {
216
246
  PriorityFeeConfig,
217
247
  } from "./utils";
218
248
 
249
+ // v0.6.4 — Escrow validation, merchant middleware, x402 direct
250
+ export {
251
+ validateEscrowState,
252
+ attachSplAccounts,
253
+ toAccountMetas,
254
+ MissingEscrowAtaError,
255
+ SapMerchantValidator,
256
+ parseX402Headers,
257
+ getX402DirectPayments,
258
+ } from "./utils";
259
+ export type {
260
+ SplAccountMeta,
261
+ EscrowValidationResult,
262
+ ParsedX402Headers,
263
+ MerchantValidationResult,
264
+ X402DirectPayment,
265
+ SettlementPayload,
266
+ GetX402DirectOptions,
267
+ } from "./utils";
268
+
219
269
  // ── Errors ───────────────────────────────────────────
220
270
  export {
221
271
  SapError,
@@ -252,6 +302,10 @@ export {
252
302
  ToolsModule,
253
303
  VaultModule,
254
304
  EscrowModule,
305
+ EscrowV2Module,
306
+ ReceiptModule,
307
+ StakingModule,
308
+ SubscriptionModule,
255
309
  AttestationModule,
256
310
  LedgerModule,
257
311
  BaseModule,
@@ -0,0 +1,375 @@
1
+ /**
2
+ * @module escrow-v2
3
+ * @description V2 escrow settlement layer — supports settlement security
4
+ * modes (CoSigned, DisputeWindow), receipt-based dispute resolution,
5
+ * pending settlements, and automatic resolution via merkle proofs.
6
+ *
7
+ * @category Modules
8
+ * @since v0.7.0
9
+ * @packageDocumentation
10
+ */
11
+
12
+ import {
13
+ SystemProgram,
14
+ type PublicKey,
15
+ type TransactionSignature,
16
+ type AccountMeta,
17
+ } from "@solana/web3.js";
18
+ import { BN } from "@coral-xyz/anchor";
19
+ import { BaseModule } from "./base";
20
+ import {
21
+ deriveAgent,
22
+ deriveAgentStats,
23
+ deriveEscrowV2,
24
+ derivePendingSettlement as derivePendingPda,
25
+ deriveDispute as deriveDisputePda,
26
+ } from "../pda";
27
+ import type {
28
+ EscrowAccountV2Data,
29
+ PendingSettlementData,
30
+ DisputeRecordData,
31
+ CreateEscrowV2Args,
32
+ } from "../types";
33
+ import {
34
+ buildPriorityFeeIxs,
35
+ buildRpcOptions,
36
+ } from "../utils/priority-fee";
37
+ import type { SettleOptions } from "../utils/priority-fee";
38
+
39
+ /**
40
+ * @name EscrowV2Module
41
+ * @description Manages V2 escrow accounts with settlement security modes,
42
+ * dispute windows, and pending settlement flows.
43
+ *
44
+ * @category Modules
45
+ * @since v0.7.0
46
+ * @extends BaseModule
47
+ */
48
+ export class EscrowV2Module extends BaseModule {
49
+ // ── Helpers ──────────────────────────────────────────
50
+
51
+ /** Convert BN | number | bigint → number for PDA seed functions. */
52
+ private toNum(v: BN | number | bigint): number {
53
+ return BN.isBN(v) ? v.toNumber() : Number(v);
54
+ }
55
+
56
+ // ── PDA helpers ──────────────────────────────────────
57
+
58
+ deriveEscrow(
59
+ agentPda: PublicKey,
60
+ depositor?: PublicKey,
61
+ nonce: BN | number | bigint = 0,
62
+ ): readonly [PublicKey, number] {
63
+ return deriveEscrowV2(agentPda, depositor ?? this.walletPubkey, this.toNum(nonce));
64
+ }
65
+
66
+ derivePendingSettlement(
67
+ escrowV2Pda: PublicKey,
68
+ settlementIndex: BN | number | bigint,
69
+ ): readonly [PublicKey, number] {
70
+ return derivePendingPda(escrowV2Pda, this.toNum(settlementIndex));
71
+ }
72
+
73
+ deriveDispute(
74
+ pendingSettlementPda: PublicKey,
75
+ ): readonly [PublicKey, number] {
76
+ return deriveDisputePda(pendingSettlementPda);
77
+ }
78
+
79
+ // ── Instructions ─────────────────────────────────────
80
+
81
+ async create(
82
+ agentWallet: PublicKey,
83
+ args: CreateEscrowV2Args,
84
+ splAccounts: AccountMeta[] = [],
85
+ ): Promise<TransactionSignature> {
86
+ const [agentPda] = deriveAgent(agentWallet);
87
+ const [escrowPda] = this.deriveEscrow(agentPda, undefined, args.escrowNonce);
88
+
89
+ return this.methods
90
+ .createEscrowV2(
91
+ this.bn(args.escrowNonce),
92
+ this.bn(args.pricePerCall),
93
+ this.bn(args.maxCalls),
94
+ this.bn(args.initialDeposit),
95
+ args.expiresAt,
96
+ args.volumeCurve,
97
+ args.tokenMint,
98
+ args.tokenDecimals,
99
+ args.settlementSecurity,
100
+ this.bn(args.disputeWindowSlots),
101
+ args.coSigner,
102
+ args.arbiter,
103
+ )
104
+ .accounts({
105
+ depositor: this.walletPubkey,
106
+ agent: agentPda,
107
+ escrow: escrowPda,
108
+ systemProgram: SystemProgram.programId,
109
+ })
110
+ .remainingAccounts(splAccounts)
111
+ .rpc();
112
+ }
113
+
114
+ async deposit(
115
+ agentWallet: PublicKey,
116
+ nonce: BN | number | bigint,
117
+ amount: BN | number | bigint,
118
+ splAccounts: AccountMeta[] = [],
119
+ ): Promise<TransactionSignature> {
120
+ const [agentPda] = deriveAgent(agentWallet);
121
+ const [escrowPda] = this.deriveEscrow(agentPda, undefined, nonce);
122
+
123
+ return this.methods
124
+ .depositEscrowV2(this.bn(nonce), this.bn(amount))
125
+ .accounts({
126
+ depositor: this.walletPubkey,
127
+ escrow: escrowPda,
128
+ systemProgram: SystemProgram.programId,
129
+ })
130
+ .remainingAccounts(splAccounts)
131
+ .rpc();
132
+ }
133
+
134
+ async settle(
135
+ depositorWallet: PublicKey,
136
+ nonce: BN | number | bigint,
137
+ callsToSettle: BN | number | bigint,
138
+ serviceHash: number[],
139
+ splAccounts: AccountMeta[] = [],
140
+ opts?: SettleOptions,
141
+ ): Promise<TransactionSignature> {
142
+ const [agentPda] = deriveAgent(this.walletPubkey);
143
+ const [escrowPda] = this.deriveEscrow(agentPda, depositorWallet, nonce);
144
+ const [statsPda] = deriveAgentStats(agentPda);
145
+
146
+ const preIxs = buildPriorityFeeIxs(opts);
147
+ const rpcOpts = buildRpcOptions(opts);
148
+
149
+ let builder = this.methods
150
+ .settleCallsV2(this.bn(nonce), this.bn(callsToSettle), serviceHash)
151
+ .accounts({
152
+ wallet: this.walletPubkey,
153
+ agent: agentPda,
154
+ agentStats: statsPda,
155
+ escrow: escrowPda,
156
+ systemProgram: SystemProgram.programId,
157
+ })
158
+ .remainingAccounts(splAccounts);
159
+
160
+ if (preIxs.length > 0) {
161
+ builder = builder.preInstructions(preIxs);
162
+ }
163
+
164
+ return builder.rpc(rpcOpts);
165
+ }
166
+
167
+ async createPendingSettlement(
168
+ agentWallet: PublicKey,
169
+ depositorWallet: PublicKey,
170
+ nonce: BN | number | bigint,
171
+ settlementIndex: BN | number | bigint,
172
+ callsToSettle: BN | number | bigint,
173
+ amount: BN | number | bigint,
174
+ serviceHash: number[],
175
+ receiptMerkleRoot: number[] = new Array(32).fill(0),
176
+ ): Promise<TransactionSignature> {
177
+ const [agentPda] = deriveAgent(agentWallet);
178
+ const [escrowPda] = this.deriveEscrow(agentPda, depositorWallet, nonce);
179
+ const [pendingPda] = this.derivePendingSettlement(escrowPda, settlementIndex);
180
+
181
+ return this.methods
182
+ .createPendingSettlement(
183
+ this.bn(settlementIndex),
184
+ this.bn(callsToSettle),
185
+ this.bn(amount),
186
+ serviceHash,
187
+ receiptMerkleRoot,
188
+ )
189
+ .accounts({
190
+ wallet: this.walletPubkey,
191
+ agent: agentPda,
192
+ escrow: escrowPda,
193
+ pendingSettlement: pendingPda,
194
+ systemProgram: SystemProgram.programId,
195
+ })
196
+ .rpc();
197
+ }
198
+
199
+ async finalizeSettlement(
200
+ agentWallet: PublicKey,
201
+ depositorWallet: PublicKey,
202
+ nonce: BN | number | bigint,
203
+ settlementIndex: BN | number | bigint,
204
+ ): Promise<TransactionSignature> {
205
+ const [agentPda] = deriveAgent(agentWallet);
206
+ const [escrowPda] = this.deriveEscrow(agentPda, depositorWallet, nonce);
207
+ const [pendingPda] = this.derivePendingSettlement(escrowPda, settlementIndex);
208
+ const [statsPda] = deriveAgentStats(agentPda);
209
+
210
+ return this.methods
211
+ .finalizeSettlement()
212
+ .accounts({
213
+ payer: this.walletPubkey,
214
+ agentWallet,
215
+ escrow: escrowPda,
216
+ pendingSettlement: pendingPda,
217
+ agentStats: statsPda,
218
+ })
219
+ .rpc();
220
+ }
221
+
222
+ async fileDispute(
223
+ agentWallet: PublicKey,
224
+ nonce: BN | number | bigint,
225
+ settlementIndex: BN | number | bigint,
226
+ evidenceHash: number[],
227
+ disputeType: number = 0,
228
+ ): Promise<TransactionSignature> {
229
+ const [agentPda] = deriveAgent(agentWallet);
230
+ const [escrowPda] = this.deriveEscrow(agentPda, undefined, nonce);
231
+ const [pendingPda] = this.derivePendingSettlement(escrowPda, settlementIndex);
232
+ const [disputePda] = this.deriveDispute(pendingPda);
233
+
234
+ return this.methods
235
+ .fileDispute(evidenceHash, disputeType)
236
+ .accounts({
237
+ depositor: this.walletPubkey,
238
+ escrow: escrowPda,
239
+ pendingSettlement: pendingPda,
240
+ dispute: disputePda,
241
+ systemProgram: SystemProgram.programId,
242
+ })
243
+ .rpc();
244
+ }
245
+
246
+ /**
247
+ * @deprecated Since v0.7.0 — Arbiter-based resolution removed.
248
+ * Use {@link ReceiptModule.submitReceiptProof} + {@link ReceiptModule.autoResolveDispute} instead.
249
+ */
250
+ async resolveDispute(
251
+ _depositorWallet: PublicKey,
252
+ _agentWallet: PublicKey,
253
+ _nonce: BN | number | bigint,
254
+ _settlementIndex: BN | number | bigint,
255
+ _outcome: number,
256
+ ): Promise<TransactionSignature> {
257
+ throw new Error("resolveDispute removed in v0.7.0 — use ReceiptModule.autoResolveDispute");
258
+ }
259
+
260
+ async closeDispute(
261
+ pendingSettlementPda: PublicKey,
262
+ ): Promise<TransactionSignature> {
263
+ const [disputePda] = this.deriveDispute(pendingSettlementPda);
264
+
265
+ return this.methods
266
+ .closeDispute()
267
+ .accounts({
268
+ depositor: this.walletPubkey,
269
+ dispute: disputePda,
270
+ })
271
+ .rpc();
272
+ }
273
+
274
+ async closePendingSettlement(
275
+ pendingSettlementPda: PublicKey,
276
+ ): Promise<TransactionSignature> {
277
+ return this.methods
278
+ .closePendingSettlement()
279
+ .accounts({
280
+ payer: this.walletPubkey,
281
+ pendingSettlement: pendingSettlementPda,
282
+ })
283
+ .rpc();
284
+ }
285
+
286
+ async withdraw(
287
+ agentWallet: PublicKey,
288
+ nonce: BN | number | bigint,
289
+ amount: BN | number | bigint,
290
+ ): Promise<TransactionSignature> {
291
+ const [agentPda] = deriveAgent(agentWallet);
292
+ const [escrowPda] = this.deriveEscrow(agentPda, undefined, nonce);
293
+
294
+ return this.methods
295
+ .withdrawEscrowV2(this.bn(amount))
296
+ .accounts({
297
+ depositor: this.walletPubkey,
298
+ escrow: escrowPda,
299
+ })
300
+ .rpc();
301
+ }
302
+
303
+ async close(
304
+ agentWallet: PublicKey,
305
+ nonce: BN | number | bigint = 0,
306
+ ): Promise<TransactionSignature> {
307
+ const [agentPda] = deriveAgent(agentWallet);
308
+ const [escrowPda] = this.deriveEscrow(agentPda, undefined, nonce);
309
+
310
+ return this.methods
311
+ .closeEscrowV2()
312
+ .accounts({
313
+ depositor: this.walletPubkey,
314
+ escrow: escrowPda,
315
+ })
316
+ .rpc();
317
+ }
318
+
319
+ /**
320
+ * @deprecated Since v0.7.0 — Migration instruction removed from program.
321
+ */
322
+ async migrateFromV1(
323
+ _agentWallet: PublicKey,
324
+ ): Promise<TransactionSignature> {
325
+ throw new Error("migrateFromV1 removed in v0.7.0 — migration instruction was deleted");
326
+ }
327
+
328
+ // ── Fetchers ─────────────────────────────────────────
329
+
330
+ async fetch(
331
+ agentPda: PublicKey,
332
+ depositor?: PublicKey,
333
+ nonce: BN | number | bigint = 0,
334
+ ): Promise<EscrowAccountV2Data> {
335
+ const [pda] = this.deriveEscrow(agentPda, depositor, nonce);
336
+ return this.fetchAccount<EscrowAccountV2Data>("escrowAccountV2", pda);
337
+ }
338
+
339
+ async fetchNullable(
340
+ agentPda: PublicKey,
341
+ depositor?: PublicKey,
342
+ nonce: BN | number | bigint = 0,
343
+ ): Promise<EscrowAccountV2Data | null> {
344
+ const [pda] = this.deriveEscrow(agentPda, depositor, nonce);
345
+ return this.fetchAccountNullable<EscrowAccountV2Data>("escrowAccountV2", pda);
346
+ }
347
+
348
+ async fetchByPda(escrowPda: PublicKey): Promise<EscrowAccountV2Data> {
349
+ return this.fetchAccount<EscrowAccountV2Data>("escrowAccountV2", escrowPda);
350
+ }
351
+
352
+ async fetchPendingSettlement(
353
+ pendingPda: PublicKey,
354
+ ): Promise<PendingSettlementData> {
355
+ return this.fetchAccount<PendingSettlementData>("pendingSettlement", pendingPda);
356
+ }
357
+
358
+ async fetchPendingSettlementNullable(
359
+ pendingPda: PublicKey,
360
+ ): Promise<PendingSettlementData | null> {
361
+ return this.fetchAccountNullable<PendingSettlementData>("pendingSettlement", pendingPda);
362
+ }
363
+
364
+ async fetchDispute(
365
+ disputePda: PublicKey,
366
+ ): Promise<DisputeRecordData> {
367
+ return this.fetchAccount<DisputeRecordData>("disputeRecord", disputePda);
368
+ }
369
+
370
+ async fetchDisputeNullable(
371
+ disputePda: PublicKey,
372
+ ): Promise<DisputeRecordData | null> {
373
+ return this.fetchAccountNullable<DisputeRecordData>("disputeRecord", disputePda);
374
+ }
375
+ }
@@ -59,6 +59,10 @@ import type { SettleOptions } from "../utils/priority-fee";
59
59
  * tokenDecimals: null,
60
60
  * });
61
61
  * ```
62
+ *
63
+ * @deprecated Since v0.7.0 — Use {@link EscrowV2Module} (`client.escrowV2`) instead.
64
+ * V1 escrows lack settlement security, dispute resolution, and staking integration.
65
+ * Existing V1 escrows can be migrated via `client.escrowV2.migrateFromV1()`.
62
66
  */
63
67
  export class EscrowModule extends BaseModule {
64
68
  // ── PDA helpers ──────────────────────────────────────
@@ -18,6 +18,10 @@ export { IndexingModule } from "./indexing";
18
18
  export { ToolsModule } from "./tools";
19
19
  export { VaultModule } from "./vault";
20
20
  export { EscrowModule } from "./escrow";
21
+ export { EscrowV2Module } from "./escrow-v2";
22
+ export { ReceiptModule } from "./receipt";
23
+ export { StakingModule } from "./staking";
24
+ export { SubscriptionModule } from "./subscription";
21
25
  export { AttestationModule } from "./attestation";
22
26
  export { LedgerModule } from "./ledger";
23
27
  export { BaseModule } from "./base";
@@ -0,0 +1,207 @@
1
+ /**
2
+ * @module receipt
3
+ * @description Receipt-based trustless dispute resolution (v0.7).
4
+ *
5
+ * Agents inscribe merkle roots of call receipt batches on-chain.
6
+ * During disputes, agents submit merkle inclusion proofs to prove delivery.
7
+ * After the proof deadline, anyone can trigger automatic proportional resolution.
8
+ *
9
+ * @category Modules
10
+ * @since v0.7.0
11
+ * @packageDocumentation
12
+ */
13
+
14
+ import {
15
+ SystemProgram,
16
+ type PublicKey,
17
+ type TransactionSignature,
18
+ } from "@solana/web3.js";
19
+ import { BN } from "@coral-xyz/anchor";
20
+ import { BaseModule } from "./base";
21
+ import {
22
+ deriveAgent,
23
+ deriveAgentStats,
24
+ deriveEscrowV2,
25
+ derivePendingSettlement as derivePendingPda,
26
+ deriveDispute as deriveDisputePda,
27
+ deriveReceiptBatch as deriveReceiptPda,
28
+ } from "../pda";
29
+ import type { ReceiptBatchData } from "../types";
30
+
31
+ /**
32
+ * @name ReceiptModule
33
+ * @description Manages receipt batch inscriptions, merkle proof submissions,
34
+ * and automatic dispute resolution for the v0.7 trustless settlement layer.
35
+ *
36
+ * @category Modules
37
+ * @since v0.7.0
38
+ * @extends BaseModule
39
+ */
40
+ export class ReceiptModule extends BaseModule {
41
+ // ── Helpers ──────────────────────────────────────────
42
+
43
+ private toNum(v: BN | number | bigint): number {
44
+ return BN.isBN(v) ? v.toNumber() : Number(v);
45
+ }
46
+
47
+ // ── PDA helpers ──────────────────────────────────────
48
+
49
+ deriveReceiptBatch(
50
+ escrowV2Pda: PublicKey,
51
+ batchIndex: number,
52
+ ): readonly [PublicKey, number] {
53
+ return deriveReceiptPda(escrowV2Pda, batchIndex);
54
+ }
55
+
56
+ // ── Instructions ─────────────────────────────────────
57
+
58
+ /**
59
+ * Inscribe a receipt batch merkle root on-chain.
60
+ *
61
+ * Called by the **agent** to commit a cryptographic proof of calls delivered.
62
+ * The `merkleRoot` is the root of a merkle tree whose leaves are individual
63
+ * call receipt hashes.
64
+ *
65
+ * @param depositorWallet - The depositor's wallet (needed for escrow PDA derivation).
66
+ * @param nonce - Escrow nonce.
67
+ * @param batchIndex - Zero-based batch index (must equal escrow.receipt_batch_count).
68
+ * @param merkleRoot - 32-byte merkle root of the receipt batch.
69
+ * @param callCount - Number of calls in the batch.
70
+ * @param periodStart - Unix timestamp for the start of the covered period.
71
+ * @param periodEnd - Unix timestamp for the end of the covered period.
72
+ */
73
+ async inscribeReceiptBatch(
74
+ depositorWallet: PublicKey,
75
+ nonce: BN | number | bigint,
76
+ batchIndex: number,
77
+ merkleRoot: number[],
78
+ callCount: BN | number | bigint,
79
+ periodStart: BN | number | bigint,
80
+ periodEnd: BN | number | bigint,
81
+ ): Promise<TransactionSignature> {
82
+ const [agentPda] = deriveAgent(this.walletPubkey);
83
+ const [escrowPda] = deriveEscrowV2(agentPda, depositorWallet, this.toNum(nonce));
84
+ const [receiptPda] = this.deriveReceiptBatch(escrowPda, batchIndex);
85
+
86
+ return this.methods
87
+ .inscribeReceiptBatch(
88
+ batchIndex,
89
+ merkleRoot,
90
+ this.bn(callCount),
91
+ this.bn(periodStart),
92
+ this.bn(periodEnd),
93
+ )
94
+ .accounts({
95
+ wallet: this.walletPubkey,
96
+ agent: agentPda,
97
+ escrow: escrowPda,
98
+ receiptBatch: receiptPda,
99
+ systemProgram: SystemProgram.programId,
100
+ })
101
+ .rpc();
102
+ }
103
+
104
+ /**
105
+ * Submit a merkle inclusion proof during a dispute.
106
+ *
107
+ * Called by the **agent** to prove delivery of specific calls.
108
+ * Each proof verifies that a set of receipt hashes are included
109
+ * in the previously inscribed merkle root.
110
+ *
111
+ * @param depositorWallet - The depositor's wallet.
112
+ * @param nonce - Escrow nonce.
113
+ * @param settlementIndex - Index of the disputed pending settlement.
114
+ * @param batchIndex - Receipt batch index containing the proof.
115
+ * @param provenCount - Number of calls proven in this proof submission.
116
+ * @param proof - Array of 32-byte merkle proof hashes.
117
+ * @param leaf - The 32-byte leaf hash being proven.
118
+ */
119
+ async submitReceiptProof(
120
+ depositorWallet: PublicKey,
121
+ nonce: BN | number | bigint,
122
+ settlementIndex: BN | number | bigint,
123
+ batchIndex: number,
124
+ provenCount: BN | number | bigint,
125
+ proof: number[][],
126
+ leaf: number[],
127
+ ): Promise<TransactionSignature> {
128
+ const [agentPda] = deriveAgent(this.walletPubkey);
129
+ const [escrowPda] = deriveEscrowV2(agentPda, depositorWallet, this.toNum(nonce));
130
+ const [pendingPda] = derivePendingPda(escrowPda, this.toNum(settlementIndex));
131
+ const [disputePda] = deriveDisputePda(pendingPda);
132
+ const [receiptPda] = this.deriveReceiptBatch(escrowPda, batchIndex);
133
+
134
+ return this.methods
135
+ .submitReceiptProof(
136
+ batchIndex,
137
+ this.bn(provenCount),
138
+ proof,
139
+ leaf,
140
+ )
141
+ .accounts({
142
+ wallet: this.walletPubkey,
143
+ agent: agentPda,
144
+ escrow: escrowPda,
145
+ pendingSettlement: pendingPda,
146
+ dispute: disputePda,
147
+ receiptBatch: receiptPda,
148
+ })
149
+ .rpc();
150
+ }
151
+
152
+ /**
153
+ * Trigger automatic dispute resolution after the proof deadline.
154
+ *
155
+ * Permissionless crank — anyone can call this once the deadline has passed.
156
+ * Resolution is proportional: if the agent proved N of M claimed calls,
157
+ * N/M of the settlement amount goes to the agent, the rest is refunded.
158
+ *
159
+ * @param agentWallet - The agent's wallet.
160
+ * @param depositorWallet - The depositor's wallet.
161
+ * @param nonce - Escrow nonce.
162
+ * @param settlementIndex - Index of the disputed pending settlement.
163
+ */
164
+ async autoResolveDispute(
165
+ agentWallet: PublicKey,
166
+ depositorWallet: PublicKey,
167
+ nonce: BN | number | bigint,
168
+ settlementIndex: BN | number | bigint,
169
+ ): Promise<TransactionSignature> {
170
+ const [agentPda] = deriveAgent(agentWallet);
171
+ const [escrowPda] = deriveEscrowV2(agentPda, depositorWallet, this.toNum(nonce));
172
+ const [pendingPda] = derivePendingPda(escrowPda, this.toNum(settlementIndex));
173
+ const [disputePda] = deriveDisputePda(pendingPda);
174
+ const [statsPda] = deriveAgentStats(agentPda);
175
+
176
+ return this.methods
177
+ .autoResolveDispute()
178
+ .accounts({
179
+ payer: this.walletPubkey,
180
+ depositor: depositorWallet,
181
+ agentWallet,
182
+ escrow: escrowPda,
183
+ pendingSettlement: pendingPda,
184
+ dispute: disputePda,
185
+ agentStats: statsPda,
186
+ })
187
+ .rpc();
188
+ }
189
+
190
+ // ── Fetchers ─────────────────────────────────────────
191
+
192
+ async fetchReceiptBatch(
193
+ escrowV2Pda: PublicKey,
194
+ batchIndex: number,
195
+ ): Promise<ReceiptBatchData> {
196
+ const [pda] = this.deriveReceiptBatch(escrowV2Pda, batchIndex);
197
+ return this.fetchAccount<ReceiptBatchData>("receiptBatch", pda);
198
+ }
199
+
200
+ async fetchReceiptBatchNullable(
201
+ escrowV2Pda: PublicKey,
202
+ batchIndex: number,
203
+ ): Promise<ReceiptBatchData | null> {
204
+ const [pda] = this.deriveReceiptBatch(escrowV2Pda, batchIndex);
205
+ return this.fetchAccountNullable<ReceiptBatchData>("receiptBatch", pda);
206
+ }
207
+ }