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)
We should notice that, if tokenX or tokenY is chain token (like ETH on ethereum or BNB on bsc), we should specify one field in params to indicate sdk collecting in form of Chain Token or collecting in form of Wrapped Chain Token (like WETH on ethereum or WBNB on bsc).
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 collecting in form of Chain Token. In the sdk version 1.2.* or later, you have two ways to indicate sdk.
The first way is as before, specifing strictERC20Token field. The second way is specifing strictERC20Token as undefined and specifying the corresponding token in this param as WETH or ETH.
3. estimate gas (optional)¶
of course you can skip this step if you don’t want to limit gas.
1const gasLimit = await collectLimitOrderCalling.estimateGas(options)
4. 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).