import { ApiInterfaceRx } from '@polkadot/api/types' ; import { map } from 'rxjs/operators' ; import primitivesConfig from '@acala-network/type-definitions/primitives' ; import { Observable } from 'rxjs' ; import { CurrencyId , Balance } from '@acala-network/types/interfaces' ; import { memo } from '@polkadot/api-derive/util' ; import { DerivedDexPool } from '../types/dex' ; const TOKEN_SORT = primitivesConfig . types . TokenSymbol . _enum ; function sortTokens ( token1 , token2 ) { const result = [ token1 , token2 ] ; return result . sort ( ( a , b ) => TOKEN_SORT [ a . asToken . toString ( ) ] - TOKEN_SORT [ b . asToken . toString ( ) ] ) ; } name description param { CurrencyId } currency export function pool ( instanceId , api ) { return memo ( instanceId , ( token1 , token2 ) => { const params = sortTokens ( token1 , token2 ) ; return api . query . dex . liquidityPool < [ Balance , Balance ] > ( params ) . pipe ( map ( ( result ) => { const [ token1Amount , token2Amount ] = result ; if ( token1 . eq ( params [ 0 ] ) && token2 . eq ( params [ 1 ] ) ) { return [ token1Amount , token2Amount ] ; } return [ token2Amount , token1Amount ] ; } ) ) ; } ) ; }