fetch limit order¶
here, we provide a simple example to fetch limit orders of iZiSwap from an user’s address
by calling fetchLimitOrderOfAccount()
The full example code of this chapter can be spotted here.
1. some imports¶
before we use function of fetchLimitOrderOfAccount(), 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 { fetchToken } from 'iziswap-sdk/lib/base/token/token';
5import { fetchLimitOrderOfAccount, getLimitOrderManagerContract } from 'iziswap-sdk/lib/limitOrder/view';
the detail of these imports can be viewed in following content
2. specify which chain, rpc url, 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)
here
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, of course you can fill fields of BaseChain by yourself
privateKey is a string, which is your private key, and should be configured by your self
web3 package is a public package to interact with block chain
rpc is the rpc url on the chain you specified
3. get web3.eth.Contract object of LimitOrderManager¶
1const limitOrderAddress = '0x9Bf8399c9f5b777cbA2052F83E213ff59e51612B'
2const limitOrderManager = getLimitOrderManagerContract(limitOrderAddress, web3)
here, getLimitOrderManagerContract is an api provided by our sdk, which returns a web3.eth.Contract object of LimitOrderManager
4. cache some erc20 tokens to speed up fetching(optional)¶
the function fetchLimitOrderOfAccount() has a parameter called tokenList: TokenInfoFormatted[], which is a cache of list of TokenInfoFormatted
each returned liquidity will contain fields tokenX and tokenY which are both TokenInfoFormatted type
if we fill tokenList with most erc20 tokens involved by these liquidities in advance, we may speed up calling of fetchLiquiditiesOfAccount() and fetchLiquiditiesByTokenIds()
ofcourse, you can 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'
3
4const testA = await fetchToken(testAAddress, chain, web3)
5const testB = await fetchToken(testBAddress, chain, web3)
5. fetch!¶
1const {activeOrders, deactiveOrders} = await fetchLimitOrderOfAccount(
2 chain, web3, limitOrderManager, account.address, [testA]
3)
4
5console.log('active orders len: ', activeOrders.length)
6console.log('deactive orders len: ', deactiveOrders.length)
7console.log(activeOrders)
here,
chain is BaseChain obj specified in 2
web3 is 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 []
return of fetchLimitOrderOfAccount() is list of LimitOrder object, each has following fields
1export interface LimitOrder {
2 // slot idx of the limit in use's limit order set
3 idx: string,
4 lastAccEarn: string,
5 // original undecimal amount of token on sale when this limit order created
6 // if sellXEarnY is true, the token is tokenX, otherwise tokenY
7 amount: string,
8 // undecimal amount of saled
9 filled: string,
10 // undecimal amount of
11 sellingRemain: string,
12 // undecimal amount of token on sale currently
13 sellingDec: string,
14 accSellingDec: string,
15 // undecimal amount of claimed earning token
16 // if sellXEarnY is true, the token is tokenY, otherwise tokenX
17 earn: string,
18 // undecimal amount of unclaimed earning token
19 pending: string,
20 poolId: string,
21 // address of pool
22 poolAddress: string,
23
24 // if sellXEarnY is true, sell tokenX
25 tokenX: TokenInfoFormatted,
26 tokenY: TokenInfoFormatted,
27 createTime: Number,
28 // point of limit order
29 point: number,
30 // price of this order
31 priceXByY: BigNumber,
32 priceXByYDecimal: number,
33 // true for sellX, false for sellY
34 sellXEarnY: boolean,
35 // whether this limit order is active
36 active: boolean
37}
after this step, we have successfully fetched all limit orders of the user.