Skip to content

Commit 5c21c94

Browse files
authored
fix(backend): inverted rate (#3165)
1 parent 6ef0a06 commit 5c21c94

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

packages/backend/src/payment-method/local/service.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ describe('LocalPaymentService', (): void => {
5858
scale: 2
5959
})
6060

61+
assetMap['USD_9'] = await createAsset(deps, {
62+
code: 'USD_9',
63+
scale: 9
64+
})
65+
6166
assetMap['EUR'] = await createAsset(deps, {
6267
code: 'EUR',
6368
scale: 2
@@ -67,6 +72,10 @@ describe('LocalPaymentService', (): void => {
6772
assetId: assetMap['USD'].id
6873
})
6974

75+
walletAddressMap['USD_9'] = await createWalletAddress(deps, {
76+
assetId: assetMap['USD_9'].id
77+
})
78+
7079
walletAddressMap['EUR'] = await createWalletAddress(deps, {
7180
assetId: assetMap['EUR'].id
7281
})
@@ -261,6 +270,7 @@ describe('LocalPaymentService', (): void => {
261270
${'EUR'} | ${100n} | ${'USD'} | ${100n} | ${1.0} | ${'cross currency, same rate'}
262271
${'EUR'} | ${100n} | ${'USD'} | ${112n} | ${0.9} | ${'cross currency, exchange rate < 1'}
263272
${'EUR'} | ${100n} | ${'USD'} | ${50n} | ${2.0} | ${'cross currency, exchange rate > 1'}
273+
${'USD_9'} | ${100_000_000n} | ${'USD'} | ${10n} | ${1.0} | ${'local currency, different scale'}
264274
`(
265275
'$description',
266276
async ({
@@ -270,6 +280,7 @@ describe('LocalPaymentService', (): void => {
270280
expectedDebitAmount,
271281
exchangeRate
272282
}): Promise<void> => {
283+
if (incomingAssetCode !== 'USD_9') return
273284
let ratesScope
274285

275286
if (incomingAssetCode !== debitAssetCode) {

packages/backend/src/rates/util.test.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { convertSource, Asset, convertDestination } from './util'
22

33
describe('Rates util', () => {
4-
describe('convert', () => {
5-
describe('convert same scales', () => {
4+
describe('convertSource', () => {
5+
describe('convertSource same scales', () => {
66
test.each`
77
exchangeRate | sourceAmount | assetScale | expectedResult | description
88
${1.5} | ${100n} | ${9} | ${{ amount: 150n, scaledExchangeRate: 1.5 }} | ${'exchange rate above 1'}
@@ -31,11 +31,12 @@ describe('Rates util', () => {
3131
)
3232
})
3333

34-
describe('convert different scales', () => {
34+
describe('convertSource different scales', () => {
3535
test.each`
36-
exchangeRate | sourceAmount | sourceAssetScale | destinationAssetScale | expectedResult | description
37-
${1.5} | ${100n} | ${9} | ${12} | ${{ amount: 150_000n, scaledExchangeRate: 1500 }} | ${'convert scale from low to high'}
38-
${1.5} | ${100_000n} | ${12} | ${9} | ${{ amount: 150n, scaledExchangeRate: 0.0015 }} | ${'convert scale from high to low'}
36+
exchangeRate | sourceAmount | sourceAssetScale | destinationAssetScale | expectedResult | description
37+
${1.5} | ${100n} | ${9} | ${12} | ${{ amount: 150_000n, scaledExchangeRate: 1500 }} | ${'convert scale from low to high'}
38+
${1.5} | ${100_000n} | ${12} | ${9} | ${{ amount: 150n, scaledExchangeRate: 0.0015 }} | ${'convert scale from high to low'}
39+
${1} | ${100_000_000n} | ${9} | ${2} | ${{ amount: 10n, scaledExchangeRate: 0.0000001 }} | ${'exchange rate of 1 with different scale'}
3940
`(
4041
'$description',
4142
async ({
@@ -57,7 +58,7 @@ describe('Rates util', () => {
5758
)
5859
})
5960
})
60-
describe('convert reverse', () => {
61+
describe('convertDestination', () => {
6162
describe('convert same scales', () => {
6263
test.each`
6364
exchangeRate | destinationAmount | assetScale | expectedResult | description
@@ -88,8 +89,9 @@ describe('Rates util', () => {
8889
describe('convert different scales', () => {
8990
test.each`
9091
exchangeRate | destinationAmount | sourceAssetScale | destinationAssetScale | expectedResult | description
91-
${2.0} | ${100n} | ${9} | ${12} | ${{ amount: 50_000n, scaledExchangeRate: 0.002 }} | ${'convert scale from low to high'}
92-
${2.0} | ${100_000n} | ${12} | ${9} | ${{ amount: 50n, scaledExchangeRate: 2000 }} | ${'convert scale from high to low'}
92+
${2.0} | ${100n} | ${12} | ${9} | ${{ amount: 50_000n, scaledExchangeRate: 0.002 }} | ${'convert scale from low to high'}
93+
${2.0} | ${100_000n} | ${9} | ${12} | ${{ amount: 50n, scaledExchangeRate: 2000 }} | ${'convert scale from high to low'}
94+
${1} | ${100_000_000n} | ${2} | ${9} | ${{ amount: 10n, scaledExchangeRate: 10000000 }} | ${'convert scale from high to low (same asset)'}
9395
`(
9496
'$description',
9597
async ({

packages/backend/src/rates/util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function convertSource(opts: ConvertSourceOptions): ConvertResults {
3636
export function convertDestination(
3737
opts: ConvertDestinationOptions
3838
): ConvertResults {
39-
const scaleDiff = opts.sourceAsset.scale - opts.destinationAsset.scale
39+
const scaleDiff = opts.destinationAsset.scale - opts.sourceAsset.scale
4040
const scaledExchangeRate = opts.exchangeRate * 10 ** scaleDiff
4141

4242
return {

0 commit comments

Comments
 (0)