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: use correct values for swap order surplus calculation #3653

Merged
merged 1 commit into from
May 3, 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
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,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))
}
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
Loading