Introduction to Solidity smart contracts
Headshot Waylon
Jan 20, 2022
by Waylon Jepsen
Developer Evangelist

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.

Code Snippet Background
 pragma solidity >=0.4.16 <0.9.0;

contract MyContract {
	 # Logic goes here
}

State Variables

State variables are variables that the contract keeps track of internally and change over time as calls are made to the contract. The modification of state variables requires computation and will thus require a transaction fee denominated in gas.

Types

Solidity is a typed language and includes a variety of built-in data types for our convenience. Some important ones to note are the address type used to specify wallet addresses, the uint32 used to denote unsigned integers of 32 bytes, a boolean bool, and the familiar string data type. There are more included types and if curious I would look at the documentation for a more complete set of the data types included in solidity.

public, public view and public payable

Functions in solidity are specified by the function keyword and then the function's name. Parts can be specified to have some additional properties in solidity. For example, with the view keyword, the function can just return the information without modifying any state variables. This is nice because it doesn't use any computation and thus doesn't cost anything. An example would be a getter function that looks like this.

function get() public view returns(uint32)

A function with the public keyword is available for anyone on-chain to call, public view means anyone can call it and it will return some value to be viewed usually public view functions are followed by the returns keyword with the type of the value to be returned.

An example of a simple contract with a setter and getter would look like this.

Code Snippet Background
pragma solidity >=0.7.0 <0.8.9;

contract MyContract {
	// the message we're storing
    string message;

    function set_message(string memory message_) public {
    // only allow the owner to update the message
   		if (msg.sender != owner) return;
             message = message_;
        }
        
    // return a string
    function get_message() public view returns (string memory) {
         return message;
     }
}

Furthermore, if a function deals with transferring erc20 or erc721 digital assets, the payable keyword is used in the function declaration. Note that the payable keyword only applies to tokens in the ERC standards. If you prefer to transfer a different type of digital asset like a Hedera Token Service minted token, you don't have to use this payable keyword.

Additional Resources

Solidity is a rapidly evolving ecosystem and growing developer community that we're excited to support. To continue your learning here are a few more recommendations:


Once you're ready to code, create a Hedera testnet account and follow our getting started with smart contracts tutorial