This section of the Smart Contract Security course covers writing EVM-based smart contracts in Solidity and testing them with JavaScript. We also delve into some common development environments and design patterns.

Code

Smart contract code executes on the Ethereum Virtual Machine (EVM), a single state machine that exists across all nodes connected to the Ethereum network. Code is executed by every machine during the mining processes and corresponding state changes are made. As state-changing code must execute on all machines, processing must be paid for. Each opcode has an associated gas cost. More complex code will be more expensive to run, giving developers a strong incentive to reduce complexity where possible. A list of opcodes and their associated gas costs is available at the link below:

https://evm.codes

A number of high-level languages that compile to EVM bytecode have been developed. Chief among them is the Solidity, which resembles C. The Pythonesque Vyper comes in a distant second place. And there are a few others as well. We will focus on Solidity for this workshop.

Intro to Solidity

Solidity is a statically typed, object-oriented programming language with C-like syntax. It is constantly evolving and not yet at 1.0, with the latest release as of this writing being 0.8.13.

A basic Solidity program is made up of a contract, analogous to a class in other object-oriented languages. This contract can contain state variables and functions, some of which will be externally callable. All smart contract protocols function by having users, administrators and bots execute functions within blockchain transactions.

An empty contract definition looks like this:

contract MyContract is ParentContract, OtherParentContract... {
}

The is syntax allows for multiple inheritance.

Types

All state and local variables must have a declared type. The following types are available: