@oobe-protocol-labs/synapse-sap-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.
Files changed (315) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +882 -0
  3. package/dist/cjs/constants/index.js +43 -0
  4. package/dist/cjs/constants/index.js.map +1 -0
  5. package/dist/cjs/constants/limits.js +161 -0
  6. package/dist/cjs/constants/limits.js.map +1 -0
  7. package/dist/cjs/constants/programs.js +78 -0
  8. package/dist/cjs/constants/programs.js.map +1 -0
  9. package/dist/cjs/constants/seeds.js +57 -0
  10. package/dist/cjs/constants/seeds.js.map +1 -0
  11. package/dist/cjs/core/client.js +391 -0
  12. package/dist/cjs/core/client.js.map +1 -0
  13. package/dist/cjs/core/connection.js +319 -0
  14. package/dist/cjs/core/connection.js.map +1 -0
  15. package/dist/cjs/core/index.js +24 -0
  16. package/dist/cjs/core/index.js.map +1 -0
  17. package/dist/cjs/errors/index.js +334 -0
  18. package/dist/cjs/errors/index.js.map +1 -0
  19. package/dist/cjs/events/index.js +136 -0
  20. package/dist/cjs/events/index.js.map +1 -0
  21. package/dist/cjs/idl/index.js +63 -0
  22. package/dist/cjs/idl/index.js.map +1 -0
  23. package/dist/cjs/idl/synapse_agent_sap.json +9710 -0
  24. package/dist/cjs/index.js +147 -0
  25. package/dist/cjs/index.js.map +1 -0
  26. package/dist/cjs/modules/agent.js +272 -0
  27. package/dist/cjs/modules/agent.js.map +1 -0
  28. package/dist/cjs/modules/attestation.js +147 -0
  29. package/dist/cjs/modules/attestation.js.map +1 -0
  30. package/dist/cjs/modules/base.js +128 -0
  31. package/dist/cjs/modules/base.js.map +1 -0
  32. package/dist/cjs/modules/escrow.js +246 -0
  33. package/dist/cjs/modules/escrow.js.map +1 -0
  34. package/dist/cjs/modules/feedback.js +166 -0
  35. package/dist/cjs/modules/feedback.js.map +1 -0
  36. package/dist/cjs/modules/index.js +35 -0
  37. package/dist/cjs/modules/index.js.map +1 -0
  38. package/dist/cjs/modules/indexing.js +375 -0
  39. package/dist/cjs/modules/indexing.js.map +1 -0
  40. package/dist/cjs/modules/ledger.js +234 -0
  41. package/dist/cjs/modules/ledger.js.map +1 -0
  42. package/dist/cjs/modules/tools.js +319 -0
  43. package/dist/cjs/modules/tools.js.map +1 -0
  44. package/dist/cjs/modules/vault.js +410 -0
  45. package/dist/cjs/modules/vault.js.map +1 -0
  46. package/dist/cjs/pda/index.js +377 -0
  47. package/dist/cjs/pda/index.js.map +1 -0
  48. package/dist/cjs/plugin/index.js +934 -0
  49. package/dist/cjs/plugin/index.js.map +1 -0
  50. package/dist/cjs/plugin/protocols.js +282 -0
  51. package/dist/cjs/plugin/protocols.js.map +1 -0
  52. package/dist/cjs/plugin/schemas.js +831 -0
  53. package/dist/cjs/plugin/schemas.js.map +1 -0
  54. package/dist/cjs/postgres/adapter.js +715 -0
  55. package/dist/cjs/postgres/adapter.js.map +1 -0
  56. package/dist/cjs/postgres/index.js +50 -0
  57. package/dist/cjs/postgres/index.js.map +1 -0
  58. package/dist/cjs/postgres/serializers.js +381 -0
  59. package/dist/cjs/postgres/serializers.js.map +1 -0
  60. package/dist/cjs/postgres/sync.js +221 -0
  61. package/dist/cjs/postgres/sync.js.map +1 -0
  62. package/dist/cjs/postgres/types.js +44 -0
  63. package/dist/cjs/postgres/types.js.map +1 -0
  64. package/dist/cjs/registries/builder.js +414 -0
  65. package/dist/cjs/registries/builder.js.map +1 -0
  66. package/dist/cjs/registries/discovery.js +362 -0
  67. package/dist/cjs/registries/discovery.js.map +1 -0
  68. package/dist/cjs/registries/index.js +51 -0
  69. package/dist/cjs/registries/index.js.map +1 -0
  70. package/dist/cjs/registries/session.js +433 -0
  71. package/dist/cjs/registries/session.js.map +1 -0
  72. package/dist/cjs/registries/x402.js +577 -0
  73. package/dist/cjs/registries/x402.js.map +1 -0
  74. package/dist/cjs/types/accounts.js +13 -0
  75. package/dist/cjs/types/accounts.js.map +1 -0
  76. package/dist/cjs/types/common.js +13 -0
  77. package/dist/cjs/types/common.js.map +1 -0
  78. package/dist/cjs/types/enums.js +174 -0
  79. package/dist/cjs/types/enums.js.map +1 -0
  80. package/dist/cjs/types/index.js +36 -0
  81. package/dist/cjs/types/index.js.map +1 -0
  82. package/dist/cjs/types/instructions.js +92 -0
  83. package/dist/cjs/types/instructions.js.map +1 -0
  84. package/dist/cjs/utils/hash.js +58 -0
  85. package/dist/cjs/utils/hash.js.map +1 -0
  86. package/dist/cjs/utils/index.js +27 -0
  87. package/dist/cjs/utils/index.js.map +1 -0
  88. package/dist/cjs/utils/serialization.js +105 -0
  89. package/dist/cjs/utils/serialization.js.map +1 -0
  90. package/dist/cjs/utils/validation.js +36 -0
  91. package/dist/cjs/utils/validation.js.map +1 -0
  92. package/dist/esm/constants/index.js +29 -0
  93. package/dist/esm/constants/index.js.map +1 -0
  94. package/dist/esm/constants/limits.js +158 -0
  95. package/dist/esm/constants/limits.js.map +1 -0
  96. package/dist/esm/constants/programs.js +75 -0
  97. package/dist/esm/constants/programs.js.map +1 -0
  98. package/dist/esm/constants/seeds.js +54 -0
  99. package/dist/esm/constants/seeds.js.map +1 -0
  100. package/dist/esm/core/client.js +384 -0
  101. package/dist/esm/core/client.js.map +1 -0
  102. package/dist/esm/core/connection.js +315 -0
  103. package/dist/esm/core/connection.js.map +1 -0
  104. package/dist/esm/core/index.js +19 -0
  105. package/dist/esm/core/index.js.map +1 -0
  106. package/dist/esm/errors/index.js +325 -0
  107. package/dist/esm/errors/index.js.map +1 -0
  108. package/dist/esm/events/index.js +132 -0
  109. package/dist/esm/events/index.js.map +1 -0
  110. package/dist/esm/idl/index.js +57 -0
  111. package/dist/esm/idl/index.js.map +1 -0
  112. package/dist/esm/idl/synapse_agent_sap.json +9710 -0
  113. package/dist/esm/index.js +70 -0
  114. package/dist/esm/index.js.map +1 -0
  115. package/dist/esm/modules/agent.js +268 -0
  116. package/dist/esm/modules/agent.js.map +1 -0
  117. package/dist/esm/modules/attestation.js +143 -0
  118. package/dist/esm/modules/attestation.js.map +1 -0
  119. package/dist/esm/modules/base.js +124 -0
  120. package/dist/esm/modules/base.js.map +1 -0
  121. package/dist/esm/modules/escrow.js +242 -0
  122. package/dist/esm/modules/escrow.js.map +1 -0
  123. package/dist/esm/modules/feedback.js +162 -0
  124. package/dist/esm/modules/feedback.js.map +1 -0
  125. package/dist/esm/modules/index.js +23 -0
  126. package/dist/esm/modules/index.js.map +1 -0
  127. package/dist/esm/modules/indexing.js +371 -0
  128. package/dist/esm/modules/indexing.js.map +1 -0
  129. package/dist/esm/modules/ledger.js +230 -0
  130. package/dist/esm/modules/ledger.js.map +1 -0
  131. package/dist/esm/modules/tools.js +315 -0
  132. package/dist/esm/modules/tools.js.map +1 -0
  133. package/dist/esm/modules/vault.js +406 -0
  134. package/dist/esm/modules/vault.js.map +1 -0
  135. package/dist/esm/pda/index.js +357 -0
  136. package/dist/esm/pda/index.js.map +1 -0
  137. package/dist/esm/plugin/index.js +927 -0
  138. package/dist/esm/plugin/index.js.map +1 -0
  139. package/dist/esm/plugin/protocols.js +279 -0
  140. package/dist/esm/plugin/protocols.js.map +1 -0
  141. package/dist/esm/plugin/schemas.js +828 -0
  142. package/dist/esm/plugin/schemas.js.map +1 -0
  143. package/dist/esm/postgres/adapter.js +678 -0
  144. package/dist/esm/postgres/adapter.js.map +1 -0
  145. package/dist/esm/postgres/index.js +27 -0
  146. package/dist/esm/postgres/index.js.map +1 -0
  147. package/dist/esm/postgres/serializers.js +362 -0
  148. package/dist/esm/postgres/serializers.js.map +1 -0
  149. package/dist/esm/postgres/sync.js +217 -0
  150. package/dist/esm/postgres/sync.js.map +1 -0
  151. package/dist/esm/postgres/types.js +41 -0
  152. package/dist/esm/postgres/types.js.map +1 -0
  153. package/dist/esm/registries/builder.js +410 -0
  154. package/dist/esm/registries/builder.js.map +1 -0
  155. package/dist/esm/registries/discovery.js +358 -0
  156. package/dist/esm/registries/discovery.js.map +1 -0
  157. package/dist/esm/registries/index.js +44 -0
  158. package/dist/esm/registries/index.js.map +1 -0
  159. package/dist/esm/registries/session.js +429 -0
  160. package/dist/esm/registries/session.js.map +1 -0
  161. package/dist/esm/registries/x402.js +573 -0
  162. package/dist/esm/registries/x402.js.map +1 -0
  163. package/dist/esm/types/accounts.js +12 -0
  164. package/dist/esm/types/accounts.js.map +1 -0
  165. package/dist/esm/types/common.js +12 -0
  166. package/dist/esm/types/common.js.map +1 -0
  167. package/dist/esm/types/enums.js +171 -0
  168. package/dist/esm/types/enums.js.map +1 -0
  169. package/dist/esm/types/index.js +25 -0
  170. package/dist/esm/types/index.js.map +1 -0
  171. package/dist/esm/types/instructions.js +89 -0
  172. package/dist/esm/types/instructions.js.map +1 -0
  173. package/dist/esm/utils/hash.js +53 -0
  174. package/dist/esm/utils/hash.js.map +1 -0
  175. package/dist/esm/utils/index.js +19 -0
  176. package/dist/esm/utils/index.js.map +1 -0
  177. package/dist/esm/utils/serialization.js +98 -0
  178. package/dist/esm/utils/serialization.js.map +1 -0
  179. package/dist/esm/utils/validation.js +33 -0
  180. package/dist/esm/utils/validation.js.map +1 -0
  181. package/dist/types/constants/index.d.ts +27 -0
  182. package/dist/types/constants/index.d.ts.map +1 -0
  183. package/dist/types/constants/limits.d.ts +149 -0
  184. package/dist/types/constants/limits.d.ts.map +1 -0
  185. package/dist/types/constants/programs.d.ts +69 -0
  186. package/dist/types/constants/programs.d.ts.map +1 -0
  187. package/dist/types/constants/seeds.d.ts +61 -0
  188. package/dist/types/constants/seeds.d.ts.map +1 -0
  189. package/dist/types/core/client.d.ts +323 -0
  190. package/dist/types/core/client.d.ts.map +1 -0
  191. package/dist/types/core/connection.d.ts +279 -0
  192. package/dist/types/core/connection.d.ts.map +1 -0
  193. package/dist/types/core/index.d.ts +20 -0
  194. package/dist/types/core/index.d.ts.map +1 -0
  195. package/dist/types/errors/index.d.ts +276 -0
  196. package/dist/types/errors/index.d.ts.map +1 -0
  197. package/dist/types/events/index.d.ts +248 -0
  198. package/dist/types/events/index.d.ts.map +1 -0
  199. package/dist/types/idl/index.d.ts +70 -0
  200. package/dist/types/idl/index.d.ts.map +1 -0
  201. package/dist/types/index.d.ts +68 -0
  202. package/dist/types/index.d.ts.map +1 -0
  203. package/dist/types/modules/agent.d.ts +166 -0
  204. package/dist/types/modules/agent.d.ts.map +1 -0
  205. package/dist/types/modules/attestation.d.ts +96 -0
  206. package/dist/types/modules/attestation.d.ts.map +1 -0
  207. package/dist/types/modules/base.d.ts +126 -0
  208. package/dist/types/modules/base.d.ts.map +1 -0
  209. package/dist/types/modules/escrow.d.ts +151 -0
  210. package/dist/types/modules/escrow.d.ts.map +1 -0
  211. package/dist/types/modules/feedback.d.ts +105 -0
  212. package/dist/types/modules/feedback.d.ts.map +1 -0
  213. package/dist/types/modules/index.d.ts +24 -0
  214. package/dist/types/modules/index.d.ts.map +1 -0
  215. package/dist/types/modules/indexing.d.ts +200 -0
  216. package/dist/types/modules/indexing.d.ts.map +1 -0
  217. package/dist/types/modules/ledger.d.ts +150 -0
  218. package/dist/types/modules/ledger.d.ts.map +1 -0
  219. package/dist/types/modules/tools.d.ts +182 -0
  220. package/dist/types/modules/tools.d.ts.map +1 -0
  221. package/dist/types/modules/vault.d.ts +240 -0
  222. package/dist/types/modules/vault.d.ts.map +1 -0
  223. package/dist/types/pda/index.d.ts +296 -0
  224. package/dist/types/pda/index.d.ts.map +1 -0
  225. package/dist/types/plugin/index.d.ts +171 -0
  226. package/dist/types/plugin/index.d.ts.map +1 -0
  227. package/dist/types/plugin/protocols.d.ts +152 -0
  228. package/dist/types/plugin/protocols.d.ts.map +1 -0
  229. package/dist/types/plugin/schemas.d.ts +823 -0
  230. package/dist/types/plugin/schemas.d.ts.map +1 -0
  231. package/dist/types/postgres/adapter.d.ts +355 -0
  232. package/dist/types/postgres/adapter.d.ts.map +1 -0
  233. package/dist/types/postgres/index.d.ts +24 -0
  234. package/dist/types/postgres/index.d.ts.map +1 -0
  235. package/dist/types/postgres/serializers.d.ts +30 -0
  236. package/dist/types/postgres/serializers.d.ts.map +1 -0
  237. package/dist/types/postgres/sync.d.ts +132 -0
  238. package/dist/types/postgres/sync.d.ts.map +1 -0
  239. package/dist/types/postgres/types.d.ts +167 -0
  240. package/dist/types/postgres/types.d.ts.map +1 -0
  241. package/dist/types/registries/builder.d.ts +340 -0
  242. package/dist/types/registries/builder.d.ts.map +1 -0
  243. package/dist/types/registries/discovery.d.ts +333 -0
  244. package/dist/types/registries/discovery.d.ts.map +1 -0
  245. package/dist/types/registries/index.d.ts +48 -0
  246. package/dist/types/registries/index.d.ts.map +1 -0
  247. package/dist/types/registries/session.d.ts +323 -0
  248. package/dist/types/registries/session.d.ts.map +1 -0
  249. package/dist/types/registries/x402.d.ts +463 -0
  250. package/dist/types/registries/x402.d.ts.map +1 -0
  251. package/dist/types/types/accounts.d.ts +565 -0
  252. package/dist/types/types/accounts.d.ts.map +1 -0
  253. package/dist/types/types/common.d.ts +166 -0
  254. package/dist/types/types/common.d.ts.map +1 -0
  255. package/dist/types/types/enums.d.ts +238 -0
  256. package/dist/types/types/enums.d.ts.map +1 -0
  257. package/dist/types/types/index.d.ts +28 -0
  258. package/dist/types/types/index.d.ts.map +1 -0
  259. package/dist/types/types/instructions.d.ts +366 -0
  260. package/dist/types/types/instructions.d.ts.map +1 -0
  261. package/dist/types/utils/hash.d.ts +48 -0
  262. package/dist/types/utils/hash.d.ts.map +1 -0
  263. package/dist/types/utils/index.d.ts +19 -0
  264. package/dist/types/utils/index.d.ts.map +1 -0
  265. package/dist/types/utils/serialization.d.ts +69 -0
  266. package/dist/types/utils/serialization.d.ts.map +1 -0
  267. package/dist/types/utils/validation.d.ts +29 -0
  268. package/dist/types/utils/validation.d.ts.map +1 -0
  269. package/package.json +178 -0
  270. package/src/constants/index.ts +44 -0
  271. package/src/constants/limits.ts +165 -0
  272. package/src/constants/programs.ts +83 -0
  273. package/src/constants/seeds.ts +66 -0
  274. package/src/core/client.ts +416 -0
  275. package/src/core/connection.ts +409 -0
  276. package/src/core/index.ts +20 -0
  277. package/src/errors/index.ts +346 -0
  278. package/src/events/index.ts +335 -0
  279. package/src/idl/index.ts +76 -0
  280. package/src/idl/synapse_agent_sap.json +9710 -0
  281. package/src/index.ts +253 -0
  282. package/src/modules/agent.ts +319 -0
  283. package/src/modules/attestation.ts +168 -0
  284. package/src/modules/base.ts +158 -0
  285. package/src/modules/escrow.ts +308 -0
  286. package/src/modules/feedback.ts +186 -0
  287. package/src/modules/index.ts +24 -0
  288. package/src/modules/indexing.ts +444 -0
  289. package/src/modules/ledger.ts +262 -0
  290. package/src/modules/tools.ts +411 -0
  291. package/src/modules/vault.ts +533 -0
  292. package/src/pda/index.ts +512 -0
  293. package/src/plugin/index.ts +1202 -0
  294. package/src/plugin/protocols.ts +404 -0
  295. package/src/plugin/schemas.ts +909 -0
  296. package/src/postgres/adapter.ts +904 -0
  297. package/src/postgres/index.ts +59 -0
  298. package/src/postgres/schema.sql +683 -0
  299. package/src/postgres/serializers.ts +485 -0
  300. package/src/postgres/sync.ts +254 -0
  301. package/src/postgres/types.ts +245 -0
  302. package/src/registries/builder.ts +607 -0
  303. package/src/registries/discovery.ts +572 -0
  304. package/src/registries/index.ts +77 -0
  305. package/src/registries/session.ts +613 -0
  306. package/src/registries/x402.ts +906 -0
  307. package/src/types/accounts.ts +618 -0
  308. package/src/types/common.ts +187 -0
  309. package/src/types/enums.ts +214 -0
  310. package/src/types/index.ts +92 -0
  311. package/src/types/instructions.ts +413 -0
  312. package/src/utils/hash.ts +57 -0
  313. package/src/utils/index.ts +19 -0
  314. package/src/utils/serialization.ts +98 -0
  315. package/src/utils/validation.ts +36 -0
