Web3 1.2.5: Revert reason strings

How to use the new feature

A new Web3 version was just released and it comes with a new feature that should make your life easier. With the latest version 1.2.5, you can now see the the revert reason if you use the new handleRevert option.

You can activate it easily by using web3.eth.handleRevert = true . Now when you use call or send functions, precisely one of the following:

  • web3.eth.call()
  • web3.eth.sendTransaction()
  • contract.methods.myMethod(…).send(…)
  • contract.methods.myMethod(…).call(…)

you will see a new message like

Error: Your request got reverted with the following reason string: This is the revert reason!\

If you have to use sendSignedTransaction, that is not yet implemented. However, you can work around that by catching a reverted transaction and then calling it.

try {
    const result = await sendTxWithSendSignedTransaction()
    console.log({ result })
} catch (error) {
    TestContract.methods.myMethod(myParam).call({
        from,
        value,
    }).then(result => { throw Error('unlikely to happen') })
    .catch(revertReason => console.log({ revertReason }))
}

This will hopefully make it a lot easier trying to find out why transactions reverted in a live system. Incorporate this into your error logging system to make the most use of it.


Markus Waas

Solidity Developer

More great blog posts from Markus Waas

  • Exploring the Openzeppelin CrossChain Functionality

    What is the new CrossChain support and how can you use it.

    For the first time Openzeppelin Contracts have added CrossChain Support. In particular the following chains are currently supported: Polygon: One of the most popular sidechains right now. We've discussed it previously here. Optimism: A Layer 2 chain based on optimistic rollups. We discussed the...

  • Deploying Solidity Contracts in Hedera

    What is Hedera and how can you use it.

    Hedera is a relatively new chain that exists since a few years, but recently added token service and smart contract capabilities. You can now write and deploy Solidity contracts to it, but it works a little differently than what you might be used to. Let's take a look! What is the Hedera Network?...

  • Writing ERC-20 Tests in Solidity with Foundry

    Blazing fast tests, no more BigNumber.js, only Solidity

    Maybe you are new to programming and are just starting to learn Solidity? One annoyance for you might have been that you were basically required to learn a second language (JavaScript/TypeScript) to write tests. This was undoubtedly a downside which is now gone with the new foundry framework. But...

  • ERC-4626: Extending ERC-20 for Interest Management 

    How the newly finalized standard works and can help you with Defi

    Many Defi projects have an ERC-20 token which represents ownership over an interest generating asset. This is for example the case for lending/borrowing platforms (money markets) like Compound and Aave. As a lender you will receive aDAI or cDAI. And since lenders receive interest payments for...

  • Advancing the NFT standard: ERC721-Permit

    And how to avoid the two step approve + transferFrom with ERC721-Permit (EIP-4494)

    There's a new standard in the making. To understand how this really works, I recommend you take a look at my tutorials on: ERC721 ERC20-Permit ecrecover incl EIP712 But we'll try to cover the basics here also. You might be familiar already with ERC20-Permit (EIP-2612). It adds a new permit...

  • Moonbeam: The EVM of Polkadot

    Deploying and onboarding users to Moonbeam or Moonriver

    We've covered several Layer 2 sidechains before: Polygon xDAI Binance Smart Chain Evmos Aurora (NEAR) But Moonbeam is unique since it's a parachain of the Polkadot ecosystem. It only just launched which means you are now able to deploy smart contracts to the chain. Being able to deploy EVM...

  • Advanced MultiSwap: How to better arbitrage with Solidity

    Making multiple swaps across different decentralized exchanges in a single transaction

    If you want maximum arbitrage performance, you need to swap tokens between exchanges in a single transaction. Or maybe you just want to save gas on certain swaps you perform regularly. Or maybe you have your own custom use case for swapping between decentralized exchanges. And of course maybe you...

  • Deploying Solidity Smart Contracts to Solana

    What is Solana and how can you deploy Solidity smart contracts to it?

    Solana is a new blockchain focusing on performance. It supports smart contracts like Ethereum which they call Programs. You can develop those in Rust, but there's also a new project now to compile Solidity to Solana. In other words you can deploy your contracts written in Solidity now to Solana!...

  • Smock 2: The powerful mocking tool for Hardhat

    Features of smock v2 and how to use them with examples

    We’ve covered mocking contracts before as well as the first version of the new mocking tool Smock 2. It simplifies the mocking process greatly and also gives you more testing power. You’ll be able to change the return values for functions as well as changing internal contract storage directly!...

© 2024 Solidity Dev Studio. All rights reserved.

This website is powered by Scrivito, the next generation React CMS.