From 450496bc2f63fc9df12962e13e445b780566a0fa Mon Sep 17 00:00:00 2001 From: James Mealy Date: Fri, 3 May 2024 14:49:16 +0200 Subject: [PATCH] fix: order surplus calculation (#3653) --- .../swap/helpers/__tests__/utils.test.ts | 33 ++++++++++++++++ src/features/swap/helpers/utils.ts | 38 +++++++++++++------ 2 files changed, 60 insertions(+), 11 deletions(-) diff --git a/src/features/swap/helpers/__tests__/utils.test.ts b/src/features/swap/helpers/__tests__/utils.test.ts index 0b1d5b106c..fd1b610eea 100644 --- a/src/features/swap/helpers/__tests__/utils.test.ts +++ b/src/features/swap/helpers/__tests__/utils.test.ts @@ -141,5 +141,38 @@ describe('Swap helpers', () => { expect(result).toEqual('0') }) + + // eslint-disable-next-line no-only-tests/no-only-tests + it('returns the surplus amount for buy orders', () => { + const mockOrder = { + executedSellAmount: '10000000000000000000', //10 + executedBuyAmount: '50', + buyToken: { decimals: 8 }, + sellToken: { decimals: 18 }, + sellAmount: '15000000000000000000', //15 + buyAmount: '5000000000', + kind: 'buy', + } as unknown as SwapOrder + + const result = getSurplusPrice(mockOrder) + + expect(result).toEqual(5) + }) + + it('returns the surplus amount for sell orders', () => { + const mockOrder = { + executedSellAmount: '100000000000000000000', + executedBuyAmount: '10000000000', //100 + buyToken: { decimals: 8 }, + sellToken: { decimals: 18 }, + sellAmount: '100000000000000000000', + buyAmount: '5000000000', //50 + kind: 'sell', + } as unknown as SwapOrder + + const result = getSurplusPrice(mockOrder) + + expect(result).toEqual(50) + }) }) }) diff --git a/src/features/swap/helpers/utils.ts b/src/features/swap/helpers/utils.ts index 3c32b2af40..eef75ce9ee 100644 --- a/src/features/swap/helpers/utils.ts +++ b/src/features/swap/helpers/utils.ts @@ -7,6 +7,15 @@ type Quantity = { decimals: number } +enum OrderKind { + SELL = 'sell', + BUY = 'buy', +} + +function calculateDifference(amountA: string, amountB: string, decimals: number): number { + return asDecimal(BigInt(amountA), decimals) - asDecimal(BigInt(amountB), decimals) +} + function asDecimal(amount: number | bigint, decimals: number): number { return Number(formatUnits(amount, decimals)) } @@ -47,13 +56,20 @@ const calculateRatio = (a: Quantity, b: Quantity) => { return asDecimal(BigInt(a.amount), a.decimals) / asDecimal(BigInt(b.amount), b.decimals) } -export const getSurplusPrice = (order: Pick): number => { - const { executedBuyAmount, buyAmount, buyToken } = order - - const surplus = - asDecimal(BigInt(executedBuyAmount), buyToken.decimals) - asDecimal(BigInt(buyAmount), buyToken.decimals) - - return surplus +export const getSurplusPrice = ( + order: Pick< + SwapOrder, + 'executedBuyAmount' | 'buyAmount' | 'buyToken' | 'executedSellAmount' | 'sellAmount' | 'sellToken' | 'kind' + >, +): number => { + const { kind, executedSellAmount, sellAmount, sellToken, executedBuyAmount, buyAmount, buyToken } = order + if (kind === OrderKind.BUY) { + return calculateDifference(sellAmount, executedSellAmount, sellToken.decimals) + } else if (kind === OrderKind.SELL) { + return calculateDifference(executedBuyAmount, buyAmount, buyToken.decimals) + } else { + return 0 + } } export const getFilledPercentage = ( @@ -62,10 +78,10 @@ export const getFilledPercentage = ( let executed: number let total: number - if (order.kind === 'buy') { + if (order.kind === OrderKind.BUY) { executed = Number(order.executedBuyAmount) total = Number(order.buyAmount) - } else if (order.kind === 'sell') { + } else if (order.kind === OrderKind.SELL) { executed = Number(order.executedSellAmount) total = Number(order.sellAmount) } else { @@ -78,9 +94,9 @@ export const getFilledPercentage = ( export const getFilledAmount = ( order: Pick, ): string => { - if (order.kind === 'buy') { + if (order.kind === OrderKind.BUY) { return formatUnits(order.executedBuyAmount, order.buyToken.decimals) - } else if (order.kind === 'sell') { + } else if (order.kind === OrderKind.SELL) { return formatUnits(order.executedSellAmount, order.sellToken.decimals) } else { return '0'