Skip to content

Commit

Permalink
fix: typeguard + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stackchain committed Sep 27, 2023
1 parent 4bb6b08 commit ef002b3
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
7 changes: 3 additions & 4 deletions apps/wallet-mobile/src/features/Swap/common/mocks.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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),
Expand All @@ -44,5 +43,5 @@ export const mocks = {
slippage: 1,
type: 'market' as Type,
},
},
} as SwapState,
}
8 changes: 7 additions & 1 deletion packages/swap/src/helpers/transformers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,17 @@ describe('asYoroiPools', () => {
})

describe('asYoroiPool', () => {
it('success', () => {
it('success (supported pool)', () => {
const result = transformers.asYoroiPool(openswapMocks.getPools[0]!)

expect(result).toEqual<Swap.Pool>(apiMocks.getPools[0]!)
})

it('success (unsupported pool)', () => {
const result = transformers.asYoroiPool(openswapMocks.getPools[3]!)

expect(result).toBeNull()
})
})

describe('asYoroiBalanceToken', () => {
Expand Down
15 changes: 11 additions & 4 deletions packages/swap/src/helpers/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down Expand Up @@ -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 []
}
Expand Down Expand Up @@ -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)
}
3 changes: 3 additions & 0 deletions packages/swap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -37,3 +38,5 @@ export {
swapStorageMaker,
swapStorageSlippageKey,
} from './adapters/async-storage/storage'

export {supportedProviders} from './translators/constants'
4 changes: 2 additions & 2 deletions packages/types/src/swap/order.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {BalanceAmount, BalanceQuantity} from '../balance/token'
import {SwapPool} from './pool'
import {SwapPool, SwapPoolProvider} from './pool'

export type SwapOrderType = 'market' | 'limit'

Expand Down Expand Up @@ -29,7 +29,7 @@ export type SwapCreateOrderResponse = {
}

export type SwapOpenOrder = {
provider: SwapPool['provider']
provider: SwapPoolProvider
from: BalanceAmount
to: BalanceAmount
deposit: BalanceAmount
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 @@ -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
Expand Down

0 comments on commit ef002b3

Please sign in to comment.