@agentbadge/database 0.6.15 → 0.6.17

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.
@@ -0,0 +1,378 @@
1
+ const canonDecimal = (v) => {
2
+ const s = String(v);
3
+ if (/[eE]/.test(s))
4
+ throw new Error(`exponent form: ${s}`);
5
+ const m = /^(-?\d+)(?:\.(\d+))?$/.exec(s);
6
+ if (!m)
7
+ throw new Error(`non-canonical decimal: ${s}`);
8
+ const frac = (m[2] ?? "").replace(/0+$/, "");
9
+ if (frac.length > 6)
10
+ throw new Error(`decimal exceeds 6dp: ${s}`);
11
+ return frac ? `${m[1]}.${frac}` : m[1];
12
+ };
13
+ const nowIso = () => new Date().toISOString();
14
+ export class InMemoryMilestonePlanStore {
15
+ rows = new Map();
16
+ async create(p) {
17
+ if (this.rows.has(p.id))
18
+ return null;
19
+ for (const x of this.rows.values()) {
20
+ if (x.agreementRef === p.agreementRef)
21
+ return null;
22
+ }
23
+ const ts = nowIso();
24
+ const row = {
25
+ ...p,
26
+ currency: p.currency ?? "USDC",
27
+ totalUsd: canonDecimal(p.totalUsd),
28
+ createdAt: ts,
29
+ updatedAt: ts,
30
+ };
31
+ this.rows.set(row.id, row);
32
+ return row;
33
+ }
34
+ async getById(id) {
35
+ return this.rows.get(id) ?? null;
36
+ }
37
+ async getByAgreement(agreementRef) {
38
+ return ([...this.rows.values()].find((r) => r.agreementRef === agreementRef) ??
39
+ null);
40
+ }
41
+ async list(opts = {}) {
42
+ let out = [...this.rows.values()].filter((r) => (opts.status === undefined || r.status === opts.status) &&
43
+ (opts.buyerWallet === undefined || r.buyerWallet === opts.buyerWallet) &&
44
+ (opts.providerWallet === undefined ||
45
+ r.providerWallet === opts.providerWallet));
46
+ out.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
47
+ if (opts.limit !== undefined)
48
+ out = out.slice(0, opts.limit);
49
+ return out;
50
+ }
51
+ async applyStatus(id, expectedFrom, to) {
52
+ const row = this.rows.get(id);
53
+ if (!row || row.status !== expectedFrom)
54
+ return null;
55
+ const next = { ...row, status: to, updatedAt: nowIso() };
56
+ this.rows.set(id, next);
57
+ return next;
58
+ }
59
+ }
60
+ export class InMemoryMilestoneStore {
61
+ rows = new Map();
62
+ async create(m) {
63
+ if (this.rows.has(m.id))
64
+ return null;
65
+ for (const x of this.rows.values()) {
66
+ if (x.planId === m.planId && x.milestoneId === m.milestoneId)
67
+ return null;
68
+ }
69
+ const ts = nowIso();
70
+ const row = {
71
+ ...m,
72
+ amountUsd: canonDecimal(m.amountUsd),
73
+ acceptanceCriteria: m.acceptanceCriteria ?? null,
74
+ dueAtMs: m.dueAtMs ?? null,
75
+ submissionRef: m.submissionRef ?? null,
76
+ escrowRef: m.escrowRef ?? null,
77
+ createdAt: ts,
78
+ updatedAt: ts,
79
+ };
80
+ this.rows.set(row.id, row);
81
+ return row;
82
+ }
83
+ async getById(id) {
84
+ return this.rows.get(id) ?? null;
85
+ }
86
+ async getByPlanMilestone(planId, milestoneId) {
87
+ return ([...this.rows.values()].find((r) => r.planId === planId && r.milestoneId === milestoneId) ?? null);
88
+ }
89
+ async listByPlan(planId) {
90
+ return [...this.rows.values()]
91
+ .filter((r) => r.planId === planId)
92
+ .sort((a, b) => a.seq - b.seq);
93
+ }
94
+ async applyStatus(id, expectedFrom, to, patch) {
95
+ const row = this.rows.get(id);
96
+ if (!row || row.status !== expectedFrom)
97
+ return null;
98
+ const next = {
99
+ ...row,
100
+ status: to,
101
+ ...(patch?.submissionRef !== undefined
102
+ ? { submissionRef: patch.submissionRef }
103
+ : {}),
104
+ ...(patch?.escrowRef !== undefined ? { escrowRef: patch.escrowRef } : {}),
105
+ updatedAt: nowIso(),
106
+ };
107
+ this.rows.set(id, next);
108
+ return next;
109
+ }
110
+ }
111
+ export class InMemoryPayoutIntentStore {
112
+ rows = new Map();
113
+ async create(i) {
114
+ if (this.rows.has(i.id))
115
+ return null;
116
+ for (const x of this.rows.values()) {
117
+ if (x.milestoneRef === i.milestoneRef)
118
+ return null;
119
+ }
120
+ const ts = nowIso();
121
+ const row = {
122
+ ...i,
123
+ executor: i.executor ?? "MANUAL",
124
+ completedAtMs: null,
125
+ completedBy: null,
126
+ createdAt: ts,
127
+ updatedAt: ts,
128
+ };
129
+ this.rows.set(row.id, row);
130
+ return row;
131
+ }
132
+ async getById(id) {
133
+ return this.rows.get(id) ?? null;
134
+ }
135
+ async getByMilestone(milestoneRef) {
136
+ return ([...this.rows.values()].find((r) => r.milestoneRef === milestoneRef) ??
137
+ null);
138
+ }
139
+ async list(opts = {}) {
140
+ let out = [...this.rows.values()].filter((r) => opts.status === undefined || r.status === opts.status);
141
+ out.sort((a, b) => b.createdAtMs - a.createdAtMs);
142
+ if (opts.limit !== undefined)
143
+ out = out.slice(0, opts.limit);
144
+ return out;
145
+ }
146
+ async applyStatus(id, expectedFrom, to, patch) {
147
+ const row = this.rows.get(id);
148
+ if (!row || row.status !== expectedFrom)
149
+ return null;
150
+ const next = {
151
+ ...row,
152
+ status: to,
153
+ ...(patch?.completedAtMs !== undefined
154
+ ? { completedAtMs: patch.completedAtMs }
155
+ : {}),
156
+ ...(patch?.completedBy !== undefined
157
+ ? { completedBy: patch.completedBy }
158
+ : {}),
159
+ updatedAt: nowIso(),
160
+ };
161
+ this.rows.set(id, next);
162
+ return next;
163
+ }
164
+ }
165
+ export class InMemoryPayoutLegStore {
166
+ rows = new Map();
167
+ async create(l) {
168
+ if (this.rows.has(l.id))
169
+ return null;
170
+ const ts = nowIso();
171
+ const row = {
172
+ ...l,
173
+ amountUsdc: canonDecimal(l.amountUsdc),
174
+ network: l.network ?? "arc",
175
+ txHash: null,
176
+ completedAtMs: null,
177
+ completedBy: null,
178
+ createdAt: ts,
179
+ updatedAt: ts,
180
+ };
181
+ this.rows.set(row.id, row);
182
+ return row;
183
+ }
184
+ async getById(id) {
185
+ return this.rows.get(id) ?? null;
186
+ }
187
+ async getByTxHash(network, txHash) {
188
+ for (const r of this.rows.values()) {
189
+ if (r.txHash === txHash && r.network === network)
190
+ return r;
191
+ }
192
+ return null;
193
+ }
194
+ async listByIntent(intentId) {
195
+ return [...this.rows.values()].filter((r) => r.intentId === intentId);
196
+ }
197
+ async complete(id, expectedFrom, txHash, completedBy, completedAtMs) {
198
+ const row = this.rows.get(id);
199
+ if (!row || row.status !== expectedFrom || row.txHash != null)
200
+ return null;
201
+ for (const r of this.rows.values()) {
202
+ if (r.id !== id && r.txHash === txHash && r.network === row.network) {
203
+ return null;
204
+ }
205
+ }
206
+ const next = {
207
+ ...row,
208
+ status: "COMPLETED",
209
+ txHash,
210
+ completedBy,
211
+ completedAtMs,
212
+ updatedAt: nowIso(),
213
+ };
214
+ this.rows.set(id, next);
215
+ return next;
216
+ }
217
+ async applyStatus(id, expectedFrom, to) {
218
+ const row = this.rows.get(id);
219
+ if (!row || row.status !== expectedFrom)
220
+ return null;
221
+ const next = { ...row, status: to, updatedAt: nowIso() };
222
+ this.rows.set(id, next);
223
+ return next;
224
+ }
225
+ }
226
+ export class InMemoryDisputeStore {
227
+ rows = new Map();
228
+ async create(d) {
229
+ if (this.rows.has(d.id))
230
+ return null;
231
+ const ts = nowIso();
232
+ const row = {
233
+ ...d,
234
+ resolution: null,
235
+ splitAmounts: null,
236
+ resolvedBy: null,
237
+ resolverRef: null,
238
+ resolvedAtMs: null,
239
+ createdAt: ts,
240
+ updatedAt: ts,
241
+ };
242
+ this.rows.set(row.id, row);
243
+ return row;
244
+ }
245
+ async getById(id) {
246
+ return this.rows.get(id) ?? null;
247
+ }
248
+ async listBySubject(subjectType, subjectRef) {
249
+ return [...this.rows.values()]
250
+ .filter((r) => r.subjectType === subjectType && r.subjectRef === subjectRef)
251
+ .sort((a, b) => a.createdAtMs - b.createdAtMs);
252
+ }
253
+ async list(opts = {}) {
254
+ let out = [...this.rows.values()].filter((r) => opts.status === undefined || r.status === opts.status);
255
+ out.sort((a, b) => b.createdAtMs - a.createdAtMs);
256
+ if (opts.limit !== undefined)
257
+ out = out.slice(0, opts.limit);
258
+ return out;
259
+ }
260
+ async resolve(id, resolution, resolvedBy, resolvedAtMs, extra) {
261
+ const row = this.rows.get(id);
262
+ if (!row || row.status !== "OPEN")
263
+ return null;
264
+ const next = {
265
+ ...row,
266
+ status: "RESOLVED",
267
+ resolution,
268
+ resolvedBy,
269
+ resolvedAtMs,
270
+ splitAmounts: extra?.splitAmounts ?? null,
271
+ resolverRef: extra?.resolverRef ?? null,
272
+ updatedAt: nowIso(),
273
+ };
274
+ this.rows.set(id, next);
275
+ return next;
276
+ }
277
+ }
278
+ export class InMemorySettlementAnchorStore {
279
+ rows = new Map();
280
+ async create(a) {
281
+ if (this.rows.has(a.id))
282
+ return null;
283
+ for (const x of this.rows.values()) {
284
+ if (x.receiptRef === a.receiptRef)
285
+ return null;
286
+ }
287
+ const ts = nowIso();
288
+ const row = {
289
+ ...a,
290
+ txHash: null,
291
+ attempts: 0,
292
+ lastError: null,
293
+ anchoredAtMs: null,
294
+ createdAt: ts,
295
+ updatedAt: ts,
296
+ };
297
+ this.rows.set(row.id, row);
298
+ return row;
299
+ }
300
+ async getById(id) {
301
+ return this.rows.get(id) ?? null;
302
+ }
303
+ async getByReceiptRef(receiptRef) {
304
+ return ([...this.rows.values()].find((r) => r.receiptRef === receiptRef) ?? null);
305
+ }
306
+ async list(opts = {}) {
307
+ const set = opts.statuses !== undefined
308
+ ? new Set(opts.statuses)
309
+ : opts.status !== undefined
310
+ ? new Set([opts.status])
311
+ : undefined;
312
+ let out = [...this.rows.values()].filter((r) => set === undefined || set.has(r.status));
313
+ out.sort((a, b) => a.createdAt.localeCompare(b.createdAt));
314
+ if (opts.limit !== undefined)
315
+ out = out.slice(0, opts.limit);
316
+ return out;
317
+ }
318
+ async markAnchored(id, txHash, anchoredAtMs) {
319
+ const row = this.rows.get(id);
320
+ if (!row || row.status !== "PENDING")
321
+ return null;
322
+ const next = {
323
+ ...row,
324
+ status: "ANCHORED",
325
+ txHash,
326
+ anchoredAtMs,
327
+ updatedAt: nowIso(),
328
+ };
329
+ this.rows.set(id, next);
330
+ return next;
331
+ }
332
+ async markAttempt(id, lastError, failed) {
333
+ const row = this.rows.get(id);
334
+ if (!row || row.status === "ANCHORED")
335
+ return null;
336
+ const next = {
337
+ ...row,
338
+ attempts: row.attempts + 1,
339
+ lastError,
340
+ status: failed ? "FAILED" : "PENDING",
341
+ updatedAt: nowIso(),
342
+ };
343
+ this.rows.set(id, next);
344
+ return next;
345
+ }
346
+ }
347
+ function cloneRows(value) {
348
+ if (value === null || typeof value !== "object")
349
+ return value;
350
+ return structuredClone(value);
351
+ }
352
+ function cloneStore(store) {
353
+ return new Proxy(store, {
354
+ get(target, prop, receiver) {
355
+ const value = Reflect.get(target, prop, receiver);
356
+ if (typeof value !== "function")
357
+ return value;
358
+ return async (...args) => cloneRows(await value.apply(target, args));
359
+ },
360
+ });
361
+ }
362
+ export function createInMemorySettlementStores() {
363
+ let tail = Promise.resolve();
364
+ const stores = {
365
+ plans: cloneStore(new InMemoryMilestonePlanStore()),
366
+ milestones: cloneStore(new InMemoryMilestoneStore()),
367
+ payoutIntents: cloneStore(new InMemoryPayoutIntentStore()),
368
+ payoutLegs: cloneStore(new InMemoryPayoutLegStore()),
369
+ disputes: cloneStore(new InMemoryDisputeStore()),
370
+ anchors: cloneStore(new InMemorySettlementAnchorStore()),
371
+ atomic: async (fn) => {
372
+ const run = tail.then(() => fn(stores));
373
+ tail = run.catch(() => { });
374
+ return run;
375
+ },
376
+ };
377
+ return stores;
378
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentbadge/database",
3
- "version": "0.6.15",
3
+ "version": "0.6.17",
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",