@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,346 @@
1
+ /**
2
+ * @module errors
3
+ * @description Typed error classes for the SAP v2 SDK.
4
+ *
5
+ * All SDK errors extend {@link SapError} to allow catch-all handling,
6
+ * while concrete subclasses enable fine-grained error matching.
7
+ *
8
+ * @category Errors
9
+ * @since v0.1.0
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { SapError, SapRpcError, SapAccountNotFoundError } from "@synapse-sap/sdk/errors";
14
+ *
15
+ * try {
16
+ * const agent = await client.agent.fetch();
17
+ * } catch (err) {
18
+ * if (err instanceof SapAccountNotFoundError) {
19
+ * console.log("Agent not registered yet");
20
+ * } else if (err instanceof SapRpcError) {
21
+ * console.error("RPC failure:", err.rpcCode);
22
+ * } else if (err instanceof SapError) {
23
+ * console.error("SAP error:", err.message);
24
+ * }
25
+ * }
26
+ * ```
27
+ *
28
+ * @see {@link SapError}
29
+ * @see {@link SapValidationError}
30
+ * @see {@link SapRpcError}
31
+ * @see {@link SapAccountNotFoundError}
32
+ * @see {@link SapTimeoutError}
33
+ * @see {@link SapPermissionError}
34
+ */
35
+
36
+ // ═══════════════════════════════════════════════════════════════════
37
+ // Base Error
38
+ // ═══════════════════════════════════════════════════════════════════
39
+
40
+ /**
41
+ * @name SapError
42
+ * @description Base class for all SAP SDK errors.
43
+ *
44
+ * Extends `Error` and sets `name` automatically so `instanceof`
45
+ * checks work correctly in both CJS and ESM environments.
46
+ *
47
+ * @category Errors
48
+ * @since v0.1.0
49
+ * @extends {Error}
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * try {
54
+ * await client.agent.fetch();
55
+ * } catch (err) {
56
+ * if (err instanceof SapError) {
57
+ * console.error(`[${err.code}] ${err.message}`);
58
+ * }
59
+ * }
60
+ * ```
61
+ *
62
+ * @see {@link SapValidationError}
63
+ * @see {@link SapRpcError}
64
+ * @see {@link SapAccountNotFoundError}
65
+ * @see {@link SapTimeoutError}
66
+ * @see {@link SapPermissionError}
67
+ */
68
+ export class SapError extends Error {
69
+ /**
70
+ * @description Machine-readable error code for programmatic handling.
71
+ * @readonly
72
+ */
73
+ readonly code: string;
74
+
75
+ /**
76
+ * @param message - A human-readable description of the error.
77
+ * @param code - Machine-readable error code (defaults to `"SAP_ERROR"`).
78
+ */
79
+ constructor(message: string, code = "SAP_ERROR") {
80
+ super(message);
81
+ this.name = "SapError";
82
+ this.code = code;
83
+ // Fix prototype chain for instanceof checks
84
+ Object.setPrototypeOf(this, new.target.prototype);
85
+ }
86
+ }
87
+
88
+ // ═══════════════════════════════════════════════════════════════════
89
+ // Validation Error
90
+ // ═══════════════════════════════════════════════════════════════════
91
+
92
+ /**
93
+ * @name SapValidationError
94
+ * @description Thrown when SDK-side input validation fails before sending a
95
+ * transaction or making an RPC call.
96
+ *
97
+ * @category Errors
98
+ * @since v0.1.0
99
+ * @extends {SapError}
100
+ *
101
+ * @example
102
+ * ```ts
103
+ * try {
104
+ * await client.agent.register({ name: "" });
105
+ * } catch (err) {
106
+ * if (err instanceof SapValidationError) {
107
+ * console.error(`Validation failed on field "${err.field}": ${err.message}`);
108
+ * }
109
+ * }
110
+ * ```
111
+ *
112
+ * @see {@link SapError}
113
+ */
114
+ export class SapValidationError extends SapError {
115
+ /**
116
+ * @description Field or parameter that failed validation (if applicable).
117
+ * @readonly
118
+ */
119
+ readonly field?: string;
120
+
121
+ /**
122
+ * @param message - A human-readable description of the validation failure.
123
+ * @param field - The name of the field or parameter that failed validation.
124
+ */
125
+ constructor(message: string, field?: string) {
126
+ super(message, "SAP_VALIDATION_ERROR");
127
+ this.name = "SapValidationError";
128
+ this.field = field;
129
+ }
130
+ }
131
+
132
+ // ═══════════════════════════════════════════════════════════════════
133
+ // RPC / Transaction Error
134
+ // ═══════════════════════════════════════════════════════════════════
135
+
136
+ /**
137
+ * @name SapRpcError
138
+ * @description Wraps Solana RPC or Anchor transaction errors with additional
139
+ * context (error code, logs).
140
+ *
141
+ * @category Errors
142
+ * @since v0.1.0
143
+ * @extends {SapError}
144
+ *
145
+ * @example
146
+ * ```ts
147
+ * try {
148
+ * await client.agent.register(params);
149
+ * } catch (err) {
150
+ * if (err instanceof SapRpcError) {
151
+ * console.error(`RPC error ${err.rpcCode}: ${err.message}`);
152
+ * err.logs?.forEach((log) => console.debug(log));
153
+ * }
154
+ * }
155
+ * ```
156
+ *
157
+ * @see {@link SapRpcError.fromAnchor}
158
+ * @see {@link SapError}
159
+ */
160
+ export class SapRpcError extends SapError {
161
+ /**
162
+ * @description Anchor / program error code, if available.
163
+ * @readonly
164
+ */
165
+ readonly rpcCode?: number;
166
+ /**
167
+ * @description Transaction logs returned by the RPC node.
168
+ * @readonly
169
+ */
170
+ readonly logs?: readonly string[];
171
+
172
+ /**
173
+ * @param message - A human-readable description of the RPC or transaction error.
174
+ * @param opts - Optional additional context.
175
+ * @param opts.rpcCode - Anchor or Solana program error code.
176
+ * @param opts.logs - Raw transaction logs from the RPC node.
177
+ */
178
+ constructor(
179
+ message: string,
180
+ opts?: { rpcCode?: number; logs?: readonly string[] },
181
+ ) {
182
+ super(message, "SAP_RPC_ERROR");
183
+ this.name = "SapRpcError";
184
+ this.rpcCode = opts?.rpcCode;
185
+ this.logs = opts?.logs;
186
+ }
187
+
188
+ /**
189
+ * @static
190
+ * @description Create a {@link SapRpcError} from an Anchor-style error object.
191
+ * Extracts `code` and `logs` automatically from the Anchor error shape.
192
+ *
193
+ * @param err - The raw Anchor or Solana error object.
194
+ * @returns A new {@link SapRpcError} populated with extracted code, logs, and message.
195
+ *
196
+ * @example
197
+ * ```ts
198
+ * try {
199
+ * await program.methods.registerAgent(args).rpc();
200
+ * } catch (raw) {
201
+ * throw SapRpcError.fromAnchor(raw);
202
+ * }
203
+ * ```
204
+ *
205
+ * @see {@link SapRpcError}
206
+ */
207
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
208
+ static fromAnchor(err: any): SapRpcError {
209
+ const code = err?.error?.errorCode?.number ?? err?.code;
210
+ const logs: string[] = err?.logs ?? err?.error?.logs ?? [];
211
+ const msg =
212
+ err?.error?.errorMessage ?? err?.message ?? "Unknown RPC error";
213
+ return new SapRpcError(msg, { rpcCode: code, logs });
214
+ }
215
+ }
216
+
217
+ // ═══════════════════════════════════════════════════════════════════
218
+ // Account Not Found
219
+ // ═══════════════════════════════════════════════════════════════════
220
+
221
+ /**
222
+ * @name SapAccountNotFoundError
223
+ * @description Thrown when an expected on-chain account does not exist or has
224
+ * been closed.
225
+ *
226
+ * @category Errors
227
+ * @since v0.1.0
228
+ * @extends {SapError}
229
+ *
230
+ * @example
231
+ * ```ts
232
+ * try {
233
+ * const agent = await client.agent.fetch(agentPda);
234
+ * } catch (err) {
235
+ * if (err instanceof SapAccountNotFoundError) {
236
+ * console.warn(`Missing ${err.accountType ?? "account"}: ${err.address}`);
237
+ * }
238
+ * }
239
+ * ```
240
+ *
241
+ * @see {@link SapError}
242
+ */
243
+ export class SapAccountNotFoundError extends SapError {
244
+ /**
245
+ * @description The base-58 encoded account address that was not found.
246
+ * @readonly
247
+ */
248
+ readonly address: string;
249
+ /**
250
+ * @description The expected account type (e.g. `"AgentRecord"`, `"VaultAccount"`).
251
+ * @readonly
252
+ */
253
+ readonly accountType?: string;
254
+
255
+ /**
256
+ * @param address - The base-58 encoded address of the missing account.
257
+ * @param accountType - Optional label for the expected account type.
258
+ */
259
+ constructor(address: string, accountType?: string) {
260
+ const label = accountType ? `${accountType} account` : "Account";
261
+ super(`${label} not found: ${address}`, "SAP_ACCOUNT_NOT_FOUND");
262
+ this.name = "SapAccountNotFoundError";
263
+ this.address = address;
264
+ this.accountType = accountType;
265
+ }
266
+ }
267
+
268
+ // ═══════════════════════════════════════════════════════════════════
269
+ // Timeout Error
270
+ // ═══════════════════════════════════════════════════════════════════
271
+
272
+ /**
273
+ * @name SapTimeoutError
274
+ * @description Thrown when an operation (e.g., transaction confirmation) exceeds
275
+ * the configured timeout.
276
+ *
277
+ * @category Errors
278
+ * @since v0.1.0
279
+ * @extends {SapError}
280
+ *
281
+ * @example
282
+ * ```ts
283
+ * try {
284
+ * await client.agent.register(params, { timeoutMs: 30_000 });
285
+ * } catch (err) {
286
+ * if (err instanceof SapTimeoutError) {
287
+ * console.error(`Timed out after ${err.timeoutMs}ms: ${err.message}`);
288
+ * }
289
+ * }
290
+ * ```
291
+ *
292
+ * @see {@link SapError}
293
+ */
294
+ export class SapTimeoutError extends SapError {
295
+ /**
296
+ * @description Timeout duration in milliseconds that was exceeded.
297
+ * @readonly
298
+ */
299
+ readonly timeoutMs: number;
300
+
301
+ /**
302
+ * @param message - A human-readable description of the timeout.
303
+ * @param timeoutMs - The timeout threshold in milliseconds.
304
+ */
305
+ constructor(message: string, timeoutMs: number) {
306
+ super(message, "SAP_TIMEOUT");
307
+ this.name = "SapTimeoutError";
308
+ this.timeoutMs = timeoutMs;
309
+ }
310
+ }
311
+
312
+ // ═══════════════════════════════════════════════════════════════════
313
+ // Permission Error
314
+ // ═══════════════════════════════════════════════════════════════════
315
+
316
+ /**
317
+ * @name SapPermissionError
318
+ * @description Thrown when the current wallet lacks the required permission
319
+ * (authority, delegate, etc.) for an operation.
320
+ *
321
+ * @category Errors
322
+ * @since v0.1.0
323
+ * @extends {SapError}
324
+ *
325
+ * @example
326
+ * ```ts
327
+ * try {
328
+ * await client.agent.update(agentPda, updates);
329
+ * } catch (err) {
330
+ * if (err instanceof SapPermissionError) {
331
+ * console.error(`Permission denied: ${err.message}`);
332
+ * }
333
+ * }
334
+ * ```
335
+ *
336
+ * @see {@link SapError}
337
+ */
338
+ export class SapPermissionError extends SapError {
339
+ /**
340
+ * @param message - A human-readable description of the permission failure.
341
+ */
342
+ constructor(message: string) {
343
+ super(message, "SAP_PERMISSION_DENIED");
344
+ this.name = "SapPermissionError";
345
+ }
346
+ }
@@ -0,0 +1,335 @@
1
+ /**
2
+ * @module events
3
+ * @description Decode SAP v2 events from transaction logs.
4
+ *
5
+ * Uses Anchor's event parser under the hood. Provides a
6
+ * strongly-typed, filterable event stream.
7
+ *
8
+ * @category Events
9
+ * @since v0.1.0
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { EventParser } from "@synapse-sap/sdk/events";
14
+ *
15
+ * const events = client.events.parseLogs(txLogs);
16
+ * for (const event of events) {
17
+ * if (event.name === "PaymentSettledEvent") {
18
+ * console.log(event.data);
19
+ * }
20
+ * }
21
+ * ```
22
+ */
23
+
24
+ import { type Program, BN, EventParser as AnchorEventParser } from "@coral-xyz/anchor";
25
+ import type { PublicKey } from "@solana/web3.js";
26
+
27
+ // ═══════════════════════════════════════════════════════════════════
28
+ // Event Data Types
29
+ // ═══════════════════════════════════════════════════════════════════
30
+
31
+ /**
32
+ * Decoded payload for the `RegisteredEvent` emitted when a new agent is registered.
33
+ *
34
+ * @interface RegisteredEventData
35
+ * @name RegisteredEventData
36
+ * @description Contains the agent PDA, owner wallet, display name, capabilities list, and timestamp.
37
+ * @category Events
38
+ * @since v0.1.0
39
+ * @see {@link SapEvent}
40
+ */
41
+ export interface RegisteredEventData {
42
+ readonly name: "RegisteredEvent";
43
+ readonly data: {
44
+ agent: PublicKey;
45
+ wallet: PublicKey;
46
+ name: string;
47
+ capabilities: string[];
48
+ timestamp: BN;
49
+ };
50
+ }
51
+
52
+ /**
53
+ * Decoded payload for the `UpdatedEvent` emitted when an agent’s profile is modified.
54
+ *
55
+ * @interface UpdatedEventData
56
+ * @name UpdatedEventData
57
+ * @description Contains the agent PDA, owner wallet, a list of changed field names, and timestamp.
58
+ * @category Events
59
+ * @since v0.1.0
60
+ * @see {@link SapEvent}
61
+ */
62
+ export interface UpdatedEventData {
63
+ readonly name: "UpdatedEvent";
64
+ readonly data: {
65
+ agent: PublicKey;
66
+ wallet: PublicKey;
67
+ updatedFields: string[];
68
+ timestamp: BN;
69
+ };
70
+ }
71
+
72
+ /**
73
+ * Decoded payload for the `FeedbackEvent` emitted when a reviewer submits feedback.
74
+ *
75
+ * @interface FeedbackEventData
76
+ * @name FeedbackEventData
77
+ * @description Contains the agent PDA, reviewer wallet, numeric score, tag, and timestamp.
78
+ * @category Events
79
+ * @since v0.1.0
80
+ * @see {@link SapEvent}
81
+ */
82
+ export interface FeedbackEventData {
83
+ readonly name: "FeedbackEvent";
84
+ readonly data: {
85
+ agent: PublicKey;
86
+ reviewer: PublicKey;
87
+ score: number;
88
+ tag: string;
89
+ timestamp: BN;
90
+ };
91
+ }
92
+
93
+ /**
94
+ * Decoded payload for the `MemoryInscribedEvent` emitted when data is written to a vault.
95
+ *
96
+ * @interface MemoryInscribedEventData
97
+ * @name MemoryInscribedEventData
98
+ * @description Contains vault/session PDAs, sequence info, encrypted data, nonce, content hash,
99
+ * fragment metadata, compression flag, and timestamp.
100
+ * @category Events
101
+ * @since v0.1.0
102
+ * @see {@link SapEvent}
103
+ */
104
+ export interface MemoryInscribedEventData {
105
+ readonly name: "MemoryInscribedEvent";
106
+ readonly data: {
107
+ vault: PublicKey;
108
+ session: PublicKey;
109
+ sequence: number;
110
+ epochIndex: number;
111
+ encryptedData: number[];
112
+ nonce: number[];
113
+ contentHash: number[];
114
+ totalFragments: number;
115
+ fragmentIndex: number;
116
+ compression: number;
117
+ dataLen: number;
118
+ nonceVersion: number;
119
+ timestamp: BN;
120
+ };
121
+ }
122
+
123
+ /**
124
+ * Decoded payload for the `PaymentSettledEvent` emitted when an escrow settlement occurs.
125
+ *
126
+ * @interface PaymentSettledEventData
127
+ * @name PaymentSettledEventData
128
+ * @description Contains escrow/agent/depositor PDAs, settlement counts, amounts, service hash,
129
+ * and remaining balance.
130
+ * @category Events
131
+ * @since v0.1.0
132
+ * @see {@link SapEvent}
133
+ */
134
+ export interface PaymentSettledEventData {
135
+ readonly name: "PaymentSettledEvent";
136
+ readonly data: {
137
+ escrow: PublicKey;
138
+ agent: PublicKey;
139
+ depositor: PublicKey;
140
+ callsSettled: BN;
141
+ amount: BN;
142
+ serviceHash: number[];
143
+ totalCallsSettled: BN;
144
+ remainingBalance: BN;
145
+ timestamp: BN;
146
+ };
147
+ }
148
+
149
+ /**
150
+ * Decoded payload for the `LedgerEntryEvent` emitted when data is appended to a ledger.
151
+ *
152
+ * @interface LedgerEntryEventData
153
+ * @name LedgerEntryEventData
154
+ * @description Contains session/ledger PDAs, entry index, raw data, content hash,
155
+ * Merkle root, and timestamp.
156
+ * @category Events
157
+ * @since v0.1.0
158
+ * @see {@link SapEvent}
159
+ */
160
+ export interface LedgerEntryEventData {
161
+ readonly name: "LedgerEntryEvent";
162
+ readonly data: {
163
+ session: PublicKey;
164
+ ledger: PublicKey;
165
+ entryIndex: number;
166
+ data: number[];
167
+ contentHash: number[];
168
+ dataLen: number;
169
+ merkleRoot: number[];
170
+ timestamp: BN;
171
+ };
172
+ }
173
+
174
+ /**
175
+ * Discriminated union of all known SAP v2 event types.
176
+ *
177
+ * @name SapEvent
178
+ * @description Use the `name` discriminant to narrow to a specific event payload.
179
+ * @category Events
180
+ * @since v0.1.0
181
+ */
182
+ export type SapEvent =
183
+ | RegisteredEventData
184
+ | UpdatedEventData
185
+ | FeedbackEventData
186
+ | MemoryInscribedEventData
187
+ | PaymentSettledEventData
188
+ | LedgerEntryEventData;
189
+
190
+ // ═══════════════════════════════════════════════════════════════════
191
+ // Event Name Literals
192
+ // ═══════════════════════════════════════════════════════════════════
193
+
194
+ /**
195
+ * Exhaustive list of all SAP v2 event name strings.
196
+ *
197
+ * @name SAP_EVENT_NAMES
198
+ * @description Frozen array of every event discriminant emitted by the program.
199
+ * Useful for log filtering and exhaustive switch checks.
200
+ * @category Events
201
+ * @since v0.1.0
202
+ * @see {@link SapEventName}
203
+ */
204
+ export const SAP_EVENT_NAMES = [
205
+ "RegisteredEvent",
206
+ "UpdatedEvent",
207
+ "DeactivatedEvent",
208
+ "ReactivatedEvent",
209
+ "ClosedEvent",
210
+ "FeedbackEvent",
211
+ "FeedbackUpdatedEvent",
212
+ "FeedbackRevokedEvent",
213
+ "ReputationUpdatedEvent",
214
+ "CallsReportedEvent",
215
+ "VaultInitializedEvent",
216
+ "SessionOpenedEvent",
217
+ "MemoryInscribedEvent",
218
+ "EpochOpenedEvent",
219
+ "SessionClosedEvent",
220
+ "VaultClosedEvent",
221
+ "SessionPdaClosedEvent",
222
+ "EpochPageClosedEvent",
223
+ "VaultNonceRotatedEvent",
224
+ "DelegateAddedEvent",
225
+ "DelegateRevokedEvent",
226
+ "ToolPublishedEvent",
227
+ "ToolSchemaInscribedEvent",
228
+ "ToolUpdatedEvent",
229
+ "ToolDeactivatedEvent",
230
+ "ToolReactivatedEvent",
231
+ "ToolClosedEvent",
232
+ "ToolInvocationReportedEvent",
233
+ "CheckpointCreatedEvent",
234
+ "EscrowCreatedEvent",
235
+ "EscrowDepositedEvent",
236
+ "PaymentSettledEvent",
237
+ "EscrowWithdrawnEvent",
238
+ "BatchSettledEvent",
239
+ "AttestationCreatedEvent",
240
+ "AttestationRevokedEvent",
241
+ "LedgerEntryEvent",
242
+ "LedgerSealedEvent",
243
+ ] as const;
244
+
245
+ /**
246
+ * String literal union of all SAP v2 event names.
247
+ *
248
+ * @name SapEventName
249
+ * @description Derived from {@link SAP_EVENT_NAMES} for compile-time exhaustiveness.
250
+ * @category Events
251
+ * @since v0.1.0
252
+ */
253
+ export type SapEventName = (typeof SAP_EVENT_NAMES)[number];
254
+
255
+ // ═══════════════════════════════════════════════════════════════════
256
+ // Event Parser
257
+ // ═══════════════════════════════════════════════════════════════════
258
+
259
+ /**
260
+ * A single parsed event extracted from transaction logs.
261
+ *
262
+ * @interface ParsedEvent
263
+ * @name ParsedEvent
264
+ * @description Lightweight container with a `name` discriminant and untyped `data` record.
265
+ * Narrow via {@link SapEventName} for type-safe access.
266
+ * @category Events
267
+ * @since v0.1.0
268
+ */
269
+ export interface ParsedEvent {
270
+ readonly name: string;
271
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
272
+ readonly data: Record<string, any>;
273
+ }
274
+
275
+ /**
276
+ * Typed wrapper around Anchor’s `EventParser` for SAP v2 programs.
277
+ *
278
+ * Extracts strongly-typed {@link ParsedEvent} objects from raw
279
+ * transaction log lines.
280
+ *
281
+ * @name EventParser
282
+ * @description Instantiate with an Anchor `Program` and call {@link EventParser.parseLogs}
283
+ * to decode events, or {@link EventParser.filterByName} to narrow results.
284
+ * @category Events
285
+ * @since v0.1.0
286
+ *
287
+ * @example
288
+ * ```ts
289
+ * const parser = new EventParser(program);
290
+ * const events = parser.parseLogs(txLogs);
291
+ * const settled = parser.filterByName(events, "PaymentSettledEvent");
292
+ * ```
293
+ */
294
+ export class EventParser {
295
+ private readonly parser: AnchorEventParser;
296
+
297
+ /**
298
+ * Create a new `EventParser`.
299
+ *
300
+ * @param program - An Anchor `Program` instance whose IDL defines the events to decode.
301
+ */
302
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
303
+ constructor(program: Program<any>) {
304
+ this.parser = new AnchorEventParser(program.programId, program.coder);
305
+ }
306
+
307
+ /**
308
+ * Parse raw transaction log lines into typed events.
309
+ *
310
+ * @param logs - Array of log strings from a confirmed transaction.
311
+ * @returns {ParsedEvent[]} Decoded events found in the logs.
312
+ */
313
+ parseLogs(logs: string[]): ParsedEvent[] {
314
+ const events: ParsedEvent[] = [];
315
+ const generator = this.parser.parseLogs(logs);
316
+ for (const event of generator) {
317
+ events.push(event as ParsedEvent);
318
+ }
319
+ return events;
320
+ }
321
+
322
+ /**
323
+ * Filter a list of parsed events by event name.
324
+ *
325
+ * @param events - The full array of {@link ParsedEvent} objects to filter.
326
+ * @param name - The {@link SapEventName} to match against.
327
+ * @returns {ParsedEvent[]} Only events whose `name` matches.
328
+ */
329
+ filterByName<N extends SapEventName>(
330
+ events: ParsedEvent[],
331
+ name: N,
332
+ ): ParsedEvent[] {
333
+ return events.filter((e) => e.name === name);
334
+ }
335
+ }