Skip to content

Commit

Permalink
chore: added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stackchain committed Oct 20, 2023
1 parent 3dcace8 commit e57ed14
Show file tree
Hide file tree
Showing 4 changed files with 448 additions and 2 deletions.
29 changes: 28 additions & 1 deletion packages/swap/src/helpers/pools/getBestBuyPool.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Balance} from '@yoroi/types'
import {Balance, Swap} from '@yoroi/types'

import {getBestBuyPool} from './getBestBuyPool'
import {getBuyAmount} from '../orders/amounts/getBuyAmount'
Expand Down Expand Up @@ -48,6 +48,33 @@ describe('getBestBuyPool', () => {
expect(getBestBuyPool([mocks.mockedPools1[0]!], sell)).toBeUndefined()
})

it('should return undefined if the buy quantity turns to be 0', () => {
const pools: Array<Swap.Pool> = [
{
tokenA: {quantity: '1000000000000000000000', tokenId: 'tokenA'},
tokenB: {quantity: '1', tokenId: 'tokenB'},
ptPriceTokenA: '1',
ptPriceTokenB: '0.0695404765',
fee: '0.3', // 0.3%
provider: 'muesliswap_v2',
batcherFee: {quantity: '950000', tokenId: ''},
deposit: {quantity: '2000000', tokenId: ''},
poolId: '1',
lpToken: {
quantity: '0',
tokenId: '0',
},
},
]

const sell: Balance.Amount = {
quantity: '1',
tokenId: 'tokenA',
}

expect(getBestBuyPool(pools, sell)).toBeUndefined()
})

it('should return undefined if pools list is empty', () => {
const sell: Balance.Amount = {
quantity: '1',
Expand Down
107 changes: 107 additions & 0 deletions packages/swap/src/helpers/pools/getBestPoolCalculation.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {mocks} from '../mocks'
import {SwapOrderCalculation} from '../../translators/reactjs/state/state'
import {getBestPoolCalculation} from './getBestPoolCalculation'

describe('getBestPoolCalculation', () => {
Expand All @@ -9,4 +10,110 @@ describe('getBestPoolCalculation', () => {

expect(bestCalculation?.pool.poolId).toBe('3')
})

it('should skip no supply pools', () => {
const calculations: ReadonlyArray<SwapOrderCalculation> = [
{
order: {
side: 'buy',
slippage: 10,
orderType: 'market',
amounts: {
sell: {
quantity: '0',
tokenId: 'tokenA',
},
buy: {
quantity: '100000001',
tokenId: 'tokenB',
},
},
},
sides: {
buy: {
quantity: '100000001',
tokenId: 'tokenB',
},
sell: {
quantity: '7157210',
tokenId: 'tokenA',
},
},
cost: {
ptTotalFeeNoFEF: {
quantity: '4500000',
tokenId: '',
},
ptTotalFee: {
quantity: '4500000',
tokenId: '',
},
batcherFee: {
quantity: '2500000',
tokenId: '',
},
deposit: {
quantity: '2000000',
tokenId: '',
},
frontendFeeInfo: {
fee: {
tokenId: '',
quantity: '0',
},
},
liquidityFee: {
tokenId: 'tokenA',
quantity: '3579',
},
},
buyAmountWithSlippage: {
quantity: '90000000',
tokenId: 'tokenB',
},
hasSupply: false,
prices: {
base: '0.07101454479564951518',
market: '0.07101454479564951518',
withSlippage: '0.07952455555555555556',
withFees: '0.09657209903427900966',
withFeesAndSlippage: '0.10730233333333333333',
difference: '35.989182655713084861',
withFeesNoFEF: '0.09657209903427900966',
withFeesAndSlippageNoFEF: '0.10730233333333333333',
differenceNoFEF: '35.989182655713084861',
},
pool: {
tokenA: {
quantity: '973669994',
tokenId: 'tokenA',
},
tokenB: {
quantity: '13710853133',
tokenId: 'tokenB',
},
ptPriceTokenA: '1',
ptPriceTokenB: '0.0695404765',
fee: '0.05',
provider: 'sundaeswap',
batcherFee: {
quantity: '2500000',
tokenId: '',
},
deposit: {
quantity: '2000000',
tokenId: '',
},
poolId: '6',
lpToken: {
quantity: '0',
tokenId: '0',
},
},
},
]
const bestCalculation = getBestPoolCalculation(calculations)

expect(bestCalculation).toBeUndefined()
})
})
29 changes: 28 additions & 1 deletion packages/swap/src/helpers/pools/getBestSellPool.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Balance} from '@yoroi/types'
import {Balance, Swap} from '@yoroi/types'

import {getBestSellPool} from './getBestSellPool'
import {getSellAmount} from '../orders/amounts/getSellAmount'
Expand Down Expand Up @@ -50,6 +50,33 @@ describe('getBestSellPool', () => {
expect(getBestSellPool([mocks.mockedPools4[0]!], sell)).toBeUndefined()
})

it('should return undefined if the sell quantity turns to be 0', () => {
const pools: Array<Swap.Pool> = [
{
tokenA: {quantity: '0', tokenId: 'tokenA'},
tokenB: {quantity: '1', tokenId: 'tokenB'},
ptPriceTokenA: '1',
ptPriceTokenB: '0.0695404765',
fee: '0.3', // 0.3%
provider: 'muesliswap_v2',
batcherFee: {quantity: '950000', tokenId: ''},
deposit: {quantity: '2000000', tokenId: ''},
poolId: '1',
lpToken: {
quantity: '0',
tokenId: '0',
},
},
]

const buy: Balance.Amount = {
quantity: '1000000000000000',
tokenId: 'tokenA',
}

expect(getBestSellPool(pools, buy)).toBeUndefined()
})

it('should return undefined if pools list is empty', () => {
const sell: Balance.Amount = {
quantity: '1',
Expand Down
Loading

0 comments on commit e57ed14

Please sign in to comment.