@dvmkit/dvm-cli 0.4.1 → 0.5.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.
- package/README.md +8 -0
- package/dist/{chunk-24VB7FR7.js → chunk-3YE43JJR.js} +4 -4
- package/dist/{chunk-SKKZ4OAA.js → chunk-4KINCBHL.js} +53 -56
- package/dist/{chunk-XN4YSEO6.js → chunk-EE2MREOM.js} +152 -93
- package/dist/chunk-ERJFRCJO.js +150 -0
- package/dist/chunk-FL2ICZW5.js +383 -0
- package/dist/chunk-FSYEZERJ.js +1667 -0
- package/dist/{chunk-V2BE4E5V.js → chunk-ID5G54HJ.js} +223 -73
- package/dist/{chunk-4NWEJTAW.js → chunk-MJ4X4BEW.js} +7953 -8036
- package/dist/{chunk-Z75N4GTG.js → chunk-QICJRKB7.js} +23557 -16012
- package/dist/{chunk-ECEM3ZAS.js → chunk-R55XNXAR.js} +53 -185
- package/dist/{chunk-R4MSO2GX.js → chunk-SHZMNEXE.js} +65 -24
- package/dist/chunk-TZWZTS37.js +1517 -0
- package/dist/{chunk-VY277ROG.js → chunk-VOO5IAK7.js} +600 -97
- package/dist/{chunk-2VVVWBQA.js → chunk-WTM2EICO.js} +2 -352
- package/dist/chunk-XHOFQ3OS.js +717 -0
- package/dist/{chunk-YLWATJ7F.js → chunk-Z4CZUMUT.js} +937 -1599
- package/dist/{client-OWIOFBKS.js → client-RYDA62DU.js} +11 -8
- package/dist/{dist-2G3QBGID.js → dist-GKCEIY2A.js} +7 -4
- package/dist/dvm.js +478 -70
- package/dist/{internal-OBEYRHMV.js → internal-INUTGTXQ.js} +56 -33
- package/dist/{payout-reporter-RG6XNGPI-KNY74HLM.js → payout-reporter-3Y2U55ZX-CMKACKA3.js} +2 -2
- package/dist/{pocket-refill-NXTJIVB5.js → pocket-refill-K5T4LZF6.js} +18 -14
- package/dist/{revenue-reporter-ASZ7SHHH-ISAMJDZQ.js → revenue-reporter-MAERPYGK-CCPZXGFA.js} +2 -2
- package/dist/tempo-AXTE4ZEA.js +34 -0
- package/dist/{tempo-lifecycle-DFIXQ54Q-LNXPKWK5.js → tempo-lifecycle-ZBBLFBMH-HWI4PLYP.js} +11 -7
- package/dist/{tempo-wallet-4QKSV65O-J6ZZQAD7.js → tempo-wallet-B5XXV3YH-SRQBMYFV.js} +2 -2
- package/dist/{x402-5H27DCBE-UEQQQ5UY.js → x402-R6HPBY5D-VMSW7Z32.js} +3 -3
- package/package.json +2 -2
- package/skills/dvm/calling.md +2 -0
- package/skills/dvm/credit.md +2 -0
- package/dist/chunk-3SWX5BQP.js +0 -66
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
import { createRequire as __dvmCreateRequire } from "node:module";
|
|
3
3
|
const require = __dvmCreateRequire(import.meta.url);
|
|
4
4
|
import {
|
|
5
|
+
diagnosticConsole,
|
|
5
6
|
withSdkInitLock
|
|
6
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-TZWZTS37.js";
|
|
7
8
|
|
|
8
|
-
// node_modules/@dvmkit/sdk/dist/chunk-
|
|
9
|
+
// node_modules/@dvmkit/sdk/dist/chunk-GAOAYHZA.js
|
|
9
10
|
import { createHash, randomUUID } from "crypto";
|
|
10
11
|
var MemoryX402BatchStore = class {
|
|
11
12
|
batches = /* @__PURE__ */ new Map();
|
|
@@ -38,7 +39,6 @@ var PostgresX402BatchStore = class {
|
|
|
38
39
|
this.db = db;
|
|
39
40
|
}
|
|
40
41
|
db;
|
|
41
|
-
/** Create the table (idempotent), under the SDK init lock like every other boot DDL. */
|
|
42
42
|
async init() {
|
|
43
43
|
await withSdkInitLock(this.db, async () => {
|
|
44
44
|
await this.db.query(`
|
|
@@ -53,9 +53,13 @@ var PostgresX402BatchStore = class {
|
|
|
53
53
|
first_failed_at BIGINT,
|
|
54
54
|
last_error TEXT,
|
|
55
55
|
next_retry_at BIGINT,
|
|
56
|
+
obligation_ids JSONB NOT NULL DEFAULT '[]'::jsonb,
|
|
56
57
|
updated_at BIGINT NOT NULL
|
|
57
58
|
)
|
|
58
59
|
`);
|
|
60
|
+
await this.db.query(
|
|
61
|
+
"ALTER TABLE payout_x402_batches ADD COLUMN IF NOT EXISTS obligation_ids JSONB NOT NULL DEFAULT '[]'::jsonb"
|
|
62
|
+
);
|
|
59
63
|
});
|
|
60
64
|
}
|
|
61
65
|
async get(scope) {
|
|
@@ -72,15 +76,16 @@ var PostgresX402BatchStore = class {
|
|
|
72
76
|
attempts: row.attempts,
|
|
73
77
|
firstFailedAt: row.first_failed_at === null ? null : Number(row.first_failed_at),
|
|
74
78
|
lastError: row.last_error,
|
|
75
|
-
nextRetryAt: row.next_retry_at === null ? null : Number(row.next_retry_at)
|
|
79
|
+
nextRetryAt: row.next_retry_at === null ? null : Number(row.next_retry_at),
|
|
80
|
+
...row.obligation_ids.length ? { obligationIds: row.obligation_ids } : {}
|
|
76
81
|
};
|
|
77
82
|
}
|
|
78
83
|
async put(batch) {
|
|
79
84
|
await this.db.query(
|
|
80
85
|
`INSERT INTO payout_x402_batches (
|
|
81
86
|
scope, batch_id, claimed_native, voucher_count, claims, opened_at,
|
|
82
|
-
attempts, first_failed_at, last_error, next_retry_at, updated_at
|
|
83
|
-
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
|
87
|
+
attempts, first_failed_at, last_error, next_retry_at, obligation_ids, updated_at
|
|
88
|
+
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
|
|
84
89
|
ON CONFLICT (scope) DO UPDATE SET
|
|
85
90
|
batch_id = EXCLUDED.batch_id,
|
|
86
91
|
claimed_native = EXCLUDED.claimed_native,
|
|
@@ -91,6 +96,7 @@ var PostgresX402BatchStore = class {
|
|
|
91
96
|
first_failed_at = EXCLUDED.first_failed_at,
|
|
92
97
|
last_error = EXCLUDED.last_error,
|
|
93
98
|
next_retry_at = EXCLUDED.next_retry_at,
|
|
99
|
+
obligation_ids = EXCLUDED.obligation_ids,
|
|
94
100
|
updated_at = EXCLUDED.updated_at`,
|
|
95
101
|
[
|
|
96
102
|
batch.scope,
|
|
@@ -103,6 +109,7 @@ var PostgresX402BatchStore = class {
|
|
|
103
109
|
batch.firstFailedAt,
|
|
104
110
|
batch.lastError,
|
|
105
111
|
batch.nextRetryAt,
|
|
112
|
+
JSON.stringify(batch.obligationIds ?? []),
|
|
106
113
|
Date.now()
|
|
107
114
|
]
|
|
108
115
|
);
|
|
@@ -143,6 +150,7 @@ var PayoutReporter = class {
|
|
|
143
150
|
ownsBatchStore;
|
|
144
151
|
intervalMs;
|
|
145
152
|
now;
|
|
153
|
+
hostedObligations;
|
|
146
154
|
lastX402SnapshotAt = 0;
|
|
147
155
|
x402;
|
|
148
156
|
tempo;
|
|
@@ -157,8 +165,8 @@ var PayoutReporter = class {
|
|
|
157
165
|
this.batches = opts.batchStore ?? (opts.db ? new PostgresX402BatchStore(opts.db) : new MemoryX402BatchStore());
|
|
158
166
|
this.intervalMs = opts.snapshotIntervalMs ?? PAYOUT_SNAPSHOT_INTERVAL_MS;
|
|
159
167
|
this.now = opts.now ?? Date.now;
|
|
168
|
+
this.hostedObligations = opts.hostedObligations;
|
|
160
169
|
}
|
|
161
|
-
/** Boot DDL for the durable batch store, when this reporter owns one. */
|
|
162
170
|
async init() {
|
|
163
171
|
if (this.ownsBatchStore && this.batches instanceof PostgresX402BatchStore) {
|
|
164
172
|
await this.batches.init();
|
|
@@ -173,10 +181,12 @@ var PayoutReporter = class {
|
|
|
173
181
|
if (this.timer) return;
|
|
174
182
|
this.warmup = setTimeout(() => {
|
|
175
183
|
void this.snapshot();
|
|
184
|
+
void this.reconcileHosted();
|
|
176
185
|
}, WARMUP_MS);
|
|
177
186
|
this.warmup.unref();
|
|
178
187
|
this.timer = setInterval(() => {
|
|
179
188
|
void this.snapshot();
|
|
189
|
+
void this.reconcileHosted();
|
|
180
190
|
}, this.intervalMs);
|
|
181
191
|
this.timer.unref();
|
|
182
192
|
}
|
|
@@ -186,6 +196,45 @@ var PayoutReporter = class {
|
|
|
186
196
|
if (this.timer) clearInterval(this.timer);
|
|
187
197
|
this.timer = null;
|
|
188
198
|
}
|
|
199
|
+
async reconcileHosted() {
|
|
200
|
+
if (!this.hostedObligations) return;
|
|
201
|
+
try {
|
|
202
|
+
for (const failure of await this.hostedObligations.recoverOriginalX402Refunds()) {
|
|
203
|
+
warn("hosted_refund_recovery_pending", { id: failure.id, error: message(failure.error) });
|
|
204
|
+
}
|
|
205
|
+
for (const candidate of await this.hostedObligations.recoveryCandidates()) {
|
|
206
|
+
if (candidate.blockHash) continue;
|
|
207
|
+
try {
|
|
208
|
+
await this.hostedObligations.observeCanonical(candidate.id);
|
|
209
|
+
} catch (error) {
|
|
210
|
+
warn("hosted_effect_observation_pending", { id: candidate.id, error: message(error) });
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
for (const failure of await this.hostedObligations.reconcileX402Batches()) {
|
|
214
|
+
warn("hosted_batch_recovery_pending", { id: failure.id, error: message(failure.error) });
|
|
215
|
+
}
|
|
216
|
+
for (const candidate of await this.hostedObligations.recoveryCandidates()) {
|
|
217
|
+
if (!candidate.blockHash || candidate.reportId) continue;
|
|
218
|
+
try {
|
|
219
|
+
if (candidate.rail === "tempo" && (candidate.operation === "settle" || candidate.operation === "close") && candidate.deltaNative !== null && BigInt(candidate.deltaNative) > 0n) {
|
|
220
|
+
await this.tempoSettled({
|
|
221
|
+
txHash: candidate.txHash,
|
|
222
|
+
channelId: candidate.channelId,
|
|
223
|
+
trigger: candidate.operation,
|
|
224
|
+
amount: BigInt(candidate.deltaNative),
|
|
225
|
+
delta: BigInt(candidate.deltaNative)
|
|
226
|
+
});
|
|
227
|
+
} else if (candidate.rail === "x402" && candidate.operation === "settle" && this.x402) {
|
|
228
|
+
await this.recordX402Settle(this.x402, candidate.txHash);
|
|
229
|
+
}
|
|
230
|
+
} catch (error) {
|
|
231
|
+
warn("hosted_effect_report_pending", { id: candidate.id, error: message(error) });
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
} catch (error) {
|
|
235
|
+
warn("hosted_effect_recovery_pending", { error: message(error) });
|
|
236
|
+
}
|
|
237
|
+
}
|
|
189
238
|
enqueuePayout(payload, tx) {
|
|
190
239
|
return this.transport.enqueuePayout(payload, tx);
|
|
191
240
|
}
|
|
@@ -203,8 +252,6 @@ var PayoutReporter = class {
|
|
|
203
252
|
warn("payout_flush_failed", { error: message(err) });
|
|
204
253
|
}
|
|
205
254
|
}
|
|
206
|
-
// ── cashu ─────────────────────────────────────────────────────────────
|
|
207
|
-
/** The hook `mark-melted` runs inside its transaction: the mark and its payout commit together. */
|
|
208
255
|
meltHook() {
|
|
209
256
|
return {
|
|
210
257
|
enqueue: (event, tx) => this.cashuMelted(event, tx),
|
|
@@ -215,17 +262,9 @@ var PayoutReporter = class {
|
|
|
215
262
|
};
|
|
216
263
|
}
|
|
217
264
|
/**
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
* belongs to one mint. The amount is the face value melted; the mint's fee
|
|
222
|
-
* and the Lightning amount received are builder-machine facts this DVM does
|
|
223
|
-
* not see.
|
|
224
|
-
*
|
|
225
|
-
* Queued through `tx` when the caller is inside the transaction that marks
|
|
226
|
-
* the rows, and a failure is thrown rather than swallowed so that mark rolls
|
|
227
|
-
* back with it — see {@link TransactionalPayoutHook}. Without `tx` the rows
|
|
228
|
-
* are queued on their own and delivered at once.
|
|
265
|
+
* Report melted face value per mint, not the Lightning amount received after fees. Hash the
|
|
266
|
+
* payment preimage; do not publish it. With tx, propagate enqueue failures and defer
|
|
267
|
+
* delivery until commit. Without tx, enqueue independently and start delivery.
|
|
229
268
|
*/
|
|
230
269
|
async cashuMelted(event, tx) {
|
|
231
270
|
const byMint = /* @__PURE__ */ new Map();
|
|
@@ -265,8 +304,6 @@ var PayoutReporter = class {
|
|
|
265
304
|
void this.snapshotCashu();
|
|
266
305
|
}
|
|
267
306
|
}
|
|
268
|
-
// ── tempo ─────────────────────────────────────────────────────────────
|
|
269
|
-
/** The host attaches the session store and ledger readers once they exist. */
|
|
270
307
|
attachTempo(reader) {
|
|
271
308
|
this.tempo = reader;
|
|
272
309
|
}
|
|
@@ -277,8 +314,24 @@ var PayoutReporter = class {
|
|
|
277
314
|
*/
|
|
278
315
|
async tempoSettled(event) {
|
|
279
316
|
try {
|
|
317
|
+
let hostedIds;
|
|
318
|
+
if (this.hostedObligations) {
|
|
319
|
+
hostedIds = await this.hostedObligations.submittedIds(
|
|
320
|
+
"tempo",
|
|
321
|
+
event.trigger === "close" ? "close" : "settle",
|
|
322
|
+
event.txHash
|
|
323
|
+
);
|
|
324
|
+
if (hostedIds.length === 1) {
|
|
325
|
+
const observed = await this.hostedObligations.observeCanonical(hostedIds[0]);
|
|
326
|
+
if (BigInt(observed.deltaNative) !== event.delta) {
|
|
327
|
+
throw new Error("Hosted Tempo callback differs from finalized settlement amount");
|
|
328
|
+
}
|
|
329
|
+
} else if (await this.hostedObligations.hasCompleteHistory()) {
|
|
330
|
+
throw new Error("Hosted Tempo payout has no exact original settlement obligation");
|
|
331
|
+
}
|
|
332
|
+
}
|
|
280
333
|
if (event.delta <= 0n) return;
|
|
281
|
-
|
|
334
|
+
const payload = {
|
|
282
335
|
dvmId: this.dvmId,
|
|
283
336
|
payoutId: `tempo:${event.txHash}`,
|
|
284
337
|
rail: "tempo",
|
|
@@ -293,13 +346,26 @@ var PayoutReporter = class {
|
|
|
293
346
|
...this.tempo?.network ? { network: this.tempo.network } : {}
|
|
294
347
|
},
|
|
295
348
|
landedAt: this.now()
|
|
296
|
-
}
|
|
349
|
+
};
|
|
350
|
+
if (this.hostedObligations) {
|
|
351
|
+
if (hostedIds?.length !== 1 || !this.transport.enqueueHostedPayout) {
|
|
352
|
+
if (await this.hostedObligations.hasCompleteHistory()) {
|
|
353
|
+
throw new Error("Hosted Tempo payout has no exact original settlement obligation");
|
|
354
|
+
}
|
|
355
|
+
await this.reportPayout(payload);
|
|
356
|
+
} else {
|
|
357
|
+
await this.transport.enqueueHostedPayout(payload, hostedIds);
|
|
358
|
+
void this.flush();
|
|
359
|
+
}
|
|
360
|
+
} else {
|
|
361
|
+
await this.reportPayout(payload);
|
|
362
|
+
}
|
|
297
363
|
} catch (err) {
|
|
298
364
|
warn("payout_report_failed", { rail: "tempo", kind: event.trigger, error: message(err) });
|
|
365
|
+
if (this.hostedObligations) throw err;
|
|
299
366
|
}
|
|
300
367
|
void this.snapshotTempo();
|
|
301
368
|
}
|
|
302
|
-
/** The hook `reconcile-tempo-drain` runs inside its transaction: the booking and its payout commit together. */
|
|
303
369
|
tempoRepairHook() {
|
|
304
370
|
return {
|
|
305
371
|
enqueue: (event, tx) => this.tempoCloseReconciled(event, tx),
|
|
@@ -310,46 +376,59 @@ var PayoutReporter = class {
|
|
|
310
376
|
};
|
|
311
377
|
}
|
|
312
378
|
/**
|
|
313
|
-
*
|
|
314
|
-
*
|
|
315
|
-
*
|
|
316
|
-
* figure is the receipt's payee total for the channel.
|
|
317
|
-
*
|
|
318
|
-
* Queued through `tx` when the caller is inside the transaction that books
|
|
319
|
-
* the drain, and thrown rather than swallowed so that booking rolls back
|
|
320
|
-
* with it — see {@link TransactionalPayoutHook}.
|
|
379
|
+
* Report the receipt’s payee total under the live close’s transaction ID, flagged repaired.
|
|
380
|
+
* The platform upgrades a live row if one exists. Join tx and propagate enqueue failures;
|
|
381
|
+
* this does not report the caller refund.
|
|
321
382
|
*/
|
|
322
383
|
async tempoCloseReconciled(event, tx) {
|
|
323
|
-
|
|
384
|
+
let settled = BigInt(event.settledToPayee);
|
|
385
|
+
let hostedIds;
|
|
386
|
+
if (this.hostedObligations) {
|
|
387
|
+
hostedIds = await this.hostedObligations.submittedIds("tempo", "close", event.txHash);
|
|
388
|
+
if (hostedIds.length === 1) {
|
|
389
|
+
const alreadySettled = await this.hostedObligations.originalTempoCloseSettled(
|
|
390
|
+
hostedIds[0],
|
|
391
|
+
event.channelId,
|
|
392
|
+
event.txHash
|
|
393
|
+
);
|
|
394
|
+
settled -= alreadySettled;
|
|
395
|
+
if (settled < 0n) throw new Error("Hosted Tempo repair precedes its original settled amount");
|
|
396
|
+
} else if (await this.hostedObligations.hasCompleteHistory(tx)) {
|
|
397
|
+
throw new Error("Hosted Tempo repair has no original close obligation");
|
|
398
|
+
}
|
|
399
|
+
}
|
|
324
400
|
if (settled <= 0n) return;
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
...this.tempo?.network ? { network: this.tempo.network } : {}
|
|
339
|
-
},
|
|
340
|
-
landedAt: this.now(),
|
|
341
|
-
repaired: true,
|
|
342
|
-
repairKind: "tempo_close_reconciled"
|
|
401
|
+
const payload = {
|
|
402
|
+
dvmId: this.dvmId,
|
|
403
|
+
payoutId: `tempo:${event.txHash}`,
|
|
404
|
+
rail: "tempo",
|
|
405
|
+
kind: "close",
|
|
406
|
+
nativeAmount: Number(settled),
|
|
407
|
+
nativeAsset: "usdc",
|
|
408
|
+
recipient: this.tempo?.recipient ?? null,
|
|
409
|
+
refs: {
|
|
410
|
+
channelId: event.channelId,
|
|
411
|
+
txHash: event.txHash,
|
|
412
|
+
trigger: "close",
|
|
413
|
+
...this.tempo?.network ? { network: this.tempo.network } : {}
|
|
343
414
|
},
|
|
344
|
-
|
|
345
|
-
|
|
415
|
+
landedAt: this.now(),
|
|
416
|
+
repaired: true,
|
|
417
|
+
repairKind: "tempo_close_reconciled"
|
|
418
|
+
};
|
|
419
|
+
if (hostedIds?.length === 1) {
|
|
420
|
+
if (!this.transport.enqueueHostedPayout) {
|
|
421
|
+
throw new Error("Hosted Tempo repair lacks its durable report queue");
|
|
422
|
+
}
|
|
423
|
+
await this.transport.enqueueHostedPayout(payload, hostedIds, tx);
|
|
424
|
+
} else {
|
|
425
|
+
await this.enqueuePayout(payload, tx);
|
|
426
|
+
}
|
|
346
427
|
if (!tx) {
|
|
347
428
|
void this.flush();
|
|
348
429
|
void this.snapshotTempo();
|
|
349
430
|
}
|
|
350
431
|
}
|
|
351
|
-
// ── x402 ──────────────────────────────────────────────────────────────
|
|
352
|
-
/** The observer the batch-settlement server attaches to and routes its manager through. */
|
|
353
432
|
x402Observer() {
|
|
354
433
|
return {
|
|
355
434
|
attach: (ctx) => {
|
|
@@ -365,17 +444,24 @@ var PayoutReporter = class {
|
|
|
365
444
|
const ctx = this.x402;
|
|
366
445
|
if (ctx) await this.recordX402SettleFailure(ctx, error);
|
|
367
446
|
void this.snapshotX402();
|
|
447
|
+
},
|
|
448
|
+
recordRefundClaim: async (transaction, delta) => {
|
|
449
|
+
const ctx = this.x402;
|
|
450
|
+
if (!ctx) throw new Error("Hosted x402 refund has no payout context");
|
|
451
|
+
await this.recordX402Claim(ctx, {
|
|
452
|
+
delta,
|
|
453
|
+
vouchers: 1,
|
|
454
|
+
transactions: [transaction],
|
|
455
|
+
operation: "refund"
|
|
456
|
+
});
|
|
457
|
+
void this.snapshotX402();
|
|
368
458
|
}
|
|
369
459
|
};
|
|
370
460
|
}
|
|
371
461
|
/**
|
|
372
|
-
*
|
|
373
|
-
*
|
|
374
|
-
*
|
|
375
|
-
* fleet lock the before/after read is consistent. A channel the claim
|
|
376
|
-
* removed on its way through — a refund's claim-then-delete (internal-review) —
|
|
377
|
-
* moved whatever it still owed before it went, and that value reaches
|
|
378
|
-
* `pay_to` in this batch too, so it counts at its pre-claim figure.
|
|
462
|
+
* Read claim deltas from storage because upstream returns voucher counts. Under the fleet
|
|
463
|
+
* lock, before/after reads are consistent. A deleted channel contributes its pre-claim
|
|
464
|
+
* unclaimed value, including refund claim-then-delete.
|
|
379
465
|
*/
|
|
380
466
|
trackX402Manager(manager) {
|
|
381
467
|
const claim = async (...args) => {
|
|
@@ -396,7 +482,8 @@ var PayoutReporter = class {
|
|
|
396
482
|
}
|
|
397
483
|
await this.recordX402Claim(ctx, {
|
|
398
484
|
delta,
|
|
399
|
-
vouchers: results.reduce((sum, r) => sum + r.vouchers, 0)
|
|
485
|
+
vouchers: results.reduce((sum, r) => sum + r.vouchers, 0),
|
|
486
|
+
transactions: results.map((result) => result.transaction)
|
|
400
487
|
});
|
|
401
488
|
void this.snapshotX402();
|
|
402
489
|
}
|
|
@@ -415,10 +502,32 @@ var PayoutReporter = class {
|
|
|
415
502
|
throw err;
|
|
416
503
|
}
|
|
417
504
|
};
|
|
505
|
+
const refund = async (channelIds) => {
|
|
506
|
+
const ctx = this.x402;
|
|
507
|
+
const before = ctx ? await claimedByChannel(ctx.storage) : /* @__PURE__ */ new Map();
|
|
508
|
+
const results = await manager.refund(channelIds);
|
|
509
|
+
if (ctx && this.hostedObligations) {
|
|
510
|
+
for (const result of results) {
|
|
511
|
+
const original = before.get(result.channel.toLowerCase());
|
|
512
|
+
if (!original) throw new Error("Hosted x402 refund lost its original channel snapshot");
|
|
513
|
+
const delta = original.charged - original.claimed;
|
|
514
|
+
if (delta > 0n) {
|
|
515
|
+
await this.recordX402Claim(ctx, {
|
|
516
|
+
delta,
|
|
517
|
+
vouchers: 1,
|
|
518
|
+
transactions: [result.transaction],
|
|
519
|
+
operation: "refund"
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
return results;
|
|
525
|
+
};
|
|
418
526
|
return new Proxy(manager, {
|
|
419
527
|
get(target, prop, receiver) {
|
|
420
528
|
if (prop === "claim") return claim;
|
|
421
529
|
if (prop === "settle") return settle;
|
|
530
|
+
if (prop === "refund" && target.refund) return refund;
|
|
422
531
|
const value = Reflect.get(target, prop, receiver);
|
|
423
532
|
return typeof value === "function" ? value.bind(target) : value;
|
|
424
533
|
}
|
|
@@ -427,7 +536,11 @@ var PayoutReporter = class {
|
|
|
427
536
|
async recordX402Claim(ctx, claim) {
|
|
428
537
|
try {
|
|
429
538
|
const now = this.now();
|
|
430
|
-
const
|
|
539
|
+
const existing = await this.batches.get(ctx.scope);
|
|
540
|
+
if (this.hostedObligations && !existing) {
|
|
541
|
+
throw new Error("Hosted x402 claim lost its pre-effect payout batch");
|
|
542
|
+
}
|
|
543
|
+
const open = existing ?? {
|
|
431
544
|
scope: ctx.scope,
|
|
432
545
|
batchId: randomUUID(),
|
|
433
546
|
claimedNative: 0n,
|
|
@@ -442,9 +555,22 @@ var PayoutReporter = class {
|
|
|
442
555
|
open.claimedNative += claim.delta;
|
|
443
556
|
open.voucherCount += claim.vouchers;
|
|
444
557
|
open.claims += 1;
|
|
558
|
+
if (this.hostedObligations) {
|
|
559
|
+
const ids = (await Promise.all(claim.transactions.map((txHash) => this.hostedObligations.submittedIds(
|
|
560
|
+
"x402",
|
|
561
|
+
claim.operation ?? "claim",
|
|
562
|
+
txHash,
|
|
563
|
+
open.batchId
|
|
564
|
+
)))).flat();
|
|
565
|
+
if (!ids.length || ids.length < claim.vouchers || new Set(ids).size !== ids.length) {
|
|
566
|
+
throw new Error("Hosted x402 claim has no exact reserved voucher set");
|
|
567
|
+
}
|
|
568
|
+
open.obligationIds = [.../* @__PURE__ */ new Set([...open.obligationIds ?? [], ...ids])];
|
|
569
|
+
}
|
|
445
570
|
await this.batches.put(open);
|
|
446
571
|
} catch (err) {
|
|
447
572
|
warn("payout_batch_track_failed", { scope: ctx.scope, error: message(err) });
|
|
573
|
+
if (this.hostedObligations) throw err;
|
|
448
574
|
}
|
|
449
575
|
}
|
|
450
576
|
async recordX402Settle(ctx, transaction) {
|
|
@@ -459,6 +585,23 @@ var PayoutReporter = class {
|
|
|
459
585
|
warn("payout_batch_empty", { scope: ctx.scope, transaction, batchId: open.batchId });
|
|
460
586
|
return;
|
|
461
587
|
}
|
|
588
|
+
if (this.hostedObligations) {
|
|
589
|
+
if (!open.obligationIds?.length) {
|
|
590
|
+
throw new Error("Hosted x402 payout has no original claim identities");
|
|
591
|
+
}
|
|
592
|
+
const settleId = await this.hostedObligations.settlementId(open.batchId, transaction);
|
|
593
|
+
const settled = await this.hostedObligations.observeCanonical(settleId);
|
|
594
|
+
if (BigInt(settled.deltaNative) !== open.claimedNative) {
|
|
595
|
+
throw new Error("Hosted x402 settled amount differs from its original batch");
|
|
596
|
+
}
|
|
597
|
+
let claimed = 0n;
|
|
598
|
+
for (const id of open.obligationIds) {
|
|
599
|
+
claimed += BigInt((await this.hostedObligations.observeCanonical(id)).deltaNative);
|
|
600
|
+
}
|
|
601
|
+
if (claimed !== open.claimedNative) {
|
|
602
|
+
throw new Error("Hosted x402 batch amount differs from finalized original claims");
|
|
603
|
+
}
|
|
604
|
+
}
|
|
462
605
|
const payload = {
|
|
463
606
|
dvmId: this.dvmId,
|
|
464
607
|
payoutId: `x402:batch:${open.batchId}`,
|
|
@@ -476,17 +619,25 @@ var PayoutReporter = class {
|
|
|
476
619
|
},
|
|
477
620
|
landedAt: this.now()
|
|
478
621
|
};
|
|
479
|
-
const closed = await this.batches.settle(
|
|
480
|
-
|
|
481
|
-
open.
|
|
482
|
-
|
|
483
|
-
|
|
622
|
+
const closed = await this.batches.settle(ctx.scope, open.batchId, async (tx) => {
|
|
623
|
+
if (!this.hostedObligations) return this.enqueuePayout(payload, tx);
|
|
624
|
+
if (!this.transport.enqueueHostedPayout || !open.obligationIds?.length) {
|
|
625
|
+
throw new Error("Hosted x402 payout lacks its original claims or durable queue");
|
|
626
|
+
}
|
|
627
|
+
const settleId = await this.hostedObligations.settlementId(open.batchId, transaction);
|
|
628
|
+
await this.transport.enqueueHostedPayout(
|
|
629
|
+
payload,
|
|
630
|
+
[...open.obligationIds, settleId],
|
|
631
|
+
tx
|
|
632
|
+
);
|
|
633
|
+
});
|
|
484
634
|
if (!closed) {
|
|
485
635
|
warn("payout_batch_raced", { scope: ctx.scope, transaction, batchId: open.batchId });
|
|
486
636
|
return;
|
|
487
637
|
}
|
|
488
638
|
} catch (err) {
|
|
489
639
|
warn("payout_report_failed", { rail: "x402", kind: "batch", error: message(err) });
|
|
640
|
+
if (this.hostedObligations) throw err;
|
|
490
641
|
return;
|
|
491
642
|
}
|
|
492
643
|
void this.flush();
|
|
@@ -505,7 +656,6 @@ var PayoutReporter = class {
|
|
|
505
656
|
warn("payout_batch_track_failed", { scope: ctx.scope, error: message(trackErr) });
|
|
506
657
|
}
|
|
507
658
|
}
|
|
508
|
-
// ── snapshots ─────────────────────────────────────────────────────────
|
|
509
659
|
/** Every rail this reporter can read, each on its own failure boundary. */
|
|
510
660
|
async snapshot() {
|
|
511
661
|
await this.snapshotCashu();
|
|
@@ -642,7 +792,7 @@ var PayoutReporter = class {
|
|
|
642
792
|
/**
|
|
643
793
|
* Tempo: each active channel's spent-but-unsettled balance — earned, not
|
|
644
794
|
* yet paid to the recipient — and the cooperative closes wedged between
|
|
645
|
-
* chain and ledger
|
|
795
|
+
* chain and ledger, aged past the same floor the repair queue
|
|
646
796
|
* uses so an in-flight close is not reported as stuck.
|
|
647
797
|
*/
|
|
648
798
|
async snapshotTempo() {
|
|
@@ -749,7 +899,7 @@ function message(err) {
|
|
|
749
899
|
return err instanceof Error ? err.message : String(err);
|
|
750
900
|
}
|
|
751
901
|
function warn(level, fields) {
|
|
752
|
-
|
|
902
|
+
diagnosticConsole.warn({ level, ...fields });
|
|
753
903
|
}
|
|
754
904
|
|
|
755
905
|
export {
|