Compile, Deploy, and Use a Smart Contract

Deploy and interact with a smart contract in a development environment.

Coding a smart contract is the first step toward the implementation of a decentralized system. But creating a smart contract is only the first step. Once developed, the next steps are crucial to bringing it to life. Then it’s time to compile, deploy, and interact with it.

Let's go over the steps required to execute a functional smart contract that operates on a blockchain network.

Ethereum developing environment

A smart contract must be compiled first to be deployed and used. A compiler translates code written in one programming language into another language or machine code. The solc compiler is used to convert smart contracts written in Solidity into bytecode that can be executed on the Ethereum Virtual Machine (EVM). 

Although a compiler can be used to do this job, a more comprehensive and advanced tool for smart contract development can provide a range of additional features and benefits that make it a better choice for more complex projects. These tools provide a whole developing environment that allows:

  • Testing: Developers can write automated tests for their contracts.

  • Network Simulation: A local sandboxed blockchain network that developers can use. It is especially useful when testing complex interactions between contracts.

  • Debugging Tools: They make it easier for developers to identify and fix issues in their code.

  • Contract Deployment: Easy-to-use interface for deploying contracts to a blockchain network.

In this lesson, we’re going to use the Hardhat development environment for Ethereum smart contracts. Hardhat has all the previously listed features and a large, active community of developers who contribute to the project and provide support to others. This can be especially helpful for getting help with difficult or complex problems.

Compiling a contract

Hardhat runs in the Javascript runtime environment Node.js. It can be installed through npm (Node Package Manager). The installation process is out of the scope of this lesson. The structure of a Hardhat project typically includes the following files and directories:

  • hardhat.config.js: The configuration file for Hardhat, where various options can be set, such as the network to use, the solc compiler version, and more.

  • contracts/: This directory contains the Solidity source code for the smart contracts. Each smart contract should have its own file.

  • test/: This directory contains the test files for the smart contracts. The tests are written in JavaScript and can be run using Hardhat.

  • scripts/: This directory can contain some automation scripts (like the deploy script).

  • artifacts/: This directory contains the smart contract artifacts. An artifact is a JSONJSON stands for JavaScript Object Notation and is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. It is based on a subset of the JavaScript programming language and is represented by key-value pairs. file with all the information needed to deploy and interact with a smart contract.

  • node_modules/: This directory contains the npm packages that the project depends on, including the Hardhat framework.

  • package.json: This file contains the metadata of the project, including the dependencies and scripts needed to run the project.

The command to compile the smart contracts in the contracts folder is:

Get hands-on with 1400+ tech skills courses.