---
eip: 140
label: EIP-140
title: REVERT instruction
status: Final
type: Standards Track
category: Core
created: 2017-02-06
official: https://eips.ethereum.org/EIPS/eip-140
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-140.md
canonical: https://eips.huatuo.xyz/eip-140/
lang: en
tldr: Adds the REVERT opcode, which aborts execution and rolls back state while refunding unused gas and returning an error message to the caller.
tags: [evm, opcode, errors, byzantium, core]
---

# EIP-140: REVERT instruction

> The REVERT instruction provides a way to stop execution and revert state changes, without consuming all provided gas and with the ability to return a reason.

**TL;DR:** Adds the REVERT opcode, which aborts execution and rolls back state while refunding unused gas and returning an error message to the caller.

## What problem does it solve?

Before the Byzantium hard fork (2017), a contract had only two ways to abort: run out of gas, or hit an invalid instruction. Both consumed **all** the remaining gas, even when the contract had simply checked a condition and found it false. Neither could explain why. The caller saw only "failed".

EIP-140 adds a clean way to fail: stop, undo every state change, keep the unused gas, and hand back a message.

## How it works

- New opcode `REVERT` at `0xfd`, taking two stack items: `memory_offset` and `memory_length`.
- Execution stops and all state changes from the current call (including logs) are rolled back.
- The memory slice is returned to the caller as **return data**, the same way `RETURN` returns data. The caller can read it with `RETURNDATASIZE` / `RETURNDATACOPY` from [EIP-211](/eip-211/).
- Gas cost is the same as `RETURN`: you pay only for memory. The remaining gas goes back to the caller.
- The calling `CALL` (or `CREATE` / `CREATE2`) pushes `0`, as it does for any failure. For a creation, no code is deployed.
- If there is not enough gas for `REVERT` itself, or a stack underflow happens, it acts as a normal out-of-gas error and consumes all gas.

The EIP does not define what the returned bytes mean. Solidity encodes them as `Error(string)` for `require(cond, "msg")` (selector `0x08c379a0`), `Panic(uint256)` for assertion and arithmetic failures, and ABI-encoded custom errors for `revert MyError(...)`.

## Sample code

High-level Solidity compiles to `REVERT`:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Vault {
    error InsufficientBalance(uint256 have, uint256 want);
    mapping(address => uint256) public balance;

    function withdraw(uint256 amount) external {
        uint256 b = balance[msg.sender];
        if (b < amount) revert InsufficientBalance(b, amount); // REVERT with ABI-encoded error
        balance[msg.sender] = b - amount;
        payable(msg.sender).transfer(amount);
    }
}
```

The raw opcode in inline assembly, returning a 32-byte payload:

```solidity
assembly {
    mstore(0, 0x2a)   // put 42 in memory
    revert(0, 32)     // REVERT(offset=0, length=32)
}
```

Decoding the reason off-chain (ethers v6):

```ts
try {
  await vault.withdraw(10n ** 18n);
} catch (e: any) {
  console.log(e.revert?.name, e.revert?.args); // "InsufficientBalance", [have, want]
}
```

## Things to know

- `REVERT` is not free. You still pay for the gas used up to that point. Only the *remaining* gas is refunded, unlike the old invalid-opcode approach.
- `assert` failures in Solidity before 0.8.0 compiled to the invalid opcode `0xfe` and used up all gas. Since 0.8.0 they use `REVERT` with `Panic(uint256)`.
- The return-data format for reasons was proposed separately in [EIP-838](/eip-838/), which remains a Draft. The `Error(string)` convention is defined by Solidity, not by an EIP.
- Shipped in Byzantium together with [EIP-211](/eip-211/). See [EIP-609](/eip-609/) for the full fork list.


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-140
