diff --git a/apps/wallet-mobile/src/features/Swap/common/mocks.ts b/apps/wallet-mobile/src/features/Swap/common/mocks.ts index 18611a9670..c336ec767f 100644 --- a/apps/wallet-mobile/src/features/Swap/common/mocks.ts +++ b/apps/wallet-mobile/src/features/Swap/common/mocks.ts @@ -1,9 +1,8 @@ -import {mockSwapStateDefault} from '@yoroi/swap' +import {mockSwapStateDefault, SwapState} from '@yoroi/swap' import {mocks as walletMocks} from '../../../yoroi-wallets/mocks/wallet' import {asQuantity} from '../../../yoroi-wallets/utils' -type ProviderType = 'sundaeswap' | 'minswap' | 'wingriders' | 'muesliswap_v1' | 'muesliswap_v2' | 'muesliswap_v3' type Type = 'market' | 'limit' export const mocks = { @@ -34,7 +33,7 @@ export const mocks = { }, poolId: '0029cb7c88c7567b63d1a512c0ed626aa169688ec980730c0473b913.702083', price: 0.0890390378168252, - provider: 'sundaeswap' as ProviderType, + provider: 'sundaeswap', tokenA: {quantity: asQuantity(20630071), tokenId: ''}, tokenB: { quantity: asQuantity(231696922), @@ -44,5 +43,5 @@ export const mocks = { slippage: 1, type: 'market' as Type, }, - }, + } as SwapState, } diff --git a/packages/swap/src/helpers/transformers.test.ts b/packages/swap/src/helpers/transformers.test.ts index 0b2d0805bc..77c797cb37 100644 --- a/packages/swap/src/helpers/transformers.test.ts +++ b/packages/swap/src/helpers/transformers.test.ts @@ -188,11 +188,17 @@ describe('asYoroiPools', () => { }) describe('asYoroiPool', () => { - it('success', () => { + it('success (supported pool)', () => { const result = transformers.asYoroiPool(openswapMocks.getPools[0]!) expect(result).toEqual(apiMocks.getPools[0]!) }) + + it('success (unsupported pool)', () => { + const result = transformers.asYoroiPool(openswapMocks.getPools[3]!) + + expect(result).toBeNull() + }) }) describe('asYoroiBalanceToken', () => { diff --git a/packages/swap/src/helpers/transformers.ts b/packages/swap/src/helpers/transformers.ts index e27bf2feb3..af74deaccf 100644 --- a/packages/swap/src/helpers/transformers.ts +++ b/packages/swap/src/helpers/transformers.ts @@ -107,7 +107,7 @@ export const transformersMaker = ( return balanceToken } - const asYoroiPool = (openswapPool: OpenSwap.Pool): Swap.Pool => { + const asYoroiPool = (openswapPool: OpenSwap.Pool): Swap.Pool | null => { const { batcherFee, fee, @@ -120,6 +120,9 @@ export const transformersMaker = ( price, poolId, } = openswapPool + + if (provider && !isSupportedProvider(provider)) return null + const pool: Swap.Pool = { tokenA: asYoroiAmount(tokenA), tokenB: asYoroiAmount(tokenB), @@ -160,7 +163,9 @@ export const transformersMaker = ( */ const asYoroiPools = (openswapPools: OpenSwap.Pool[]): Swap.Pool[] => { if (openswapPools?.length > 0) - return openswapPools.filter(filterBySupportedProviders).map(asYoroiPool) + return openswapPools + .map(asYoroiPool) + .filter((pool): pool is Swap.Pool => pool !== null) return [] } @@ -201,6 +206,8 @@ export const asTokenFingerprint = ({ export const asUtf8 = (hex: string) => Buffer.from(hex, 'hex').toString('utf-8') -function filterBySupportedProviders(pool: OpenSwap.Pool) { - return supportedProviders.includes(pool.provider as Swap.SupportedProvider) +function isSupportedProvider( + provider: string, +): provider is Swap.SupportedProvider { + return supportedProviders.includes(provider as Swap.SupportedProvider) } diff --git a/packages/swap/src/index.ts b/packages/swap/src/index.ts index afa5920952..96689ec04e 100644 --- a/packages/swap/src/index.ts +++ b/packages/swap/src/index.ts @@ -20,6 +20,7 @@ export {makeLimitOrder} from './helpers/orders/makeLimitOrder' export {getPoolUrlByProvider} from './helpers/pools/getPoolUrlByProvider' export {SwapProvider} from './translators/reactjs/provider/SwapProvider' +export {SwapState} from './translators/reactjs/state/state' export {useSwapCreateOrder} from './translators/reactjs/hooks/useSwapCreateOrder' export {useSwapOrdersByStatusCompleted} from './translators/reactjs/hooks/useSwapOrdersByStatusCompleted' export {useSwapOrdersByStatusOpen} from './translators/reactjs/hooks/useSwapOrdersByStatusOpen' @@ -37,3 +38,5 @@ export { swapStorageMaker, swapStorageSlippageKey, } from './adapters/async-storage/storage' + +export {supportedProviders} from './translators/constants' diff --git a/packages/types/src/swap/order.ts b/packages/types/src/swap/order.ts index 401e913117..b93ba07f64 100644 --- a/packages/types/src/swap/order.ts +++ b/packages/types/src/swap/order.ts @@ -1,5 +1,5 @@ import {BalanceAmount, BalanceQuantity} from '../balance/token' -import {SwapPool} from './pool' +import {SwapPool, SwapPoolProvider} from './pool' export type SwapOrderType = 'market' | 'limit' @@ -29,7 +29,7 @@ export type SwapCreateOrderResponse = { } export type SwapOpenOrder = { - provider: SwapPool['provider'] + provider: SwapPoolProvider from: BalanceAmount to: BalanceAmount deposit: BalanceAmount diff --git a/packages/types/src/swap/pool.ts b/packages/types/src/swap/pool.ts index 7dcbf0115e..4baa839c3f 100644 --- a/packages/types/src/swap/pool.ts +++ b/packages/types/src/swap/pool.ts @@ -23,7 +23,7 @@ export type SwapSupportedProvider = Extract< > export type SwapPool = { - provider: SwapPoolProvider + provider: SwapSupportedProvider fee: string // % pool liquidity provider fee, usually 0.3. tokenA: BalanceAmount tokenB: BalanceAmount