description |
---|
Combine multiple transactions and make them atomic |
With Blocto, you can combine multiple transactions into a single transaction for the following advantages:
- Save gas fee
- Make multiple transactions atomic, so they either all succeed or all fail
There are two ways to combine transactions:
import Web3 from 'web3';
// Use the Ethereum provider injected by Blocto app
const txHash = await window.ethereum.request({
method: 'wallet_sendMultiCallTransaction',
params: [
[
web3.eth.sendTransaction.request(SOME_REQUEST),
web3.eth.sendTransaction.request(SOME_OTHER_REQUEST)
],
true // revert flag, could be true or false
]
});
console.log(txHash); // ex: 0x12a45b...
import Web3 from 'web3';
// Use the Ethereum provider injected by Blocto app
const web3 = new Web3(window.ethereum);
const batch = new web3.BatchRequest();
batch.add(web3.eth.sendTransaction.request(SOME_REQUEST));
batch.add(web3.eth.sendTransaction.request(SOME_OTHER_REQUEST));
const responses = await batch.execute();
For example, if you are building a campaign for PoolTogether. You want to let user claim a DAI token from a smart contract, approve PoolTogether from spending user's DAI and deposit the DAI into PoolTogether, you can do something like:
import Web3 from 'web3';
// approve DAI
const approveDAIReq = web3.eth.sendTransaction.request({
from: address,
to: '0x6b175474e89094c44da98b954eedeac495271d0f',
data: '0x095ea7b300000000000000000000000029fe7d60ddf151e5b52e5fab4f1325da6b2bd9580000000000000000000000000000000000000000000845951614014849ffffff',
}, 'latest');
// put in PoolTogether
const putInPoolTogetherReq = web3.eth.sendTransaction.request({
from: address,
to: '0x29fe7D60DdF151E5b52e5FAB4f1325da6b2bD958',
data: '0x234409440000000000000000000000000000000000000000000000000de0b6b3a7640000',
}, 'latest');
// Use the Ethereum provider injected by Blocto app
const txHash = await window.ethereum.request({
method: 'wallet_sendMultiCallTransaction',
params: [
[
approveDAIReq,
putInPoolTogetherReq
],
true
]
});
console.log(txHash) // ex: 0x12a45b...
import Web3 from 'web3';
// Use the Ethereum provider injected by Blocto app
const web3 = new Web3(window.ethereum);
const batch = new web3.BatchRequest();
// claim DAI from some promotion smart contract
batch.add(web3.eth.sendTransaction.request({
from: address,
to: 'SOME_PROMOTION_CONTRACT',
data: 'SOME_METHOD_HASH',
}, 'latest'));
// approve DAI
batch.add(web3.eth.sendTransaction.request({
from: address,
to: '0x6b175474e89094c44da98b954eedeac495271d0f',
data: '0x095ea7b300000000000000000000000029fe7d60ddf151e5b52e5fab4f1325da6b2bd9580000000000000000000000000000000000000000000845951614014849ffffff',
}, 'latest'));
// put in PoolTogether
batch.add(web3.eth.sendTransaction.request({
from: address,
to: '0x29fe7D60DdF151E5b52e5FAB4f1325da6b2bD958',
data: '0x234409440000000000000000000000000000000000000000000000000de0b6b3a7640000',
}, 'latest'));
const responses = await batch.execute();
For more information about batch transactions, check out web3.js documentation.