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(swap) refactor makePossibleMarketOrder helper #2785

Merged
merged 8 commits into from
Oct 17, 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const supportedProviders: ReadonlyArray<Swap.SupportedProvider> = [
'sundaeswap',
'muesliswap',
'vyfi',
// 'muesliswap_v2', // TODO: enable after more clarification - right now user is not receiving tokens back when choosing this pool
'muesliswap_v2',
] as const

const LiquidityDexList = () => {
Expand Down
2 changes: 1 addition & 1 deletion apps/wallet-mobile/src/features/Swap/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const createYoroiEntry = (
const amountEntry = {}

const sellTokenId = createOrder.amounts.sell.tokenId
// summing fees is missing the frontend fee
// TODO Frontend Fee is not added. Once will be defined needs to be added here
if (sellTokenId === wallet.primaryTokenInfo.id) {
amountEntry[sellTokenId] = Quantities.sum([
createOrder.selectedPool.deposit.quantity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export const CreateOrder = () => {
const orderResult: Swap.CreateOrderData | undefined = makePossibleMarketOrder(
orderDetails.sell,
orderDetails.buy,
orderDetails.pools,
orderDetails.selectedPool,
orderDetails.slippage,
orderDetails.address,
)
Expand Down
2 changes: 1 addition & 1 deletion packages/openswap/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,5 @@ export const supportedProviders: ReadonlyArray<Provider> = [
'sundaeswap',
'vyfi',
'wingriders',
// 'muesliswap_v2' // TODO: enable after more clarification - right now user is not receiving tokens back when choosing this pool
'muesliswap_v2',
] as const
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Balance, Swap} from '@yoroi/types'
import {Swap} from '@yoroi/types'

import {makePossibleMarketOrder} from './makePossibleMarketOrder'

Expand All @@ -12,7 +12,7 @@ describe('makePossibleMarketOrder', () => {
quantity: '177' as const, // the expected buy quantity becsause makePossibleMarketOrder will ignore the buy quantity
tokenId: 'tokenB',
}
const pool1: Swap.Pool = {
const bestPool1: Swap.Pool = {
tokenA: {quantity: '4500000', tokenId: 'tokenA'},
tokenB: {quantity: '9000000', tokenId: 'tokenB'},
ptPriceTokenA: '0',
Expand All @@ -27,47 +27,21 @@ describe('makePossibleMarketOrder', () => {
tokenId: '0',
},
}
const pool2: Swap.Pool = {
tokenA: {quantity: '5500000', tokenId: 'tokenA'},
tokenB: {quantity: '9000000', tokenId: 'tokenB'},
ptPriceTokenA: '0',
ptPriceTokenB: '0',
fee: '0.3',
provider: 'sundaeswap',
batcherFee: {quantity: '10', tokenId: ''},
deposit: {quantity: '1', tokenId: ''},
poolId: '0',
lpToken: {
quantity: '0',
tokenId: '0',
},
}
const pools = [pool1, pool2]

const slippage = 10
const address = '0xAddressHere'

const result = makePossibleMarketOrder(sell, buy, pools, slippage, address)
const result = makePossibleMarketOrder(
sell,
buy,
bestPool1,
slippage,
address,
)

expect(result?.selectedPool).toEqual(pool1)
expect(result?.selectedPool).toEqual(bestPool1)
expect(result?.slippage).toEqual(slippage)
expect(result?.address).toEqual(address)
expect(result?.amounts.buy.quantity).toEqual(buy.quantity)
})

it('should return undefined if no pools are provided', () => {
const sell = {
quantity: '100' as Balance.Quantity,
tokenId: 'tokenA',
}
const buy = {
quantity: '200' as Balance.Quantity,
tokenId: 'tokenB',
}
const pools: Swap.Pool[] = []
const slippage = 10
const address = '0xAddressHere'

const result = makePossibleMarketOrder(sell, buy, pools, slippage, address)
expect(result).toBeUndefined()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,38 @@ import {getQuantityWithSlippage} from '../amounts/getQuantityWithSlippage'
*
* @param sell - The amount to sell.
* @param buy - The desired buy amount.
* @param pools - Array of liquidity pools to choose from.
* @param bestPool - best liquidity pool.
* @param slippage - Maximum acceptable slippage in percentage.
* @param address - The address placing the order.
*
* @returns The best market order data, or undefined if no pool is available.
* @returns The best market order data
*/
export const makePossibleMarketOrder = (
sell: Balance.Amount,
buy: Balance.Amount,
pools: Readonly<Swap.Pool[]>,
bestPool: Readonly<Swap.Pool>,
slippage: number,
address: string,
): Swap.CreateOrderData | undefined => {
if (pools.length === 0) return undefined

const findBestOrder = (
bestOrder: Swap.CreateOrderData | undefined,
currentPool: Swap.Pool,
): Swap.CreateOrderData => {
const rawBuy = getBuyAmount(currentPool, sell)

const buyQuantityWithSlippage = getQuantityWithSlippage(
rawBuy.quantity,
slippage,
)

const newOrder: Swap.CreateOrderData = {
selectedPool: currentPool,
slippage,
amounts: {
sell,
buy: {
tokenId: buy.tokenId,
quantity: buyQuantityWithSlippage,
},
const rawBuy = getBuyAmount(bestPool, sell)

const buyQuantityWithSlippage = getQuantityWithSlippage(
rawBuy.quantity,
slippage,
)

const newOrder: Swap.CreateOrderData = {
selectedPool: bestPool,
slippage,
amounts: {
sell,
buy: {
tokenId: buy.tokenId,
quantity: buyQuantityWithSlippage,
},
address,
}

if (
bestOrder === undefined ||
BigInt(bestOrder.amounts.buy.quantity) <
BigInt(newOrder.amounts.buy.quantity)
) {
return newOrder
}

return bestOrder
},
address,
}

return pools.reduce(findBestOrder, undefined)
return newOrder
}
2 changes: 1 addition & 1 deletion packages/swap/src/translators/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const supportedProviders: ReadonlyArray<Swap.SupportedProvider> = [
'wingriders',
'sundaeswap',
'muesliswap',
// 'muesliswap_v2', // TODO: enable after more clarification - right now user is not receiving tokens back when choosing this pool
'muesliswap_v2',
'vyfi',
] as const

Expand Down