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

chore: Added tests #2815

Merged
merged 2 commits into from
Oct 22, 2023
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
8 changes: 4 additions & 4 deletions packages/swap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@
],
"coverageThreshold": {
"global": {
"branches": 60,
"functions": 60,
"lines": 60,
"statements": 60
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
},
"modulePathIgnorePatterns": [
Expand Down
68 changes: 68 additions & 0 deletions packages/swap/src/helpers/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,72 @@ const mockedPools5: Swap.Pool[] = [
},
]

const mockedPools6: Swap.Pool[] = [
{
tokenA: {quantity: '100', tokenId: 'tokenA'},
tokenB: {quantity: '200', tokenId: 'tokenB'},
ptPriceTokenA: '1',
ptPriceTokenB: '0.5',
fee: '0',
provider: 'vyfi',
batcherFee: {quantity: '0', tokenId: ''},
deposit: {quantity: '0', tokenId: ''},
poolId: '0',
lpToken: {
quantity: '0',
tokenId: 'no.token',
},
},
{
tokenB: {quantity: '100', tokenId: 'tokenB'},
tokenA: {quantity: '200', tokenId: 'tokenA'},
ptPriceTokenB: '0.5',
ptPriceTokenA: '1',
fee: '0',
provider: 'minswap',
batcherFee: {quantity: '0', tokenId: ''},
deposit: {quantity: '0', tokenId: ''},
poolId: '1',
lpToken: {
quantity: '0',
tokenId: '0',
},
},
]

const mockedPools7: Swap.Pool[] = [
{
tokenA: {quantity: '100', tokenId: 'tokenA'},
tokenB: {quantity: '200', tokenId: 'tokenB'},
ptPriceTokenA: '0',
ptPriceTokenB: '1',
fee: '0',
provider: 'vyfi',
batcherFee: {quantity: '0', tokenId: ''},
deposit: {quantity: '0', tokenId: ''},
poolId: '0',
lpToken: {
quantity: '0',
tokenId: 'no.token',
},
},
{
tokenB: {quantity: '100', tokenId: 'tokenB'},
tokenA: {quantity: '200', tokenId: 'tokenA'},
ptPriceTokenB: '0',
ptPriceTokenA: '1',
fee: '0',
provider: 'minswap',
batcherFee: {quantity: '0', tokenId: ''},
deposit: {quantity: '0', tokenId: ''},
poolId: '1',
lpToken: {
quantity: '0',
tokenId: '0',
},
},
]

const mockedOrderCalculations1: SwapOrderCalculation[] = [
{
order: {
Expand Down Expand Up @@ -1723,6 +1789,8 @@ export const mocks = {
mockedPools3,
mockedPools4,
mockedPools5,
mockedPools6,
mockedPools7,

mockedOrderCalculations1,
mockedOrderCalculations2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2903,4 +2903,112 @@ describe('makeOrderCalculations', () => {
pool: pools[0],
} as SwapOrderCalculation)
})
it('should calculate all fees and amounts correctly (case 27, zero pt sell side)', () => {
const pool: Swap.Pool = mocks.mockedPools7[1]!
const pools = [pool]
const amounts = {
sell: {
quantity: '1',
tokenId: 'tokenB',
} as Balance.Amount,
buy: {
quantity: '0',
tokenId: 'tokenA',
} as Balance.Amount,
}
const slippage = 0
const calculations = makeOrderCalculations({
orderType: 'market',
amounts: amounts,
limitPrice: undefined,
slippage: slippage,
pools: pools,
primaryTokenId: '',
lpTokenHeld: {
quantity: '50',
tokenId: 'tokenX',
},
side: 'buy',
frontendFeeTiers,
})
const expectedCalculation: SwapOrderCalculation = {
order: {
side: 'buy',
slippage: 0,
orderType: 'market',
limitPrice: undefined,
amounts: {
sell: {
quantity: '1',
tokenId: 'tokenB',
},
buy: {
quantity: '0',
tokenId: 'tokenA',
},
},
lpTokenHeld: {
quantity: '50',
tokenId: 'tokenX',
},
},
sides: {
sell: {
quantity: '0',
tokenId: 'tokenB',
},
buy: {
quantity: '0',
tokenId: 'tokenA',
},
},
cost: {
batcherFee: {
quantity: '0',
tokenId: '',
},
deposit: {
quantity: '0',
tokenId: '',
},
frontendFeeInfo: {
discountTier: undefined,
fee: {
quantity: '0',
tokenId: '',
},
},
liquidityFee: {
quantity: '0',
tokenId: 'tokenB',
},
ptTotalFeeNoFEF: {
tokenId: '',
quantity: '0',
},
ptTotalFee: {
tokenId: '',
quantity: '0',
},
},
buyAmountWithSlippage: {
quantity: '0',
tokenId: 'tokenA',
},
hasSupply: true,
prices: {
base: '0.5',
market: '0.5',
withSlippage: '0',
withFees: '0',
withFeesAndSlippage: '0',
difference: '-100',
withFeesNoFEF: '0',
withFeesAndSlippageNoFEF: '0',
differenceNoFEF: '-100',
},
pool: pool,
}
expect(calculations[0]).toStrictEqual(expectedCalculation)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ export const makeOrderCalculations = ({
(!Quantities.isZero(buy.quantity) || !Quantities.isZero(sell.quantity)) &&
Quantities.isZero(poolSupply)
const hasSupply =
!Quantities.isGreaterThan(buy.quantity, poolSupply ?? Quantities.zero) &&
!supplyRequired
!Quantities.isGreaterThan(buy.quantity, poolSupply) && !supplyRequired

// lf is sell side % of quantity ie. XToken 100 * 1% = 1 XToken
const liquidityFee: Balance.Amount = getLiquidityProviderFee(pool.fee, sell)
Expand Down
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()
})
})
Loading