@agentbadge/database 0.6.16 → 0.6.18
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/dist/database.d.ts +2 -0
- package/dist/database.js +4 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/prisma/contract.json +1800 -164
- package/dist/repositories/agent-request-repository.d.ts +1 -0
- package/dist/repositories/agent-request-repository.js +5 -0
- package/dist/repositories/settlement-repository.d.ts +84 -0
- package/dist/repositories/settlement-repository.js +465 -0
- package/dist/repositories/settlement-store.d.ts +249 -0
- package/dist/repositories/settlement-store.js +378 -0
- package/package.json +1 -1
|
@@ -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;
|
|
@@ -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