feat(state-machine): split fee into per-flow commission rates

Add cashInFeePercent (3.33%) and cashOutFeePercent (7.77%) to context.
Set feePercent from the per-flow value on SELECT_CASH_IN/SELECT_CASH_OUT
transitions. Preserve both rates across resetContext.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Patrick Mulligan 2026-03-01 12:19:06 -05:00
commit 3a78fffb65
2 changed files with 18 additions and 4 deletions

View file

@ -158,6 +158,8 @@ export function createATMMachine(
...initialContext, ...initialContext,
currency: context.currency, currency: context.currency,
inventory: context.inventory, inventory: context.inventory,
cashInFeePercent: context.cashInFeePercent,
cashOutFeePercent: context.cashOutFeePercent,
cashInSessionId: null, cashInSessionId: null,
})), })),
setStartTime: assign({ setStartTime: assign({
@ -165,6 +167,12 @@ export function createATMMachine(
txid: () => generateTxId(), txid: () => generateTxId(),
cashInSessionId: () => generateSessionId(), cashInSessionId: () => generateSessionId(),
}), }),
setCashInFee: assign({
feePercent: ({ context }) => context.cashInFeePercent,
}),
setCashOutFee: assign({
feePercent: ({ context }) => context.cashOutFeePercent,
}),
addBill: assign({ addBill: assign({
billsInserted: ({ context, event }) => { billsInserted: ({ context, event }) => {
if (event.type !== 'BILL_INSERTED') return context.billsInserted if (event.type !== 'BILL_INSERTED') return context.billsInserted
@ -383,11 +391,11 @@ export function createATMMachine(
on: { on: {
SELECT_CASH_IN: { SELECT_CASH_IN: {
target: 'cashIn', target: 'cashIn',
actions: 'setStartTime', actions: ['setStartTime', 'setCashInFee'],
}, },
SELECT_CASH_OUT: { SELECT_CASH_OUT: {
target: 'cashOut', target: 'cashOut',
actions: 'setStartTime', actions: ['setStartTime', 'setCashOutFee'],
}, },
}, },
}, },

View file

@ -31,8 +31,12 @@ export interface ATMContext {
currency: string currency: string
/** Exchange rate (sats per fiat unit) */ /** Exchange rate (sats per fiat unit) */
exchangeRate: number exchangeRate: number
/** Fee percentage (0.02 = 2%) */ /** Fee percentage (0.02 = 2%) — used at runtime for the active flow */
feePercent: number feePercent: number
/** Cash-in commission (0.02 = 2%) */
cashInFeePercent: number
/** Cash-out commission (0.02 = 2%) */
cashOutFeePercent: number
/** ATM's available balance in sats (for limiting cash-in transactions) */ /** ATM's available balance in sats (for limiting cash-in transactions) */
availableBalance: number availableBalance: number
@ -129,7 +133,9 @@ export const initialContext: ATMContext = {
satsAmount: 0, satsAmount: 0,
currency: 'USD', currency: 'USD',
exchangeRate: 0, exchangeRate: 0,
feePercent: 0.02, feePercent: 0.0333,
cashInFeePercent: 0.0333,
cashOutFeePercent: 0.0777,
availableBalance: 0, availableBalance: 0,
invoice: null, invoice: null,
clinkOffer: null, clinkOffer: null,