Fortmatic
WebsiteDashboardSupport
v1.x
v1.x
  • 🚀Get Started
  • 📦Install with NPM
  • 🧩Examples
  • 💬FAQ
  • Web3 Integration
    • Web3 Provider
    • Network Configuration
    • Get User Account
    • Send Ether Transaction
    • Smart Contract Functions
    • User Signing
    • SDK Error Handling
    • Batch Request
  • Smart Contract
    • ERC20 Transfer
    • ERC20 Approve
    • ERC20 TransferFrom
    • Generic Contract Call
  • Fortmatic Native
    • Log In
    • Log Out
    • Is User Logged In
    • Compose Transaction
    • Deposit Address
    • Configuration
  • Security
    • Domain Verification
Powered by GitBook
On this page

Was this helpful?

  1. Web3 Integration

Batch Request

PreviousSDK Error HandlingNextERC20 Transfer

Last updated 5 years ago

Was this helpful?

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.

// Initialize provider
import Fortmatic from 'fortmatic';
import Web3 from 'web3';

const fm = new Fortmatic('YOUR_API_KEY');
window.web3 = new Web3(fm.getProvider());

const toAddress = '0xb159752065EA68Ef0B22249Df25864E624fec45D';
const sendValue = web3.toWei(1, 'ether'); // Convert 1 ether to wei

// Create batch
const batch = web3.createBatch();

// Get user account wallet address first
web3.eth.getAccounts((error, accounts) => {
  if (error) throw error;
  
  // Construct ether transaction params
  const txnParams = {
    from: accounts[0],
    to: toAddress,
    value: sendValue
  }

  // Add send transaction to batch
  batch.add(web3.eth.sendTransaction.request(txnParams, (error, txnHash) => {
    if (error) throw error;
    console.log(txnHash);
  }));
  
  // Add another send transaction to batch
  batch.add(web3.eth.sendTransaction.request(txnParams, (error, txnHash) => {
    if (error) throw error;
    console.log(txnHash);
  }));
});

// Send batch to provider
batch.execute();

You could also include contract call or other web3 actions like below.

// ERC20 Approve
batch.add(tokenContractInstance.approve.request(toAddress, calculatedApproveValue, {from: accounts[0]}, (error, txnHash) => {
  if (error) throw error;
  console.log(txnHash);
}));

// Web3 Get Accounts
batch.add(web3.eth.getAccounts.request((error, accounts) => {
  if (error) throw error;
  console.log(accounts);
}));

// Web3 Get Balance
batch.add(web3.eth.getBalance.request((err, balance) => {
  if (error) throw error;
  console.log(balance);
}));

Currently supports both Web3 1.2.0+ and 1.0.0-beta versions.

// Initialize provider
import Fortmatic from 'fortmatic';
import Web3 from 'web3';

const fm = new Fortmatic('YOUR_API_KEY');
window.web3 = new Web3(fm.getProvider());

const toAddress = '0xb159752065EA68Ef0B22249Df25864E624fec45D';
const sendValue = web3.toWei(1, 'ether'); // Convert 1 ether to wei

// Create batch
const batch = new web3.eth.BatchRequest();

// Get user account wallet address first
web3.eth.getAccounts().then((accounts) => {
  
  // Construct ether transaction params
  const txnParams = {
    from: accounts[0],
    to: toAddress,
    value: sendValue
  }

  // Add send transaction to batch
  batch.add(web3.eth.sendTransaction.request(txnParams, (error, txnHash) => {
    if (error) throw error;
    console.log(txnHash);
  }));
  
  // Add another send transaction to batch
  batch.add(web3.eth.sendTransaction.request(txnParams, (error, txnHash) => {
    if (error) throw error;
    console.log(txnHash);
  }));
  
  // Send batch to provider
  batch.execute();
});

You could also include contract call or other web3 actions like below.

// ERC20 Approve
batch.add(tokenContractInstance.methods.approve(accounts[0], approveValue).send.request({from: accounts[0]}, (error, txnHash) => {
  if (error) throw error;
  console.log(txnHash);
}));

// Web3 Get Accounts
batch.add(web3.eth.getAccounts.request((error, accounts) => {
  if (error) throw error;
  console.log(accounts);
}));

// Web3 Get Balance
batch.add(web3.eth.getBalance.request(accounts[0], (error, balance) => {
  if (error) throw error;
  console.log(balance);
}));

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