Fetch liquidity positions

Here, we provide an example to fetch liquidity positions of a user address.

The function is realized by calling fetchLiquiditiesOfAccount(),

The full example code of this chapter can be spotted here.

1. Some imports

Before we use function of fetchLiquiditiesOfAccount(), we should import some corresponding function and data structure.

1import {BaseChain, ChainId, initialChainTable } from 'iziswap-sdk/lib/base/types'
2import {privateKey} from '../../.secret'
3import Web3 from 'web3'
4import { getLiquidityManagerContract, fetchLiquiditiesOfAccount } from 'iziswap-sdk/lib/liquidityManager/view';
5import { fetchToken } from 'iziswap-sdk/lib/base/token/token';

Details of these imports can be viewed in the following content.

2. Specify chain, rpc, web3, and account

1const chain:BaseChain = initialChainTable[ChainId.BSC]
2const rpc = 'https://bsc-dataseed2.defibit.io/'
3console.log('rpc: ', rpc)
4const web3 = new Web3(new Web3.providers.HttpProvider(rpc))
5const account =  web3.eth.accounts.privateKeyToAccount(privateKey)
6console.log('address: ', account.address)


    • BaseChain is a data structure to describe a chain, in this example we use bsc chain.

    • ChainId is an enum to describe chain id, value of the enum is equal to value of chain id.

    • initialChainTable is a mapping from some most used ChainId to BaseChain. You can fill fields of BaseChain by yourself.

    • privateKey is a string, which is your private key, and should be configured by yourself.

    • web3 is a public package to interact with block chain.

    • rpc is the rpc url on the chain you specified.

3. Get the web3.eth.Contract object of liquidityManager

1const liquidityManagerAddress = '0xBF55ef05412f1528DbD96ED9E7181f87d8C9F453' // example BSC address
2const liquidityManagerContract = getLiquidityManagerContract(liquidityManagerAddress, web3)
3console.log('liquidity manager address: ', liquidityManagerAddress)

Here, getLiquidityManagerContract is an api provided by our sdk, which returns a web3.eth.Contract object of LiquidityManager.

4. Cache some erc20 tokens to speed up fetching(optional)

Both of the function fetchLiquiditiesOfAccount() and fetchLiquiditiesByTokenIds() have a parameter called tokenList: TokenInfoFormatted[], which is a cache list of TokenInfoFormatted.

Each returned liquidity position contains fields tokenX and tokenY, which are both of TokenInfoFormatted type.

If you fill tokenList with most erc20 tokens involved by these liquidities in advance, you may speed up the calling of fetchLiquiditiesOfAccount() and fetchLiquiditiesByTokenIds().

You can also skip this section and just transfer [] to the tokenList parameter in next section.

Here, we cache 2 erc20 tokens testA and testB in advance.

1const testAAddress = '0xCFD8A067e1fa03474e79Be646c5f6b6A27847399'
2const testBAddress = '0xAD1F11FBB288Cd13819cCB9397E59FAAB4Cdc16F'
4const testA = await fetchToken(testAAddress, chain, web3)
5const testB = await fetchToken(testBAddress, chain, web3)

5. Fetch!

1const liquidities = await fetchLiquiditiesOfAccount(
2    chain,
3    web3,
4    liquidityManagerContract,
5    account.address,
6    [testA, testB]
8console.log('liquidity len: ', liquidities.length)
9console.log('liquidtys: ', liquidities)


    • chain is a BaseChain obj specified in 2.

    • web3 is a Web3 obj specified in 2.

    • liquidityManagerContract is constructed in 3.

    • account.address is generated from private key in 2.

    • [testA, testB] is parameter tokenList which is cache of list of possible erc20 token info needed, of course we can fill tokenList with [].

The function return of fetchLiquiditiesOfAccount() is list of Liquidity object, each has following fields.

 1export interface Liquidity {
 2    // value of nft-id, a int value, but may be too large, so transformed into decimal system string
 3    tokenId: string;
 4    // left_point_on_pool of liquidity
 5    // describe min_undecimal_price_X_by_Y of this liquidity
 6    leftPoint: number;
 7    // right_point_on_pool of liquidity
 8    // describe max_undecimal_price_X_by_Y of this liquidity
 9    rightPoint: number;
10    // value of liquidity on each point in [leftPoint, rightPoint),
11    // a int value, but may be too large, so transformed into decimal system string
12    liquidity: string;
13    lastFeeScaleX_128: string;
14    lastFeeScaleY_128: string;
15    // undecimal amount of uncollected tokenX fee or withdrawed tokenX,
16    remainTokenX: string;
17    // undecimal amount of uncollected tokenY fee or withdrawed tokenY
18    remainTokenY: string;
19    // undecimal amount of tokenX in the liquidity (after latest withdraw or add or mint)
20    amountX: string;
21    // undecimal amount of tokenY in the liquidity (after latest withdraw or add or mint)
22    amountY: string;
23    poolId: string;
24    poolAddress: string;
25    tokenX: TokenInfoFormatted;
26    tokenY: TokenInfoFormatted;
27    // 2000 means 0.2%
28    fee: number;
29    // state() of pool
30    state: State;

Finally, we have successfully fetched all liquidity positions of an address.