fetch limit order and dec¶
here, we provide a simple example to fetch limit orders of iZiSwap from an user’s address, and select one of them to decrease(or called cancel)
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 and get calling of decrease¶
first, select an order or your own
1const activeOrderAt2 = activeOrders[2]
second, get calling for decreasing the limit order
1const orderIdx = activeOrderAt2.idx
2const gasPrice = '5000000000'
3// dec limit order of orderIdx
4const {decLimOrderCalling, options} = getDecLimOrderCall(
5 limitOrderManager,
6 orderIdx,
7 activeOrderAt2.sellingRemain,
8 '0xffffffff',
9 account.address,
10 chain,
11 gasPrice
12)
3. estimate gas (optional)¶
of course you can skip this step if you don’t want to limit gas.
1const gasLimit = await decLimOrderCalling.estimateGas(options)
4. finally, send transaction!¶
for metamask or other explorer’s wallet provider, you can easily write
1await decLimOrderCalling.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: decLimOrderCalling.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 decrease a limit order (if no revert occurred).