Settle multiple immediate deal payments
Verify and publish checkpoint container / container (push) Successful in 10m59s
Verify and publish checkpoint container / container (push) Successful in 10m59s
This commit is contained in:
1 parent
cacc88906c
commit
b03efd2371
11 files changed
+652
-18
No files matched your search
+37
-1
@@ -15,6 +15,7 @@ export type PublicDealPayment = {
|
||||
dueTurn: number;
|
||||
};
|
||||
export type PublicDealPaymentStatus = 'pending' | 'paid' | 'void' | 'released';
|
||||
export type PublicDealImmediatePayment = Extract<PublicDealPayment, { kind: 'immediateBribe' }>;
|
||||
export type PublicDealDeferredPayment = Extract<PublicDealPayment, { kind: 'deferredBribe' }>;
|
||||
/** The index is the stable identity of a term within its immutable offer. */
|
||||
export type PublicDealAdditionalPayment = {
|
||||
@@ -22,6 +23,7 @@ export type PublicDealAdditionalPayment = {
|
||||
paymentStatus?: PublicDealPaymentStatus;
|
||||
};
|
||||
export const MAX_ADDITIONAL_DEAL_PAYMENTS = 12;
|
||||
export const MAX_ADDITIONAL_IMMEDIATE_DEAL_PAYMENTS = 12;
|
||||
/** One invited recipient commits to one native action (or its absence).
|
||||
* Old two-party records identify that recipient through `to`. */
|
||||
export type PublicDealTravel = {
|
||||
@@ -50,6 +52,8 @@ export type PublicDeal = {
|
||||
/** Present only for accepted or released deferred payments. */
|
||||
paymentStatus?: PublicDealPaymentStatus;
|
||||
additionalPayments?: PublicDealAdditionalPayment[];
|
||||
/** Immutable additional transfers, executed under final acceptance. */
|
||||
additionalImmediatePayments?: PublicDealImmediatePayment[];
|
||||
travel?: PublicDealTravel;
|
||||
};
|
||||
|
||||
@@ -173,7 +177,7 @@ export function validatePublicDeals(value: unknown, players: readonly PublicDeal
|
||||
requireDeal(Array.isArray(value), 'The public deal ledger is malformed.');
|
||||
const ids = new Set<string>();
|
||||
for (const deal of value) {
|
||||
const keys = object(deal) ? Object.keys(deal).filter(key => key !== 'payment' && key !== 'paymentStatus' && key !== 'travel' && key !== 'additionalPayments').sort().join(',') : '';
|
||||
const keys = object(deal) ? Object.keys(deal).filter(key => key !== 'payment' && key !== 'paymentStatus' && key !== 'travel' && key !== 'additionalPayments' && key !== 'additionalImmediatePayments').sort().join(',') : '';
|
||||
const multi = keys === 'acceptVotes,from,id,participants,releaseVotes,status,terms,to,turn';
|
||||
requireDeal(object(deal) && (multi || keys === 'from,id,releaseVotes,status,terms,to,turn') &&
|
||||
id(deal.id) && id(deal.from) && id(deal.to) && deal.from !== deal.to &&
|
||||
@@ -242,6 +246,15 @@ export function validatePublicDeals(value: unknown, players: readonly PublicDeal
|
||||
'An additional payment status does not match the deal lifecycle.');
|
||||
}
|
||||
}
|
||||
if (Object.hasOwn(deal, 'additionalImmediatePayments')) {
|
||||
requireDeal(Array.isArray(deal.additionalImmediatePayments) && deal.additionalImmediatePayments.length >= 1 &&
|
||||
deal.additionalImmediatePayments.length <= MAX_ADDITIONAL_IMMEDIATE_DEAL_PAYMENTS,
|
||||
'Choose between one and twelve additional immediate payments.');
|
||||
for (const term of deal.additionalImmediatePayments) {
|
||||
const parsed = parsePublicDealPayment(term, parties, deal.turn as number);
|
||||
requireDeal(parsed.kind === 'immediateBribe', 'Additional immediate payments must settle on acceptance.');
|
||||
}
|
||||
}
|
||||
const votes = deal.releaseVotes as string[];
|
||||
requireDeal(new Set(votes).size === votes.length && votes.every(vote => parties.includes(vote)) &&
|
||||
(!multi || orderedSubset(votes, parties)) &&
|
||||
@@ -306,6 +319,29 @@ export function setPublicDealAdditionalPayments(deal: PublicDeal, value: unknown
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Attach immutable transfers to an unchanged fresh offer. Settlement progress
|
||||
* belongs to the engine continuation, never the public offer or its caller. */
|
||||
export function setPublicDealAdditionalImmediatePayments(deal: PublicDeal, value: unknown, players: readonly PublicDealSeat[]): PublicDeal {
|
||||
validatePublicDeals([deal], players);
|
||||
requireDeal(deal.status === 'offered' && !Object.hasOwn(deal, 'additionalImmediatePayments') &&
|
||||
!Object.hasOwn(deal, 'paymentStatus') && (deal.participants
|
||||
? !!deal.acceptVotes && deal.acceptVotes.length === 1 && deal.acceptVotes[0] === deal.from
|
||||
: !Object.hasOwn(deal, 'acceptVotes')),
|
||||
'Only a fresh offer may add immediate payment terms.');
|
||||
requireDeal(Array.isArray(value) && value.length >= 1 && value.length <= MAX_ADDITIONAL_IMMEDIATE_DEAL_PAYMENTS,
|
||||
'Choose between one and twelve additional immediate payments.');
|
||||
const parties = publicDealParticipants(deal);
|
||||
notAllied(participants(players, parties));
|
||||
const additionalImmediatePayments = value.map(value => {
|
||||
const payment = parsePublicDealPayment(value, parties, deal.turn);
|
||||
requireDeal(payment.kind === 'immediateBribe', 'Additional immediate payments must settle on acceptance.');
|
||||
return payment;
|
||||
});
|
||||
const result = {...deal, additionalImmediatePayments};
|
||||
validatePublicDeals([result], players);
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Attach a travel term while constructing a fresh offer. */
|
||||
export function setPublicDealTravel(deal: PublicDeal, value: unknown, players: readonly PublicDealSeat[]): PublicDeal {
|
||||
validatePublicDeals([deal], players);
|
||||
|
||||
Reference in new issue
Block a user