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: Order tests/type #2593

Merged
merged 1 commit into from
Aug 8, 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
1 change: 1 addition & 0 deletions packages/swap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"publish:prod": "yarn build && npm publish --scope yoroi --access public",
"release": "release-it",
"test": "jest",
"test:watch": "jest --watch",
"tsc": "tsc --noEmit -p tsconfig.json"
},
"commitlint": {
Expand Down
42 changes: 21 additions & 21 deletions packages/swap/src/helpers/order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ describe('getQuantityWithSlippage', () => {
describe('getReceiveAmountbyChangingSell', () => {
it('should calculate the correct receive amount when selling tokenA', () => {
const pool = {
tokenA: {quantity: '1', tokenId: 'tokenA'},
tokenB: {quantity: '1000', tokenId: 'tokenB'},
fee: {quantity: '10', tokenId: ''},
tokenA: {quantity: '4500000', tokenId: 'tokenA'},
tokenB: {quantity: '9000000', tokenId: 'tokenB'},
fee: '0.3', // 0.3%
provider: 'minswap',
price: 2,
batcherFee: {quantity: '1', tokenId: ''},
Expand All @@ -64,17 +64,17 @@ describe('getReceiveAmountbyChangingSell', () => {
}
const result = getReceiveAmountbyChangingSell(pool, sell)
expect(result.sell).toEqual(sell)
expect(result.buy.quantity).toBe('989')
expect(result.buy.quantity).toBe('197')
expect(result.buy.tokenId).toBe('tokenB')
})
})

describe('getSellAmountByChangingReceive', () => {
it('should calculate the correct sell amount when buying tokenA', () => {
const pool = {
tokenA: {quantity: '10', tokenId: 'tokenA'},
tokenB: {quantity: '1000', tokenId: 'tokenB'},
fee: {quantity: '10', tokenId: ''},
tokenA: {quantity: '4500000', tokenId: 'tokenA'},
tokenB: {quantity: '9000000', tokenId: 'tokenB'},
fee: '0.5', // 0.5%
provider: 'minswap',
price: 2,
batcherFee: {quantity: '1', tokenId: ''},
Expand All @@ -87,12 +87,12 @@ describe('getSellAmountByChangingReceive', () => {
},
} as Swap.Pool
const buy = {
quantity: '1' as Balance.Quantity,
quantity: '100' as Balance.Quantity,
tokenId: 'tokenA',
}
const result = getSellAmountByChangingReceive(pool, buy)
expect(result.buy).toEqual(buy)
expect(result.sell.quantity).toBe('126')
expect(result.sell.quantity).toBe('204')
expect(result.sell.tokenId).toBe('tokenB')
})
})
Expand All @@ -108,9 +108,9 @@ describe('makeLimitOrder', () => {
tokenId: 'tokenB',
}
const pool: Swap.Pool = {
tokenA: {quantity: '10', tokenId: 'tokenA'},
tokenB: {quantity: '1000', tokenId: 'tokenB'},
fee: {quantity: '10', tokenId: ''},
tokenA: {quantity: '4500000', tokenId: 'tokenA'},
tokenB: {quantity: '9000000', tokenId: 'tokenB'},
fee: '0.3',
provider: 'minswap',
price: 2,
batcherFee: {quantity: '1', tokenId: ''},
Expand Down Expand Up @@ -143,17 +143,17 @@ describe('makeLimitOrder', () => {
describe('makePossibleMarketOrder', () => {
it('should create a possible market order with the best pool', () => {
const sell = {
quantity: '100' as Balance.Quantity,
quantity: '100' as const,
tokenId: 'tokenA',
}
const buy = {
quantity: '200' as Balance.Quantity,
quantity: '177' as const, // the expected buy quantity becsause makePossibleMarketOrder will ignore the buy quantity
tokenId: 'tokenB',
}
const pool1: Swap.Pool = {
tokenA: {quantity: '10', tokenId: 'tokenA'},
tokenB: {quantity: '1000', tokenId: 'tokenB'},
fee: {quantity: '10', tokenId: ''},
tokenA: {quantity: '4500000', tokenId: 'tokenA'},
tokenB: {quantity: '9000000', tokenId: 'tokenB'},
fee: '0.3',
provider: 'minswap',
price: 2,
batcherFee: {quantity: '1', tokenId: ''},
Expand All @@ -166,9 +166,9 @@ describe('makePossibleMarketOrder', () => {
},
}
const pool2: Swap.Pool = {
tokenA: {quantity: '10', tokenId: 'tokenA'},
tokenB: {quantity: '100', tokenId: 'tokenB'},
fee: {quantity: '10', tokenId: ''},
tokenA: {quantity: '5500000', tokenId: 'tokenA'},
tokenB: {quantity: '9000000', tokenId: 'tokenB'},
fee: '0.3',
provider: 'sundaeswap',
price: 2,
batcherFee: {quantity: '10', tokenId: ''},
Expand All @@ -190,7 +190,7 @@ describe('makePossibleMarketOrder', () => {
expect(result?.poolId).toEqual('0')
expect(result?.slippage).toEqual(slippage)
expect(result?.address).toEqual(address)
expect(result?.amounts.sell).toEqual(sell)
expect(result?.amounts.buy.quantity).toEqual(buy.quantity)
})

it('should return undefined if no pools are provided', () => {
Expand Down
7 changes: 3 additions & 4 deletions packages/swap/src/helpers/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const getReceiveAmountbyChangingSell = (
const fromAmount = BigInt(sell.quantity)

const poolFee = ceilDivision(
BigInt(Number(pool.fee.quantity) * 1000) * fromAmount,
BigInt(Number(pool.fee) * 1000) * fromAmount,
BigInt(100 * 1000),
)

Expand All @@ -38,7 +38,7 @@ export const getReceiveAmountbyChangingSell = (
quantity: getReceiveAmount(poolB, poolA) as Balance.Quantity,
tokenId: pool.tokenA.tokenId,
}

return {sell, buy}
}

Expand All @@ -52,7 +52,7 @@ export const getSellAmountByChangingReceive = (

const toAmount = BigInt(buy.quantity)

const poolFee = BigInt(100 * 1000) - BigInt(Number(pool.fee.quantity) * 1000)
const poolFee = BigInt(100 * 1000) - BigInt(Number(pool.fee) * 1000)

const getSendAmount = (poolA_: bigint, poolB_: bigint) => {
const newPoolA =
Expand Down Expand Up @@ -152,7 +152,6 @@ export const makePossibleMarketOrder = (
return order
}

// TODO: i think it should filter for the tokenA/B here
return pools.reduce(findBestOrder, undefined)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/swap/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type SwapPool = {
| 'muesliswap_v1'
| 'muesliswap_v2'
| 'muesliswap_v3'
fee: BalanceAmount
fee: string // % pool liquidity provider fee, usually 0.3.
tokenA: BalanceAmount
tokenB: BalanceAmount
price: number // float, current price in tokenA / tokenB according to the pool, NOT SUITABLE for price calculations, just for display purposes, i.e. 0.9097362621640215.
Expand Down