🔮
Ethereum
  • General
    • What is Ethereum
      • Ethereum & Bitcoin General Comparison
      • Singleton State
      • The Ethereum Virtual Machine (EVM)
      • Opcodes (operation codes) EVM
      • Ethereum Client
      • Forks
      • Gas (wei)
        • EIP-1559
        • Table Conversion (wei)
      • Proof of Stake (PoS)
      • Proof of Authority (PoA)
      • The Beacon Chain
      • Networks
        • Ethereum mainnet
        • Goerli
      • Account-based model & UTXO-based model
      • Externally Owned Account (EOA)
      • Node Clients
        • Geth
        • Nethermind
      • Contract Account
      • Smart Contract Upgradeability
      • Ultrasound Money
      • Merkle Trees
        • Patricia Merkle Tree
      • Tries
        • State Trie
        • Storage Trie
        • Transactions Trie
        • Receipts Trie
      • Transactions
        • Ethereum Transaction Architecture
      • World State
        • Chain of States
        • Chain of Blocks
        • Stack of Transactions / Mempool
      • Contract Creation
      • Message Call Transaction
      • P2P Network
      • Web3.js
      • Ether.js
        • Smart Contract Interaction Example
      • Web3.js vs Ether.js
      • Node Providers
      • ENS (Ethereum Name Service)
      • Web3 dapp
      • Escrow
      • Multi-signature
      • ERC-20 tokens
        • Send ERC20s to Contracts
      • NFTs
        • ERC-721 and ERC-1155
      • Solidity
        • State Variables
        • Data Location
        • Numbers
        • Modifiers
        • View & Pure Modifiers
        • Data Types
          • Modifiers
          • Modifiers (Functions)
          • Address & Address Payable
        • Hardhat
        • Payable Functions
        • Receive Function
        • Fallback Function
        • Global Variables
        • Self Destruct
        • Create2 Function
        • Revert function
        • Require function
        • Assert Function
        • Calldata
        • Interface
        • Mapping
        • Array
        • Struct
        • Inheritance
          • Virtual & Overwrite
          • Multiple inheritance
          • Hierarchical Inheritance
        • Events
          • Indexed (keyword)
          • LOG0 - LOG4
        • Multi-signature Example
        • Smart Contracts
          • Context
      • Application Binary Interface (ABI )
  • Extras
    • Terminology
      • Bytecode
      • Keccak-256
      • Turing complete
Powered by GitBook
On this page
  1. General
  2. What is Ethereum
  3. Solidity

Revert function

PreviousCreate2 FunctionNextRequire function

Last updated 2 years ago

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.

Expressions and Control Structures — Solidity 0.8.19 documentation
Logo