If you have dabbled in web3, you have likely heard about the programing language Solidity. Solidity is the world's most popular way to write smart contracts which are then compiled and deployed to the Ethereum Virtual Machine (EVM). After these programs are deployed, they exist on-chain and can be interacted with by anyone. These contracts make up decentralized exchanges, NFT projects, lending protocols, and a plethora of other decentralized applications.
With Hedera’s testnet update today, we’re able to begin using 'smart contracts 2.0', the EVM compatible Hedera Smart Contract Service for building scalable decentralized applications. If you’re starting out, here is an overview and some helpful resources to jumpstart your journey. If you want to dive in headfirst and get hands-on, check out our deploying your first smart contract tutorial.
Ethereum Virtual Machine (EVM)
The EVM is a distributed computing environment that executes smart contract logic. It is a virtual machine, an abstraction between executing code and hardware. The EVM executes smart contract logic by compiling the smart contract down to a set of opcodes.
Opcodes are low-level instruction sets used by computers to perform basic operations that make up the functionality of computers. If you are curious about the EVM's opcodes check out this full list of them and what they do or get an idea of the Hedera supported opcodes. This is what is happening under the hood when someone is interacting with a smart contract.
While the EVM did come out of the Ethereum ecosystem it also runs on a variety of other protocols including Hedera. Hedera uses the open-source Hyperledger Besu EVM. Regardless of the implementation, the demand for distributed computing environments is high. And the demand for developers who can write smart contracts is even higher. The most popular and widely used is solidity, so let's get into it.
Development Frameworks
When writing a smart contract, you likely want to be able to run tests, deploy to testnets, and make sure things are working before you deploy your contract to the mainnet. This is why almost everyone uses a development framework when writing or programmatically interacting with smart contracts. These frameworks provide us with tools to test, deploy, and interact with smart contracts in a variety of languages. One of the most popular frameworks is HardHat, a JavaScript framework. Others include Brownie (Python) and Foundry (Rust). Foundry is neat because it also supports fuzzing tests with your contracts; a great way to look for security vulnerabilities and anomaly behavior.
To start writing the smart contract, pick a development framework that works for you and learn its available tools.
Gas
When smart contracts are executed, a fee must be paid to the network validators to execute transactions. The computational unit of measurement used to denote this is called gas. In Ethereum, the price of gas depends on the congestion of the network. This is subject to change in Eth2 and varies depending on the network. Be sure to learn about how the network you are building on determines its gas price.
Solidity Contracts
All Solidity contract files end in a .sol extension and have a line at the top that specifies what version of solidity they are to be compiled with. For example the line
pragma solidity >=0.4.16 <0.9.0;
is telling the compiler this contract is written for version 0.4.16 up to but not including version 0.9.0.
After the compiler version is specified each contract starts with the contract keyword followed by the name of your contract and an opening and closing curly brace. It has become common practice to have the contract names be camel case. All the logic of the contract will reside within the curly brace.