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
+83
-3
@@ -15,6 +15,13 @@ export type PublicDealPayment = {
|
||||
dueTurn: number;
|
||||
};
|
||||
export type PublicDealPaymentStatus = 'pending' | 'paid' | 'void' | 'released';
|
||||
export type PublicDealDeferredPayment = Extract<PublicDealPayment, { kind: 'deferredBribe' }>;
|
||||
/** The index is the stable identity of a term within its immutable offer. */
|
||||
export type PublicDealAdditionalPayment = {
|
||||
payment: PublicDealDeferredPayment;
|
||||
paymentStatus?: PublicDealPaymentStatus;
|
||||
};
|
||||
export const MAX_ADDITIONAL_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 = {
|
||||
@@ -42,6 +49,7 @@ export type PublicDeal = {
|
||||
payment?: PublicDealPayment;
|
||||
/** Present only for accepted or released deferred payments. */
|
||||
paymentStatus?: PublicDealPaymentStatus;
|
||||
additionalPayments?: PublicDealAdditionalPayment[];
|
||||
travel?: PublicDealTravel;
|
||||
};
|
||||
|
||||
@@ -86,6 +94,17 @@ function notAllied(seats: readonly PublicDealSeat[]): void {
|
||||
export function publicDealParticipants(deal: PublicDeal): string[] {
|
||||
return deal.participants ? [...deal.participants] : [deal.from, deal.to];
|
||||
}
|
||||
/** Enumerate legacy and indexed deferred terms without rewriting older saves. */
|
||||
export function publicDealDeferredTerms(deal: PublicDeal): Array<{
|
||||
payment: PublicDealDeferredPayment;
|
||||
paymentStatus?: PublicDealPaymentStatus;
|
||||
paymentIndex?: number;
|
||||
}> {
|
||||
const legacy = deal.payment?.kind === 'deferredBribe'
|
||||
? [{payment: deal.payment, ...(deal.paymentStatus ? {paymentStatus: deal.paymentStatus} : {})}]
|
||||
: [];
|
||||
return [...legacy, ...(deal.additionalPayments ?? []).map((term,paymentIndex) => ({...term,paymentIndex}))];
|
||||
}
|
||||
export function publicDealAcceptVotes(deal: PublicDeal): string[] {
|
||||
if (deal.acceptVotes) return [...deal.acceptVotes];
|
||||
return deal.status === 'active' || deal.status === 'released'
|
||||
@@ -154,7 +173,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').sort().join(',') : '';
|
||||
const keys = object(deal) ? Object.keys(deal).filter(key => key !== 'payment' && key !== 'paymentStatus' && key !== 'travel' && key !== 'additionalPayments').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 &&
|
||||
@@ -206,6 +225,23 @@ export function validatePublicDeals(value: unknown, players: readonly PublicDeal
|
||||
!Object.hasOwn(deal, 'paymentStatus'), 'The deferred payment status does not match the deal lifecycle.');
|
||||
} else requireDeal(!Object.hasOwn(deal, 'paymentStatus'),
|
||||
'Only an accepted deferred payment has a payment status.');
|
||||
if (Object.hasOwn(deal, 'additionalPayments')) {
|
||||
requireDeal(Array.isArray(deal.additionalPayments) && deal.additionalPayments.length >= 1 &&
|
||||
deal.additionalPayments.length <= MAX_ADDITIONAL_DEAL_PAYMENTS,
|
||||
'Choose between one and twelve additional deferred payments.');
|
||||
for (const term of deal.additionalPayments) {
|
||||
requireDeal(object(term) && (Object.keys(term).sort().join(',') === 'payment' ||
|
||||
Object.keys(term).sort().join(',') === 'payment,paymentStatus'),
|
||||
'An additional payment term is malformed.');
|
||||
const parsed = parsePublicDealPayment(term.payment, parties, deal.turn as number);
|
||||
requireDeal(parsed.kind === 'deferredBribe', 'Additional payments must have a due turn.');
|
||||
const status = term.paymentStatus;
|
||||
requireDeal(deal.status === 'active' ? status === 'pending' || status === 'paid' || status === 'void' :
|
||||
deal.status === 'released' ? status === 'released' || status === 'paid' :
|
||||
!Object.hasOwn(term, 'paymentStatus'),
|
||||
'An additional payment status does not match the deal lifecycle.');
|
||||
}
|
||||
}
|
||||
const votes = deal.releaseVotes as string[];
|
||||
requireDeal(new Set(votes).size === votes.length && votes.every(vote => parties.includes(vote)) &&
|
||||
(!multi || orderedSubset(votes, parties)) &&
|
||||
@@ -248,6 +284,28 @@ export function createMultipartyPublicDeal(
|
||||
...paymentTerm };
|
||||
}
|
||||
|
||||
/** Attach independent deferred payment terms to an unchanged fresh offer. */
|
||||
export function setPublicDealAdditionalPayments(deal: PublicDeal, value: unknown, players: readonly PublicDealSeat[]): PublicDeal {
|
||||
validatePublicDeals([deal], players);
|
||||
requireDeal(deal.status === 'offered' && !Object.hasOwn(deal, 'additionalPayments') &&
|
||||
!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 deferred payment terms.');
|
||||
requireDeal(Array.isArray(value) && value.length >= 1 && value.length <= MAX_ADDITIONAL_DEAL_PAYMENTS,
|
||||
'Choose between one and twelve additional deferred payments.');
|
||||
const parties = publicDealParticipants(deal);
|
||||
notAllied(participants(players, parties));
|
||||
const additionalPayments = value.map(value => {
|
||||
const payment = parsePublicDealPayment(value, parties, deal.turn);
|
||||
requireDeal(payment.kind === 'deferredBribe', 'Additional payments must have a due turn.');
|
||||
return {payment};
|
||||
});
|
||||
const result = {...deal, additionalPayments};
|
||||
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);
|
||||
@@ -270,6 +328,7 @@ export function respondPublicDeal(deal: PublicDeal, players: readonly PublicDeal
|
||||
if (!accept) return { ...deal, status: 'declined', releaseVotes: [] };
|
||||
notAllied(participants(players, parties));
|
||||
if (!deal.participants) return { ...deal, status: 'active', releaseVotes: [],
|
||||
...(deal.additionalPayments ? {additionalPayments: deal.additionalPayments.map(term => ({...term, paymentStatus:'pending' as const}))} : {}),
|
||||
...(deal.payment?.kind === 'deferredBribe' ? { paymentStatus: 'pending' as const } : {}) };
|
||||
const alreadyAccepted = publicDealAcceptVotes(deal);
|
||||
requireDeal(!alreadyAccepted.includes(actor), 'This participant already accepted the offer.');
|
||||
@@ -279,6 +338,7 @@ export function respondPublicDeal(deal: PublicDeal, players: readonly PublicDeal
|
||||
const accepts = parties.filter(seat => seat === actor || alreadyAccepted.includes(seat));
|
||||
const active = accepts.length === parties.length;
|
||||
return { ...deal, acceptVotes: accepts, status: active ? 'active' : 'offered', releaseVotes: [],
|
||||
...(active && deal.additionalPayments ? {additionalPayments: deal.additionalPayments.map(term => ({...term, paymentStatus:'pending' as const}))} : {}),
|
||||
...(active && deal.payment?.kind === 'deferredBribe' ? { paymentStatus: 'pending' as const } : {}) };
|
||||
}
|
||||
|
||||
@@ -300,6 +360,8 @@ export function releasePublicDeal(deal: PublicDeal, players: readonly PublicDeal
|
||||
if (deal.participants) votes = parties.filter(seat => votes.includes(seat));
|
||||
const released = votes.length === parties.length;
|
||||
return { ...deal, status: released ? 'released' : 'active', releaseVotes: votes,
|
||||
...(released && deal.additionalPayments ? {additionalPayments: deal.additionalPayments.map(term => ({...term,
|
||||
paymentStatus: term.paymentStatus === 'paid' ? 'paid' as const : 'released' as const}))} : {}),
|
||||
...(released && deal.payment?.kind === 'deferredBribe' && deal.paymentStatus !== 'paid'
|
||||
? { paymentStatus: 'released' as const } : {}) };
|
||||
}
|
||||
@@ -307,8 +369,17 @@ export function releasePublicDeal(deal: PublicDeal, players: readonly PublicDeal
|
||||
/** The engine must first authorize the native payer and exact bribe action.
|
||||
* Apply this transition on its detached action state, so native failure rolls
|
||||
* both the payment and the status change back together. */
|
||||
export function markPublicDealPaid(deal: PublicDeal, players: readonly PublicDealSeat[], payer: string): PublicDeal {
|
||||
export function markPublicDealPaid(deal: PublicDeal, players: readonly PublicDealSeat[], payer: string, paymentIndex?: number): PublicDeal {
|
||||
validatePublicDeals([deal], players);
|
||||
if (paymentIndex !== undefined) {
|
||||
requireDeal(Number.isSafeInteger(paymentIndex) && paymentIndex >= 0 &&
|
||||
paymentIndex < (deal.additionalPayments?.length ?? 0), 'Choose an existing additional payment.');
|
||||
const term = deal.additionalPayments![paymentIndex];
|
||||
requireDeal(deal.status === 'active' && term.paymentStatus === 'pending' && term.payment.payer === payer,
|
||||
'Only the payer may settle a pending deferred deal payment.');
|
||||
return {...deal, additionalPayments: deal.additionalPayments!.map((row,index) =>
|
||||
index === paymentIndex ? {...row, paymentStatus:'paid' as const} : row)};
|
||||
}
|
||||
requireDeal(deal.status === 'active' && deal.payment?.kind === 'deferredBribe' &&
|
||||
deal.paymentStatus === 'pending' && deal.payment.payer === payer,
|
||||
'Only the payer may settle a pending deferred deal payment.');
|
||||
@@ -316,8 +387,17 @@ export function markPublicDealPaid(deal: PublicDeal, players: readonly PublicDea
|
||||
}
|
||||
|
||||
/** The caller must establish actual external impossibility before voiding. */
|
||||
export function voidPublicDealPayment(deal: PublicDeal, players: readonly PublicDealSeat[]): PublicDeal {
|
||||
export function voidPublicDealPayment(deal: PublicDeal, players: readonly PublicDealSeat[], paymentIndex?: number): PublicDeal {
|
||||
validatePublicDeals([deal], players);
|
||||
if (paymentIndex !== undefined) {
|
||||
requireDeal(Number.isSafeInteger(paymentIndex) && paymentIndex >= 0 &&
|
||||
paymentIndex < (deal.additionalPayments?.length ?? 0), 'Choose an existing additional payment.');
|
||||
const term = deal.additionalPayments![paymentIndex];
|
||||
requireDeal(deal.status === 'active' && term.paymentStatus === 'pending',
|
||||
'Only a pending deferred deal payment can become impossible.');
|
||||
return {...deal, additionalPayments: deal.additionalPayments!.map((row,index) =>
|
||||
index === paymentIndex ? {...row, paymentStatus:'void' as const} : row)};
|
||||
}
|
||||
requireDeal(deal.status === 'active' && deal.payment?.kind === 'deferredBribe' && deal.paymentStatus === 'pending',
|
||||
'Only a pending deferred deal payment can become impossible.');
|
||||
return { ...deal, paymentStatus: 'void' };
|
||||
|
||||
Reference in new issue
Block a user