fix: correct bill denomination unit conversion and exchange rate
- Fix state machine to convert bill denominations from dollars to cents (e.g., $20 bill → 2000 cents in fiatAmount) - Use fixed 1,000 sats/$ rate for development (~$100k BTC) - Update test to expect correct cents value Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
5ef43fb036
commit
940afe3a65
4 changed files with 8 additions and 14 deletions
|
|
@ -828,19 +828,12 @@ function createATMServices(
|
|||
|
||||
/**
|
||||
* Get current exchange rate
|
||||
* TODO: Fetch from Lightning.Pub or external price feed
|
||||
*/
|
||||
getExchangeRate: async (currency: string): Promise<number> => {
|
||||
console.log('[ATM Service] Fetching exchange rate for', currency)
|
||||
|
||||
try {
|
||||
const rate = await lightningPub.getExchangeRate(currency)
|
||||
console.log('[ATM Service] Exchange rate:', rate.satsPerUnit, 'sats/' + currency)
|
||||
return rate.satsPerUnit
|
||||
} catch (error) {
|
||||
console.warn('[ATM Service] Failed to fetch rate, using fallback:', error)
|
||||
// Fallback rate for development
|
||||
return 2500 // sats per USD (~$40k BTC)
|
||||
}
|
||||
console.log('[ATM Service] Using fixed exchange rate for', currency)
|
||||
// Fixed rate for development/testing
|
||||
return 1000 // sats per USD (~$100k BTC)
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ const mockServices: ATMServices = {
|
|||
|
||||
getExchangeRate: async (currency) => {
|
||||
console.log('[Mock] Fetching exchange rate for', currency)
|
||||
return 2500
|
||||
return 1000 // 1000 sats per USD (~$100k BTC)
|
||||
},
|
||||
|
||||
sendNostrReceipt: async (context) => {
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ describe('ATM State Machine', () => {
|
|||
|
||||
const context = actor.getSnapshot().context
|
||||
expect(context.billsInserted).toEqual([20, 10])
|
||||
expect(context.fiatAmount).toBe(30) // $30 in cents? No, as dollars
|
||||
expect(context.fiatAmount).toBe(3000) // $30 in cents
|
||||
})
|
||||
|
||||
it('should return to idle on CANCEL', async () => {
|
||||
|
|
|
|||
|
|
@ -145,7 +145,8 @@ export function createATMMachine(services: Partial<ATMServices> = {}) {
|
|||
},
|
||||
fiatAmount: ({ context, event }) => {
|
||||
if (event.type !== 'BILL_INSERTED') return context.fiatAmount
|
||||
return context.fiatAmount + event.denomination
|
||||
// denomination is in dollars, fiatAmount is in cents
|
||||
return context.fiatAmount + event.denomination * 100
|
||||
},
|
||||
}),
|
||||
calculateSats: assign({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue