From 535de539cb9863cd0d1784212a54a7141cc5c994 Mon Sep 17 00:00:00 2001 From: Sorin Chis Date: Tue, 17 Oct 2023 10:07:44 +0300 Subject: [PATCH] Fix(swap) refactor makePossibleMarketOrder helper (#2785) --- .../LiquidityPool/LiquidityPool.stories.tsx | 2 +- .../src/features/Swap/common/helpers.ts | 2 +- .../CreateOrder/CreateOrder.tsx | 2 +- packages/openswap/src/api.ts | 2 +- .../factories/makePossibleMarketOrder.test.ts | 48 ++++----------- .../factories/makePossibleMarketOrder.ts | 59 +++++++------------ packages/swap/src/translators/constants.ts | 2 +- 7 files changed, 37 insertions(+), 80 deletions(-) diff --git a/apps/wallet-mobile/src/features/Swap/common/LiquidityPool/LiquidityPool.stories.tsx b/apps/wallet-mobile/src/features/Swap/common/LiquidityPool/LiquidityPool.stories.tsx index 35803180ba..2cea3aaa46 100644 --- a/apps/wallet-mobile/src/features/Swap/common/LiquidityPool/LiquidityPool.stories.tsx +++ b/apps/wallet-mobile/src/features/Swap/common/LiquidityPool/LiquidityPool.stories.tsx @@ -14,7 +14,7 @@ const supportedProviders: ReadonlyArray = [ '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 = () => { diff --git a/apps/wallet-mobile/src/features/Swap/common/helpers.ts b/apps/wallet-mobile/src/features/Swap/common/helpers.ts index c4e7221e0d..2bb7ce638d 100644 --- a/apps/wallet-mobile/src/features/Swap/common/helpers.ts +++ b/apps/wallet-mobile/src/features/Swap/common/helpers.ts @@ -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, diff --git a/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/CreateOrder.tsx b/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/CreateOrder.tsx index 8ff572eb3a..79c0b4a74f 100644 --- a/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/CreateOrder.tsx +++ b/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/CreateOrder.tsx @@ -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, ) diff --git a/packages/openswap/src/api.ts b/packages/openswap/src/api.ts index 8dc1c8d5f3..ed83aedd86 100644 --- a/packages/openswap/src/api.ts +++ b/packages/openswap/src/api.ts @@ -119,5 +119,5 @@ export const supportedProviders: ReadonlyArray = [ '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 diff --git a/packages/swap/src/helpers/orders/factories/makePossibleMarketOrder.test.ts b/packages/swap/src/helpers/orders/factories/makePossibleMarketOrder.test.ts index 849f789b43..260acf5622 100644 --- a/packages/swap/src/helpers/orders/factories/makePossibleMarketOrder.test.ts +++ b/packages/swap/src/helpers/orders/factories/makePossibleMarketOrder.test.ts @@ -1,4 +1,4 @@ -import {Balance, Swap} from '@yoroi/types' +import {Swap} from '@yoroi/types' import {makePossibleMarketOrder} from './makePossibleMarketOrder' @@ -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', @@ -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() - }) }) diff --git a/packages/swap/src/helpers/orders/factories/makePossibleMarketOrder.ts b/packages/swap/src/helpers/orders/factories/makePossibleMarketOrder.ts index e5dccb5e4a..b46c4e7ffd 100644 --- a/packages/swap/src/helpers/orders/factories/makePossibleMarketOrder.ts +++ b/packages/swap/src/helpers/orders/factories/makePossibleMarketOrder.ts @@ -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, + bestPool: Readonly, 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 } diff --git a/packages/swap/src/translators/constants.ts b/packages/swap/src/translators/constants.ts index ed7ff9860b..73f4bb5b0e 100644 --- a/packages/swap/src/translators/constants.ts +++ b/packages/swap/src/translators/constants.ts @@ -8,7 +8,7 @@ export const supportedProviders: ReadonlyArray = [ '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