How to Deploy a Smart Contract?
Last updated
Last updated
Wallet application - for deploying and connecting with blockchain networks. We recommend MetaMask.
Etherscan API key - this can be obtained from Etherscan's website: here.
Infura project ID - this can be obtained from the Infura website: here.
The following sections will focus on using the Sepolia testnet as the blockchain network to deploy a smart contract. We recommend having some test tokens from one of the faucets before attempting to deploy any contract.
There are several methods for deploying smart contracts. On this page, we provide methods using the below tools and frameworks:
The choice of tool depends on each individual's requirements, and the coding language of each tool. Feel free to pick the one you feel most comfortable with.
The Solidity example provided here can be used for testing the deployment of each tool you would like to try. You may write your own smart contract and use this page as a guideline for deployment.
Using Remix IDE as a tool for writing and deploying smart contracts is a simple procedure. Remix offers a web-based IDE service anyone can access to write and deploy smart contracts through their web browsers. You may also download Remix to your machine.
To access Remix, visit their website here.
Once on the Remix interface, one of the quickest ways to begin is by using their default template. You can initiate the process by choosing Start Coding.
In the project boilerplate, there are a few directories which might need to be acknowledged by the user/s before modifying the code. We recommend reading either the <README.txt>
file or the relevant developer documents for more information.
In this guide, we are going to simplify the deployment process by using the default directories and files from this boilerplate.
Remix also supports connectivity with wallet browser extensions, such as MetaMask.
On the left sidebar, click on the "Deploy & run transactions" section. Select the very first dropdown box and then select the wallet that you want to connect. In this picture, we use MetaMask Flask as a hot wallet which already connects to the Sepolia testnet.
We can write our own smart contract by simply adding a new file and calling it <MyContract.sol>
, as in the figure below.
At this point, the Solidity smart contract will have to be compiled before deploying. Check out "Solidity compiler" from the left sidebar and press "Compile MyContract.sol" as in the picture below.
After the smart contract has been compiled successfully, you may now deploy it. Head to the "Deploy & run transactions" section from the left sidebar to use the interface for deployment. If your smart contract requires constructor(s) as initial parameters for deployment, you must assign the value at this step on the Remix interface.
For this script method, we will be using <deploy_with_ethers.ts>
as the main file for deploying smart contracts onto the network. In this deployment file, the only place you have to edit is the contract name and the arguments (if needed).
Remix allows users to run an individual file by right-clicking and pressing "Run".
It could take a little while after pressing Run for the smart contract to be deployed. The terminal will show the deployed address once complete.
After deploying, the verification of this deployed contract can be done. Remix has a native plugin for verifying smart contracts on Etherscan. Before that, the plugin will have to be activated by selecting the "Plugin manager" from the sidebar in the bottom-left hand corner, as shown in the below figure.
You must provide the API key from Etherscan.
Now we can do the verification by filing the contract name and contract address of the deployed smart contract.
We can now access our smart contract details through Etherscan.
Once you are in the Hardhat project and finish writing your smart contract, you can use the command below to compile your contract.
You might have to configure hardhat.config.ts
for connecting to the mainnet or testnet, depending on your requirements.
We will use the Sepolia testnet as an example of the network that we would like to deploy the smart contract on.
Once you have finished writing your smart contract, you can use the command below to compile your contract:
The script provided here is an example of a deploy script for a Hardhat project. The parameters for "constructor" are optional, depending on the constructor of each smart contract.
Next, you can deploy your smart contract to the blockchain network, using this Hardhat command:
After deployment, you can verify your smart contract by configuring the Hardhat config in the below code block and using the Etherscan API key.
Next, use this CLI to verify your smart contract through Hardhat:
We can now access our smart contract details through Etherscan.
For smart contract deployment using Foundry, we recommend exploring their documents here.
There are several ways to deploy smart contracts on a specific blockchain network using Foundry Forge. In this tutorial, we will focus on a simple method that you can follow.
To begin, after you have created the Foundry project, you can now configure your <.env>
file, as in the example below. This is for assigning the crucial variables, such as the wallet’s private key. Keep in mind that the variable names depend entirely on the developer.
Foundry projects consist of many directories, including test, script, etc.
This method of forge create
is probably the simplest way to deploy your smart contracts using Foundry Forge.
We will be using the smart contract file named Sample.sol
, with the contract name SampleContract
as an example for this scenario. Since we have other details in the .env
file set up already, we can use the variables from that file - assuming that this smart contract does not require parameters when deploying.
The contract name is defined in the Solidity smart contract file as in the code block below:
Once you have successfully deployed your smart contract, you can verify it by using either Etherscan or Foundry CLI.
Use the command below to verify your smart contract on Etherscan using CLI.
This means that we can fill the required values from the example as in the line below. Given that the chain ID of Sepolia testnet is 11155111.
You have now successfully verified your smart contract on Etherscan.