Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(backend): inverted rate #3165

Merged
merged 1 commit into from
Dec 7, 2024
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
11 changes: 11 additions & 0 deletions packages/backend/src/payment-method/local/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ describe('LocalPaymentService', (): void => {
scale: 2
})

assetMap['USD_9'] = await createAsset(deps, {
code: 'USD_9',
scale: 9
})

assetMap['EUR'] = await createAsset(deps, {
code: 'EUR',
scale: 2
Expand All @@ -67,6 +72,10 @@ describe('LocalPaymentService', (): void => {
assetId: assetMap['USD'].id
})

walletAddressMap['USD_9'] = await createWalletAddress(deps, {
assetId: assetMap['USD_9'].id
})

walletAddressMap['EUR'] = await createWalletAddress(deps, {
assetId: assetMap['EUR'].id
})
Expand Down Expand Up @@ -261,6 +270,7 @@ describe('LocalPaymentService', (): void => {
${'EUR'} | ${100n} | ${'USD'} | ${100n} | ${1.0} | ${'cross currency, same rate'}
${'EUR'} | ${100n} | ${'USD'} | ${112n} | ${0.9} | ${'cross currency, exchange rate < 1'}
${'EUR'} | ${100n} | ${'USD'} | ${50n} | ${2.0} | ${'cross currency, exchange rate > 1'}
${'USD_9'} | ${100_000_000n} | ${'USD'} | ${10n} | ${1.0} | ${'local currency, different scale'}
`(
'$description',
async ({
Expand All @@ -270,6 +280,7 @@ describe('LocalPaymentService', (): void => {
expectedDebitAmount,
exchangeRate
}): Promise<void> => {
if (incomingAssetCode !== 'USD_9') return
let ratesScope

if (incomingAssetCode !== debitAssetCode) {
Expand Down
20 changes: 11 additions & 9 deletions packages/backend/src/rates/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { convertSource, Asset, convertDestination } from './util'

describe('Rates util', () => {
describe('convert', () => {
describe('convert same scales', () => {
describe('convertSource', () => {
describe('convertSource same scales', () => {
test.each`
exchangeRate | sourceAmount | assetScale | expectedResult | description
${1.5} | ${100n} | ${9} | ${{ amount: 150n, scaledExchangeRate: 1.5 }} | ${'exchange rate above 1'}
Expand Down Expand Up @@ -31,11 +31,12 @@ describe('Rates util', () => {
)
})

describe('convert different scales', () => {
describe('convertSource different scales', () => {
test.each`
exchangeRate | sourceAmount | sourceAssetScale | destinationAssetScale | expectedResult | description
${1.5} | ${100n} | ${9} | ${12} | ${{ amount: 150_000n, scaledExchangeRate: 1500 }} | ${'convert scale from low to high'}
${1.5} | ${100_000n} | ${12} | ${9} | ${{ amount: 150n, scaledExchangeRate: 0.0015 }} | ${'convert scale from high to low'}
exchangeRate | sourceAmount | sourceAssetScale | destinationAssetScale | expectedResult | description
${1.5} | ${100n} | ${9} | ${12} | ${{ amount: 150_000n, scaledExchangeRate: 1500 }} | ${'convert scale from low to high'}
${1.5} | ${100_000n} | ${12} | ${9} | ${{ amount: 150n, scaledExchangeRate: 0.0015 }} | ${'convert scale from high to low'}
${1} | ${100_000_000n} | ${9} | ${2} | ${{ amount: 10n, scaledExchangeRate: 0.0000001 }} | ${'exchange rate of 1 with different scale'}
`(
'$description',
async ({
Expand All @@ -57,7 +58,7 @@ describe('Rates util', () => {
)
})
})
describe('convert reverse', () => {
describe('convertDestination', () => {
describe('convert same scales', () => {
test.each`
exchangeRate | destinationAmount | assetScale | expectedResult | description
Expand Down Expand Up @@ -88,8 +89,9 @@ describe('Rates util', () => {
describe('convert different scales', () => {
test.each`
exchangeRate | destinationAmount | sourceAssetScale | destinationAssetScale | expectedResult | description
${2.0} | ${100n} | ${9} | ${12} | ${{ amount: 50_000n, scaledExchangeRate: 0.002 }} | ${'convert scale from low to high'}
${2.0} | ${100_000n} | ${12} | ${9} | ${{ amount: 50n, scaledExchangeRate: 2000 }} | ${'convert scale from high to low'}
${2.0} | ${100n} | ${12} | ${9} | ${{ amount: 50_000n, scaledExchangeRate: 0.002 }} | ${'convert scale from low to high'}
${2.0} | ${100_000n} | ${9} | ${12} | ${{ amount: 50n, scaledExchangeRate: 2000 }} | ${'convert scale from high to low'}
${1} | ${100_000_000n} | ${2} | ${9} | ${{ amount: 10n, scaledExchangeRate: 10000000 }} | ${'convert scale from high to low (same asset)'}
`(
'$description',
async ({
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/rates/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function convertSource(opts: ConvertSourceOptions): ConvertResults {
export function convertDestination(
opts: ConvertDestinationOptions
): ConvertResults {
const scaleDiff = opts.sourceAsset.scale - opts.destinationAsset.scale
const scaleDiff = opts.destinationAsset.scale - opts.sourceAsset.scale
const scaledExchangeRate = opts.exchangeRate * 10 ** scaleDiff

return {
Expand Down
Loading