@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,904 @@
1
+ /**
2
+ * @module postgres/adapter
3
+ * @description PostgreSQL adapter for SAP v2 — syncs on-chain accounts
4
+ * to a relational database for off-chain querying and analytics.
5
+ *
6
+ * The adapter uses `pg` (node-postgres) as the database driver.
7
+ * It is database-driver agnostic at the interface level — you can
8
+ * substitute any client that implements the `PgClient` interface.
9
+ *
10
+ * @category Postgres
11
+ * @since v0.1.0
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * import { SapPostgres } from "@synapse-sap/sdk/postgres";
16
+ * import { Pool } from "pg";
17
+ *
18
+ * const pool = new Pool({ connectionString: "postgresql://..." });
19
+ * const pg = new SapPostgres(pool, sapClient);
20
+ *
21
+ * // Run schema migration
22
+ * await pg.migrate();
23
+ *
24
+ * // Sync all agents to PostgreSQL
25
+ * await pg.syncAgents();
26
+ *
27
+ * // Full sync (all account types)
28
+ * await pg.syncAll();
29
+ *
30
+ * // Query off-chain
31
+ * const agents = await pg.query("SELECT * FROM sap_agents WHERE is_active = true");
32
+ * ```
33
+ */
34
+
35
+ import type { PublicKey } from "@solana/web3.js";
36
+ import type { SapClient } from "../core/client";
37
+ import type {
38
+ SyncOptions,
39
+ SyncCursorRow,
40
+ SapAccountType,
41
+ } from "./types";
42
+ import {
43
+ serializeGlobalRegistry,
44
+ serializeAgent,
45
+ serializeAgentStats,
46
+ serializeFeedback,
47
+ serializeCapabilityIndex,
48
+ serializeProtocolIndex,
49
+ serializeVault,
50
+ serializeSession,
51
+ serializeEpochPage,
52
+ serializeDelegate,
53
+ serializeTool,
54
+ serializeCheckpoint,
55
+ serializeEscrow,
56
+ serializeToolCategoryIndex,
57
+ serializeAttestation,
58
+ serializeLedger,
59
+ serializeLedgerPage,
60
+ } from "./serializers";
61
+ import { deriveGlobalRegistry } from "../pda";
62
+
63
+ import * as fs from "fs";
64
+ import * as path from "path";
65
+
66
+ // ═══════════════════════════════════════════════════════════════════
67
+ // Database Client Interface (driver-agnostic)
68
+ // ═══════════════════════════════════════════════════════════════════
69
+
70
+ /**
71
+ * @interface PgClient
72
+ * @description Minimal PostgreSQL client interface.
73
+ * Compatible with `pg.Pool`, `pg.Client`, or any wrapper
74
+ * that provides `query()`.
75
+ * @category Postgres
76
+ * @since v0.1.0
77
+ */
78
+ export interface PgClient {
79
+ query(text: string, values?: unknown[]): Promise<{ rows: unknown[]; rowCount: number }>;
80
+ }
81
+
82
+ // ═══════════════════════════════════════════════════════════════════
83
+ // SapPostgres Adapter
84
+ // ═══════════════════════════════════════════════════════════════════
85
+
86
+ /**
87
+ * @name SapPostgres
88
+ * @description PostgreSQL off-chain mirror for SAP v2 on-chain data.
89
+ *
90
+ * Connects to a PostgreSQL database and synchronizes all 22 on-chain
91
+ * account types into relational tables. Supports incremental sync,
92
+ * event logging, and cursor-based pagination.
93
+ *
94
+ * @category Postgres
95
+ * @since v0.1.0
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * import { SapPostgres } from "@synapse-sap/sdk/postgres";
100
+ * import { Pool } from "pg";
101
+ * import { SapClient } from "@synapse-sap/sdk";
102
+ *
103
+ * const pool = new Pool({ connectionString: process.env.DATABASE_URL });
104
+ * const sap = SapClient.from(provider);
105
+ * const pg = new SapPostgres(pool, sap);
106
+ *
107
+ * await pg.migrate(); // Create tables
108
+ * await pg.syncAll(); // Mirror on-chain state
109
+ *
110
+ * // Read from PostgreSQL
111
+ * const { rows } = await pg.query(
112
+ * "SELECT * FROM sap_agents WHERE is_active = true ORDER BY reputation_score DESC"
113
+ * );
114
+ * ```
115
+ */
116
+ export class SapPostgres {
117
+ private readonly db: PgClient;
118
+ private readonly client: SapClient;
119
+ private readonly debug: boolean;
120
+
121
+ constructor(db: PgClient, client: SapClient, debug = false) {
122
+ this.db = db;
123
+ this.client = client;
124
+ this.debug = debug;
125
+ }
126
+
127
+ // ═════════════════════════════════════════════
128
+ // Schema Migration
129
+ // ═════════════════════════════════════════════
130
+
131
+ /**
132
+ * @name migrate
133
+ * @description Run the SQL schema migration to create all SAP tables,
134
+ * indexes, views, and enum types. Safe to call multiple times
135
+ * (uses `CREATE IF NOT EXISTS`).
136
+ * @returns {Promise<void>}
137
+ * @since v0.1.0
138
+ */
139
+ async migrate(): Promise<void> {
140
+ const schemaPath = path.join(__dirname, "schema.sql");
141
+ const sql = fs.readFileSync(schemaPath, "utf-8");
142
+ await this.db.query(sql);
143
+ this.log("Schema migration complete");
144
+ }
145
+
146
+ /**
147
+ * @name migrateWithSQL
148
+ * @description Run migration with a custom SQL string.
149
+ * Useful when the schema.sql file is bundled differently.
150
+ * @param sql - The full SQL schema to execute.
151
+ * @since v0.1.0
152
+ */
153
+ async migrateWithSQL(sql: string): Promise<void> {
154
+ await this.db.query(sql);
155
+ this.log("Schema migration complete (custom SQL)");
156
+ }
157
+
158
+ // ═════════════════════════════════════════════
159
+ // Raw Query
160
+ // ═════════════════════════════════════════════
161
+
162
+ /**
163
+ * @name query
164
+ * @description Execute a raw SQL query against the database.
165
+ * @param text - SQL query string.
166
+ * @param values - Parameterized values.
167
+ * @returns Query result with rows and rowCount.
168
+ * @since v0.1.0
169
+ */
170
+ async query<T = unknown>(
171
+ text: string,
172
+ values?: unknown[],
173
+ ): Promise<{ rows: T[]; rowCount: number }> {
174
+ this.log(`QUERY: ${text.substring(0, 120)}...`);
175
+ return this.db.query(text, values) as Promise<{
176
+ rows: T[];
177
+ rowCount: number;
178
+ }>;
179
+ }
180
+
181
+ // ═════════════════════════════════════════════
182
+ // Upsert Helper
183
+ // ═════════════════════════════════════════════
184
+
185
+ /**
186
+ * @name upsert
187
+ * @description Insert or update a row in the specified table.
188
+ * Uses `ON CONFLICT (pda) DO UPDATE` for idempotent writes.
189
+ * @param table - Target table name.
190
+ * @param row - Key-value record to insert.
191
+ * @since v0.1.0
192
+ */
193
+ async upsert(table: string, row: Record<string, unknown>): Promise<void> {
194
+ const keys = Object.keys(row);
195
+ const values = Object.values(row);
196
+ const placeholders = keys.map((_, i) => `$${i + 1}`);
197
+ const updates = keys
198
+ .filter((k) => k !== "pda")
199
+ .map((k) => `${k} = $${keys.indexOf(k) + 1}`)
200
+ .join(", ");
201
+
202
+ const sql = `
203
+ INSERT INTO ${table} (${keys.join(", ")})
204
+ VALUES (${placeholders.join(", ")})
205
+ ON CONFLICT (pda) DO UPDATE SET ${updates}
206
+ `;
207
+
208
+ await this.db.query(sql, values);
209
+ }
210
+
211
+ /**
212
+ * @name upsertBatch
213
+ * @description Upsert multiple rows in a single transaction.
214
+ * @param table - Target table name.
215
+ * @param rows - Array of key-value records.
216
+ * @since v0.1.0
217
+ */
218
+ async upsertBatch(
219
+ table: string,
220
+ rows: Record<string, unknown>[],
221
+ ): Promise<void> {
222
+ if (rows.length === 0) return;
223
+ await this.db.query("BEGIN");
224
+ try {
225
+ for (const row of rows) {
226
+ await this.upsert(table, row);
227
+ }
228
+ await this.db.query("COMMIT");
229
+ } catch (err) {
230
+ await this.db.query("ROLLBACK");
231
+ throw err;
232
+ }
233
+ }
234
+
235
+ // ═════════════════════════════════════════════
236
+ // Sync Cursors
237
+ // ═════════════════════════════════════════════
238
+
239
+ /**
240
+ * @name getCursor
241
+ * @description Get the sync cursor for a given account type.
242
+ * @param accountType - The account type to check.
243
+ * @since v0.1.0
244
+ */
245
+ async getCursor(accountType: SapAccountType): Promise<SyncCursorRow | null> {
246
+ const { rows } = await this.db.query(
247
+ "SELECT * FROM sap_sync_cursors WHERE account_type = $1",
248
+ [accountType],
249
+ );
250
+ return (rows[0] as SyncCursorRow) ?? null;
251
+ }
252
+
253
+ /**
254
+ * @name updateCursor
255
+ * @description Update the sync cursor after a successful sync.
256
+ * @param accountType - The account type synced.
257
+ * @param slot - The last synced slot.
258
+ * @param signature - Optional last TX signature.
259
+ * @since v0.1.0
260
+ */
261
+ async updateCursor(
262
+ accountType: SapAccountType | string,
263
+ slot: number,
264
+ signature?: string,
265
+ ): Promise<void> {
266
+ await this.db.query(
267
+ `UPDATE sap_sync_cursors
268
+ SET last_slot = $1, last_signature = $2, updated_at = NOW()
269
+ WHERE account_type = $3`,
270
+ [slot, signature ?? null, accountType],
271
+ );
272
+ }
273
+
274
+ // ═════════════════════════════════════════════
275
+ // Individual Sync Methods
276
+ // ═════════════════════════════════════════════
277
+
278
+ /**
279
+ * @name syncGlobal
280
+ * @description Sync the GlobalRegistry singleton to PostgreSQL.
281
+ * @since v0.1.0
282
+ */
283
+ async syncGlobal(): Promise<void> {
284
+ const [globalPda] = deriveGlobalRegistry();
285
+ const pdaStr = globalPda.toBase58();
286
+ try {
287
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
288
+ const data = await (this.client.program.account as any).globalRegistry.fetch(globalPda);
289
+ const slot = await this.client.program.provider.connection.getSlot();
290
+ const row = serializeGlobalRegistry(pdaStr, data, slot);
291
+ await this.upsert("sap_global_registry", row);
292
+ await this.updateCursor("global_registry", slot);
293
+ this.log(`Synced global registry`);
294
+ } catch {
295
+ this.log("Global registry not found (not initialized yet)");
296
+ }
297
+ }
298
+
299
+ /**
300
+ * @name syncAgents
301
+ * @description Sync all AgentAccount PDAs to PostgreSQL.
302
+ * @since v0.1.0
303
+ */
304
+ async syncAgents(): Promise<number> {
305
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
306
+ const accounts = await (this.client.program.account as any).agentAccount.all();
307
+ const slot = await this.client.program.provider.connection.getSlot();
308
+ const rows = accounts.map(
309
+ (a: { publicKey: PublicKey; account: unknown }) =>
310
+ serializeAgent(a.publicKey.toBase58(), a.account as never, slot),
311
+ );
312
+ await this.upsertBatch("sap_agents", rows);
313
+ await this.updateCursor("agents", slot);
314
+ this.log(`Synced ${rows.length} agents`);
315
+ return rows.length;
316
+ }
317
+
318
+ /**
319
+ * @name syncAgentStats
320
+ * @description Sync all AgentStats PDAs to PostgreSQL.
321
+ * @since v0.1.0
322
+ */
323
+ async syncAgentStats(): Promise<number> {
324
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
325
+ const accounts = await (this.client.program.account as any).agentStats.all();
326
+ const slot = await this.client.program.provider.connection.getSlot();
327
+ const rows = accounts.map(
328
+ (a: { publicKey: PublicKey; account: unknown }) =>
329
+ serializeAgentStats(a.publicKey.toBase58(), a.account as never, slot),
330
+ );
331
+ await this.upsertBatch("sap_agent_stats", rows);
332
+ await this.updateCursor("agent_stats", slot);
333
+ this.log(`Synced ${rows.length} agent stats`);
334
+ return rows.length;
335
+ }
336
+
337
+ /**
338
+ * @name syncFeedbacks
339
+ * @description Sync all FeedbackAccount PDAs to PostgreSQL.
340
+ * @since v0.1.0
341
+ */
342
+ async syncFeedbacks(): Promise<number> {
343
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
344
+ const accounts = await (this.client.program.account as any).feedbackAccount.all();
345
+ const slot = await this.client.program.provider.connection.getSlot();
346
+ const rows = accounts.map(
347
+ (a: { publicKey: PublicKey; account: unknown }) =>
348
+ serializeFeedback(a.publicKey.toBase58(), a.account as never, slot),
349
+ );
350
+ await this.upsertBatch("sap_feedbacks", rows);
351
+ await this.updateCursor("feedbacks", slot);
352
+ this.log(`Synced ${rows.length} feedbacks`);
353
+ return rows.length;
354
+ }
355
+
356
+ /**
357
+ * @name syncTools
358
+ * @description Sync all ToolDescriptor PDAs to PostgreSQL.
359
+ * @since v0.1.0
360
+ */
361
+ async syncTools(): Promise<number> {
362
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
363
+ const accounts = await (this.client.program.account as any).toolDescriptor.all();
364
+ const slot = await this.client.program.provider.connection.getSlot();
365
+ const rows = accounts.map(
366
+ (a: { publicKey: PublicKey; account: unknown }) =>
367
+ serializeTool(a.publicKey.toBase58(), a.account as never, slot),
368
+ );
369
+ await this.upsertBatch("sap_tools", rows);
370
+ await this.updateCursor("tools", slot);
371
+ this.log(`Synced ${rows.length} tools`);
372
+ return rows.length;
373
+ }
374
+
375
+ /**
376
+ * @name syncEscrows
377
+ * @description Sync all EscrowAccount PDAs to PostgreSQL.
378
+ * @since v0.1.0
379
+ */
380
+ async syncEscrows(): Promise<number> {
381
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
382
+ const accounts = await (this.client.program.account as any).escrowAccount.all();
383
+ const slot = await this.client.program.provider.connection.getSlot();
384
+ const rows = accounts.map(
385
+ (a: { publicKey: PublicKey; account: unknown }) =>
386
+ serializeEscrow(a.publicKey.toBase58(), a.account as never, slot),
387
+ );
388
+ await this.upsertBatch("sap_escrows", rows);
389
+ await this.updateCursor("escrows", slot);
390
+ this.log(`Synced ${rows.length} escrows`);
391
+ return rows.length;
392
+ }
393
+
394
+ /**
395
+ * @name syncAttestations
396
+ * @description Sync all AgentAttestation PDAs to PostgreSQL.
397
+ * @since v0.1.0
398
+ */
399
+ async syncAttestations(): Promise<number> {
400
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
401
+ const accounts = await (this.client.program.account as any).agentAttestation.all();
402
+ const slot = await this.client.program.provider.connection.getSlot();
403
+ const rows = accounts.map(
404
+ (a: { publicKey: PublicKey; account: unknown }) =>
405
+ serializeAttestation(a.publicKey.toBase58(), a.account as never, slot),
406
+ );
407
+ await this.upsertBatch("sap_attestations", rows);
408
+ await this.updateCursor("attestations", slot);
409
+ this.log(`Synced ${rows.length} attestations`);
410
+ return rows.length;
411
+ }
412
+
413
+ /**
414
+ * @name syncVaults
415
+ * @description Sync all MemoryVault PDAs to PostgreSQL.
416
+ * @since v0.1.0
417
+ */
418
+ async syncVaults(): Promise<number> {
419
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
420
+ const accounts = await (this.client.program.account as any).memoryVault.all();
421
+ const slot = await this.client.program.provider.connection.getSlot();
422
+ const rows = accounts.map(
423
+ (a: { publicKey: PublicKey; account: unknown }) =>
424
+ serializeVault(a.publicKey.toBase58(), a.account as never, slot),
425
+ );
426
+ await this.upsertBatch("sap_memory_vaults", rows);
427
+ await this.updateCursor("memory_vaults", slot);
428
+ this.log(`Synced ${rows.length} vaults`);
429
+ return rows.length;
430
+ }
431
+
432
+ /**
433
+ * @name syncSessions
434
+ * @description Sync all SessionLedger PDAs to PostgreSQL.
435
+ * @since v0.1.0
436
+ */
437
+ async syncSessions(): Promise<number> {
438
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
439
+ const accounts = await (this.client.program.account as any).sessionLedger.all();
440
+ const slot = await this.client.program.provider.connection.getSlot();
441
+ const rows = accounts.map(
442
+ (a: { publicKey: PublicKey; account: unknown }) =>
443
+ serializeSession(a.publicKey.toBase58(), a.account as never, slot),
444
+ );
445
+ await this.upsertBatch("sap_sessions", rows);
446
+ await this.updateCursor("sessions", slot);
447
+ this.log(`Synced ${rows.length} sessions`);
448
+ return rows.length;
449
+ }
450
+
451
+ /**
452
+ * @name syncLedgers
453
+ * @description Sync all MemoryLedger PDAs to PostgreSQL.
454
+ * @since v0.1.0
455
+ */
456
+ async syncLedgers(): Promise<number> {
457
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
458
+ const accounts = await (this.client.program.account as any).memoryLedger.all();
459
+ const slot = await this.client.program.provider.connection.getSlot();
460
+ const rows = accounts.map(
461
+ (a: { publicKey: PublicKey; account: unknown }) =>
462
+ serializeLedger(a.publicKey.toBase58(), a.account as never, slot),
463
+ );
464
+ await this.upsertBatch("sap_memory_ledgers", rows);
465
+ await this.updateCursor("memory_ledgers", slot);
466
+ this.log(`Synced ${rows.length} ledgers`);
467
+ return rows.length;
468
+ }
469
+
470
+ /**
471
+ * @name syncLedgerPages
472
+ * @description Sync all LedgerPage PDAs to PostgreSQL.
473
+ * @since v0.1.0
474
+ */
475
+ async syncLedgerPages(): Promise<number> {
476
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
477
+ const accounts = await (this.client.program.account as any).ledgerPage.all();
478
+ const slot = await this.client.program.provider.connection.getSlot();
479
+ const rows = accounts.map(
480
+ (a: { publicKey: PublicKey; account: unknown }) =>
481
+ serializeLedgerPage(a.publicKey.toBase58(), a.account as never, slot),
482
+ );
483
+ await this.upsertBatch("sap_ledger_pages", rows);
484
+ await this.updateCursor("ledger_pages", slot);
485
+ this.log(`Synced ${rows.length} ledger pages`);
486
+ return rows.length;
487
+ }
488
+
489
+ /**
490
+ * @name syncCapabilityIndexes
491
+ * @description Sync all CapabilityIndex PDAs to PostgreSQL.
492
+ * @since v0.1.0
493
+ */
494
+ async syncCapabilityIndexes(): Promise<number> {
495
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
496
+ const accounts = await (this.client.program.account as any).capabilityIndex.all();
497
+ const slot = await this.client.program.provider.connection.getSlot();
498
+ const rows = accounts.map(
499
+ (a: { publicKey: PublicKey; account: unknown }) =>
500
+ serializeCapabilityIndex(a.publicKey.toBase58(), a.account as never, slot),
501
+ );
502
+ await this.upsertBatch("sap_capability_indexes", rows);
503
+ await this.updateCursor("capability_indexes", slot);
504
+ this.log(`Synced ${rows.length} capability indexes`);
505
+ return rows.length;
506
+ }
507
+
508
+ /**
509
+ * @name syncProtocolIndexes
510
+ * @description Sync all ProtocolIndex PDAs to PostgreSQL.
511
+ * @since v0.1.0
512
+ */
513
+ async syncProtocolIndexes(): Promise<number> {
514
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
515
+ const accounts = await (this.client.program.account as any).protocolIndex.all();
516
+ const slot = await this.client.program.provider.connection.getSlot();
517
+ const rows = accounts.map(
518
+ (a: { publicKey: PublicKey; account: unknown }) =>
519
+ serializeProtocolIndex(a.publicKey.toBase58(), a.account as never, slot),
520
+ );
521
+ await this.upsertBatch("sap_protocol_indexes", rows);
522
+ await this.updateCursor("protocol_indexes", slot);
523
+ this.log(`Synced ${rows.length} protocol indexes`);
524
+ return rows.length;
525
+ }
526
+
527
+ /**
528
+ * @name syncToolCategoryIndexes
529
+ * @description Sync all ToolCategoryIndex PDAs to PostgreSQL.
530
+ * @since v0.1.0
531
+ */
532
+ async syncToolCategoryIndexes(): Promise<number> {
533
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
534
+ const accounts = await (this.client.program.account as any).toolCategoryIndex.all();
535
+ const slot = await this.client.program.provider.connection.getSlot();
536
+ const rows = accounts.map(
537
+ (a: { publicKey: PublicKey; account: unknown }) =>
538
+ serializeToolCategoryIndex(a.publicKey.toBase58(), a.account as never, slot),
539
+ );
540
+ await this.upsertBatch("sap_tool_category_indexes", rows);
541
+ await this.updateCursor("tool_category_indexes", slot);
542
+ this.log(`Synced ${rows.length} tool category indexes`);
543
+ return rows.length;
544
+ }
545
+
546
+ /**
547
+ * @name syncEpochPages
548
+ * @description Sync all EpochPage PDAs to PostgreSQL.
549
+ * @since v0.1.0
550
+ */
551
+ async syncEpochPages(): Promise<number> {
552
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
553
+ const accounts = await (this.client.program.account as any).epochPage.all();
554
+ const slot = await this.client.program.provider.connection.getSlot();
555
+ const rows = accounts.map(
556
+ (a: { publicKey: PublicKey; account: unknown }) =>
557
+ serializeEpochPage(a.publicKey.toBase58(), a.account as never, slot),
558
+ );
559
+ await this.upsertBatch("sap_epoch_pages", rows);
560
+ await this.updateCursor("epoch_pages", slot);
561
+ this.log(`Synced ${rows.length} epoch pages`);
562
+ return rows.length;
563
+ }
564
+
565
+ /**
566
+ * @name syncDelegates
567
+ * @description Sync all VaultDelegate PDAs to PostgreSQL.
568
+ * @since v0.1.0
569
+ */
570
+ async syncDelegates(): Promise<number> {
571
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
572
+ const accounts = await (this.client.program.account as any).vaultDelegate.all();
573
+ const slot = await this.client.program.provider.connection.getSlot();
574
+ const rows = accounts.map(
575
+ (a: { publicKey: PublicKey; account: unknown }) =>
576
+ serializeDelegate(a.publicKey.toBase58(), a.account as never, slot),
577
+ );
578
+ await this.upsertBatch("sap_vault_delegates", rows);
579
+ await this.updateCursor("vault_delegates", slot);
580
+ this.log(`Synced ${rows.length} delegates`);
581
+ return rows.length;
582
+ }
583
+
584
+ /**
585
+ * @name syncCheckpoints
586
+ * @description Sync all SessionCheckpoint PDAs to PostgreSQL.
587
+ * @since v0.1.0
588
+ */
589
+ async syncCheckpoints(): Promise<number> {
590
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
591
+ const accounts = await (this.client.program.account as any).sessionCheckpoint.all();
592
+ const slot = await this.client.program.provider.connection.getSlot();
593
+ const rows = accounts.map(
594
+ (a: { publicKey: PublicKey; account: unknown }) =>
595
+ serializeCheckpoint(a.publicKey.toBase58(), a.account as never, slot),
596
+ );
597
+ await this.upsertBatch("sap_checkpoints", rows);
598
+ await this.updateCursor("checkpoints", slot);
599
+ this.log(`Synced ${rows.length} checkpoints`);
600
+ return rows.length;
601
+ }
602
+
603
+ // ═════════════════════════════════════════════
604
+ // Full Sync
605
+ // ═════════════════════════════════════════════
606
+
607
+ /**
608
+ * @name syncAll
609
+ * @description Sync all on-chain account types to PostgreSQL.
610
+ *
611
+ * Fetches every account via `program.account.*.all()` and
612
+ * upserts into the corresponding table. Reports progress
613
+ * via the `onProgress` callback.
614
+ *
615
+ * @param options - Optional sync configuration.
616
+ * @returns Summary of synced account counts.
617
+ * @since v0.1.0
618
+ *
619
+ * @example
620
+ * ```ts
621
+ * const result = await pg.syncAll({
622
+ * onProgress: (synced, total, type) => {
623
+ * console.log(`[${type}] ${synced}/${total}`);
624
+ * },
625
+ * });
626
+ * console.log("Total synced:", result.totalRecords);
627
+ * ```
628
+ */
629
+ async syncAll(options?: SyncOptions): Promise<SyncAllResult> {
630
+ const result: SyncAllResult = {
631
+ agents: 0,
632
+ agentStats: 0,
633
+ feedbacks: 0,
634
+ tools: 0,
635
+ escrows: 0,
636
+ attestations: 0,
637
+ vaults: 0,
638
+ sessions: 0,
639
+ epochPages: 0,
640
+ delegates: 0,
641
+ checkpoints: 0,
642
+ ledgers: 0,
643
+ ledgerPages: 0,
644
+ capabilityIndexes: 0,
645
+ protocolIndexes: 0,
646
+ toolCategoryIndexes: 0,
647
+ totalRecords: 0,
648
+ durationMs: 0,
649
+ };
650
+
651
+ const start = Date.now();
652
+ const total = 16;
653
+ const onProgress = options?.onProgress;
654
+
655
+ // Sync in dependency order
656
+ await this.syncGlobal();
657
+ if (onProgress) onProgress(1, total, "global_registry");
658
+
659
+ result.agents = await this.syncAgents();
660
+ if (onProgress) onProgress(2, total, "agents");
661
+
662
+ result.agentStats = await this.syncAgentStats();
663
+ if (onProgress) onProgress(3, total, "agent_stats");
664
+
665
+ result.feedbacks = await this.syncFeedbacks();
666
+ if (onProgress) onProgress(4, total, "feedbacks");
667
+
668
+ result.tools = await this.syncTools();
669
+ if (onProgress) onProgress(5, total, "tools");
670
+
671
+ result.escrows = await this.syncEscrows();
672
+ if (onProgress) onProgress(6, total, "escrows");
673
+
674
+ result.attestations = await this.syncAttestations();
675
+ if (onProgress) onProgress(7, total, "attestations");
676
+
677
+ result.vaults = await this.syncVaults();
678
+ if (onProgress) onProgress(8, total, "vaults");
679
+
680
+ result.sessions = await this.syncSessions();
681
+ if (onProgress) onProgress(9, total, "sessions");
682
+
683
+ result.epochPages = await this.syncEpochPages();
684
+ if (onProgress) onProgress(10, total, "epoch_pages");
685
+
686
+ result.delegates = await this.syncDelegates();
687
+ if (onProgress) onProgress(11, total, "delegates");
688
+
689
+ result.checkpoints = await this.syncCheckpoints();
690
+ if (onProgress) onProgress(12, total, "checkpoints");
691
+
692
+ result.ledgers = await this.syncLedgers();
693
+ if (onProgress) onProgress(13, total, "ledgers");
694
+
695
+ result.ledgerPages = await this.syncLedgerPages();
696
+ if (onProgress) onProgress(14, total, "ledger_pages");
697
+
698
+ result.capabilityIndexes = await this.syncCapabilityIndexes();
699
+ if (onProgress) onProgress(15, total, "capability_indexes");
700
+
701
+ result.protocolIndexes = await this.syncProtocolIndexes();
702
+ result.toolCategoryIndexes = await this.syncToolCategoryIndexes();
703
+ if (onProgress) onProgress(16, total, "indexes");
704
+
705
+ result.totalRecords =
706
+ result.agents +
707
+ result.agentStats +
708
+ result.feedbacks +
709
+ result.tools +
710
+ result.escrows +
711
+ result.attestations +
712
+ result.vaults +
713
+ result.sessions +
714
+ result.epochPages +
715
+ result.delegates +
716
+ result.checkpoints +
717
+ result.ledgers +
718
+ result.ledgerPages +
719
+ result.capabilityIndexes +
720
+ result.protocolIndexes +
721
+ result.toolCategoryIndexes;
722
+
723
+ result.durationMs = Date.now() - start;
724
+ this.log(
725
+ `Full sync complete: ${result.totalRecords} records in ${result.durationMs}ms`,
726
+ );
727
+
728
+ return result;
729
+ }
730
+
731
+ // ═════════════════════════════════════════════
732
+ // Event Sync
733
+ // ═════════════════════════════════════════════
734
+
735
+ /**
736
+ * @name syncEvent
737
+ * @description Store a parsed SAP event in the events log table.
738
+ * @param eventName - The event name (e.g. "RegisteredEvent").
739
+ * @param txSignature - The transaction signature.
740
+ * @param slot - The Solana slot.
741
+ * @param data - The parsed event data.
742
+ * @param agentPda - Optional agent PDA for indexing.
743
+ * @param wallet - Optional wallet for indexing.
744
+ * @since v0.1.0
745
+ */
746
+ async syncEvent(
747
+ eventName: string,
748
+ txSignature: string,
749
+ slot: number,
750
+ data: Record<string, unknown>,
751
+ agentPda?: string,
752
+ wallet?: string,
753
+ ): Promise<void> {
754
+ await this.db.query(
755
+ `INSERT INTO sap_events (event_name, tx_signature, slot, data, agent_pda, wallet)
756
+ VALUES ($1, $2, $3, $4, $5, $6)`,
757
+ [eventName, txSignature, slot, JSON.stringify(data), agentPda ?? null, wallet ?? null],
758
+ );
759
+ }
760
+
761
+ // ═════════════════════════════════════════════
762
+ // Convenience Queries
763
+ // ═════════════════════════════════════════════
764
+
765
+ /**
766
+ * @name getAgent
767
+ * @description Fetch a single agent by PDA or wallet.
768
+ * @param pdaOrWallet - Agent PDA (base58) or owner wallet.
769
+ * @since v0.1.0
770
+ */
771
+ async getAgent(pdaOrWallet: string): Promise<unknown | null> {
772
+ const { rows } = await this.db.query(
773
+ "SELECT * FROM sap_agents WHERE pda = $1 OR wallet = $1 LIMIT 1",
774
+ [pdaOrWallet],
775
+ );
776
+ return rows[0] ?? null;
777
+ }
778
+
779
+ /**
780
+ * @name getActiveAgents
781
+ * @description Fetch all active agents, ordered by reputation.
782
+ * @param limit - Max agents to return (default: 100).
783
+ * @since v0.1.0
784
+ */
785
+ async getActiveAgents(limit = 100): Promise<unknown[]> {
786
+ const { rows } = await this.db.query(
787
+ `SELECT * FROM sap_active_agents
788
+ ORDER BY reputation_score DESC
789
+ LIMIT $1`,
790
+ [limit],
791
+ );
792
+ return rows;
793
+ }
794
+
795
+ /**
796
+ * @name getEscrowBalance
797
+ * @description Fetch escrow balance for a specific agent/depositor pair.
798
+ * @param agentPda - Agent PDA (base58).
799
+ * @param depositor - Depositor wallet (base58).
800
+ * @since v0.1.0
801
+ */
802
+ async getEscrowBalance(
803
+ agentPda: string,
804
+ depositor: string,
805
+ ): Promise<unknown | null> {
806
+ const { rows } = await this.db.query(
807
+ "SELECT * FROM sap_escrow_balances WHERE agent = $1 AND depositor = $2",
808
+ [agentPda, depositor],
809
+ );
810
+ return rows[0] ?? null;
811
+ }
812
+
813
+ /**
814
+ * @name getAgentTools
815
+ * @description Fetch all active tools for a given agent.
816
+ * @param agentPda - Agent PDA (base58).
817
+ * @since v0.1.0
818
+ */
819
+ async getAgentTools(agentPda: string): Promise<unknown[]> {
820
+ const { rows } = await this.db.query(
821
+ "SELECT * FROM sap_agent_tools WHERE agent = $1",
822
+ [agentPda],
823
+ );
824
+ return rows;
825
+ }
826
+
827
+ /**
828
+ * @name getRecentEvents
829
+ * @description Fetch the most recent events.
830
+ * @param limit - Max events to return (default: 50).
831
+ * @param eventName - Optional filter by event name.
832
+ * @since v0.1.0
833
+ */
834
+ async getRecentEvents(
835
+ limit = 50,
836
+ eventName?: string,
837
+ ): Promise<unknown[]> {
838
+ if (eventName) {
839
+ const { rows } = await this.db.query(
840
+ "SELECT * FROM sap_events WHERE event_name = $1 ORDER BY id DESC LIMIT $2",
841
+ [eventName, limit],
842
+ );
843
+ return rows;
844
+ }
845
+ const { rows } = await this.db.query(
846
+ "SELECT * FROM sap_events ORDER BY id DESC LIMIT $1",
847
+ [limit],
848
+ );
849
+ return rows;
850
+ }
851
+
852
+ /**
853
+ * @name getSyncStatus
854
+ * @description Get the sync status for all account types.
855
+ * @since v0.1.0
856
+ */
857
+ async getSyncStatus(): Promise<SyncCursorRow[]> {
858
+ const { rows } = await this.db.query(
859
+ "SELECT * FROM sap_sync_cursors ORDER BY account_type",
860
+ );
861
+ return rows as SyncCursorRow[];
862
+ }
863
+
864
+ // ═════════════════════════════════════════════
865
+ // Internal
866
+ // ═════════════════════════════════════════════
867
+
868
+ private log(msg: string): void {
869
+ if (this.debug) {
870
+ console.log(`[SapPostgres] ${msg}`);
871
+ }
872
+ }
873
+ }
874
+
875
+ // ═══════════════════════════════════════════════════════════════════
876
+ // Result Types
877
+ // ═══════════════════════════════════════════════════════════════════
878
+
879
+ /**
880
+ * @interface SyncAllResult
881
+ * @description Result of a full sync operation.
882
+ * @category Postgres
883
+ * @since v0.1.0
884
+ */
885
+ export interface SyncAllResult {
886
+ agents: number;
887
+ agentStats: number;
888
+ feedbacks: number;
889
+ tools: number;
890
+ escrows: number;
891
+ attestations: number;
892
+ vaults: number;
893
+ sessions: number;
894
+ epochPages: number;
895
+ delegates: number;
896
+ checkpoints: number;
897
+ ledgers: number;
898
+ ledgerPages: number;
899
+ capabilityIndexes: number;
900
+ protocolIndexes: number;
901
+ toolCategoryIndexes: number;
902
+ totalRecords: number;
903
+ durationMs: number;
904
+ }