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