1

Problem Overview

I am facing an issue while interacting with a smart contract using Web3.js version 4.2.0. The specific method causing trouble is broadcastMessage(string), designed to store a string in the smart contract. The interaction works flawlessly on a local Ganache node but encounters failures when transitioning to MetaMask and the Sepolia Testnet.

Error Message

TransactionRevertedWithoutReasonError: Transaction has been reverted by the EVM: {"blockHash":"0xd1b86dd20d864c230d2981cbbc7b518be04d4bacbe6f8200fb5cc9fcd6445ea9","blockNumber":"4985960","cumulativeGasUsed":"11627592","effectiveGasPrice":"18227291649","from":"0x9fa22690964e1ad25d44123e935d1888c1b82e2b","gasUsed":"21045","logs":[],"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","status":"0","to":"0x1dc2f31f8c6310c9507292b8340cc7a3a3d5766a","transactionHash":"0xd65880748ef1c83988bedaa644d89b93b306cbac8315ef8a0624ea42dbef95c7","transactionIndex":"101","type":"2"} at web3.js?v=babb5c47:26221:15 at Generator.next () at web3.js?v=babb5c47:26206:67 at new Promise () at __awaiter29 (web3.js?v=babb5c47:26188:10) at getTransactionError (web3.js?v=babb5c47:26210:10) at SendTxHelper. (web3.js?v=babb5c47:26401:29) at Generator.next () at web3.js?v=babb5c47:26292:67 at new Promise ()

Initial Approach

Following the official Web3.js documentation (https://docs.web3js.org/guides/smart_contracts/deploying_and_interacting_with_smart_contracts?programming-language=typescript#step-7-interact-with-the-smart-contract-using-web3js), I initially used the following code to interact with the smart contract:

this.smartContract.methods.broadcastMessage(message).send({ from: fromAddress, gas: '3000000', value: feeInWei.toString() });

However, this approach consistently fails with the mentioned error.

Alternative Approach

Referring to a Stack Overflow post (https://stackoverflow.com/a/77136409/13583700), I refactored the code as follows, and it successfully resolves the issue:

const gasEstimate = await this.smartContract.methods.broadcastMessage(message).estimateGas({ from: fromAddress });
const encode = await this.smartContract.methods.broadcastMessage(message).encodeABI();

web3.eth.sendTransaction({
      from: fromAddress,
      to: this.smartContractAddress,
      value: feeInWei.toString(),
      data: encode,
      gas: gasEstimate.toString()
});

Additional Context

The issue arises specifically when switching from a local Ganache node to MetaMask and Sepolia Testnet. The initial approach worked seamlessly with the local node, raising concerns about potential bugs in my code.

Expected Outcome

I am seeking insights into why the initial approach fails and whether it might be related to changes in network environments. Understanding the root cause is crucial for ensuring the reliability of smart contract interactions across different network setups.

Environment

  • TypeScript
  • Web3.js version: 4.2.0
  • Network: MetaMask and Sepolia Testnet
  • Browser: Brave

Steps to Reproduce

  1. Deploy smart contract on Sepolia Testnet.
  2. Interact with broadcastMessage(string) method using the initial approach mentioned in the documentation.
  3. Observe the transaction revert error.

Note: The alternative approach using estimateGas and encodeABI resolves the issue, but understanding the cause of the initial failure is crucial for code robustness.

Feel free to ask for any additional information to help diagnose and resolve this issue.

1
  • Hi! While we're happy that you've already resolved the issue, please take out the solution and spin it off into an answer - this will help future readers. Cheers! Jan 2 at 17:41

1 Answer 1

1

Referring to a Stack Overflow post (https://stackoverflow.com/a/77136409/13583700), I refactored the code as follows, and it successfully resolves the issue:

const gasEstimate = await this.smartContract.methods.broadcastMessage(message).estimateGas({ from: fromAddress });
const encode = await this.smartContract.methods.broadcastMessage(message).encodeABI();

web3.eth.sendTransaction({
      from: fromAddress,
      to: this.smartContractAddress,
      value: feeInWei.toString(),
      data: encode,
      gas: gasEstimate.toString()
});

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.