Developing smart contracts
This guide will let you get started writing Solidity contracts
In this guide we will be going over the following:
Setting up a Solidity Project
Compiling Solidity Source Code
Adding More Contracts
Using OpenZeppelin Contracts
About Solidity
We won’t be covering language concepts such as syntax or keywords in this guide. For that, you’ll need to check out the following curated content, which features great learning resources for both newcomers and experienced developers:
For a general overview of how Ethereum and smart contracts work, the official website hosts a Learn about Ethereum section with lots of beginner-friendly content.
If you’re new to the language, the official Solidity documentation is a good resource to have handy. Take a look at their security recommendations, which nicely go over the differences between blockchains and traditional software platforms.
Consensys' best practices are quite extensive, and include both proven patterns to learn from and known pitfalls to avoid.
The Ethernaut web-based game will have you look for subtle vulnerabilities in smart contracts as you advance through levels of increasing difficulty.
With that out of the way, let’s get started!
Setting up a Project
The first step is to install a development tool.
The most popular development framework for Ethereum is Hardhat, and we cover its most common use with ethers.js. The next most popular is Truffle which uses web3.js. Each has its strengths and it is useful to be comfortable using all of them.
In this guide, we will show how to develop, test and deploy smart contracts using hardhat
To get started with Hardhat we will install it in our project directory.
Once installed, we can run npx hardhat
. This will create a Hardhat config file (hardhat.config.js
) in our project directory.
First contract
We store our Solidity source files (.sol
) in a contracts
directory. This is equivalent to the src
directory you may be familiar with from other languages.
We can now write our first simple smart contract, called Box
: it will let people store a value that can be later retrieved.
We will save this file as contracts/Box.sol
. Each .sol
file should have the code for a single contract, and be named after it.
Compiling Solidity
The Ethereum Virtual Machine (EVM) cannot execute Solidity code directly: we first need to compile it into EVM bytecode.
Our Box.sol
contract uses Solidity 0.8 so we need to first configure Hardhat to use an appropriate solc version.
We specify a Solidity 0.8 solc version in our hardhat.config.js
Last updated