Skip to content

Commit

Permalink
refactor: added tests to unsupported pools
Browse files Browse the repository at this point in the history
  • Loading branch information
stackchain committed Sep 27, 2023
1 parent b8e3f84 commit 4bb6b08
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
32 changes: 32 additions & 0 deletions packages/swap/src/adapters/openswap-api/openswap.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
5 changes: 4 additions & 1 deletion packages/swap/src/helpers/transformers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,13 @@ describe('asYoroiPools', () => {
expect(result).toEqual<Array<Swap.Pool>>([])
})

it('success', () => {
it('success (filter out unsupported pools)', () => {
const result = transformers.asYoroiPools(openswapMocks.getPools)

expect(result).toEqual<Array<Swap.Pool>>(apiMocks.getPools)

// should filter out unsupported pools
expect(result.length).toBe(openswapMocks.getPools.length - 1)
})
})

Expand Down
21 changes: 12 additions & 9 deletions packages/swap/src/helpers/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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)
}

0 comments on commit 4bb6b08

Please sign in to comment.