Revert function

The REVERT opcode is a low-level instruction in the Ethereum Virtual Machine (EVM) that allows smart contract developers to revert the state of a transaction in case of an error or an invalid condition.

When the EVM executes a REVERT instruction, it immediately halts the execution of the current transaction and reverts all the state changes that were made during the current transaction. It returns any remaining gas to the transaction sender and provides an error message that describes the reason for the revert.

In Solidity, you can use the revert() function to explicitly revert the transaction and provide an error message. For example:

function myFunction() public {
    require(msg.sender == owner, "Only the contract owner can call this function");
    if (someCondition) {
        revert("Invalid condition");
    }
    // Rest of the function logic
}

In this example, if the someCondition variable evaluates to true, the function execution will be reverted with an error message "Invalid condition".

Note that as of Solidity ^0.8.0, revert is a statement and NOT a function. However, for backward compatibility reasons, revert can still be used in function form.

Last updated