@@ -0,0 +1,906 @@
1
+ /**
2
+ * @module registries/x402
3
+ * @description x402 payment flow registry — high-level helpers for
4
+ * the complete x402 HTTP micropayment lifecycle.
5
+ *
6
+ * Implements the x402 payment standard on SAP:
7
+ *
8
+ * ┌──────────┐ HTTP 402 ┌──────────┐
9
+ * │ Client │ ──────────────→│ Agent │
10
+ * └────┬─────┘ └────┬─────┘
11
+ * │ 1. Discover pricing │
12
+ * │ 2. Create/fund escrow │
13
+ * │ 3. Call via x402 header │
14
+ * │ │
15
+ * │ 4. Agent serves request │
16
+ * │ 5. Agent settles onchain │
17
+ * │ ← PaymentSettledEvent ← │
18
+ * │ 6. Client verifies │
19
+ * └───────────────────────────┘
20
+ *
21
+ * This registry provides:
22
+ * - Pricing estimation with volume curve support
23
+ * - x402 HTTP header generation
24
+ * - Escrow lifecycle management
25
+ * - Settlement verification
26
+ * - Balance/expiry monitoring
27
+ *
28
+ * @category Registries
29
+ * @since v0.1.0
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * const x402 = client.x402;
34
+ *
35
+ * // === CLIENT SIDE ===
36
+ *
37
+ * // 1. Estimate cost before committing
38
+ * const estimate = x402.estimateCost(agentWallet, 100);
39
+ *
40
+ * // 2. Prepare payment (creates escrow + deposits)
41
+ * const ctx = await x402.preparePayment(agentWallet, {
42
+ * pricePerCall: 1000,
43
+ * maxCalls: 100,
44
+ * deposit: 100_000,
45
+ * });
46
+ *
47
+ * // 3. Build x402 HTTP headers for API calls
48
+ * const headers = x402.buildPaymentHeaders(ctx);
49
+ *
50
+ * // 4. Check balance
51
+ * const balance = await x402.getBalance(agentWallet);
52
+ *
53
+ * // === AGENT SIDE ===
54
+ *
55
+ * // 5. Settle after serving calls
56
+ * const receipt = await x402.settle(depositorWallet, 5, serviceData);
57
+ *
58
+ * // 6. Batch settle for efficiency
59
+ * const batchReceipt = await x402.settleBatch(depositorWallet, settlements);
60
+ *
61
+ * // 7. Verify a settlement TX
62
+ * const verified = await x402.verifySettlement(txSignature);
63
+ * ```
64
+ */
65
+
66
+ import {
67
+ SystemProgram,
68
+ type PublicKey,
69
+ type TransactionSignature,
70
+ } from "@solana/web3.js";
71
+ import { type AnchorProvider, BN } from "@coral-xyz/anchor";
72
+ import type { SapProgram } from "../modules/base";
73
+ import {
74
+ deriveAgent,
75
+ deriveAgentStats,
76
+ deriveEscrow,
77
+ } from "../pda";
78
+ import { sha256, hashToArray } from "../utils";
79
+ import type {
80
+ EscrowAccountData,
81
+ AgentAccountData,
82
+ VolumeCurveBreakpoint,
83
+ Settlement,
84
+ } from "../types";
85
+
86
+ // ═══════════════════════════════════════════════════════════════════
87
+ // Public Types
88
+ // ═══════════════════════════════════════════════════════════════════
89
+
90
+ /**
91
+ * @interface CostEstimate
92
+ * @name CostEstimate
93
+ * @description Cost estimation result from {@link X402Registry.estimateCost} or
94
+ * {@link X402Registry.calculateCost}. Includes total cost, effective price per call,
95
+ * and per-tier breakdown when volume curves apply.
96
+ * @category Registries
97
+ * @since v0.1.0
98
+ */
99
+ export interface CostEstimate {
100
+ /** Total cost in smallest token unit. */
101
+ readonly totalCost: BN;
102
+ /** Number of calls estimated. */
103
+ readonly calls: number;
104
+ /** Effective price per call (weighted average). */
105
+ readonly effectivePricePerCall: BN;
106
+ /** Whether volume curve applies. */
107
+ readonly hasVolumeCurve: boolean;
108
+ /** Breakdown by tier (if volume curve). */
109
+ readonly tiers: Array<{
110
+ readonly calls: number;
111
+ readonly pricePerCall: BN;
112
+ readonly subtotal: BN;
113
+ }>;
114
+ }
115
+
116
+ /**
117
+ * @interface PaymentContext
118
+ * @name PaymentContext
119
+ * @description x402 payment context after escrow creation via {@link X402Registry.preparePayment}.
120
+ * Contains all information needed to build x402 HTTP headers and track the payment flow.
121
+ * @category Registries
122
+ * @since v0.1.0
123
+ */
124
+ export interface PaymentContext {
125
+ /** Escrow PDA address. */
126
+ readonly escrowPda: PublicKey;
127
+ /** Agent PDA address. */
128
+ readonly agentPda: PublicKey;
129
+ /** Agent wallet. */
130
+ readonly agentWallet: PublicKey;
131
+ /** Depositor (client) wallet. */
132
+ readonly depositorWallet: PublicKey;
133
+ /** Price per call in smallest token unit. */
134
+ readonly pricePerCall: BN;
135
+ /** Max calls allowed. */
136
+ readonly maxCalls: BN;
137
+ /** Escrow creation TX signature. */
138
+ readonly txSignature: TransactionSignature;
139
+ }
140
+
141
+ /**
142
+ * @interface PreparePaymentOptions
143
+ * @name PreparePaymentOptions
144
+ * @description Options for preparing an x402 payment via {@link X402Registry.preparePayment}.
145
+ * Defines pricing, deposit, expiry, volume curve, and token configuration.
146
+ * @category Registries
147
+ * @since v0.1.0
148
+ */
149
+ export interface PreparePaymentOptions {
150
+ /** Base price per call (smallest token unit). */
151
+ readonly pricePerCall: number | string | BN;
152
+ /** Max calls allowed (0 = unlimited). */
153
+ readonly maxCalls?: number | string | BN;
154
+ /** Initial deposit amount (smallest token unit). */
155
+ readonly deposit: number | string | BN;
156
+ /** Expiry timestamp in unix seconds (0 = never). */
157
+ readonly expiresAt?: number | string | BN;
158
+ /** Volume curve breakpoints. */
159
+ readonly volumeCurve?: Array<{
160
+ afterCalls: number;
161
+ pricePerCall: number | string | BN;
162
+ }>;
163
+ /** SPL token mint (null = native SOL). */
164
+ readonly tokenMint?: PublicKey | null;
165
+ /** Token decimals (default: 9 for SOL). */
166
+ readonly tokenDecimals?: number;
167
+ }
168
+
169
+ /**
170
+ * @interface X402Headers
171
+ * @name X402Headers
172
+ * @description x402 HTTP headers for API requests.
173
+ * Include these headers when calling an agent’s x402 endpoint.
174
+ * Built by {@link X402Registry.buildPaymentHeaders} or
175
+ * {@link X402Registry.buildPaymentHeadersFromEscrow}.
176
+ * @category Registries
177
+ * @since v0.1.0
178
+ */
179
+ export interface X402Headers {
180
+ /** x402 protocol header. */
181
+ readonly "X-Payment-Protocol": "SAP-x402";
182
+ /** Escrow PDA address (base58). */
183
+ readonly "X-Payment-Escrow": string;
184
+ /** Agent PDA address (base58). */
185
+ readonly "X-Payment-Agent": string;
186
+ /** Client wallet address (base58). */
187
+ readonly "X-Payment-Depositor": string;
188
+ /** Max calls remaining. */
189
+ readonly "X-Payment-MaxCalls": string;
190
+ /** Price per call. */
191
+ readonly "X-Payment-PricePerCall": string;
192
+ /** SAP program ID. */
193
+ readonly "X-Payment-Program": string;
194
+ /** Solana cluster. */
195
+ readonly "X-Payment-Network": string;
196
+ }
197
+
198
+ /**
199
+ * @interface EscrowBalance
200
+ * @name EscrowBalance
201
+ * @description Escrow balance and status returned by {@link X402Registry.getBalance}.
202
+ * Includes current balance, deposit/settlement totals, remaining calls,
203
+ * expiry status, and affordable call estimate.
204
+ * @category Registries
205
+ * @since v0.1.0
206
+ */
207
+ export interface EscrowBalance {
208
+ /** Current balance. */
209
+ readonly balance: BN;
210
+ /** Total deposited. */
211
+ readonly totalDeposited: BN;
212
+ /** Total settled. */
213
+ readonly totalSettled: BN;
214
+ /** Total calls settled. */
215
+ readonly totalCallsSettled: BN;
216
+ /** Calls remaining (maxCalls - settled, or Infinity if unlimited). */
217
+ readonly callsRemaining: number;
218
+ /** Is the escrow expired? */
219
+ readonly isExpired: boolean;
220
+ /** Estimated calls affordable with current balance. */
221
+ readonly affordableCalls: number;
222
+ }
223
+
224
+ /**
225
+ * @interface SettlementResult
226
+ * @name SettlementResult
227
+ * @description Settlement result with verification data from {@link X402Registry.settle}.
228
+ * Contains the transaction signature, calls settled, amount transferred,
229
+ * and the service hash used.
230
+ * @category Registries
231
+ * @since v0.1.0
232
+ */
233
+ export interface SettlementResult {
234
+ /** Transaction signature. */
235
+ readonly txSignature: TransactionSignature;
236
+ /** Calls settled. */
237
+ readonly callsSettled: number;
238
+ /** Amount transferred. */
239
+ readonly amount: BN;
240
+ /** Service hash used. */
241
+ readonly serviceHash: number[];
242
+ }
243
+
244
+ /**
245
+ * @interface BatchSettlementResult
246
+ * @name BatchSettlementResult
247
+ * @description Batch settlement result from {@link X402Registry.settleBatch}.
248
+ * Aggregates totals across all individual settlements in the batch.
249
+ * @category Registries
250
+ * @since v0.1.0
251
+ */
252
+ export interface BatchSettlementResult {
253
+ /** Transaction signature. */
254
+ readonly txSignature: TransactionSignature;
255
+ /** Total calls settled. */
256
+ readonly totalCalls: number;
257
+ /** Total amount transferred. */
258
+ readonly totalAmount: BN;
259
+ /** Number of individual settlements in batch. */
260
+ readonly settlementCount: number;
261
+ }
262
+
263
+ // ═══════════════════════════════════════════════════════════════════
264
+ // x402 Registry
265
+ // ═══════════════════════════════════════════════════════════════════
266
+
267
+ /**
268
+ * @name X402Registry
269
+ * @description x402 payment flow registry for the SAP network.
270
+ *
271
+ * Provides the complete x402 HTTP micropayment lifecycle: pricing
272
+ * estimation, escrow management, HTTP header generation, settlement,
273
+ * and balance monitoring. Used by both clients (payers) and agents (payees).
274
+ *
275
+ * @category Registries
276
+ * @since v0.1.0
277
+ *
278
+ * @example
279
+ * ```ts
280
+ * const x402 = client.x402;
281
+ *
282
+ * // Client: prepare payment and build headers
283
+ * const ctx = await x402.preparePayment(agentWallet, {
284
+ * pricePerCall: 1000, maxCalls: 100, deposit: 100_000,
285
+ * });
286
+ * const headers = x402.buildPaymentHeaders(ctx);
287
+ *
288
+ * // Agent: settle calls after serving
289
+ * const receipt = await x402.settle(depositorWallet, 5, "service-data");
290
+ * ```
291
+ */
292
+ export class X402Registry {
293
+ private readonly wallet: PublicKey;
294
+
295
+ constructor(private readonly program: SapProgram) {
296
+ this.wallet = (program.provider as AnchorProvider).wallet.publicKey;
297
+ }
298
+
299
+ // ── Pricing & Estimation ─────────────────────────────
300
+
301
+ /**
302
+ * @name estimateCost
303
+ * @description Estimate the cost of N calls to an agent.
304
+ * Reads the escrow data if it exists, falls back to the agent’s pricing.
305
+ * Supports volume curve pricing for tiered cost calculation.
306
+ *
307
+ * @param agentWallet - Agent wallet address.
308
+ * @param calls - Number of calls to estimate.
309
+ * @param opts - Optional: provide pricing directly to avoid on-chain fetch.
310
+ * @param opts.pricePerCall - Base price per call.
311
+ * @param opts.volumeCurve - Volume curve breakpoints.
312
+ * @param opts.totalCallsBefore - Total calls already settled (for curve offset).
313
+ * @returns A {@link CostEstimate} with total cost and per-tier breakdown.
314
+ * @since v0.1.0
315
+ */
316
+ async estimateCost(
317
+ agentWallet: PublicKey,
318
+ calls: number,
319
+ opts?: {
320
+ pricePerCall?: BN;
321
+ volumeCurve?: VolumeCurveBreakpoint[];
322
+ totalCallsBefore?: number;
323
+ },
324
+ ): Promise<CostEstimate> {
325
+ let pricePerCall: BN;
326
+ let volumeCurve: VolumeCurveBreakpoint[];
327
+ let totalBefore: number;
328
+
329
+ if (opts?.pricePerCall) {
330
+ pricePerCall = opts.pricePerCall;
331
+ volumeCurve = opts.volumeCurve ?? [];
332
+ totalBefore = opts.totalCallsBefore ?? 0;
333
+ } else {
334
+ // Try to read from existing escrow
335
+ const [agentPda] = deriveAgent(agentWallet);
336
+ const [escrowPda] = deriveEscrow(agentPda, this.wallet);
337
+ const escrow = await this.fetchNullable<EscrowAccountData>(
338
+ "escrowAccount",
339
+ escrowPda,
340
+ );
341
+
342
+ if (escrow) {
343
+ pricePerCall = escrow.pricePerCall;
344
+ volumeCurve = escrow.volumeCurve ?? [];
345
+ totalBefore = escrow.totalCallsSettled.toNumber();
346
+ } else {
347
+ // Fall back to agent's first pricing tier
348
+ const agent = await this.fetchNullable<AgentAccountData>(
349
+ "agentAccount",
350
+ agentPda,
351
+ );
352
+ if (!agent || agent.pricing.length === 0) {
353
+ return {
354
+ totalCost: new BN(0),
355
+ calls,
356
+ effectivePricePerCall: new BN(0),
357
+ hasVolumeCurve: false,
358
+ tiers: [],
359
+ };
360
+ }
361
+ pricePerCall = agent.pricing[0]!.pricePerCall;
362
+ volumeCurve = agent.pricing[0]!.volumeCurve ?? [];
363
+ totalBefore = 0;
364
+ }
365
+ }
366
+
367
+ return this.calculateCost(pricePerCall, volumeCurve, totalBefore, calls);
368
+ }
369
+
370
+ /**
371
+ * @name calculateCost
372
+ * @description Pure cost calculation (no network calls).
373
+ * Implements the same tiered pricing logic as the on-chain program.
374
+ *
375
+ * @param basePrice - Base price per call in smallest token unit.
376
+ * @param volumeCurve - Volume curve breakpoints.
377
+ * @param totalCallsBefore - Total calls already settled (cursor offset).
378
+ * @param calls - Number of calls to calculate cost for.
379
+ * @returns A {@link CostEstimate} with total cost and per-tier breakdown.
380
+ * @since v0.1.0
381
+ */
382
+ calculateCost(
383
+ basePrice: BN,
384
+ volumeCurve: VolumeCurveBreakpoint[],
385
+ totalCallsBefore: number,
386
+ calls: number,
387
+ ): CostEstimate {
388
+ const tiers: CostEstimate["tiers"] = [];
389
+
390
+ if (volumeCurve.length === 0) {
391
+ const totalCost = basePrice.mul(new BN(calls));
392
+ return {
393
+ totalCost,
394
+ calls,
395
+ effectivePricePerCall: basePrice,
396
+ hasVolumeCurve: false,
397
+ tiers: [{ calls, pricePerCall: basePrice, subtotal: totalCost }],
398
+ };
399
+ }
400
+
401
+ let remaining = calls;
402
+ let cursor = totalCallsBefore;
403
+ let totalCost = new BN(0);
404
+
405
+ while (remaining > 0) {
406
+ let currentPrice = basePrice;
407
+ let nextThreshold: number | null = null;
408
+
409
+ for (const bp of volumeCurve) {
410
+ const threshold = bp.afterCalls;
411
+ if (cursor >= threshold) {
412
+ currentPrice = bp.pricePerCall;
413
+ } else {
414
+ nextThreshold = threshold;
415
+ break;
416
+ }
417
+ }
418
+
419
+ const callsAtPrice = nextThreshold !== null
420
+ ? Math.min(remaining, nextThreshold - cursor)
421
+ : remaining;
422
+
423
+ const subtotal = currentPrice.mul(new BN(callsAtPrice));
424
+ totalCost = totalCost.add(subtotal);
425
+ tiers.push({ calls: callsAtPrice, pricePerCall: currentPrice, subtotal });
426
+
427
+ remaining -= callsAtPrice;
428
+ cursor += callsAtPrice;
429
+ }
430
+
431
+ const effectivePricePerCall = calls > 0
432
+ ? totalCost.div(new BN(calls))
433
+ : new BN(0);
434
+
435
+ return {
436
+ totalCost,
437
+ calls,
438
+ effectivePricePerCall,
439
+ hasVolumeCurve: true,
440
+ tiers,
441
+ };
442
+ }
443
+
444
+ // ── Escrow Lifecycle (Client Side) ───────────────────
445
+
446
+ /**
447
+ * @name preparePayment
448
+ * @description Prepare an x402 payment flow — creates and funds an escrow.
449
+ * Derives the escrow PDA, sends the `createEscrow` instruction, and returns
450
+ * a {@link PaymentContext} for building x402 headers.
451
+ *
452
+ * @param agentWallet - The agent’s wallet public key.
453
+ * @param opts - Payment options (price, max calls, deposit, etc.).
454
+ * @returns A {@link PaymentContext} with escrow details and transaction signature.
455
+ * @since v0.1.0
456
+ *
457
+ * @example
458
+ * ```ts
459
+ * const ctx = await x402.preparePayment(agentWallet, {
460
+ * pricePerCall: 1000,
461
+ * maxCalls: 100,
462
+ * deposit: 100_000,
463
+ * });
464
+ * ```
465
+ */
466
+ async preparePayment(
467
+ agentWallet: PublicKey,
468
+ opts: PreparePaymentOptions,
469
+ ): Promise<PaymentContext> {
470
+ const [agentPda] = deriveAgent(agentWallet);
471
+ const [escrowPda] = deriveEscrow(agentPda, this.wallet);
472
+ const [statsPda] = deriveAgentStats(agentPda);
473
+
474
+ const pricePerCall = new BN(opts.pricePerCall.toString());
475
+ const maxCalls = new BN((opts.maxCalls ?? 0).toString());
476
+ const initialDeposit = new BN(opts.deposit.toString());
477
+ const expiresAt = new BN((opts.expiresAt ?? 0).toString());
478
+
479
+ const volumeCurve: VolumeCurveBreakpoint[] = (opts.volumeCurve ?? []).map(
480
+ (v) => ({
481
+ afterCalls: v.afterCalls,
482
+ pricePerCall: new BN(v.pricePerCall.toString()),
483
+ }),
484
+ );
485
+
486
+ const txSignature = await this.methods
487
+ .createEscrow(
488
+ pricePerCall,
489
+ maxCalls,
490
+ initialDeposit,
491
+ expiresAt,
492
+ volumeCurve,
493
+ opts.tokenMint ?? null,
494
+ opts.tokenDecimals ?? 9,
495
+ )
496
+ .accounts({
497
+ depositor: this.wallet,
498
+ agent: agentPda,
499
+ agentStats: statsPda,
500
+ escrow: escrowPda,
501
+ systemProgram: SystemProgram.programId,
502
+ })
503
+ .rpc();
504
+
505
+ return {
506
+ escrowPda,
507
+ agentPda,
508
+ agentWallet,
509
+ depositorWallet: this.wallet,
510
+ pricePerCall,
511
+ maxCalls,
512
+ txSignature,
513
+ };
514
+ }
515
+
516
+ /**
517
+ * @name addFunds
518
+ * @description Add more funds to an existing escrow.
519
+ *
520
+ * @param agentWallet - Agent wallet of the escrow.
521
+ * @param amount - Amount to deposit in smallest token unit.
522
+ * @returns The transaction signature.
523
+ * @since v0.1.0
524
+ */
525
+ async addFunds(
526
+ agentWallet: PublicKey,
527
+ amount: number | string | BN,
528
+ ): Promise<TransactionSignature> {
529
+ const [agentPda] = deriveAgent(agentWallet);
530
+ const [escrowPda] = deriveEscrow(agentPda, this.wallet);
531
+
532
+ return this.methods
533
+ .depositEscrow(new BN(amount.toString()))
534
+ .accounts({
535
+ depositor: this.wallet,
536
+ escrow: escrowPda,
537
+ systemProgram: SystemProgram.programId,
538
+ })
539
+ .rpc();
540
+ }
541
+
542
+ /**
543
+ * @name withdrawFunds
544
+ * @description Withdraw remaining funds from an escrow.
545
+ *
546
+ * @param agentWallet - Agent wallet of the escrow.
547
+ * @param amount - Amount to withdraw in smallest token unit.
548
+ * @returns The transaction signature.
549
+ * @since v0.1.0
550
+ */
551
+ async withdrawFunds(
552
+ agentWallet: PublicKey,
553
+ amount: number | string | BN,
554
+ ): Promise<TransactionSignature> {
555
+ const [agentPda] = deriveAgent(agentWallet);
556
+ const [escrowPda] = deriveEscrow(agentPda, this.wallet);
557
+
558
+ return this.methods
559
+ .withdrawEscrow(new BN(amount.toString()))
560
+ .accounts({
561
+ depositor: this.wallet,
562
+ escrow: escrowPda,
563
+ systemProgram: SystemProgram.programId,
564
+ })
565
+ .rpc();
566
+ }
567
+
568
+ /**
569
+ * @name closeEscrow
570
+ * @description Close an empty escrow (balance must be 0).
571
+ * Reclaims the rent-exempt lamports.
572
+ *
573
+ * @param agentWallet - Agent wallet of the escrow.
574
+ * @returns The transaction signature.
575
+ * @since v0.1.0
576
+ */
577
+ async closeEscrow(agentWallet: PublicKey): Promise<TransactionSignature> {
578
+ const [agentPda] = deriveAgent(agentWallet);
579
+ const [escrowPda] = deriveEscrow(agentPda, this.wallet);
580
+
581
+ return this.methods
582
+ .closeEscrow()
583
+ .accounts({
584
+ depositor: this.wallet,
585
+ escrow: escrowPda,
586
+ })
587
+ .rpc();
588
+ }
589
+
590
+ // ── x402 HTTP Headers ────────────────────────────────
591
+
592
+ /**
593
+ * @name buildPaymentHeaders
594
+ * @description Build x402 HTTP headers for API requests.
595
+ * Include these headers when calling an agent’s x402 endpoint.
596
+ *
597
+ * @param ctx - Payment context from {@link X402Registry.preparePayment}.
598
+ * @param opts - Optional settings.
599
+ * @param opts.network - Solana cluster name (defaults to `"mainnet-beta"`).
600
+ * @returns An {@link X402Headers} object ready to merge into HTTP requests.
601
+ * @since v0.1.0
602
+ */
603
+ buildPaymentHeaders(
604
+ ctx: PaymentContext,
605
+ opts?: { network?: string },
606
+ ): X402Headers {
607
+ return {
608
+ "X-Payment-Protocol": "SAP-x402",
609
+ "X-Payment-Escrow": ctx.escrowPda.toBase58(),
610
+ "X-Payment-Agent": ctx.agentPda.toBase58(),
611
+ "X-Payment-Depositor": ctx.depositorWallet.toBase58(),
612
+ "X-Payment-MaxCalls": ctx.maxCalls.toString(),
613
+ "X-Payment-PricePerCall": ctx.pricePerCall.toString(),
614
+ "X-Payment-Program": this.program.programId.toBase58(),
615
+ "X-Payment-Network": opts?.network ?? "mainnet-beta",
616
+ };
617
+ }
618
+
619
+ /**
620
+ * @name buildPaymentHeadersFromEscrow
621
+ * @description Build x402 headers directly from an agent wallet (fetches escrow data).
622
+ * Convenience method that fetches the escrow account on-chain.
623
+ *
624
+ * @param agentWallet - Agent wallet to look up the escrow for.
625
+ * @param opts - Optional settings.
626
+ * @param opts.network - Solana cluster name (defaults to `"mainnet-beta"`).
627
+ * @returns An {@link X402Headers} object, or `null` if no escrow exists.
628
+ * @since v0.1.0
629
+ */
630
+ async buildPaymentHeadersFromEscrow(
631
+ agentWallet: PublicKey,
632
+ opts?: { network?: string },
633
+ ): Promise<X402Headers | null> {
634
+ const [agentPda] = deriveAgent(agentWallet);
635
+ const [escrowPda] = deriveEscrow(agentPda, this.wallet);
636
+
637
+ const escrow = await this.fetchNullable<EscrowAccountData>(
638
+ "escrowAccount",
639
+ escrowPda,
640
+ );
641
+ if (!escrow) return null;
642
+
643
+ return {
644
+ "X-Payment-Protocol": "SAP-x402",
645
+ "X-Payment-Escrow": escrowPda.toBase58(),
646
+ "X-Payment-Agent": agentPda.toBase58(),
647
+ "X-Payment-Depositor": this.wallet.toBase58(),
648
+ "X-Payment-MaxCalls": escrow.maxCalls.toString(),
649
+ "X-Payment-PricePerCall": escrow.pricePerCall.toString(),
650
+ "X-Payment-Program": this.program.programId.toBase58(),
651
+ "X-Payment-Network": opts?.network ?? "mainnet-beta",
652
+ };
653
+ }
654
+
655
+ // ── Settlement (Agent Side) ──────────────────────────
656
+
657
+ /**
658
+ * @name settle
659
+ * @description Settle calls — agent claims payment for calls served.
660
+ * Must be called by the agent owner wallet. Calculates the settlement
661
+ * amount using the escrow’s pricing and volume curve.
662
+ *
663
+ * @param depositorWallet - The client wallet that funded the escrow.
664
+ * @param callsToSettle - Number of calls to settle.
665
+ * @param serviceData - Raw service data (auto-hashed to `service_hash`).
666
+ * @returns A {@link SettlementResult} with transaction details and amount.
667
+ * @since v0.1.0
668
+ */
669
+ async settle(
670
+ depositorWallet: PublicKey,
671
+ callsToSettle: number,
672
+ serviceData: string | Buffer | Uint8Array,
673
+ ): Promise<SettlementResult> {
674
+ const serviceHash = hashToArray(
675
+ sha256(typeof serviceData === "string" ? serviceData : Buffer.from(serviceData)),
676
+ );
677
+
678
+ const [agentPda] = deriveAgent(this.wallet);
679
+ const [escrowPda] = deriveEscrow(agentPda, depositorWallet);
680
+ const [statsPda] = deriveAgentStats(agentPda);
681
+
682
+ // Prefetch to calculate amount
683
+ const escrow = await this.fetch<EscrowAccountData>(
684
+ "escrowAccount",
685
+ escrowPda,
686
+ );
687
+ const estimate = this.calculateCost(
688
+ escrow.pricePerCall,
689
+ escrow.volumeCurve,
690
+ escrow.totalCallsSettled.toNumber(),
691
+ callsToSettle,
692
+ );
693
+
694
+ const txSignature = await this.methods
695
+ .settleCalls(new BN(callsToSettle), serviceHash)
696
+ .accounts({
697
+ wallet: this.wallet,
698
+ agent: agentPda,
699
+ agentStats: statsPda,
700
+ escrow: escrowPda,
701
+ systemProgram: SystemProgram.programId,
702
+ })
703
+ .rpc();
704
+
705
+ return {
706
+ txSignature,
707
+ callsSettled: callsToSettle,
708
+ amount: estimate.totalCost,
709
+ serviceHash,
710
+ };
711
+ }
712
+
713
+ /**
714
+ * @name settleBatch
715
+ * @description Batch settle — process up to 10 settlements in one TX.
716
+ * Must be called by the agent owner wallet. More gas-efficient than
717
+ * individual settlements.
718
+ *
719
+ * @param depositorWallet - The client wallet that funded the escrow.
720
+ * @param entries - Array of `{ calls, serviceData }` settlement entries.
721
+ * @returns A {@link BatchSettlementResult} with aggregated totals.
722
+ * @since v0.1.0
723
+ */
724
+ async settleBatch(
725
+ depositorWallet: PublicKey,
726
+ entries: Array<{
727
+ calls: number;
728
+ serviceData: string | Buffer | Uint8Array;
729
+ }>,
730
+ ): Promise<BatchSettlementResult> {
731
+ const settlements: Settlement[] = entries.map((e) => ({
732
+ callsToSettle: new BN(e.calls),
733
+ serviceHash: hashToArray(
734
+ sha256(typeof e.serviceData === "string" ? e.serviceData : Buffer.from(e.serviceData)),
735
+ ),
736
+ }));
737
+
738
+ const totalCalls = entries.reduce((sum, e) => sum + e.calls, 0);
739
+
740
+ const [agentPda] = deriveAgent(this.wallet);
741
+ const [escrowPda] = deriveEscrow(agentPda, depositorWallet);
742
+ const [statsPda] = deriveAgentStats(agentPda);
743
+
744
+ // Prefetch for amount estimation
745
+ const escrow = await this.fetch<EscrowAccountData>(
746
+ "escrowAccount",
747
+ escrowPda,
748
+ );
749
+ const estimate = this.calculateCost(
750
+ escrow.pricePerCall,
751
+ escrow.volumeCurve,
752
+ escrow.totalCallsSettled.toNumber(),
753
+ totalCalls,
754
+ );
755
+
756
+ const txSignature = await this.methods
757
+ .settleBatch(settlements)
758
+ .accounts({
759
+ wallet: this.wallet,
760
+ agent: agentPda,
761
+ agentStats: statsPda,
762
+ escrow: escrowPda,
763
+ systemProgram: SystemProgram.programId,
764
+ })
765
+ .rpc();
766
+
767
+ return {
768
+ txSignature,
769
+ totalCalls,
770
+ totalAmount: estimate.totalCost,
771
+ settlementCount: entries.length,
772
+ };
773
+ }
774
+
775
+ // ── Balance & Status ─────────────────────────────────
776
+
777
+ /**
778
+ * @name getBalance
779
+ * @description Get the current escrow balance and status.
780
+ * Returns balance, deposit/settlement totals, remaining calls,
781
+ * expiry status, and affordable call estimate.
782
+ *
783
+ * @param agentWallet - Agent wallet of the escrow.
784
+ * @param depositor - Depositor wallet (defaults to caller).
785
+ * @returns An {@link EscrowBalance}, or `null` if no escrow exists.
786
+ * @since v0.1.0
787
+ */
788
+ async getBalance(
789
+ agentWallet: PublicKey,
790
+ depositor?: PublicKey,
791
+ ): Promise<EscrowBalance | null> {
792
+ const [agentPda] = deriveAgent(agentWallet);
793
+ const dep = depositor ?? this.wallet;
794
+ const [escrowPda] = deriveEscrow(agentPda, dep);
795
+
796
+ const escrow = await this.fetchNullable<EscrowAccountData>(
797
+ "escrowAccount",
798
+ escrowPda,
799
+ );
800
+ if (!escrow) return null;
801
+
802
+ const now = Math.floor(Date.now() / 1000);
803
+ const isExpired = escrow.expiresAt.toNumber() > 0 && now >= escrow.expiresAt.toNumber();
804
+ const maxCalls = escrow.maxCalls.toNumber();
805
+ const settled = escrow.totalCallsSettled.toNumber();
806
+ const callsRemaining = maxCalls > 0 ? maxCalls - settled : Infinity;
807
+
808
+ // Estimate affordable calls with current balance
809
+ const pricePerCall = escrow.pricePerCall.toNumber();
810
+ const balance = escrow.balance.toNumber();
811
+ const affordableCalls = pricePerCall > 0
812
+ ? Math.floor(balance / pricePerCall)
813
+ : Infinity;
814
+
815
+ return {
816
+ balance: escrow.balance,
817
+ totalDeposited: escrow.totalDeposited,
818
+ totalSettled: escrow.totalSettled,
819
+ totalCallsSettled: escrow.totalCallsSettled,
820
+ callsRemaining: Math.min(callsRemaining, affordableCalls),
821
+ isExpired,
822
+ affordableCalls,
823
+ };
824
+ }
825
+
826
+ /**
827
+ * @name hasEscrow
828
+ * @description Check if an escrow exists for a given agent + depositor pair.
829
+ *
830
+ * @param agentWallet - Agent wallet to check.
831
+ * @param depositor - Depositor wallet (defaults to caller).
832
+ * @returns `true` if the escrow account exists on-chain.
833
+ * @since v0.1.0
834
+ */
835
+ async hasEscrow(
836
+ agentWallet: PublicKey,
837
+ depositor?: PublicKey,
838
+ ): Promise<boolean> {
839
+ const [agentPda] = deriveAgent(agentWallet);
840
+ const [escrowPda] = deriveEscrow(agentPda, depositor ?? this.wallet);
841
+ const info = await this.program.provider.connection.getAccountInfo(escrowPda);
842
+ return info !== null;
843
+ }
844
+
845
+ /**
846
+ * @name fetchEscrow
847
+ * @description Fetch the raw escrow account data.
848
+ *
849
+ * @param agentWallet - Agent wallet of the escrow.
850
+ * @param depositor - Depositor wallet (defaults to caller).
851
+ * @returns The raw {@link EscrowAccountData}, or `null` if not found.
852
+ * @since v0.1.0
853
+ */
854
+ async fetchEscrow(
855
+ agentWallet: PublicKey,
856
+ depositor?: PublicKey,
857
+ ): Promise<EscrowAccountData | null> {
858
+ const [agentPda] = deriveAgent(agentWallet);
859
+ const dep = depositor ?? this.wallet;
860
+ const [escrowPda] = deriveEscrow(agentPda, dep);
861
+ return this.fetchNullable<EscrowAccountData>("escrowAccount", escrowPda);
862
+ }
863
+
864
+ // ── Internals ────────────────────────────────────────
865
+
866
+ /**
867
+ * @name methods
868
+ * @description Accessor for the Anchor program methods namespace.
869
+ * @returns The program methods object for building RPC calls.
870
+ * @private
871
+ */
872
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
873
+ private get methods(): any {
874
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
875
+ return this.program.methods;
876
+ }
877
+
878
+ /**
879
+ * @name fetch
880
+ * @description Fetch an on-chain account by name and PDA. Throws if not found.
881
+ * @param name - Anchor account discriminator name.
882
+ * @param pda - Account public key to fetch.
883
+ * @returns The deserialized account data.
884
+ * @throws If the account does not exist.
885
+ * @private
886
+ */
887
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
888
+ private async fetch<T>(name: string, pda: PublicKey): Promise<T> {
889
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
890
+ return (this.program.account as any)[name].fetch(pda) as Promise<T>;
891
+ }
892
+
893
+ /**
894
+ * @name fetchNullable
895
+ * @description Fetch an on-chain account by name and PDA. Returns `null` if not found.
896
+ * @param name - Anchor account discriminator name.
897
+ * @param pda - Account public key to fetch.
898
+ * @returns The deserialized account data, or `null` if the account does not exist.
899
+ * @private
900
+ */
901
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
902
+ private async fetchNullable<T>(name: string, pda: PublicKey): Promise<T | null> {
903
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
904
+ return (this.program.account as any)[name].fetchNullable(pda) as Promise<T | null>;
905
+ }
906
+ }