3

I have created a batch of transactions with the help of web3.js.

  1. Firstly I am batching transactions with the "approve" method of the erc20 contract to approve a smart contract of the tokens present in the wallet
  2. Then I batch a custom method of "redeem".
  3. I want to sign all these transactions or(batch of transactions) with a private key.They work fine if my metamask account is connected to the dapp i need to sign it from the frontend(with my private key) without the metamask popup by using my private key i.e web3.js
  4.   const batch = new web3.BatchRequest()
      batch.add(
            tokenContractWeb3.methods.approve(contract2.address, 
            Number(bal).toString()).send({
              from: grantorAddress,
            }),
          )
      batch.add(
        anotherContractWeb3.methods.redeem(1, '0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65').send({
          from: grantorAddress,
          }),
        )
      batch.execute()
    
6
  • i am trying to fetch the batch.requests.data but i am facing a error Property 'requests' does not exist on type 'BatchRequest'.ts(2339) Jan 30, 2023 at 20:09
  • Does this answer your question? BatchRequest() is a single transaction or many?
    – natewelch_
    Jan 31, 2023 at 19:10
  • Didn’t help much Feb 1, 2023 at 5:27
  • @SukiratWarring You should initialize web3.js with a wallet provider like @truffle/hdwallet-provider for signing the transactions.
    – Ismael
    Feb 11, 2023 at 5:24
  • Did you manage to fix this? I would comment but I just created this account to ask if you managed to solve this? Apr 17, 2023 at 5:02

1 Answer 1

1

What you're trying to do isn't possible. Batching in web3.js refers to only the batching of JSON-RPC calls. That means you can batch readonly calls (i.e. balanceOf, calls getting current prices on DEXs, etc.), but not transactions that are changing the state

You will need to sign and send the transactions to the network individually.

3
  • 1
    IS IT? because i was able to batch the approve and redeem method which are not readonly calls...but i want to sign these batch of transaction with my private key in the frontend and not from the metamask popup Jan 31, 2023 at 17:48
  • Yes it is. You clearly weren't able to batch them as you receive an error, the error being that there are no requests in the BatchRequest. .send returns a 32 byte transaction hash, whereas the docs shows requests being generated using the .request method, therefore you never added any requests
    – natewelch_
    Jan 31, 2023 at 19:11
  • 1
    I was able to batch them and sign them with my metamask pop-up and the transactions were successful ….my question is i want to sign these batched transactions with my private key present in the frontend Feb 1, 2023 at 5:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.