Skip to content

Commit

Permalink
Fix: use correct values for swap order surplus calculation (#3628)
Browse files Browse the repository at this point in the history
* fix: order surplus calculation

* use enum for order kind

* keep order consistent

* feat: add unit tests and extract calculateDifference into a funciton
  • Loading branch information
jmealy authored May 2, 2024
1 parent 12a3b41 commit eb05e1e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
33 changes: 33 additions & 0 deletions src/features/swap/helpers/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
38 changes: 27 additions & 11 deletions src/features/swap/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ type Quantity = {
decimals: number
}

enum OrderKind {
SELL = 'sell',
BUY = 'buy',
}

function asDecimal(amount: number | bigint, decimals: number): number {
return Number(formatUnits(amount, decimals))
}

function calculateDifference(amountA: string, amountB: string, decimals: number): number {
return asDecimal(BigInt(amountA), decimals) - asDecimal(BigInt(amountB), decimals)
}

export const getExecutionPrice = (
order: Pick<SwapOrder, 'executedSellAmount' | 'executedBuyAmount' | 'buyToken' | 'sellToken'>,
): number => {
Expand Down Expand Up @@ -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<SwapOrder, 'executedBuyAmount' | 'buyAmount' | 'buyToken'>): 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 = (
Expand All @@ -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 {
Expand All @@ -78,9 +94,9 @@ export const getFilledPercentage = (
export const getFilledAmount = (
order: Pick<SwapOrder, 'kind' | 'executedBuyAmount' | 'executedSellAmount' | 'buyToken' | 'sellToken'>,
): 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'
Expand Down

0 comments on commit eb05e1e

Please sign in to comment.