Blocto
Search…
Batch Transaction
Combine multiple transactions and make them atomic

Introduction

With Blocto, you can combine multiple transactions into a single transaction for the following advantages:
    1.
    Save gas fee
    2.
    Make multiple transactions atomic, so they either all succeed or all fail

Usage

There are two ways to combine transactions:
1
import Web3 from 'web3';
2
​
3
// Use the Ethereum provider injected by Blocto app
4
const txHash = await window.ethereum.request({
5
method: 'blocto_sendBatchTransaction',
6
params: [
7
web3.eth.sendTransaction.request(SOME_REQUEST),
8
web3.eth.sendTransaction.request(SOME_OTHER_REQUEST)
9
]
10
})
11
​
12
console.log(txHash) // ex: 0x12a45b...
Copied!

B. Web3 Batch Request

1
import Web3 from 'web3';
2
​
3
window.ethereum.request({
4
method: 'blocto_sendBatchTransaction',
5
params: [transactionObject]
6
})
7
​
8
// Use the Ethereum provider injected by Blocto app
9
const web3 = new Web3(window.ethereum);
10
const batch = new web3.BatchRequest();
11
​
12
batch.add(web3.eth.sendTransaction.request(SOME_REQUEST));
13
batch.add(web3.eth.sendTransaction.request(SOME_OTHER_REQUEST));
14
​
15
batch.execute();
Copied!

Example

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:

A. EIP-1193

1
import Web3 from 'web3';
2
​
3
// approve DAI
4
const approveDAIReq = web3.eth.sendTransaction.request({
5
from: address,
6
to: '0x6b175474e89094c44da98b954eedeac495271d0f',
7
data: '0x095ea7b300000000000000000000000029fe7d60ddf151e5b52e5fab4f1325da6b2bd9580000000000000000000000000000000000000000000845951614014849ffffff',
8
}, 'latest')
9
​
10
// put in PoolTogether
11
const putInPoolTogetherReq = web3.eth.sendTransaction.request({
12
from: address,
13
to: '0x29fe7D60DdF151E5b52e5FAB4f1325da6b2bD958',
14
data: '0x234409440000000000000000000000000000000000000000000000000de0b6b3a7640000',
15
}, 'latest')
16
​
17
// Use the Ethereum provider injected by Blocto app
18
const txHash = await window.ethereum.request({
19
method: 'blocto_sendBatchTransaction',
20
params: [
21
approveDAIReq,
22
putInPoolTogetherReq
23
]
24
})
25
​
26
console.log(txHash) // ex: 0x12a45b...
Copied!

B. Web3 Batch Request

1
import Web3 from 'web3';
2
​
3
// Use the Ethereum provider injected by Blocto app
4
const web3 = new Web3(window.ethereum);
5
const batch = new web3.BatchRequest();
6
​
7
// claim DAI from some promotion smart contract
8
batch.add(web3.eth.sendTransaction.request({
9
from: address,
10
to: 'SOME_PROMOTION_CONTRACT',
11
data: 'SOME_METHOD_HASH',
12
}, 'latest'));
13
​
14
// approve DAI
15
batch.add(web3.eth.sendTransaction.request({
16
from: address,
17
to: '0x6b175474e89094c44da98b954eedeac495271d0f',
18
data: '0x095ea7b300000000000000000000000029fe7d60ddf151e5b52e5fab4f1325da6b2bd9580000000000000000000000000000000000000000000845951614014849ffffff',
19
}, 'latest'));
20
​
21
// put in PoolTogether
22
batch.add(web3.eth.sendTransaction.request({
23
from: address,
24
to: '0x29fe7D60DdF151E5b52e5FAB4f1325da6b2bD958',
25
data: '0x234409440000000000000000000000000000000000000000000000000de0b6b3a7640000',
26
}, 'latest'));
27
​
28
batch.execute();
Copied!
For more information about batch transactions, check out web3.js documentation.
Last modified 2mo ago
Copy link