Smart Contract EVM: What It Is and How It Works
The Ethereum Virtual Machine (EVM) is the sandboxed runtime that every Ethereum node uses to execute smart contracts: programs stored on the blockchain that run exactly as written whenever a transaction calls them. Ethereum’s EVM documentation describes it as “a decentralized virtual environment that executes code consistently and securely across all Ethereum nodes.” Developers write contracts in a language such as Solidity, compile them to EVM bytecode and pay for every computation step in gas. Many other networks run the same EVM, so contracts, tools and even addresses carry over between them.
The EVM at a Glance
| Item | Details |
|---|---|
| What it is | The runtime that executes smart contract bytecode on every Ethereum node |
| Design | A stack machine whose stack holds up to 1,024 items of 256 bits each |
| Languages | Solidity (dominant) and Vyper, compiled to EVM bytecode |
| Fees | Gas: every operation has a cost; under EIP-1559 the base fee is burned and the tip goes to the block producer |
| Determinism | The same state and the same transaction give the same result on every node |
| Main risk | Bugs in code that can’t easily be changed after deployment, such as the 2016 DAO exploit |
| Where it runs | Ethereum and EVM-compatible networks such as BNB Smart Chain, Polygon, Avalanche and Ethereum’s layer 2 rollups |
What Is a Smart Contract?
Blockchain smart contracts are self-executing programs that run on decentralized networks, automatically enforcing agreements without intermediaries. They were first proposed in the 1990s by Nick Szabo and popularized by Ethereum as a core feature of its blockchain. A smart contract executes its code once predefined conditions are met, enabling trustless transactions. A classic analogy is a vending machine: if you insert the correct amount and select an item, the machine dispenses it with no need for a shopkeeper or middleman. Because they run on a blockchain, the results are transparent, traceable and irreversible.
“A smart contract is a computerized transaction protocol that executes the terms of a contract.” – Nick Szabo, Smart Contracts (1994)
Ethereum was the first blockchain platform to implement this concept at scale, allowing developers to write their own smart contracts and deploy them on a live blockchain. These contracts have unlocked a world of decentralized applications (dApps), from finance to gaming, all powered by code instead of centralized entities. What makes this possible under the hood is the Ethereum Virtual Machine, the engine that runs Ethereum’s smart contracts on every node in the network and ensures each contract executes exactly as coded.
How Does the Ethereum Virtual Machine (EVM) Work?
The EVM (Ethereum Virtual Machine) is the runtime environment for smart contracts in Ethereum. You can think of it as a global, decentralized computer that exists across all Ethereum nodes: every node (a computer running Ethereum software) includes an instance of the EVM, and they all run the same instructions to keep the network in sync. Technically it is a stack machine: instructions push and pop values on a stack that holds up to 1,024 items, each a 256-bit word, a size that matches the 256-bit hashes and keys Ethereum works with.
When a smart contract is deployed, it is compiled from high-level code (like Solidity) into low-level bytecode, a series of opcodes (operations) that the EVM can execute. The bytecode is stored on the blockchain at the contract’s address. When someone later interacts with the contract, for example by calling one of its functions in a transaction, nodes execute that bytecode in their EVM. The EVM is a sandboxed, isolated environment: the code runs without direct access to the node’s file system or network, which keeps execution deterministic and secure across the network.
The EVM executes smart contract code in a deterministic manner. Given the same starting state and the same transaction input, every node’s EVM produces the same output, and this is crucial for consensus: all nodes must agree on the outcome of contract executions. In practice, the validator that proposes a block executes its transactions and commits to the resulting state, and every other node re-executes the same transactions to check that result, for example that 5 tokens moved from Alice to Bob. A node that arrives at a different state rejects the block and falls out of step with the rest of the network, which is why any non-determinism in the EVM would break consensus.
To maintain fairness and prevent abuse, Ethereum uses a mechanism called gas. Every operation the EVM can execute (adding numbers, storing data, sending coins and so on) has an associated gas cost. When users send a transaction that invokes a smart contract, they set a gas limit and pay a fee in ether for the computation. If execution exceeds the gas limit, it is halted and its changes are reverted, although the fee for the gas already used is still paid; this keeps infinite loops and extremely resource-intensive tasks from clogging the network. Under EIP-1559, the fee has two parts: a base fee that the protocol burns, and a priority fee, or tip, that the block producer keeps. Since Ethereum’s switch to proof of stake in 2022, that producer is a validator rather than a miner.
In summary, the EVM works as a global interpreter for Ethereum smart contracts: it takes the contract bytecode plus any input from transactions, executes the code step by step (charging gas for each step) and produces an outcome that updates the blockchain’s state once the network agrees on it.
What Are the Key Features of Smart Contracts on the EVM?
Smart contracts running on the EVM have a few fundamental characteristics that make them powerful and unique compared to traditional software:
-
Immutability: Once a smart contract is deployed to Ethereum, its code typically cannot be changed or tampered with. The contract resides on the blockchain, and any attempt to alter it would be rejected by the network. This immutability ensures that the contract runs as originally programmed: even its creator cannot directly modify it after deployment. (There are patterns for upgradeable contracts, but they must be built in from the start.) Immutability is a double-edged sword: it keeps the contract’s behavior predictable, but if the code has a bug, it can’t easily be fixed.
-
Security and trustlessness: Smart contracts enable trustless interactions. Because the code executes automatically and the blockchain verifies outcomes, participants don’t need to trust a third party or each other; they trust the code. For example, if two parties set up a contract for payment on delivery, the funds are released only when the coded conditions are met, and neither party can change the terms afterwards. Ethereum’s decentralization adds to this: the contract is run and verified by thousands of independent nodes, so there is no single server that an attacker could take over or switch off, and outcomes are extremely difficult to falsify or censor. (This “security” assumes the contract code itself is well written; the risks section below covers what happens when it isn’t.)
-
Decentralization: Smart contracts inherit the decentralization of the blockchain. The EVM runs on every full node across the globe, so no central authority controls the execution of contracts, and the network keeps running them even when some nodes go offline. No one can override or block a valid contract call. Decentralization also brings transparency: all transactions and contract states are visible on the public ledger, so anyone can audit a contract’s activity. The result is a system that is hard to censor and has no central point that could shut it down.
How Are Smart Contracts Written in Solidity?
How do developers create these smart contracts for the EVM? The most popular language for writing Ethereum smart contracts is Solidity, a high-level, contract-oriented programming language with a syntax similar to JavaScript or C++, designed specifically for Ethereum and the EVM. Developers write the smart contract’s logic in Solidity, then use a compiler to translate that code into EVM bytecode.
Let’s look at a simple example of a Solidity smart contract to see the structure:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 storedData;
function set(uint256 x) public {
storedData = x;
}
function get() public view returns (uint256) {
return storedData;
}
}
In this basic Solidity contract, called SimpleStorage, we define a single variable storedData and two functions. The set function takes an unsigned integer x and stores it in the state variable storedData. The get function is marked view, which means it doesn’t change the state; it simply returns the current value of storedData. This contract allows anyone to store a number (by calling set) and retrieve that number later (by calling get).
A few things to note in this Solidity code:
-
The first line
// SPDX-License-Identifier: GPL-3.0is a license identifier comment (not required for functionality, but good practice). -
The
pragma solidity ^0.8.0;line specifies the Solidity compiler version the code is intended for (here, any 0.8.x version). This helps ensure the code isn’t compiled with an incompatible compiler. -
uint256 storedData;declares a state variable of type 256-bit unsigned integer. State variables are stored on the blockchain and persist between function calls. -
The
setfunction ispublic, meaning anyone (or any other contract) can call it. It writes to the blockchain state by updatingstoredData. (Writing to state costs gas.) -
The
getfunction is also public, but markedviewand returns auint256.viewmeans it does not modify state, so callinggetdoesn’t cost gas if called externally (e.g., via a call from a web3 interface, since it doesn’t create a transaction).
This simple contract demonstrates the basics of a Solidity smart contract: you have state (stored variables) and functions that can modify or read that state. In a real scenario, you would compile this Solidity code (using the Solidity compiler or an IDE like Remix), which produces the EVM bytecode, as in the Solidity documentation’s introduction. You would then deploy that bytecode to the Ethereum network, which is itself a transaction that costs gas. Once deployed, the contract gets its own Ethereum address. Users can interact with the contract by sending transactions to its address, calling the set or get functions.
Solidity has many more features (like events for logging, modifiers and inheritance between contracts), but the example above is enough to illustrate the core idea of writing a smart contract. There are other languages as well (such as Vyper, which has a Python-like syntax), but Solidity remains the dominant language for EVM development. When writing Solidity smart contracts, developers must follow best practices to ensure the code behaves as expected and is secure, because once the contract is deployed, it’s immutable. Next, let’s look at what people are doing with EVM smart contracts.
What Are EVM Smart Contracts Used For?
Ethereum’s smart contracts (and by extension, the EVM) have enabled an explosion of innovation in blockchain applications. Here are a few prominent use cases and examples:
-
Decentralized Finance (DeFi): DeFi is one of the most impactful use cases of EVM smart contracts. These are financial applications built on blockchain that operate without traditional banks or intermediaries. For example, Uniswap is a decentralized exchange running on smart contracts: it lets users swap tokens automatically via liquidity pool algorithms. Aave and Compound are lending protocols where smart contracts let users lend and borrow cryptocurrencies without a bank, using collateral and algorithmic interest rates. Stablecoins like DAI are managed by smart contracts that maintain their peg to the dollar through collateralized debt positions. Contracts with a hash lock and a time lock also make atomic swaps between blockchains possible. By removing middlemen, DeFi platforms can operate 24/7 with potentially lower fees and more transparency.
-
NFTs and Digital Collectibles: Non-Fungible Tokens (NFTs) are unique digital assets (often art, collectibles or in-game items) represented by smart contracts. Ethereum’s ERC-721 and ERC-1155 standards define how NFTs are implemented. A famous example is CryptoKitties, one of the first NFT games, where each kitty is a unique token governed by a smart contract. Marketplaces like OpenSea let users buy and sell NFTs (art, music, virtual real estate and more) through smart contract transactions. NFTs have let artists and creators monetize digital works: the ownership and transfer of an NFT are enforced by the smart contract, which ensures the token is one of a kind and cannot be duplicated.
-
Decentralized Autonomous Organizations (DAOs): DAOs are organizations governed by smart contracts and community voting rather than centralized leadership. The rules and governance procedures of a DAO are encoded in smart contracts, often using token-based voting. For instance, MakerDAO, the project behind the DAI stablecoin (its website now presents it as Sky), is governed by token holders who propose and vote on changes through smart contracts. Another example is The LAO, a venture investment DAO where members pool funds and vote on investments through smart contract mechanisms. DAOs can manage treasuries worth millions of dollars with complete transparency, as all votes and fund movements are executed by the contract code. While the concept is still evolving, it represents a new way of organizing people and resources with minimal centralized control.
-
Gaming and Virtual Worlds: Smart contracts also power blockchain-based games and virtual ecosystems. In these games, in-game assets can be NFTs owned by players, and game rules or marketplaces may be governed by smart contracts. A well-known example is Decentraland, a virtual world where land and items are NFTs and the economy runs on Ethereum smart contracts. Players own their digital land and collectibles and can trade them freely outside the game, while smart contracts enforce scarcity and the rules of the game economy.
-
Supply Chain and Identity: Beyond finance and gaming, EVM smart contracts are being explored for supply chain tracking, digital identity and more. For instance, a supply chain smart contract can automatically release payments when goods arrive (using IoT devices and oracles to feed data), or log the provenance of products on an immutable ledger. Identity platforms might use smart contracts to give users control over their credentials, so that only the individual can grant access to their personal data.
These examples barely scratch the surface. Because smart contracts are general-purpose programs that run on a distributed network, new use cases keep emerging as the technology matures. With that power come real risks, covered next.
What Are the Risks and Security Considerations?
While smart contracts enable powerful new applications, they also introduce significant risks if not implemented carefully. Smart contracts are only as good as their code, and code can have bugs or unintended logic flaws. Here are some important security considerations and risks for EVM smart contracts:
-
Vulnerabilities in Contract Code: A small bug in a smart contract can lead to catastrophic consequences, because once the contract is deployed, its code is law. If there’s a flaw, attackers might exploit it to steal funds or disrupt the contract. One infamous example is The DAO hack in 2016: a Decentralized Autonomous Organization’s smart contract had a vulnerability (a reentrancy bug) that allowed an attacker to withdraw funds repeatedly. About $60 million worth of ether was drained from The DAO before it was stopped. The incident was so severe that it prompted a hard fork of Ethereum, which split the network into Ethereum and Ethereum Classic. Other known vulnerabilities include integer overflow and underflow (an issue until Solidity 0.8.0 made arithmetic revert on overflow and underflow by default), mishandled default function logic, and mistakes in access control (e.g., giving ownership to the wrong address). Developers need to be extremely careful and follow security best practices.
-
No Upgradeability (By Default): As noted, smart contracts are immutable by default. If a bug is discovered after deployment, there’s no simple way to patch it, and funds locked in the contract could be stuck or at risk. Some projects build upgradeable contracts (using proxy patterns or admin keys that can switch logic), but these add complexity and can undermine decentralization if not done carefully. In most cases, if a critical bug is found, the only recourse is to deploy a new, corrected contract and migrate users and funds to it, a process that can be difficult and damage user trust. This is why thorough testing and auditing before deployment are essential.
-
Security Audits and Best Practices: Given the high stakes, most serious smart contract projects undergo security audits by third-party firms, whose auditors review the code for vulnerabilities and logic errors. Using established libraries and templates (such as the OpenZeppelin library for ERC-20 and ERC-721 token contracts) can reduce risk because they are battle-tested. Developers are encouraged to follow practices like the Checks-Effects-Interactions pattern (to avoid reentrancy issues), use static analyzers such as Slither to detect common bugs, and run extensive tests. Bug bounty programs also reward independent researchers for reporting issues responsibly. Smart contract security is an ongoing process that starts at development and continues through deployment and use.
-
Leaked Keys in the Toolchain: Contracts are deployed and administered with ordinary private keys, and development setups often keep those keys next to RPC provider keys and, increasingly, AI assistant keys in the same
.envfile. A leaked deployer or admin key hands an attacker every privileged function the contract has. Every secret in that file deserves the same handling as exchange API keys, OpenAI API keys and Anthropic API keys: out of source control, scoped narrowly and rotated on any suspicion. -
Malicious Contracts and Approvals: Users face a different contract risk. Phishing sites ask people to sign token approvals or transactions that let a drainer contract move tokens out of their wallet, so read what you sign and revoke approvals you no longer need (how to protect a wallet from phishing).
-
Financial and Operational Risks: Even if a smart contract’s code is secure, there are other risks. Smart contracts can manage large amounts of money (think DeFi protocols with hundreds of millions in liquidity), and any flaw attracts attackers because of the high reward. There’s also the risk of user error: sending funds to the wrong contract address or calling the wrong function can result in loss, and there’s usually no recourse (no “customer support” on a decentralized blockchain to undo mistakes). Performance and scalability matter too: complex contracts can consume a lot of gas, making them expensive to run, or hit block gas limits. If a dApp becomes wildly popular (e.g., CryptoKitties in 2017), it can congest the network and drive up fees for everyone.
In summary, while EVM smart contracts bring powerful features, they must be approached with caution: vulnerabilities in smart contracts lead to lost trust and stolen funds. Developers need to code carefully and test thoroughly, and users should prefer audited projects and understand that blockchain transactions are generally final. The Ethereum community has learned from past incidents (like The DAO) and continues to improve security tools and practices to make the smart contract ecosystem safer.
Which Blockchains Are EVM-Compatible?
The EVM not only powers Ethereum but has become a standard of sorts: many other blockchains, such as BNB Smart Chain (formerly Binance Smart Chain), Polygon and Avalanche, have adopted EVM compatibility, allowing them to run Solidity smart contracts and share Ethereum’s development ecosystem. Developers can deploy similar contracts across multiple platforms, and users benefit from a growing interoperable ecosystem of dApps. These networks also share Ethereum’s address format and key derivation, so in wallets such as MetaMask one seed phrase gives the same address on all of them (seed phrase formats by network and what a derivation path is explain why), and an encrypted JSON keystore file works with any of them.
Ethereum keeps evolving: it switched to proof of stake in September 2022 (the Merge) and now scales mainly through layer 2 rollups, which the March 2024 Dencun upgrade (EIP-4844) made cheaper, but the EVM remains a cornerstone of the network. Future improvements may refine how the EVM works or introduce new virtual machine designs (researchers have proposed replacements more than once), but the core idea of a deterministic, sandboxed execution environment is likely to persist.
Understanding the EVM and how smart contracts work is key to participating in the next generation of the web, often called Web3. Whether you are a developer writing the next dApp or a user interacting with a DeFi platform, it helps to know what the Ethereum Virtual Machine is doing behind the scenes. More code-level material, from generating a BIP39 phrase in Python to handling API keys, is collected in our developer guides.
External References:
-
CoinDesk – How Does Ethereum Work? (Overview of EVM and Ethereum)
-
Solidity Documentation – Introduction to Smart Contracts (Simple Storage Example)
-
Investopedia – What Are Smart Contracts? (Definition and Examples)
-
Vibranium Audits – Reentrancy Attacks (The DAO Hack Explained)
-
Hedera – Guide to Smart Contract Security (Common vulnerabilities and practices)