In this guide you will learn how to write, compile and deploy a smart contract into Ethereum mainnet or any of its testnets.
A smart contract is a collection of code and data that resides at a specific address on the Ethereum blockchain network. Smart contracts are also a specific type of Ethereum account (they have balance and they can send transactions). However, they're not controlled by a user: they are deployed to the blockchain network and run as programmed. Users (with their Ethereum accounts) can interact with smart contracts by submitting transactions that execute functions defined on those smart contracts.
To deploy a contract to a Ethereum blockchain network it is necessary (apart from writting the contract itself) to have enought ether (ETH) on your account to carry out this operation. Deploying a smart contract is also a transaction, like a simple ETH transfer, so you will need to pay an amount of gas for contract deployment. There are two main programming languages for writting smart contracts: Solidity (similar to JavaScript) and Vyper (similar to Python). In this guide we will focus on smart contract programming using Solidity.
Solidity is an object-oriented, high-level language for implementing smart contracts. It has being designed to target the Ethereum Virtual Machine (EVM), is statically typed, supports inheritance, libraries and complex user-defined types (among other features). After writing a smart contract and before deploy it to a blockchain network, it is mandatory to compile that contract. There are multiple ways to install/use Solidity compiler: npm, Docker, Linux packages, macOs packages, binaries or building from source. I recommend using npm / Node.js Solidity compiler because it is the easiest alternative. To install it execute npm install -g solc
on your favourite terminal. After installation verify using solcjs --version
.
In order to present Solidity main features, directory examples includes a series of smart contracts that use some of those characteristics.
The Ethereum Virtual Machine or EVM is the runtime environment for smart contracts in Ethereum. It is completely isolated, which means that code running inside the EVM has no access to network, filesystem or other processes. Smart contracts even have limited access to other smart contracts. EVM key points will be exposed down below:
- Accounts. There are two types of accounts in Ethereum: external and contract. The former type is controlled by public-private pair (i.e. humans), while the latter is controlled by the code stored together with the account. Both kinds of account are treated equally by the EVM. Furthermore, every account has a balance in Ether (exactly in Wei, where
1 ether = 10^18 wei
) which can be modified by sending transactions including Ether. - Transactions. A transaction is a message that is sent from one account to another and it can include binary data (called payload) and Ether. If the target account is a contract account, its code is executed and the payload is provided as input data. If the target account is not set, the transaction creates a new contract.
- Gas. When creating a transaction, this one is charged with a certain amount of gas. Gas purpose is to limit the amount of work that is needed to execute the transaction and to pay for this execution at the same. Moreover, the gas price is set by the creator of the transaction, who will have to pay
gas_price * gas
up front from the sending account. If some gas is left after execution, it will be refunded to the transaction creator. - Storage, memory and the stack. The EVM has three areas where it can store data:
- Storage. This area is persistent between functions calls and transactions. Furthemore, it is the most expensive area (talking in terms of gas consumption) since it is costly to read, and even more to initialise and modify. Because of this cost, it is highly recommended to minimise saving data in persistence storage.
- Memory. The second area is freshly cleared for each message call. It is not a persistent area and is more costly the larger it grows (at the time of expansion, the cost in gas must be paid).
- The stack. The EVM is not a register machine but a stack machine, so all computations are performed on a data area called the stack.
For newcomers it is highly recommended to start programming Solidity using Remix. Remix IDE is an open source web and desktop application that allows a fast development cycle and has a rich set of plugins with intuitive GUIs. Remix is used for the entire journey of smart contract development as well as being a playground for learning and teaching Ethereum. It includes editor, compiler and debugger (among other features) in one single site/app.
It is written in JavaScript and, as said previously, supports both usage in the browser and in the desktop. To try web application visit https://remix.ethereum.org, and for desktop version, check latest releases on ethereum/remix-desktop GitHub repository. For further details, please consult this tutorial covering Remix IDE key points.
It is a blockchain development suite (probably the most popular one). Currently it it composed by 4 tools/projects:
- Truffle. A development environment, testing framework and asset pipeline for blockchains using the Ethereum Virutal Machine.
- Ganache. A personal blockchain for Ethereum development that can be used to deploy contracts and/or develop applications.
- Drizzle. A collection of front-end libraries that make writing dapp front-ends easier. It includes libraries to simplify dapps development in plain JavaScript, React and Vue.
- Truffle Teams. It is a DevOps tool for Dapps that allows testing efficiently with hosted continous integration or monitoring deployed contract balances, transactions and events.
All these four libraries are great, but the two most interesting for us are Truffle, the development environment, and Ganache, the one click blockchain.
While Truffle can be only installed through npm (npm install -g truffle
), Ganache is available as both a desktop application as well as a command-line tool. On the one hand, Ganache UI (or just Ganache) is a desktop application supporting Ethereum technology. On the other hand, ganache-cli is the command line version of Ganache, which can be dowloaded through npm (npm install -g ganache-cli
). All versions of Ganache are available for Windows, Mac and Linux. A simple Ganache tutorial has been written as a starting point using this tool. Also a Truffle tutorial has been developed.
Also known as VS Code, Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, macOS and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js, having also a rich set of extensions for other languages such as Java, Python, C# or Solidity. In order to install VS Code, please check Visual Studio Code dowloads site, where you can find the VS Code version that fits better on your computer.
After installing it (or if you had it already installed), select 'Extensions' tab (1 in image below), search the desired extension writing solidity on the text box (2 in image below) and install the extension developed by Juan Blanco (3 in image below). As at 2nd March 2021, it is also the Solidity extension with more downloads.
There are multiple ways to deploy a smart contract in a blockchain network. In this section, we will learn how to deploy a contract in a Ganache network using Remix IDE and using web3.js JavaScript library. In both cases is mandatory having a Ganache network running.
It is the easiest and fastest way to deploy a smart contract. First, Select Deploy & Run Transactions Remix plugin. Then, choose Web3 Provider environment (1 in image below) and a modal dialog will be prompted. It will require a Web3 Provider Endpoint, which is the Ganache RPC Server URL (2). After entering endpoint, below environment select box, network id is shown (3). Also, Ganache accounts are displayed (4).
Now, you can deploy a smart contract in your Ganache blockchain network with any of your accounts.
web3.js is a JavaScript library that allows you to interact with a local or remote Ethereum node through HTTP(s), IPC or WebSockets. It has multiple installation ways: Node/npm, Yarn, using the prebuilt JavaScript file or through a CDN. A quick tutorial indicating how to deploy a smart contract and how to interact with it using web3.js is here.
Note: It has been used web3.js as it could be used Web3 library for Java (web3j), Python (web3.py) or Ruby (ethereum.rb).
Before starting this section, please read the previous one.
Deploying a smart contract in a Ethereum testnet is quite similar that doing it in our local Ganache network. Nevertheless, it has two additional requirements: a way to interact with the desired network (Infura or Metamask) and valid ether from that network (they can be obtained through a faucet)
It is a development suite that provides instant and scalable API access to the Ethereum networks. Using Infura requires an account in their system. You can register on Infura for free.
Once your registration has been completed, you can create an Ethereum project on Infura. First, click on Ethereum tab and press button CREATE NEW PROJECT. After project creation, it will appear in your dashboard. Click on it and select tab SETTINGS, where you can find endpoints for Mainnet and some Testnets (Ropsten, Kovan, Rinkeby y Görli). Copy one of those endpoint URLs and use it to interact with the desired network.
In order to verify Infura usage, example 7 shows how to deploy a smart contract through Infura endpoint using web3.js library.
Metamask is an extension for accessing Ethereum network in several browsers as Chrome, Firefox, Brave or Edge. The extension injects the Ethereum web3 API into every website's javascript context, so that dapps can read from the blockchain. It can be dowloaded here and it looks like this:
Export an account created on Metamask is quite simple. First, click on 3 dots button (1 in image below) and select Account details (Detalles de la cuenta). This will open another screen, where it is possible to export the private key of the account (2, in image below).
It is also possible to connect your Metamask wallet with Remix IDE. Go to DEPLOY & RUN TRANSACTIONS plugin and select a Injected Web3 environment. A Metamask window will be automatically prompted asking for permission.
A Ethereum Faucet is an online platform that rewards users with small amounts of ether for completing microtasks or viewing online ads. However, testnets faucets do not require any of these, since that ether is worthless and only valid for testing purposes. For instance, faucet.ropsten.be/ is a Ropsten faucet that send one ether to the account you enter, as shown down below:
Other faucets are Metamask faucet or Dimensions Network faucet. It is important to know that faucets usually blacklist accounts and or IP address during 24-48 hours to avoid users get unlimited ether on their accounts.
Before starting this section, please read the previous one.
Finally, to deploy a smart contract in Ethereum mainnet you need real ether. When deploying a contract in this network, it must be tested several times beforehand. Executing transactions should be also limited, since each one consumes gas, with an associated expenditure of money.
The steps to follow should be the same as for the deployment in testnets: write the smart contract, compile it with Remix/Solidiy compiler and deploy it using web3.js library+Infura or Remix+Metamask.
Installing the Solidity Compiler
The Ethereum Virtual Machine (EVM)
Truffle - Smart contract development environment
Ganache - A one click blockchain