Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion packages/backend/src/payment-method/ilp/service.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { IlpPaymentService, retryableIlpErrors } from './service'
import {
IlpPaymentService,
calculateMinSendAmount,
retryableIlpErrors
} from './service'
import { initIocContainer } from '../../'
import { createTestApp, TestContainer } from '../../tests/app'
import { IAppConfig, Config } from '../../config/app'
Expand Down Expand Up @@ -972,4 +976,32 @@ describe('IlpPaymentService', (): void => {
}
})
})

describe('calculateMinSendAmount', (): void => {
test('returns reciprocal of highEstimatedExchangeRate', async (): Promise<void> => {
expect(
calculateMinSendAmount({
highEstimatedExchangeRate: Pay.Ratio.from(0.05)
} as Pay.Quote)
).toBe(20n)
expect(
calculateMinSendAmount({
highEstimatedExchangeRate: Pay.Ratio.from(0.01)
} as Pay.Quote)
).toBe(100n)
})

test('returns at least 2 even if highEstimatedExchangeRate reciprocal under 2', async (): Promise<void> => {
expect(
calculateMinSendAmount({
highEstimatedExchangeRate: Pay.Ratio.from(1)
} as Pay.Quote)
).toBe(2n)
expect(
calculateMinSendAmount({
highEstimatedExchangeRate: Pay.Ratio.from(20)
} as Pay.Quote)
).toBe(2n)
})
})
})
20 changes: 11 additions & 9 deletions packages/backend/src/payment-method/ilp/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ async function getQuote(
retryable: false,
code: PaymentMethodHandlerErrorCode.QuoteNonPositiveReceiveAmount,
details: {
minSendAmount: BigInt(
Math.ceil(ilpQuote.highEstimatedExchangeRate.reciprocal().valueOf())
)
minSendAmount: calculateMinSendAmount(ilpQuote)
}
})
}
Expand Down Expand Up @@ -172,9 +170,7 @@ async function getQuote(
retryable: false,
code: PaymentMethodHandlerErrorCode.QuoteNonPositiveReceiveAmount,
details: {
minSendAmount: BigInt(
Math.ceil(ilpQuote.highEstimatedExchangeRate.reciprocal().valueOf())
)
minSendAmount: calculateMinSendAmount(ilpQuote)
}
})
}
Expand Down Expand Up @@ -205,9 +201,7 @@ async function getQuote(
retryable: false,
code: PaymentMethodHandlerErrorCode.QuoteNonPositiveReceiveAmount,
details: {
minSendAmount: BigInt(
Math.ceil(ilpQuote.highEstimatedExchangeRate.reciprocal().valueOf())
)
minSendAmount: calculateMinSendAmount(ilpQuote)
}
})
}
Expand Down Expand Up @@ -392,6 +386,14 @@ async function pay(
}
}

export function calculateMinSendAmount(quote: Pay.Quote): bigint {
const minSendAmount = Math.ceil(
quote.highEstimatedExchangeRate.reciprocal().valueOf()
)

return BigInt(Math.max(minSendAmount, 2)) // because of rounding in ILP pay, you must always send at least 2 units of an asset
}

export function canRetryError(err: Error | Pay.PaymentError): boolean {
return err instanceof Error || !!retryableIlpErrors[err]
}
Expand Down
17 changes: 15 additions & 2 deletions packages/backend/src/payment-method/local/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,14 @@ describe('LocalPaymentService', (): void => {
})

test('fails if debit amount is non-positive', async (): Promise<void> => {
expect.assertions(4)
jest
.spyOn(await deps.use('ratesService'), 'convertDestination')
.mockImplementation(() =>
Promise.resolve({ amount: 0n, scaledExchangeRate: 1 })
)

expect.assertions(5)

try {
await localPaymentService.getQuote({
walletAddress: walletAddressMap['USD'],
Expand All @@ -205,6 +212,9 @@ describe('LocalPaymentService', (): void => {
'debit amount of local quote is non-positive'
)
expect((err as PaymentMethodHandlerError).retryable).toBe(false)
expect((err as PaymentMethodHandlerError).details).toEqual({
minSendAmount: 1n
})
}
})
test('fails if receive amount is non-positive', async (): Promise<void> => {
Expand All @@ -214,7 +224,7 @@ describe('LocalPaymentService', (): void => {
.mockImplementation(() =>
Promise.resolve({ amount: 100n, scaledExchangeRate: 1 })
)
expect.assertions(4)
expect.assertions(5)
try {
await localPaymentService.getQuote({
walletAddress: walletAddressMap['USD'],
Expand All @@ -234,6 +244,9 @@ describe('LocalPaymentService', (): void => {
'receive amount of local quote is non-positive'
)
expect((err as PaymentMethodHandlerError).retryable).toBe(false)
expect((err as PaymentMethodHandlerError).details).toEqual({
minSendAmount: 1n
})
}
})

Expand Down
6 changes: 5 additions & 1 deletion packages/backend/src/payment-method/local/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ async function getQuote(
if (debitAmountValue <= BigInt(0)) {
throw new PaymentMethodHandlerError('Received error during local quoting', {
description: 'debit amount of local quote is non-positive',
retryable: false
retryable: false,
code: PaymentMethodHandlerErrorCode.QuoteNonPositiveReceiveAmount,
details: {
minSendAmount: BigInt(Math.ceil(1 / exchangeRate))
}
})
}

Expand Down
Loading