Support multiple deferred payments in public deals
Verify and publish checkpoint container / container (push) Successful in 10m22s
Verify and publish checkpoint container / container (push) Successful in 10m22s
This commit is contained in:
1 parent
0b8a147388
commit
cacc88906c
12 files changed
+742
-50
No files matched your search
@@ -1,16 +1,22 @@
|
||||
import { validatePublicDeals, voidPublicDealPayment, type PublicDeal, type PublicDealSeat } from './public-deals';
|
||||
import { publicDealDeferredTerms, validatePublicDeals, voidPublicDealPayment, type PublicDeal, type PublicDealSeat } from './public-deals';
|
||||
|
||||
/** A deal-linked obligation is independent of every other deal and of
|
||||
* Truthtrance. It is due once for its exact gross payment, not a threshold
|
||||
* shared with a different agreement. */
|
||||
export type PendingPublicDealPayment = {
|
||||
deal: string;
|
||||
paymentIndex?: number;
|
||||
payer: string;
|
||||
recipient: string;
|
||||
dueTurn: number;
|
||||
amount: number;
|
||||
};
|
||||
|
||||
/** Projection key is stable for a term and preserves the legacy deal key. */
|
||||
export function publicDealPaymentOptionKey(deal: string, paymentIndex?: number): string {
|
||||
return paymentIndex === undefined ? deal : `${deal}:payment:${paymentIndex}`;
|
||||
}
|
||||
|
||||
export class PublicDealObligationError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
@@ -32,10 +38,12 @@ export function outstandingPublicDealPayments(
|
||||
if (!Array.isArray(deals) || !identifier(payer) || !Number.isSafeInteger(turn) || turn < 1)
|
||||
throw new PublicDealObligationError('Retain the payer and original turn of the deferred payments.');
|
||||
return deals.flatMap(deal => {
|
||||
const payment = deal.payment;
|
||||
if (deal.status !== 'active' || deal.paymentStatus !== 'pending' ||
|
||||
payment?.kind !== 'deferredBribe' || payment.payer !== payer || payment.dueTurn < turn) return [];
|
||||
return [{ deal: deal.id, payer, recipient: payment.recipient, dueTurn: payment.dueTurn, amount: payment.amount }];
|
||||
if (deal.status !== 'active') return [];
|
||||
return publicDealDeferredTerms(deal).flatMap(({payment, paymentStatus, paymentIndex}) => {
|
||||
if (paymentStatus !== 'pending' || payment.payer !== payer || payment.dueTurn < turn) return [];
|
||||
return [{deal: deal.id, ...(paymentIndex === undefined ? {} : {paymentIndex}), payer,
|
||||
recipient: payment.recipient, dueTurn: payment.dueTurn, amount: payment.amount}];
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -83,7 +91,7 @@ export function reconcilePublicDealPayments(
|
||||
throw new PublicDealObligationError('Retain the actual funded witness and cause of the change.');
|
||||
const pending = pendingPublicDealPayments(deals, payer, turn);
|
||||
const ids = witness.impossibleDealIds ?? [];
|
||||
if (new Set(ids).size !== ids.length || ids.some(id => !pending.some(row => row.deal === id)))
|
||||
if (new Set(ids).size !== ids.length || ids.some(id => !pending.some(row => row.deal === id && row.paymentIndex === undefined)))
|
||||
throw new PublicDealObligationError('Void only individually proven pending terms of this payer and turn.');
|
||||
if (witness.change === 'voluntary' && (ids.length > 0 ||
|
||||
witness.available !== null && witness.available < publicDealPaymentTotal(deals, payer, turn)))
|
||||
|
||||
Reference in new issue
Block a user