@agentbadge/database 0.3.0 → 0.4.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.
@@ -1,5 +1,6 @@
1
1
  import { type PrismaDb } from "./repositories/event-repository.js";
2
2
  import { AgentWalletRepository } from "./repositories/agent-wallet-repository.js";
3
+ import { SpendLedgerRepository } from "./repositories/spend-ledger-repository.js";
3
4
  import { type ChatSubscriptionStore, type ScanResultStore, type Store } from "./store.js";
4
5
  export interface DatabaseConfig {
5
6
  url?: string;
@@ -11,6 +12,7 @@ export interface Database {
11
12
  scanResults: ScanResultStore;
12
13
  chatSubscriptions: ChatSubscriptionStore;
13
14
  agentWallets: AgentWalletRepository | null;
15
+ spendLedger: SpendLedgerRepository | null;
14
16
  health(): Promise<boolean>;
15
17
  close(): Promise<void>;
16
18
  }
package/dist/database.js CHANGED
@@ -4,6 +4,7 @@ import { EventRepository } from "./repositories/event-repository.js";
4
4
  import { ScanResultRepository } from "./repositories/scan-result-repository.js";
5
5
  import { ChatSubscriptionRepository } from "./repositories/chat-subscription-repository.js";
6
6
  import { AgentWalletRepository } from "./repositories/agent-wallet-repository.js";
7
+ import { SpendLedgerRepository } from "./repositories/spend-ledger-repository.js";
7
8
  import { InMemoryChatSubscriptionStore, InMemoryScanResultStore, InMemoryStore, } from "./store.js";
8
9
  export function createDatabase(config) {
9
10
  const enabled = config.enabled !== false && Boolean(config.url);
@@ -15,6 +16,7 @@ export function createDatabase(config) {
15
16
  scanResults: new InMemoryScanResultStore(),
16
17
  chatSubscriptions: new InMemoryChatSubscriptionStore(),
17
18
  agentWallets: null,
19
+ spendLedger: null,
18
20
  health: () => Promise.resolve(false),
19
21
  close: () => Promise.resolve(),
20
22
  };
@@ -26,6 +28,7 @@ export function createDatabase(config) {
26
28
  scanResults: new ScanResultRepository(db),
27
29
  chatSubscriptions: new ChatSubscriptionRepository(db),
28
30
  agentWallets: new AgentWalletRepository(db),
31
+ spendLedger: new SpendLedgerRepository(db),
29
32
  health: async () => {
30
33
  try {
31
34
  const plan = db.raw.sql `SELECT 1 AS health`.returnsRow({ health: "pg/int4@1" }).build();
package/dist/index.d.ts CHANGED
@@ -13,4 +13,6 @@ export { VenueRepository } from "./repositories/venue-repository.js";
13
13
  export type { VenueJobUpsert, VenueOfferUpsert, } from "./repositories/venue-repository.js";
14
14
  export { AgentWalletRepository } from "./repositories/agent-wallet-repository.js";
15
15
  export type { AgentWalletUpsert } from "./repositories/agent-wallet-repository.js";
16
+ export { SpendLedgerRepository } from "./repositories/spend-ledger-repository.js";
17
+ export type { SpendEventInsert } from "./repositories/spend-ledger-repository.js";
16
18
  export type { PrismaDb } from "./repositories/event-repository.js";
package/dist/index.js CHANGED
@@ -8,3 +8,4 @@ export { ScanResultRepository } from "./repositories/scan-result-repository.js";
8
8
  export { ChatSubscriptionRepository } from "./repositories/chat-subscription-repository.js";
9
9
  export { VenueRepository } from "./repositories/venue-repository.js";
10
10
  export { AgentWalletRepository } from "./repositories/agent-wallet-repository.js";
11
+ export { SpendLedgerRepository } from "./repositories/spend-ledger-repository.js";
@@ -20,6 +20,10 @@
20
20
  "model": "ScanResult",
21
21
  "namespace": "public"
22
22
  },
23
+ "spendEvent": {
24
+ "model": "SpendEvent",
25
+ "namespace": "public"
26
+ },
23
27
  "venueJob": {
24
28
  "model": "VenueJob",
25
29
  "namespace": "public"
@@ -381,6 +385,157 @@
381
385
  "table": "scanResult"
382
386
  }
383
387
  },
388
+ "SpendEvent": {
389
+ "fields": {
390
+ "amountUsd": {
391
+ "nullable": false,
392
+ "type": {
393
+ "codecId": "pg/numeric@1",
394
+ "kind": "scalar"
395
+ }
396
+ },
397
+ "at": {
398
+ "nullable": false,
399
+ "type": {
400
+ "codecId": "pg/int8@1",
401
+ "kind": "scalar"
402
+ }
403
+ },
404
+ "createdAt": {
405
+ "nullable": false,
406
+ "type": {
407
+ "codecId": "pg/timestamptz-string@1",
408
+ "kind": "scalar"
409
+ }
410
+ },
411
+ "denialReason": {
412
+ "nullable": true,
413
+ "type": {
414
+ "codecId": "pg/text@1",
415
+ "kind": "scalar"
416
+ }
417
+ },
418
+ "id": {
419
+ "nullable": false,
420
+ "type": {
421
+ "codecId": "pg/text@1",
422
+ "kind": "scalar"
423
+ }
424
+ },
425
+ "kind": {
426
+ "nullable": false,
427
+ "type": {
428
+ "codecId": "pg/text@1",
429
+ "kind": "scalar"
430
+ }
431
+ },
432
+ "payload": {
433
+ "nullable": false,
434
+ "type": {
435
+ "codecId": "pg/json@1",
436
+ "kind": "scalar"
437
+ }
438
+ },
439
+ "refId": {
440
+ "nullable": false,
441
+ "type": {
442
+ "codecId": "pg/text@1",
443
+ "kind": "scalar"
444
+ }
445
+ },
446
+ "sourceChain": {
447
+ "nullable": true,
448
+ "type": {
449
+ "codecId": "pg/text@1",
450
+ "kind": "scalar"
451
+ }
452
+ },
453
+ "state": {
454
+ "nullable": false,
455
+ "type": {
456
+ "codecId": "pg/text@1",
457
+ "kind": "scalar"
458
+ }
459
+ },
460
+ "txHash": {
461
+ "nullable": true,
462
+ "type": {
463
+ "codecId": "pg/text@1",
464
+ "kind": "scalar"
465
+ }
466
+ },
467
+ "updatedAt": {
468
+ "nullable": false,
469
+ "type": {
470
+ "codecId": "pg/timestamptz-string@1",
471
+ "kind": "scalar"
472
+ }
473
+ },
474
+ "venueId": {
475
+ "nullable": true,
476
+ "type": {
477
+ "codecId": "pg/text@1",
478
+ "kind": "scalar"
479
+ }
480
+ },
481
+ "wallet": {
482
+ "nullable": false,
483
+ "type": {
484
+ "codecId": "pg/text@1",
485
+ "kind": "scalar"
486
+ }
487
+ }
488
+ },
489
+ "relations": {},
490
+ "storage": {
491
+ "fields": {
492
+ "amountUsd": {
493
+ "column": "amountUsd"
494
+ },
495
+ "at": {
496
+ "column": "at"
497
+ },
498
+ "createdAt": {
499
+ "column": "createdAt"
500
+ },
501
+ "denialReason": {
502
+ "column": "denialReason"
503
+ },
504
+ "id": {
505
+ "column": "id"
506
+ },
507
+ "kind": {
508
+ "column": "kind"
509
+ },
510
+ "payload": {
511
+ "column": "payload"
512
+ },
513
+ "refId": {
514
+ "column": "refId"
515
+ },
516
+ "sourceChain": {
517
+ "column": "sourceChain"
518
+ },
519
+ "state": {
520
+ "column": "state"
521
+ },
522
+ "txHash": {
523
+ "column": "txHash"
524
+ },
525
+ "updatedAt": {
526
+ "column": "updatedAt"
527
+ },
528
+ "venueId": {
529
+ "column": "venueId"
530
+ },
531
+ "wallet": {
532
+ "column": "wallet"
533
+ }
534
+ },
535
+ "namespaceId": "public",
536
+ "table": "spendEvent"
537
+ }
538
+ },
384
539
  "VenueJob": {
385
540
  "fields": {
386
541
  "client": {
@@ -875,6 +1030,137 @@
875
1030
  },
876
1031
  "uniques": []
877
1032
  },
1033
+ "spendEvent": {
1034
+ "columns": {
1035
+ "amountUsd": {
1036
+ "codecId": "pg/numeric@1",
1037
+ "nativeType": "numeric",
1038
+ "nullable": false
1039
+ },
1040
+ "at": {
1041
+ "codecId": "pg/int8@1",
1042
+ "nativeType": "int8",
1043
+ "nullable": false
1044
+ },
1045
+ "createdAt": {
1046
+ "codecId": "pg/timestamptz-string@1",
1047
+ "default": {
1048
+ "expression": "now()",
1049
+ "kind": "function"
1050
+ },
1051
+ "nativeType": "timestamptz",
1052
+ "nullable": false
1053
+ },
1054
+ "denialReason": {
1055
+ "codecId": "pg/text@1",
1056
+ "nativeType": "text",
1057
+ "nullable": true
1058
+ },
1059
+ "id": {
1060
+ "codecId": "pg/text@1",
1061
+ "nativeType": "text",
1062
+ "nullable": false
1063
+ },
1064
+ "kind": {
1065
+ "codecId": "pg/text@1",
1066
+ "nativeType": "text",
1067
+ "nullable": false
1068
+ },
1069
+ "payload": {
1070
+ "codecId": "pg/json@1",
1071
+ "nativeType": "json",
1072
+ "nullable": false
1073
+ },
1074
+ "refId": {
1075
+ "codecId": "pg/text@1",
1076
+ "nativeType": "text",
1077
+ "nullable": false
1078
+ },
1079
+ "sourceChain": {
1080
+ "codecId": "pg/text@1",
1081
+ "nativeType": "text",
1082
+ "nullable": true
1083
+ },
1084
+ "state": {
1085
+ "codecId": "pg/text@1",
1086
+ "nativeType": "text",
1087
+ "nullable": false
1088
+ },
1089
+ "txHash": {
1090
+ "codecId": "pg/text@1",
1091
+ "nativeType": "text",
1092
+ "nullable": true
1093
+ },
1094
+ "updatedAt": {
1095
+ "codecId": "pg/timestamptz-string@1",
1096
+ "nativeType": "timestamptz",
1097
+ "nullable": false
1098
+ },
1099
+ "venueId": {
1100
+ "codecId": "pg/text@1",
1101
+ "nativeType": "text",
1102
+ "nullable": true
1103
+ },
1104
+ "wallet": {
1105
+ "codecId": "pg/text@1",
1106
+ "nativeType": "text",
1107
+ "nullable": false
1108
+ }
1109
+ },
1110
+ "foreignKeys": [],
1111
+ "indexes": [
1112
+ {
1113
+ "columns": [
1114
+ "denialReason",
1115
+ "at"
1116
+ ],
1117
+ "name": "spendEvent_denialReason_at_idx_84ea573f",
1118
+ "prefix": "spendEvent_denialReason_at_idx",
1119
+ "unique": false
1120
+ },
1121
+ {
1122
+ "columns": [
1123
+ "state",
1124
+ "at"
1125
+ ],
1126
+ "name": "spendEvent_state_at_idx_b570e6ae",
1127
+ "prefix": "spendEvent_state_at_idx",
1128
+ "unique": false
1129
+ },
1130
+ {
1131
+ "columns": [
1132
+ "txHash"
1133
+ ],
1134
+ "name": "spendEvent_txHash_idx_3fbe8b55",
1135
+ "prefix": "spendEvent_txHash_idx",
1136
+ "unique": false
1137
+ },
1138
+ {
1139
+ "columns": [
1140
+ "venueId",
1141
+ "at"
1142
+ ],
1143
+ "name": "spendEvent_venueId_at_idx_6ab90d05",
1144
+ "prefix": "spendEvent_venueId_at_idx",
1145
+ "unique": false
1146
+ },
1147
+ {
1148
+ "columns": [
1149
+ "wallet",
1150
+ "at"
1151
+ ],
1152
+ "name": "spendEvent_wallet_at_idx_b76ec1ce",
1153
+ "prefix": "spendEvent_wallet_at_idx",
1154
+ "unique": false
1155
+ }
1156
+ ],
1157
+ "primaryKey": {
1158
+ "columns": [
1159
+ "id"
1160
+ ]
1161
+ },
1162
+ "uniques": []
1163
+ },
878
1164
  "venueJob": {
879
1165
  "columns": {
880
1166
  "client": {
@@ -1048,10 +1334,10 @@
1048
1334
  "kind": "postgres-schema"
1049
1335
  }
1050
1336
  },
1051
- "storageHash": "b8e32f5ce86590030c6c80c1f24c199aae7daf9a6869951a0b1af2903d31f177"
1337
+ "storageHash": "b27c2a0270b519942b2abfc77f8b0fdc532c38bbaa652b63209148514c405a64"
1052
1338
  },
1053
1339
  "execution": {
1054
- "executionHash": "44a1f33983dd3b516b7a09d20036645e5dd41f325ead0441f78cf5b6647741a6",
1340
+ "executionHash": "45ef075f560eb8a89eb3c9c3b8cfa9330b98008a67d01084852a871d82dce8a2",
1055
1341
  "mutations": {
1056
1342
  "defaults": [
1057
1343
  {
@@ -1158,6 +1444,21 @@
1158
1444
  "table": "scanResult"
1159
1445
  }
1160
1446
  },
1447
+ {
1448
+ "onCreate": {
1449
+ "id": "timestampNow",
1450
+ "kind": "generator"
1451
+ },
1452
+ "onUpdate": {
1453
+ "id": "timestampNow",
1454
+ "kind": "generator"
1455
+ },
1456
+ "ref": {
1457
+ "column": "updatedAt",
1458
+ "namespace": "public",
1459
+ "table": "spendEvent"
1460
+ }
1461
+ },
1161
1462
  {
1162
1463
  "onCreate": {
1163
1464
  "id": "uuidv4",
@@ -0,0 +1,32 @@
1
+ import type { JsonValue } from "@prisma/orm-postgres/target/codec-types";
2
+ import type { Models } from "../prisma/contract.d.js";
3
+ import type { PrismaDb } from "./event-repository.js";
4
+ type Row = Models.public_SpendEvent;
5
+ export interface SpendEventInsert {
6
+ id: string;
7
+ wallet: string;
8
+ venueId?: string | null;
9
+ kind: string;
10
+ amountUsd: number;
11
+ state: string;
12
+ refId: string;
13
+ txHash?: string | null;
14
+ sourceChain?: string | null;
15
+ denialReason?: string | null;
16
+ at: number;
17
+ payload: JsonValue;
18
+ }
19
+ export declare class SpendLedgerRepository {
20
+ private readonly db;
21
+ constructor(db: PrismaDb);
22
+ append(e: SpendEventInsert): Promise<Row | null>;
23
+ transition(id: string, to: "settled" | "released" | "failed", txHash?: string, sourceChain?: string): Promise<boolean>;
24
+ listByWallet(wallet: string): Promise<Row[]>;
25
+ sumByWalletInWindow(wallet: string, sinceMs: number): Promise<number>;
26
+ getByTxHash(txHash: string): Promise<Row | null>;
27
+ listDenials(opts?: {
28
+ since?: number;
29
+ limit?: number;
30
+ }): Promise<Row[]>;
31
+ }
32
+ export {};
@@ -0,0 +1,69 @@
1
+ export class SpendLedgerRepository {
2
+ db;
3
+ constructor(db) {
4
+ this.db = db;
5
+ }
6
+ async append(e) {
7
+ const existing = await this.db.orm.public.SpendEvent.where({
8
+ id: e.id,
9
+ }).first();
10
+ if (existing)
11
+ return null;
12
+ return this.db.orm.public.SpendEvent.create({
13
+ id: e.id,
14
+ wallet: e.wallet.toLowerCase(),
15
+ venueId: e.venueId ?? null,
16
+ kind: e.kind,
17
+ amountUsd: String(e.amountUsd),
18
+ state: e.state,
19
+ refId: e.refId,
20
+ txHash: e.txHash ?? null,
21
+ sourceChain: e.sourceChain ?? null,
22
+ denialReason: e.denialReason ?? null,
23
+ at: BigInt(e.at),
24
+ payload: e.payload,
25
+ });
26
+ }
27
+ async transition(id, to, txHash, sourceChain) {
28
+ const row = await this.db.orm.public.SpendEvent.where({
29
+ id,
30
+ state: "reserved",
31
+ }).update({
32
+ state: to,
33
+ ...(txHash !== undefined ? { txHash } : {}),
34
+ ...(sourceChain !== undefined ? { sourceChain } : {}),
35
+ });
36
+ return row !== null;
37
+ }
38
+ async listByWallet(wallet) {
39
+ return this.db.orm.public.SpendEvent.orderBy((e) => e.at.desc())
40
+ .where({ wallet: wallet.toLowerCase() })
41
+ .all();
42
+ }
43
+ async sumByWalletInWindow(wallet, sinceMs) {
44
+ const rows = await this.db.orm.public.SpendEvent.where({
45
+ wallet: wallet.toLowerCase(),
46
+ }).all();
47
+ let sum = 0;
48
+ for (const r of rows) {
49
+ if (r.state !== "reserved" && r.state !== "settled")
50
+ continue;
51
+ if (Number(r.at) < sinceMs)
52
+ continue;
53
+ sum += Number(r.amountUsd);
54
+ }
55
+ return sum;
56
+ }
57
+ async getByTxHash(txHash) {
58
+ return this.db.orm.public.SpendEvent.where({ txHash }).first();
59
+ }
60
+ async listDenials(opts) {
61
+ let q = this.db.orm.public.SpendEvent.orderBy((e) => e.at.desc()).where({ state: "denied" });
62
+ if (opts?.limit !== undefined)
63
+ q = q.limit(opts.limit);
64
+ const rows = await q.all();
65
+ return opts?.since !== undefined
66
+ ? rows.filter((r) => Number(r.at) >= opts.since)
67
+ : rows;
68
+ }
69
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentbadge/database",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Shared database layer for AgentBadge — Prisma ORM 8 client, repositories, store abstraction",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",