@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,465 @@
1
+ import { isUniqueViolation } from "./commerce-repository.js";
2
+ import { decimalFromDb } from "./finops-repository.js";
3
+ const dec = decimalFromDb;
4
+ const numOrNull = (v) => v === null || v === undefined ? null : Number(v);
5
+ const toPlanRow = (r) => ({
6
+ id: r.id,
7
+ agreementRef: r.agreementRef,
8
+ buyerWallet: r.buyerWallet,
9
+ providerWallet: r.providerWallet,
10
+ currency: r.currency,
11
+ totalUsd: dec(r.totalUsd),
12
+ status: r.status,
13
+ payload: r.payload,
14
+ createdAt: r.createdAt,
15
+ updatedAt: r.updatedAt,
16
+ });
17
+ const toMilestoneRow = (r) => ({
18
+ id: r.id,
19
+ planId: r.planId,
20
+ milestoneId: r.milestoneId,
21
+ seq: r.seq,
22
+ title: r.title,
23
+ amountUsd: dec(r.amountUsd),
24
+ acceptanceCriteria: r.acceptanceCriteria,
25
+ dueAtMs: numOrNull(r.dueAtMs),
26
+ releasePolicy: r.releasePolicy,
27
+ status: r.status,
28
+ submissionRef: r.submissionRef,
29
+ escrowRef: r.escrowRef,
30
+ payload: r.payload,
31
+ createdAt: r.createdAt,
32
+ updatedAt: r.updatedAt,
33
+ });
34
+ const toIntentRow = (r) => ({
35
+ id: r.id,
36
+ milestoneRef: r.milestoneRef,
37
+ executor: r.executor,
38
+ status: r.status,
39
+ createdAtMs: Number(r.createdAtMs),
40
+ completedAtMs: numOrNull(r.completedAtMs),
41
+ completedBy: r.completedBy,
42
+ payload: r.payload,
43
+ createdAt: r.createdAt,
44
+ updatedAt: r.updatedAt,
45
+ });
46
+ const toLegRow = (r) => ({
47
+ id: r.id,
48
+ intentId: r.intentId,
49
+ payee: r.payee,
50
+ amountUsdc: dec(r.amountUsdc),
51
+ role: r.role,
52
+ status: r.status,
53
+ txHash: r.txHash,
54
+ network: r.network,
55
+ completedAtMs: numOrNull(r.completedAtMs),
56
+ completedBy: r.completedBy,
57
+ payload: r.payload,
58
+ createdAt: r.createdAt,
59
+ updatedAt: r.updatedAt,
60
+ });
61
+ const toDisputeRow = (r) => ({
62
+ id: r.id,
63
+ subjectType: r.subjectType,
64
+ subjectRef: r.subjectRef,
65
+ raisedBy: r.raisedBy,
66
+ reason: r.reason,
67
+ evidenceRefs: r.evidenceRefs,
68
+ status: r.status,
69
+ resolution: r.resolution,
70
+ splitAmounts: r.splitAmounts,
71
+ resolvedBy: r.resolvedBy,
72
+ resolverRef: r.resolverRef,
73
+ createdAtMs: Number(r.createdAtMs),
74
+ resolvedAtMs: numOrNull(r.resolvedAtMs),
75
+ payload: r.payload,
76
+ createdAt: r.createdAt,
77
+ updatedAt: r.updatedAt,
78
+ });
79
+ const toAnchorRow = (r) => ({
80
+ id: r.id,
81
+ receiptRef: r.receiptRef,
82
+ receiptHash: r.receiptHash,
83
+ chainId: r.chainId,
84
+ txHash: r.txHash,
85
+ status: r.status,
86
+ attempts: r.attempts,
87
+ lastError: r.lastError,
88
+ anchoredAtMs: numOrNull(r.anchoredAtMs),
89
+ payload: r.payload,
90
+ createdAt: r.createdAt,
91
+ updatedAt: r.updatedAt,
92
+ });
93
+ export class MilestonePlanRepository {
94
+ db;
95
+ constructor(db) {
96
+ this.db = db;
97
+ }
98
+ get orm() {
99
+ return this.db.orm.public.MilestonePlan;
100
+ }
101
+ async create(p) {
102
+ const row = await catchUnique(this.orm.create({
103
+ id: p.id,
104
+ agreementRef: p.agreementRef,
105
+ buyerWallet: p.buyerWallet,
106
+ providerWallet: p.providerWallet,
107
+ currency: p.currency ?? "USDC",
108
+ totalUsd: p.totalUsd,
109
+ status: p.status,
110
+ payload: p.payload,
111
+ }));
112
+ return row ? toPlanRow(row) : null;
113
+ }
114
+ async getById(id) {
115
+ const r = await this.orm.where({ id }).first();
116
+ return r ? toPlanRow(r) : null;
117
+ }
118
+ async getByAgreement(agreementRef) {
119
+ const r = await this.orm.where({ agreementRef }).first();
120
+ return r ? toPlanRow(r) : null;
121
+ }
122
+ async list(opts = {}) {
123
+ const eq = {};
124
+ if (opts.status !== undefined)
125
+ eq["status"] = opts.status;
126
+ if (opts.buyerWallet !== undefined)
127
+ eq["buyerWallet"] = opts.buyerWallet;
128
+ if (opts.providerWallet !== undefined)
129
+ eq["providerWallet"] = opts.providerWallet;
130
+ const rows = await this.orm
131
+ .orderBy((t) => t.createdAt.desc())
132
+ .where(eq)
133
+ .all();
134
+ const out = rows.map(toPlanRow);
135
+ return opts.limit === undefined ? out : out.slice(0, opts.limit);
136
+ }
137
+ async applyStatus(id, expectedFrom, to) {
138
+ const row = await this.orm
139
+ .where({ id, status: expectedFrom })
140
+ .update({ status: to });
141
+ return row ? toPlanRow(row) : null;
142
+ }
143
+ }
144
+ export class MilestoneRepository {
145
+ db;
146
+ constructor(db) {
147
+ this.db = db;
148
+ }
149
+ get orm() {
150
+ return this.db.orm.public.Milestone;
151
+ }
152
+ async create(m) {
153
+ const row = await catchUnique(this.orm.create({
154
+ id: m.id,
155
+ planId: m.planId,
156
+ milestoneId: m.milestoneId,
157
+ seq: m.seq,
158
+ title: m.title,
159
+ amountUsd: m.amountUsd,
160
+ acceptanceCriteria: m.acceptanceCriteria ?? null,
161
+ dueAtMs: m.dueAtMs === undefined || m.dueAtMs === null
162
+ ? null
163
+ : BigInt(m.dueAtMs),
164
+ releasePolicy: m.releasePolicy,
165
+ status: m.status,
166
+ submissionRef: m.submissionRef ?? null,
167
+ escrowRef: m.escrowRef ?? null,
168
+ payload: m.payload,
169
+ }));
170
+ return row ? toMilestoneRow(row) : null;
171
+ }
172
+ async getById(id) {
173
+ const r = await this.orm.where({ id }).first();
174
+ return r ? toMilestoneRow(r) : null;
175
+ }
176
+ async getByPlanMilestone(planId, milestoneId) {
177
+ const r = await this.orm.where({ planId, milestoneId }).first();
178
+ return r ? toMilestoneRow(r) : null;
179
+ }
180
+ async listByPlan(planId) {
181
+ const rows = await this.orm
182
+ .orderBy((t) => t.seq.asc())
183
+ .where({ planId })
184
+ .all();
185
+ return rows.map(toMilestoneRow);
186
+ }
187
+ async applyStatus(id, expectedFrom, to, patch) {
188
+ const row = await this.orm
189
+ .where({ id, status: expectedFrom })
190
+ .update({
191
+ status: to,
192
+ ...(patch?.submissionRef !== undefined
193
+ ? { submissionRef: patch.submissionRef }
194
+ : {}),
195
+ ...(patch?.escrowRef !== undefined
196
+ ? { escrowRef: patch.escrowRef }
197
+ : {}),
198
+ });
199
+ return row ? toMilestoneRow(row) : null;
200
+ }
201
+ }
202
+ export class PayoutIntentRepository {
203
+ db;
204
+ constructor(db) {
205
+ this.db = db;
206
+ }
207
+ get orm() {
208
+ return this.db.orm.public.PayoutIntent;
209
+ }
210
+ async create(i) {
211
+ const row = await catchUnique(this.orm.create({
212
+ id: i.id,
213
+ milestoneRef: i.milestoneRef,
214
+ executor: i.executor ?? "MANUAL",
215
+ status: i.status,
216
+ createdAtMs: BigInt(i.createdAtMs),
217
+ payload: i.payload,
218
+ }));
219
+ return row ? toIntentRow(row) : null;
220
+ }
221
+ async getById(id) {
222
+ const r = await this.orm.where({ id }).first();
223
+ return r ? toIntentRow(r) : null;
224
+ }
225
+ async getByMilestone(milestoneRef) {
226
+ const r = await this.orm.where({ milestoneRef }).first();
227
+ return r ? toIntentRow(r) : null;
228
+ }
229
+ async list(opts = {}) {
230
+ const eq = {};
231
+ if (opts.status !== undefined)
232
+ eq["status"] = opts.status;
233
+ const rows = await this.orm
234
+ .orderBy((t) => t.createdAtMs.desc())
235
+ .where(eq)
236
+ .all();
237
+ const out = rows.map(toIntentRow);
238
+ return opts.limit === undefined ? out : out.slice(0, opts.limit);
239
+ }
240
+ async applyStatus(id, expectedFrom, to, patch) {
241
+ const row = await this.orm
242
+ .where({ id, status: expectedFrom })
243
+ .update({
244
+ status: to,
245
+ ...(patch?.completedAtMs !== undefined
246
+ ? {
247
+ completedAtMs: BigInt(patch.completedAtMs),
248
+ }
249
+ : {}),
250
+ ...(patch?.completedBy !== undefined
251
+ ? { completedBy: patch.completedBy }
252
+ : {}),
253
+ });
254
+ return row ? toIntentRow(row) : null;
255
+ }
256
+ }
257
+ export class PayoutLegRepository {
258
+ db;
259
+ constructor(db) {
260
+ this.db = db;
261
+ }
262
+ get orm() {
263
+ return this.db.orm.public.PayoutLeg;
264
+ }
265
+ async create(l) {
266
+ const row = await catchUnique(this.orm.create({
267
+ id: l.id,
268
+ intentId: l.intentId,
269
+ payee: l.payee,
270
+ amountUsdc: l.amountUsdc,
271
+ role: l.role,
272
+ status: l.status,
273
+ network: l.network ?? "arc",
274
+ payload: l.payload,
275
+ }));
276
+ return row ? toLegRow(row) : null;
277
+ }
278
+ async getById(id) {
279
+ const r = await this.orm.where({ id }).first();
280
+ return r ? toLegRow(r) : null;
281
+ }
282
+ async getByTxHash(network, txHash) {
283
+ const r = await this.orm.where({ network, txHash }).first();
284
+ return r ? toLegRow(r) : null;
285
+ }
286
+ async listByIntent(intentId) {
287
+ const rows = await this.orm.where({ intentId }).all();
288
+ return rows.map(toLegRow);
289
+ }
290
+ async complete(id, expectedFrom, txHash, completedBy, completedAtMs) {
291
+ let row;
292
+ try {
293
+ row = await this.orm
294
+ .where({ id, status: expectedFrom })
295
+ .where((t) => t.txHash.isNull())
296
+ .update({
297
+ status: "COMPLETED",
298
+ txHash,
299
+ completedBy,
300
+ completedAtMs: BigInt(completedAtMs),
301
+ });
302
+ }
303
+ catch (e) {
304
+ if (isUniqueViolation(e))
305
+ return null;
306
+ throw e;
307
+ }
308
+ return row ? toLegRow(row) : null;
309
+ }
310
+ async applyStatus(id, expectedFrom, to) {
311
+ const row = await this.orm
312
+ .where({ id, status: expectedFrom })
313
+ .update({ status: to });
314
+ return row ? toLegRow(row) : null;
315
+ }
316
+ }
317
+ export class DisputeRepository {
318
+ db;
319
+ constructor(db) {
320
+ this.db = db;
321
+ }
322
+ get orm() {
323
+ return this.db.orm.public.Dispute;
324
+ }
325
+ async create(d) {
326
+ const row = await catchUnique(this.orm.create({
327
+ id: d.id,
328
+ subjectType: d.subjectType,
329
+ subjectRef: d.subjectRef,
330
+ raisedBy: d.raisedBy,
331
+ reason: d.reason,
332
+ evidenceRefs: d.evidenceRefs,
333
+ status: d.status,
334
+ createdAtMs: BigInt(d.createdAtMs),
335
+ payload: d.payload,
336
+ }));
337
+ return row ? toDisputeRow(row) : null;
338
+ }
339
+ async getById(id) {
340
+ const r = await this.orm.where({ id }).first();
341
+ return r ? toDisputeRow(r) : null;
342
+ }
343
+ async listBySubject(subjectType, subjectRef) {
344
+ const rows = await this.orm
345
+ .orderBy((t) => t.createdAtMs.asc())
346
+ .where({ subjectType, subjectRef })
347
+ .all();
348
+ return rows.map(toDisputeRow);
349
+ }
350
+ async list(opts = {}) {
351
+ const eq = {};
352
+ if (opts.status !== undefined)
353
+ eq["status"] = opts.status;
354
+ const rows = await this.orm
355
+ .orderBy((t) => t.createdAtMs.desc())
356
+ .where(eq)
357
+ .all();
358
+ const out = rows.map(toDisputeRow);
359
+ return opts.limit === undefined ? out : out.slice(0, opts.limit);
360
+ }
361
+ async resolve(id, resolution, resolvedBy, resolvedAtMs, extra) {
362
+ const row = await this.orm.where({ id, status: "OPEN" }).update({
363
+ status: "RESOLVED",
364
+ resolution,
365
+ resolvedBy,
366
+ resolvedAtMs: BigInt(resolvedAtMs),
367
+ splitAmounts: (extra?.splitAmounts ?? null),
368
+ resolverRef: extra?.resolverRef ?? null,
369
+ });
370
+ return row ? toDisputeRow(row) : null;
371
+ }
372
+ }
373
+ export class SettlementAnchorRepository {
374
+ db;
375
+ constructor(db) {
376
+ this.db = db;
377
+ }
378
+ get orm() {
379
+ return this.db.orm.public.SettlementAnchor;
380
+ }
381
+ async create(a) {
382
+ const row = await catchUnique(this.orm.create({
383
+ id: a.id,
384
+ receiptRef: a.receiptRef,
385
+ receiptHash: a.receiptHash,
386
+ chainId: a.chainId,
387
+ status: a.status,
388
+ payload: a.payload,
389
+ }));
390
+ return row ? toAnchorRow(row) : null;
391
+ }
392
+ async getById(id) {
393
+ const r = await this.orm.where({ id }).first();
394
+ return r ? toAnchorRow(r) : null;
395
+ }
396
+ async getByReceiptRef(receiptRef) {
397
+ const r = await this.orm.where({ receiptRef }).first();
398
+ return r ? toAnchorRow(r) : null;
399
+ }
400
+ async list(opts = {}) {
401
+ const rows = await this.orm.orderBy((t) => t.createdAt.asc()).all();
402
+ const set = opts.statuses !== undefined
403
+ ? new Set(opts.statuses)
404
+ : opts.status !== undefined
405
+ ? new Set([opts.status])
406
+ : undefined;
407
+ const out = rows
408
+ .map(toAnchorRow)
409
+ .filter((r) => set === undefined || set.has(r.status));
410
+ return opts.limit === undefined ? out : out.slice(0, opts.limit);
411
+ }
412
+ async markAnchored(id, txHash, anchoredAtMs) {
413
+ const row = await this.orm.where({ id, status: "PENDING" }).update({
414
+ status: "ANCHORED",
415
+ txHash,
416
+ anchoredAtMs: BigInt(anchoredAtMs),
417
+ });
418
+ return row ? toAnchorRow(row) : null;
419
+ }
420
+ async markAttempt(id, lastError, failed) {
421
+ const existing = await this.orm.where({ id }).first();
422
+ if (!existing || existing.status === "ANCHORED")
423
+ return null;
424
+ const row = await this.orm.where({ id }).update({
425
+ attempts: existing.attempts + 1,
426
+ lastError,
427
+ status: failed ? "FAILED" : "PENDING",
428
+ });
429
+ return row ? toAnchorRow(row) : null;
430
+ }
431
+ }
432
+ async function catchUnique(p) {
433
+ try {
434
+ return await p;
435
+ }
436
+ catch (err) {
437
+ if (isUniqueViolation(err))
438
+ return null;
439
+ throw err;
440
+ }
441
+ }
442
+ export function createPrismaSettlementStores(db) {
443
+ const bundle = (client) => ({
444
+ plans: new MilestonePlanRepository(client),
445
+ milestones: new MilestoneRepository(client),
446
+ payoutIntents: new PayoutIntentRepository(client),
447
+ payoutLegs: new PayoutLegRepository(client),
448
+ disputes: new DisputeRepository(client),
449
+ anchors: new SettlementAnchorRepository(client),
450
+ atomic: outerAtomic,
451
+ });
452
+ async function outerAtomic(fn) {
453
+ for (let attempt = 0;; attempt++) {
454
+ try {
455
+ return await db.transaction((tx) => fn(bundle(tx)));
456
+ }
457
+ catch (err) {
458
+ if (attempt === 0 && isUniqueViolation(err))
459
+ continue;
460
+ throw err;
461
+ }
462
+ }
463
+ }
464
+ return bundle(db);
465
+ }
@@ -0,0 +1,249 @@
1
+ import type { JsonValue } from "@prisma/orm-postgres/target/codec-types";
2
+ export interface MilestonePlanInsert {
3
+ id: string;
4
+ agreementRef: string;
5
+ buyerWallet: string;
6
+ providerWallet: string;
7
+ currency?: string;
8
+ totalUsd: string;
9
+ status: string;
10
+ payload: JsonValue;
11
+ }
12
+ export interface MilestonePlanRow extends MilestonePlanInsert {
13
+ createdAt: string;
14
+ updatedAt: string;
15
+ }
16
+ export interface MilestonePlanStore {
17
+ create(p: MilestonePlanInsert): Promise<MilestonePlanRow | null>;
18
+ getById(id: string): Promise<MilestonePlanRow | null>;
19
+ getByAgreement(agreementRef: string): Promise<MilestonePlanRow | null>;
20
+ list(opts?: {
21
+ status?: string;
22
+ buyerWallet?: string;
23
+ providerWallet?: string;
24
+ limit?: number;
25
+ }): Promise<MilestonePlanRow[]>;
26
+ applyStatus(id: string, expectedFrom: string, to: string): Promise<MilestonePlanRow | null>;
27
+ }
28
+ export interface MilestoneInsert {
29
+ id: string;
30
+ planId: string;
31
+ milestoneId: string;
32
+ seq: number;
33
+ title: string;
34
+ amountUsd: string;
35
+ acceptanceCriteria?: string | null;
36
+ dueAtMs?: number | null;
37
+ releasePolicy: string;
38
+ status: string;
39
+ submissionRef?: string | null;
40
+ escrowRef?: string | null;
41
+ payload: JsonValue;
42
+ }
43
+ export interface MilestoneRow extends MilestoneInsert {
44
+ createdAt: string;
45
+ updatedAt: string;
46
+ }
47
+ export interface MilestoneStatusPatch {
48
+ submissionRef?: string | null;
49
+ escrowRef?: string | null;
50
+ }
51
+ export interface MilestoneStore {
52
+ create(m: MilestoneInsert): Promise<MilestoneRow | null>;
53
+ getById(id: string): Promise<MilestoneRow | null>;
54
+ getByPlanMilestone(planId: string, milestoneId: string): Promise<MilestoneRow | null>;
55
+ listByPlan(planId: string): Promise<MilestoneRow[]>;
56
+ applyStatus(id: string, expectedFrom: string, to: string, patch?: MilestoneStatusPatch): Promise<MilestoneRow | null>;
57
+ }
58
+ export interface PayoutIntentInsert {
59
+ id: string;
60
+ milestoneRef: string;
61
+ executor?: string;
62
+ status: string;
63
+ createdAtMs: number;
64
+ payload: JsonValue;
65
+ }
66
+ export interface PayoutIntentRow extends PayoutIntentInsert {
67
+ completedAtMs?: number | null;
68
+ completedBy?: string | null;
69
+ createdAt: string;
70
+ updatedAt: string;
71
+ }
72
+ export interface PayoutIntentStatusPatch {
73
+ completedAtMs?: number;
74
+ completedBy?: string;
75
+ }
76
+ export interface PayoutIntentStore {
77
+ create(i: PayoutIntentInsert): Promise<PayoutIntentRow | null>;
78
+ getById(id: string): Promise<PayoutIntentRow | null>;
79
+ getByMilestone(milestoneRef: string): Promise<PayoutIntentRow | null>;
80
+ list(opts?: {
81
+ status?: string;
82
+ limit?: number;
83
+ }): Promise<PayoutIntentRow[]>;
84
+ applyStatus(id: string, expectedFrom: string, to: string, patch?: PayoutIntentStatusPatch): Promise<PayoutIntentRow | null>;
85
+ }
86
+ export interface PayoutLegInsert {
87
+ id: string;
88
+ intentId: string;
89
+ payee: string;
90
+ amountUsdc: string;
91
+ role: string;
92
+ status: string;
93
+ network?: string;
94
+ payload: JsonValue;
95
+ }
96
+ export interface PayoutLegRow extends PayoutLegInsert {
97
+ txHash?: string | null;
98
+ completedAtMs?: number | null;
99
+ completedBy?: string | null;
100
+ createdAt: string;
101
+ updatedAt: string;
102
+ }
103
+ export interface PayoutLegStore {
104
+ create(l: PayoutLegInsert): Promise<PayoutLegRow | null>;
105
+ getById(id: string): Promise<PayoutLegRow | null>;
106
+ getByTxHash(network: string, txHash: string): Promise<PayoutLegRow | null>;
107
+ listByIntent(intentId: string): Promise<PayoutLegRow[]>;
108
+ complete(id: string, expectedFrom: string, txHash: string, completedBy: string, completedAtMs: number): Promise<PayoutLegRow | null>;
109
+ applyStatus(id: string, expectedFrom: string, to: string): Promise<PayoutLegRow | null>;
110
+ }
111
+ export interface DisputeInsert {
112
+ id: string;
113
+ subjectType: string;
114
+ subjectRef: string;
115
+ raisedBy: string;
116
+ reason: string;
117
+ evidenceRefs: JsonValue;
118
+ status: string;
119
+ createdAtMs: number;
120
+ payload: JsonValue;
121
+ }
122
+ export interface DisputeRow extends DisputeInsert {
123
+ resolution?: string | null;
124
+ splitAmounts?: JsonValue | null;
125
+ resolvedBy?: string | null;
126
+ resolverRef?: string | null;
127
+ resolvedAtMs?: number | null;
128
+ createdAt: string;
129
+ updatedAt: string;
130
+ }
131
+ export interface DisputeStore {
132
+ create(d: DisputeInsert): Promise<DisputeRow | null>;
133
+ getById(id: string): Promise<DisputeRow | null>;
134
+ listBySubject(subjectType: string, subjectRef: string): Promise<DisputeRow[]>;
135
+ list(opts?: {
136
+ status?: string;
137
+ limit?: number;
138
+ }): Promise<DisputeRow[]>;
139
+ resolve(id: string, resolution: string, resolvedBy: string, resolvedAtMs: number, extra?: {
140
+ splitAmounts?: JsonValue;
141
+ resolverRef?: string | null;
142
+ }): Promise<DisputeRow | null>;
143
+ }
144
+ export interface SettlementAnchorInsert {
145
+ id: string;
146
+ receiptRef: string;
147
+ receiptHash: string;
148
+ chainId: string;
149
+ status: string;
150
+ payload: JsonValue;
151
+ }
152
+ export interface SettlementAnchorRow extends SettlementAnchorInsert {
153
+ txHash?: string | null;
154
+ attempts: number;
155
+ lastError?: string | null;
156
+ anchoredAtMs?: number | null;
157
+ createdAt: string;
158
+ updatedAt: string;
159
+ }
160
+ export interface SettlementAnchorStore {
161
+ create(a: SettlementAnchorInsert): Promise<SettlementAnchorRow | null>;
162
+ getById(id: string): Promise<SettlementAnchorRow | null>;
163
+ getByReceiptRef(receiptRef: string): Promise<SettlementAnchorRow | null>;
164
+ list(opts?: {
165
+ status?: string;
166
+ statuses?: readonly string[];
167
+ limit?: number;
168
+ }): Promise<SettlementAnchorRow[]>;
169
+ markAnchored(id: string, txHash: string, anchoredAtMs: number): Promise<SettlementAnchorRow | null>;
170
+ markAttempt(id: string, lastError: string | null, failed: boolean): Promise<SettlementAnchorRow | null>;
171
+ }
172
+ export interface SettlementStores {
173
+ plans: MilestonePlanStore;
174
+ milestones: MilestoneStore;
175
+ payoutIntents: PayoutIntentStore;
176
+ payoutLegs: PayoutLegStore;
177
+ disputes: DisputeStore;
178
+ anchors: SettlementAnchorStore;
179
+ atomic<R>(fn: (stores: SettlementStores) => Promise<R>): Promise<R>;
180
+ }
181
+ export declare class InMemoryMilestonePlanStore implements MilestonePlanStore {
182
+ private readonly rows;
183
+ create(p: MilestonePlanInsert): Promise<MilestonePlanRow | null>;
184
+ getById(id: string): Promise<MilestonePlanRow | null>;
185
+ getByAgreement(agreementRef: string): Promise<MilestonePlanRow | null>;
186
+ list(opts?: {
187
+ status?: string;
188
+ buyerWallet?: string;
189
+ providerWallet?: string;
190
+ limit?: number;
191
+ }): Promise<MilestonePlanRow[]>;
192
+ applyStatus(id: string, expectedFrom: string, to: string): Promise<MilestonePlanRow | null>;
193
+ }
194
+ export declare class InMemoryMilestoneStore implements MilestoneStore {
195
+ private readonly rows;
196
+ create(m: MilestoneInsert): Promise<MilestoneRow | null>;
197
+ getById(id: string): Promise<MilestoneRow | null>;
198
+ getByPlanMilestone(planId: string, milestoneId: string): Promise<MilestoneRow | null>;
199
+ listByPlan(planId: string): Promise<MilestoneRow[]>;
200
+ applyStatus(id: string, expectedFrom: string, to: string, patch?: MilestoneStatusPatch): Promise<MilestoneRow | null>;
201
+ }
202
+ export declare class InMemoryPayoutIntentStore implements PayoutIntentStore {
203
+ private readonly rows;
204
+ create(i: PayoutIntentInsert): Promise<PayoutIntentRow | null>;
205
+ getById(id: string): Promise<PayoutIntentRow | null>;
206
+ getByMilestone(milestoneRef: string): Promise<PayoutIntentRow | null>;
207
+ list(opts?: {
208
+ status?: string;
209
+ limit?: number;
210
+ }): Promise<PayoutIntentRow[]>;
211
+ applyStatus(id: string, expectedFrom: string, to: string, patch?: PayoutIntentStatusPatch): Promise<PayoutIntentRow | null>;
212
+ }
213
+ export declare class InMemoryPayoutLegStore implements PayoutLegStore {
214
+ private readonly rows;
215
+ create(l: PayoutLegInsert): Promise<PayoutLegRow | null>;
216
+ getById(id: string): Promise<PayoutLegRow | null>;
217
+ getByTxHash(network: string, txHash: string): Promise<PayoutLegRow | null>;
218
+ listByIntent(intentId: string): Promise<PayoutLegRow[]>;
219
+ complete(id: string, expectedFrom: string, txHash: string, completedBy: string, completedAtMs: number): Promise<PayoutLegRow | null>;
220
+ applyStatus(id: string, expectedFrom: string, to: string): Promise<PayoutLegRow | null>;
221
+ }
222
+ export declare class InMemoryDisputeStore implements DisputeStore {
223
+ private readonly rows;
224
+ create(d: DisputeInsert): Promise<DisputeRow | null>;
225
+ getById(id: string): Promise<DisputeRow | null>;
226
+ listBySubject(subjectType: string, subjectRef: string): Promise<DisputeRow[]>;
227
+ list(opts?: {
228
+ status?: string;
229
+ limit?: number;
230
+ }): Promise<DisputeRow[]>;
231
+ resolve(id: string, resolution: string, resolvedBy: string, resolvedAtMs: number, extra?: {
232
+ splitAmounts?: JsonValue;
233
+ resolverRef?: string | null;
234
+ }): Promise<DisputeRow | null>;
235
+ }
236
+ export declare class InMemorySettlementAnchorStore implements SettlementAnchorStore {
237
+ private readonly rows;
238
+ create(a: SettlementAnchorInsert): Promise<SettlementAnchorRow | null>;
239
+ getById(id: string): Promise<SettlementAnchorRow | null>;
240
+ getByReceiptRef(receiptRef: string): Promise<SettlementAnchorRow | null>;
241
+ list(opts?: {
242
+ status?: string;
243
+ statuses?: readonly string[];
244
+ limit?: number;
245
+ }): Promise<SettlementAnchorRow[]>;
246
+ markAnchored(id: string, txHash: string, anchoredAtMs: number): Promise<SettlementAnchorRow | null>;
247
+ markAttempt(id: string, lastError: string | null, failed: boolean): Promise<SettlementAnchorRow | null>;
248
+ }
249
+ export declare function createInMemorySettlementStores(): SettlementStores;