Batch Request
If you would like to queue up multiple requests and have them processed sequentially, you can use our Batch Request. Note that if the user declines one of the requests, rest of the unprocessed requests will be declined as well.
The Fortmatic X modal will pop open once batch.execute() is called and display the number of requests the user will be interacting with.
Web3 Pre-1.0 Stable Versions
Web3 1.0 Beta Version
1
// Initialize provider
2
import Fortmatic from 'fortmatic';
3
import Web3 from 'web3';
4
​
5
const fm = new Fortmatic('YOUR_API_KEY');
6
window.web3 = new Web3(fm.getProvider());
7
​
8
const toAddress = '0xb159752065EA68Ef0B22249Df25864E624fec45D';
9
const sendValue = web3.toWei(1, 'ether'); // Convert 1 ether to wei
10
​
11
// Create batch
12
const batch = web3.createBatch();
13
​
14
// Get user account wallet address first
15
web3.eth.getAccounts((error, accounts) => {
16
if (error) throw error;
17
18
// Construct ether transaction params
19
const txnParams = {
20
from: accounts[0],
21
to: toAddress,
22
value: sendValue
23
}
24
​
25
// Add send transaction to batch
26
batch.add(web3.eth.sendTransaction.request(txnParams, (error, txnHash) => {
27
if (error) throw error;
28
console.log(txnHash);
29
}));
30
31
// Add another send transaction to batch
32
batch.add(web3.eth.sendTransaction.request(txnParams, (error, txnHash) => {
33
if (error) throw error;
34
console.log(txnHash);
35
}));
36
});
37
​
38
// Send batch to provider
39
batch.execute();
Copied!
You could also include contract call or other web3 actions like below.
1
// ERC20 Approve
2
batch.add(tokenContractInstance.approve.request(toAddress, calculatedApproveValue, {from: accounts[0]}, (error, txnHash) => {
3
if (error) throw error;
4
console.log(txnHash);
5
}));
6
​
7
// Web3 Get Accounts
8
batch.add(web3.eth.getAccounts.request((error, accounts) => {
9
if (error) throw error;
10
console.log(accounts);
11
}));
12
​
13
// Web3 Get Balance
14
batch.add(web3.eth.getBalance.request((err, balance) => {
15
if (error) throw error;
16
console.log(balance);
17
}));
Copied!
Currently supports both Web3 1.2.0+ and 1.0.0-beta versions.
1.0.0-beta.4x is not supported It is a known issue with .4x versions where a batch request is blocked and cannot be processed. For more details, please click here.
1
// Initialize provider
2
import Fortmatic from 'fortmatic';
3
import Web3 from 'web3';
4
​
5
const fm = new Fortmatic('YOUR_API_KEY');
6
window.web3 = new Web3(fm.getProvider());
7
​
8
const toAddress = '0xb159752065EA68Ef0B22249Df25864E624fec45D';
9
const sendValue = web3.toWei(1, 'ether'); // Convert 1 ether to wei
10
​
11
// Create batch
12
const batch = new web3.eth.BatchRequest();
13
​
14
// Get user account wallet address first
15
web3.eth.getAccounts().then((accounts) => {
16
17
// Construct ether transaction params
18
const txnParams = {
19
from: accounts[0],
20
to: toAddress,
21
value: sendValue
22
}
23
​
24
// Add send transaction to batch
25
batch.add(web3.eth.sendTransaction.request(txnParams, (error, txnHash) => {
26
if (error) throw error;
27
console.log(txnHash);
28
}));
29
30
// Add another send transaction to batch
31
batch.add(web3.eth.sendTransaction.request(txnParams, (error, txnHash) => {
32
if (error) throw error;
33
console.log(txnHash);
34
}));
35
36
// Send batch to provider
37
batch.execute();
38
});
39
​
40
​
Copied!
You could also include contract call or other web3 actions like below.
1
// ERC20 Approve
2
batch.add(tokenContractInstance.methods.approve(accounts[0], approveValue).send.request({from: accounts[0]}, (error, txnHash) => {
3
if (error) throw error;
4
console.log(txnHash);
5
}));
6
​
7
// Web3 Get Accounts
8
batch.add(web3.eth.getAccounts.request((error, accounts) => {
9
if (error) throw error;
10
console.log(accounts);
11
}));
12
​
13
// Web3 Get Balance
14
batch.add(web3.eth.getBalance.request(accounts[0], (error, balance) => {
15
if (error) throw error;
16
console.log(balance);
17
}));
Copied!
Last modified 2yr ago
Copy link