From 5c21c94b46435d7653d9f7d5ff5b4c1a03cf99b8 Mon Sep 17 00:00:00 2001 From: Blair Currey <12960453+BlairCurrey@users.noreply.github.com> Date: Fri, 6 Dec 2024 21:53:33 -0500 Subject: [PATCH] fix(backend): inverted rate (#3165) --- .../src/payment-method/local/service.test.ts | 11 ++++++++++ packages/backend/src/rates/util.test.ts | 20 ++++++++++--------- packages/backend/src/rates/util.ts | 2 +- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/packages/backend/src/payment-method/local/service.test.ts b/packages/backend/src/payment-method/local/service.test.ts index 94cff4100e..72196b298d 100644 --- a/packages/backend/src/payment-method/local/service.test.ts +++ b/packages/backend/src/payment-method/local/service.test.ts @@ -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 @@ -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 }) @@ -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 ({ @@ -270,6 +280,7 @@ describe('LocalPaymentService', (): void => { expectedDebitAmount, exchangeRate }): Promise => { + if (incomingAssetCode !== 'USD_9') return let ratesScope if (incomingAssetCode !== debitAssetCode) { diff --git a/packages/backend/src/rates/util.test.ts b/packages/backend/src/rates/util.test.ts index 129f9a4631..f4e28f40da 100644 --- a/packages/backend/src/rates/util.test.ts +++ b/packages/backend/src/rates/util.test.ts @@ -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'} @@ -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 ({ @@ -57,7 +58,7 @@ describe('Rates util', () => { ) }) }) - describe('convert reverse', () => { + describe('convertDestination', () => { describe('convert same scales', () => { test.each` exchangeRate | destinationAmount | assetScale | expectedResult | description @@ -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 ({ diff --git a/packages/backend/src/rates/util.ts b/packages/backend/src/rates/util.ts index 5f5d35c3da..d6d789bebf 100644 --- a/packages/backend/src/rates/util.ts +++ b/packages/backend/src/rates/util.ts @@ -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 {