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