description |
---|
Use Blocto SDK to Combine multiple transactions and make them atomic |
{% hint style="warning" %} Note that Blocto SDK for EVM-compatible chains is still in Beta. APIs are subject to breaking changes. {% endhint %}
{% hint style="info" %}
Blocto SDK now supports Web3.js v4 batch transactions with version >= v.0.9.0.
For version < v0.9.0, please use it along with Web3.js v1.
{% endhint %}
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 bloctoSDK.ethereum.request({
method: "wallet_sendMultiCallTransaction",
params: [
[
...web3.eth.sendTransaction.request(SOME_REQUEST).params,
...web3.eth.sendTransaction.request(SOME_OTHER_REQUEST).params,
],
false, // (optional) revert flag default is false
],
});
console.log(txHash); // ex: 0x12a45b...
import Web3 from "web3";
// Use the Ethereum provider injected by Blocto app
const web3 = new Web3(bloctoSDK.ethereum);
const batch = new web3.BatchRequest();
const request1 = {
jsonrpc: "2.0",
id: 10,
method: "eth_getBalance",
params: ["0xf4ffff492596ac13fee6126846350433bf9a5021", "latest"],
};
const request2 = {
jsonrpc: "2.0",
id: 12,
method: "eth_getBalance",
params: ["0xdc6bad79dab7ea733098f66f6c6f9dd008da3258", "latest"],
};
batch.add(request1);
batch.add(request2);
const txHash = await batch.execute();
console.log(txHash); // ex: [{ "id":"10", "jsonrpc":"2.0", "method":"eth_getBalance", "result":"0x0" }, ...]
{% embed url="https://codesandbox.io/s/github/blocto/blocto-sdk-examples/tree/main/src/evm/with-blocto-batch-transaction-v4?file=/src/App.js" %}
For more information about batch transactions web3.js 4.x.x documentation.
import Web3 from "web3";
// Use the Ethereum provider injected by Blocto app
const web3 = new Web3(bloctoSDK.ethereum);
const batch = new web3.BatchRequest();
batch.add(web3.eth.sendTransaction.request(SOME_REQUEST));
batch.add(web3.eth.sendTransaction.request(SOME_OTHER_REQUEST));
batch.execute();
{% embed url="https://codesandbox.io/s/github/blocto/blocto-sdk-examples/tree/main/src/evm/with-blocto-batch-transaction?file=/src/App.js" %}
For more information about batch transactions, check out web3.js documentation.