@agentbadge/database 0.6.16 → 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.
- 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/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,84 @@
|
|
|
1
|
+
import type { JsonValue } from "@prisma/orm-postgres/target/codec-types";
|
|
2
|
+
import type { PrismaDb } from "./event-repository.js";
|
|
3
|
+
import type { DisputeInsert, DisputeRow, DisputeStore, MilestoneInsert, MilestonePlanInsert, MilestonePlanRow, MilestonePlanStore, MilestoneRow, MilestoneStatusPatch, MilestoneStore, PayoutIntentInsert, PayoutIntentRow, PayoutIntentStatusPatch, PayoutIntentStore, PayoutLegInsert, PayoutLegRow, PayoutLegStore, SettlementAnchorInsert, SettlementAnchorRow, SettlementAnchorStore, SettlementStores } from "./settlement-store.js";
|
|
4
|
+
export declare class MilestonePlanRepository implements MilestonePlanStore {
|
|
5
|
+
private readonly db;
|
|
6
|
+
constructor(db: PrismaDb);
|
|
7
|
+
private get orm();
|
|
8
|
+
create(p: MilestonePlanInsert): Promise<MilestonePlanRow | null>;
|
|
9
|
+
getById(id: string): Promise<MilestonePlanRow | null>;
|
|
10
|
+
getByAgreement(agreementRef: string): Promise<MilestonePlanRow | null>;
|
|
11
|
+
list(opts?: {
|
|
12
|
+
status?: string;
|
|
13
|
+
buyerWallet?: string;
|
|
14
|
+
providerWallet?: string;
|
|
15
|
+
limit?: number;
|
|
16
|
+
}): Promise<MilestonePlanRow[]>;
|
|
17
|
+
applyStatus(id: string, expectedFrom: string, to: string): Promise<MilestonePlanRow | null>;
|
|
18
|
+
}
|
|
19
|
+
export declare class MilestoneRepository implements MilestoneStore {
|
|
20
|
+
private readonly db;
|
|
21
|
+
constructor(db: PrismaDb);
|
|
22
|
+
private get orm();
|
|
23
|
+
create(m: MilestoneInsert): Promise<MilestoneRow | null>;
|
|
24
|
+
getById(id: string): Promise<MilestoneRow | null>;
|
|
25
|
+
getByPlanMilestone(planId: string, milestoneId: string): Promise<MilestoneRow | null>;
|
|
26
|
+
listByPlan(planId: string): Promise<MilestoneRow[]>;
|
|
27
|
+
applyStatus(id: string, expectedFrom: string, to: string, patch?: MilestoneStatusPatch): Promise<MilestoneRow | null>;
|
|
28
|
+
}
|
|
29
|
+
export declare class PayoutIntentRepository implements PayoutIntentStore {
|
|
30
|
+
private readonly db;
|
|
31
|
+
constructor(db: PrismaDb);
|
|
32
|
+
private get orm();
|
|
33
|
+
create(i: PayoutIntentInsert): Promise<PayoutIntentRow | null>;
|
|
34
|
+
getById(id: string): Promise<PayoutIntentRow | null>;
|
|
35
|
+
getByMilestone(milestoneRef: string): Promise<PayoutIntentRow | null>;
|
|
36
|
+
list(opts?: {
|
|
37
|
+
status?: string;
|
|
38
|
+
limit?: number;
|
|
39
|
+
}): Promise<PayoutIntentRow[]>;
|
|
40
|
+
applyStatus(id: string, expectedFrom: string, to: string, patch?: PayoutIntentStatusPatch): Promise<PayoutIntentRow | null>;
|
|
41
|
+
}
|
|
42
|
+
export declare class PayoutLegRepository implements PayoutLegStore {
|
|
43
|
+
private readonly db;
|
|
44
|
+
constructor(db: PrismaDb);
|
|
45
|
+
private get orm();
|
|
46
|
+
create(l: PayoutLegInsert): Promise<PayoutLegRow | null>;
|
|
47
|
+
getById(id: string): Promise<PayoutLegRow | null>;
|
|
48
|
+
getByTxHash(network: string, txHash: string): Promise<PayoutLegRow | null>;
|
|
49
|
+
listByIntent(intentId: string): Promise<PayoutLegRow[]>;
|
|
50
|
+
complete(id: string, expectedFrom: string, txHash: string, completedBy: string, completedAtMs: number): Promise<PayoutLegRow | null>;
|
|
51
|
+
applyStatus(id: string, expectedFrom: string, to: string): Promise<PayoutLegRow | null>;
|
|
52
|
+
}
|
|
53
|
+
export declare class DisputeRepository implements DisputeStore {
|
|
54
|
+
private readonly db;
|
|
55
|
+
constructor(db: PrismaDb);
|
|
56
|
+
private get orm();
|
|
57
|
+
create(d: DisputeInsert): Promise<DisputeRow | null>;
|
|
58
|
+
getById(id: string): Promise<DisputeRow | null>;
|
|
59
|
+
listBySubject(subjectType: string, subjectRef: string): Promise<DisputeRow[]>;
|
|
60
|
+
list(opts?: {
|
|
61
|
+
status?: string;
|
|
62
|
+
limit?: number;
|
|
63
|
+
}): Promise<DisputeRow[]>;
|
|
64
|
+
resolve(id: string, resolution: string, resolvedBy: string, resolvedAtMs: number, extra?: {
|
|
65
|
+
splitAmounts?: JsonValue;
|
|
66
|
+
resolverRef?: string | null;
|
|
67
|
+
}): Promise<DisputeRow | null>;
|
|
68
|
+
}
|
|
69
|
+
export declare class SettlementAnchorRepository implements SettlementAnchorStore {
|
|
70
|
+
private readonly db;
|
|
71
|
+
constructor(db: PrismaDb);
|
|
72
|
+
private get orm();
|
|
73
|
+
create(a: SettlementAnchorInsert): Promise<SettlementAnchorRow | null>;
|
|
74
|
+
getById(id: string): Promise<SettlementAnchorRow | null>;
|
|
75
|
+
getByReceiptRef(receiptRef: string): Promise<SettlementAnchorRow | null>;
|
|
76
|
+
list(opts?: {
|
|
77
|
+
status?: string;
|
|
78
|
+
statuses?: readonly string[];
|
|
79
|
+
limit?: number;
|
|
80
|
+
}): Promise<SettlementAnchorRow[]>;
|
|
81
|
+
markAnchored(id: string, txHash: string, anchoredAtMs: number): Promise<SettlementAnchorRow | null>;
|
|
82
|
+
markAttempt(id: string, lastError: string | null, failed: boolean): Promise<SettlementAnchorRow | null>;
|
|
83
|
+
}
|
|
84
|
+
export declare function createPrismaSettlementStores(db: PrismaDb): SettlementStores;
|
|
@@ -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
|
+
}
|