# Ethereum, the EVM, Accounts and Gas — Blockchain & Smart Contracts (Solidity)

Source: https://www.geekswithgeeks.com/en/solidity/b-ethereum

> Describe accounts, transactions, the EVM and how gas fees work.

## A world computer with metered execution

**Ethereum** is a programmable blockchain. Its state is a set of **accounts**: **externally owned accounts (EOAs)**, controlled by private keys, and **contract accounts**, which hold code and storage and run only when called. Every account has an **address** (20 bytes) and an **ether balance**, measured in **wei** (1 ether = 10^18 wei; 1 gwei = 10^9 wei). A **transaction** is a signed message from an EOA that transfers ether, deploys a contract or calls a contract function; each has a **nonce** to prevent replay. Contracts run on the **Ethereum Virtual Machine (EVM)**, a deterministic stack machine that every node executes identically. Every operation costs **gas**, which prevents infinite loops and prices scarce resources: storage writes are expensive, arithmetic is cheap. Since **EIP-1559**, the fee is `gas used × (base fee + priority fee)`: the **base fee** adjusts automatically with demand and is burned, and the **priority fee** (tip) goes to the validator. You set a **gas limit**; if execution runs out of gas, it reverts but the gas is still spent. Many EVM-compatible chains and **layer 2** rollups (Arbitrum, Optimism, Base, zkSync and others) run the same Solidity code with lower fees.

## Calculating a transaction fee

Fee arithmetic with EIP-1559, in wei (shown as Solidity-style constants).

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

// Illustrative fee arithmetic (all values in wei)
contract FeeMath {
    uint256 public constant GWEI = 1e9;

    function fee(uint256 gasUsed, uint256 baseFeeGwei, uint256 tipGwei)
        external
        pure
        returns (uint256 totalWei, uint256 burnedWei, uint256 toValidatorWei)
    {
        burnedWei = gasUsed * baseFeeGwei * GWEI;       // base fee is burned
        toValidatorWei = gasUsed * tipGwei * GWEI;      // priority fee goes to the validator
        totalWei = burnedWei + toValidatorWei;
    }
}

// A plain ether transfer uses 21,000 gas.
// With a 20 gwei base fee and a 2 gwei tip:
//   21,000 * 22 gwei = 462,000 gwei = 0.000462 ether
```

## A taxi meter

Gas is a taxi meter for computation: every step of your contract adds to the fare. The base fee is the city's surge price (which is destroyed rather than kept), the tip persuades a driver to pick you up first, and the gas limit is the maximum fare you agreed to pay.

**Quiz:** What happens if a transaction runs out of gas?

- [ ] It completes for free
- [ ] The validator pays the rest
- [ ] It is retried automatically
- [x] Its state changes are reverted, but the gas used is still paid

*Answer:* Its state changes are reverted, but the gas used is still paid. Out-of-gas reverts the effects; the computation already performed is still charged.
