collect limit order¶
here, we provide a simple example to fetch limit orders of iZiSwap from an user’s address, and select one of them to collect
The full example code of this chapter can be spotted here.
1. fetch limit orders¶
1const chain:BaseChain = initialChainTable[ChainId.BSC]
2const rpc = 'https://bsc-dataseed2.defibit.io/'
3const web3 = new Web3(new Web3.providers.HttpProvider(rpc))
4const account = web3.eth.accounts.privateKeyToAccount(privateKey)
5
6const limitOrderAddress = '0x9Bf8399c9f5b777cbA2052F83E213ff59e51612B'
7const limitOrderManager = getLimitOrderManagerContract(limitOrderAddress, web3)
8
9
10const testAAddress = '0xCFD8A067e1fa03474e79Be646c5f6b6A27847399'
11const testBAddress = '0xAD1F11FBB288Cd13819cCB9397E59FAAB4Cdc16F'
12
13const testA = await fetchToken(testAAddress, chain, web3)
14const testB = await fetchToken(testBAddress, chain, web3)
15const fee = 2000 // 2000 means 0.2%
16
17// fetch limit order
18const {activeOrders, deactiveOrders} = await fetchLimitOrderOfAccount(
19 chain, web3, limitOrderManager, '0xD0B1c02E8A6CA05c7737A3F4a0EEDe075fa4920C', [testA]
20)
the code above is nearly the same as fetch limit order, you can view more detailed explains though this link
2. select an order to collect and get calling¶
first, select an order or your own
1const activeOrderAt2 = activeOrders[2]
second, get calling of decrease the limit order
1const orderIdx = activeOrderAt2.idx
2const gasPrice = '5000000000'
3const params: CollectLimOrderParam = {
4 orderIdx,
5 tokenX: activeOrderAt2.tokenX,
6 tokenY: activeOrderAt2.tokenY,
7 collectDecAmount: activeOrderAt2.sellingDec,
8 collectEarnAmount: '1'
9}
10// dec limit order of orderIdx
11const {collectLimitOrderCalling, options} = getCollectLimitOrderCall(
12 limitOrderManager,
13 account.address,
14 chain,
15 params,
16 gasPrice
17)
Notice: if tokenX or tokenY is chain gas token (like ETH on ethereum or BNB on bsc), and you want to collect tokenX or tokenY in form of native or wrapped-native token, you can refer to following section
3. collect native or wrapped native token¶
In the sdk version 1.2.* or later,
If you want to collect in form of native token(like BNB on bsc or ETH on ethereum …), you should replace define of params in section 2 with following code (here we are working on bsc chain), and fill strictERC20Token of params as undefined by default. And the options calculated in section 2 will contain corresponding msg.value.
1const BNBAddress = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';
2const params: CollectLimOrderParam = {
3 orderIdx,
4 tokenX: activeOrderAt2.tokenX,
5 tokenY: activeOrderAt2.tokenY,
6 collectDecAmount: activeOrderAt2.sellingDec,
7 collectEarnAmount: '1'
8}
9if (params.tokenX.address.toLowerCase() === BNBAddress.toLowerCase()) {
10 params.tokenX.symbol = 'BNB';
11}
12if (params.tokenY.address.toLowerCase() === BNBAddress.toLowerCase()) {
13 params.tokenY.symbol = 'BNB';
14}
If you want to collect in form of wrapped-native token(like WBNB on bsc or WETH on ethereum …), you should replace define of params in section 2 with following code (here we are working on bsc chain), and fill strictERC20Token of params as undefined which is the default value for that field.
1const BNBAddress = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';
2const params: CollectLimOrderParam = {
3 orderIdx,
4 tokenX: activeOrderAt2.tokenX,
5 tokenY: activeOrderAt2.tokenY,
6 collectDecAmount: activeOrderAt2.sellingDec,
7 collectEarnAmount: '1'
8}
9if (params.tokenX.address.toLowerCase() === BNBAddress.toLowerCase()) {
10 params.tokenX.symbol = 'WBNB'; // only difference with above code
11}
12if (params.tokenY.address.toLowerCase() === BNBAddress.toLowerCase()) {
13 params.tokenY.symbol = 'WBNB'; // only difference with above code
14}
we can see that, the only difference of collection native token and wrapped-native token is symbol field of params.tokenX or params.tokenY.
In the sdk version 1.1.* or before, one should specify a field named strictERC20Token to indicate that. true for collecting token in form of Wrapped Chain Token, false for paying in form of Chain Token. But we suggest you to upgrade your sdk to latest version.
4. estimate gas (optional)¶
of course you can skip this step if you don’t want to limit gas.
1const gasLimit = await collectLimitOrderCalling.estimateGas(options)
5. finally, send transaction!¶
for metamask or other explorer’s wallet provider, you can easily write
1await collectLimitOrderCalling.send({...options, gas: Number(gasLimit)})
otherwise, if you run codes in console, you could use following code
1const signedTx = await web3.eth.accounts.signTransaction(
2 {
3 ...options,
4 to: limitOrderAddress,
5 data: collectLimitOrderCalling.encodeABI(),
6 gas: new BigNumber(Number(gasLimit) * 1.1).toFixed(0, 2),
7 },
8 privateKey
9)
10// nonce += 1;
11const tx = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
after this step, we have successfully collect a limit order (if no revert occurred).