From 4bb6b0843e0dd8e617a4a8ad108c4fd5d3500098 Mon Sep 17 00:00:00 2001 From: Juliano Lazzarotto <30806844+stackchain@users.noreply.github.com> Date: Wed, 27 Sep 2023 18:56:49 +0100 Subject: [PATCH] refactor: added tests to unsupported pools --- .../adapters/openswap-api/openswap.mocks.ts | 32 +++++++++++++++++++ .../swap/src/helpers/transformers.test.ts | 5 ++- packages/swap/src/helpers/transformers.ts | 21 ++++++------ 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/packages/swap/src/adapters/openswap-api/openswap.mocks.ts b/packages/swap/src/adapters/openswap-api/openswap.mocks.ts index eff3818aaa..eaa257aae0 100644 --- a/packages/swap/src/adapters/openswap-api/openswap.mocks.ts +++ b/packages/swap/src/adapters/openswap-api/openswap.mocks.ts @@ -405,6 +405,38 @@ const getPools: OpenSwap.Pool[] = [ 'e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d86.7339a8bcda85e2c997d9f16beddbeb3ad755f5202f5cfd9cb08db346a1292c01', }, }, + { + provider: 'spectrum', // unsupported pool + fee: '0.3', + tokenA: { + amount: '1233807687', + token: '.', + }, + tokenB: { + amount: '780', + token: + 'e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed72.43414b45', + }, + price: 1581804.726923077, + batcherFee: { + amount: '2000000', + token: '.', + }, + // depositFee: { + // amount: '2000000', + // token: '.', + // }, + deposit: 2000000, + utxo: '0596860b5970ef989c56f7ae38b3c0f74bb4979ac15ee994c30760f7f4d908ce#0', + poolId: + '0be55d262b29f564998ff81efe21bdc0022621c12f15af08d0f2ddb1.7339a8bcda85e2c997d9f16beddbeb3ad755f5202f5cfd9cb08db346a1292c01', + timestamp: '2023-05-31 07:03:41', + lpToken: { + amount: '981004', + token: + 'e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d86.7339a8bcda85e2c997d9f16beddbeb3ad755f5202f5cfd9cb08db346a1292c01', + }, + }, ] export const openswapMocks = { diff --git a/packages/swap/src/helpers/transformers.test.ts b/packages/swap/src/helpers/transformers.test.ts index 22b53089ff..0b2d0805bc 100644 --- a/packages/swap/src/helpers/transformers.test.ts +++ b/packages/swap/src/helpers/transformers.test.ts @@ -177,10 +177,13 @@ describe('asYoroiPools', () => { expect(result).toEqual>([]) }) - it('success', () => { + it('success (filter out unsupported pools)', () => { const result = transformers.asYoroiPools(openswapMocks.getPools) expect(result).toEqual>(apiMocks.getPools) + + // should filter out unsupported pools + expect(result.length).toBe(openswapMocks.getPools.length - 1) }) }) diff --git a/packages/swap/src/helpers/transformers.ts b/packages/swap/src/helpers/transformers.ts index 0212cab43c..e27bf2feb3 100644 --- a/packages/swap/src/helpers/transformers.ts +++ b/packages/swap/src/helpers/transformers.ts @@ -152,10 +152,17 @@ export const transformersMaker = ( return {quantity: Quantities.zero, tokenId: ''} as const } + /** + * Filter out pools that are not supported by Yoroi + * + * @param openswapPools + * @returns {Swap.Pool[]} + */ const asYoroiPools = (openswapPools: OpenSwap.Pool[]): Swap.Pool[] => { - return openswapPools?.length > 0 - ? filterBySupportedProviders(openswapPools).map(asYoroiPool) - : [] + if (openswapPools?.length > 0) + return openswapPools.filter(filterBySupportedProviders).map(asYoroiPool) + + return [] } const asYoroiBalanceTokens = ( @@ -194,10 +201,6 @@ export const asTokenFingerprint = ({ export const asUtf8 = (hex: string) => Buffer.from(hex, 'hex').toString('utf-8') -const filterBySupportedProviders = ( - pools: OpenSwap.Pool[], -): OpenSwap.Pool[] => { - return pools.filter((pool) => - supportedProviders.includes(pool.provider as Swap.SupportedProvider), - ) +function filterBySupportedProviders(pool: OpenSwap.Pool) { + return supportedProviders.includes(pool.provider as Swap.SupportedProvider) }