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