Deploy Your First Smart Contract
Last updated
Was this helpful?
Last updated
Was this helpful?
We will walk through creating and deploying a simple smart contract on the Goerli test network using a virtual wallet (), , and (donβt worry if you donβt understand what any of this means yet, we will explain it!).
There are many ways to make requests to the Ethereum chain. For simplicity, weβll use a free account on Alchemy, a blockchain developer platform and API that allows us to communicate with the Ethereum chain without having to run our own nodes. The platform also has developer tools for monitoring and analytics that weβll take advantage of in this tutorial to understand whatβs going on under the hood in our smart contract deployment.
Once youβve created an Alchemy account, you can generate an API key by creating an app. This will allow us to make requests to the Goerli test network. If youβre not familiar with testnets, check out .
Navigate to the βCreate Appβ page in your Alchemy Dashboard by hovering over βAppsβ in the nav bar and clicking βCreate Appβ
Name your app βHello Worldβ, offer a short description, select βStagingβ for the Environment (used for your app bookkeeping), and choose βGoerliβ for your network.
Double-check that you're selecting the Goerli testnet!
Click βCreate appβ and thatβs it! Your app should appear in the table below.
After you input your Metamask account address and click βSend Requestβ, you should see a response that looks like this:
NOTE: This result is in wei not eth. Wei is used as the smallest denomination of ether. The conversion from wei to eth is: 1 eth = 10^18 wei. So if we convert 0x2B5E3AF16B1880000 to decimal we get 5*10^18 which equals 5 eth. Phew! Our fake money is all there π€.
It doesnβt really matter how you answer the installation questions, here is how we did it for reference:
Approve the package.json and weβre good to go!
Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software. It helps developers when building smart contracts and dApps locally before deploying to the live chain.
Inside our hello-world
project run:
Inside our hello-world
project folder, run:
You should then see a welcome message and option to select what you want to do. Select βcreate an empty hardhat.config.jsβ:
This will generate a hardhat.config.js
file for us, which is where weβll specify all of the set up for our project (on step 13).
To keep our project organized weβll create two new folders. Navigate to the root directory of your hello-world project in your command line and type
contracts/
is where weβll keep our hello world smart contract code file
scripts/
is where weβll keep scripts to deploy and interact with our contract
1. Navigate to the βcontractsβ folder and create a new file called HelloWorld.sol
This is a super simple smart contract that stores a message upon creation and can be updated by calling the update
function.
Weβve created a Metamask wallet, Alchemy account, and written our smart contract, now itβs time to connect the three.
Every transaction sent from your virtual wallet requires a signature using your unique private key. To provide our program with this permission, we can safely store our private key (and Alchemy API key) in an environment file.
First, install the dotenv package in your project directory:
This is a super simple smart contract that stores a message upon creation and can be updated by calling the update
function.
Your environment file must be named .env or it won't be recognized as an environment file.Do not name it process.env or .env-custom or anything else.
See below to get HTTP Alchemy API URL
Your .env
should look like this:
To actually connect these to our code, weβll reference these variables in our hardhat.config.js
file on step 13.
To actually connect these to our code, weβll reference these variables in our hardhat.config.js file on step 13.
In your project directory type:
Weβll also require ethers in our hardhat.config.js
in the next step.
Weβve added several dependencies and plugins so far, now we need to update hardhat.config.js so that our project knows about all of them.
Update your hardhat.config.js
to look like this:
To make sure everything is working so far, letβs compile our contract. The compile task is one of the built-in hardhat tasks.
From the command line run:
You might get a warning about SPDX license identifier not provided in source file
, but no need to worry about that β hopefully everything else looks good!
Now that our contract is written and our configuration file is good to go, itβs time to write our contract deploy script.
Navigate to the /scripts
folder and create a new file called deploy.js
, adding the following contents to it:
A ContractFactory
in ethers.js is an abstraction used to deploy new smart contracts, so HelloWorld
here is a factory for instances of our hello world contract. When using the hardhat-ethers
plugin ContractFactory
and Contract
, instances are connected to the first signer (owner) by default.
-- CODE language-js line-numbers -- const hello_world = await HelloWorld.deploy();
Calling deploy()
on a ContractFactory
will start the deployment, and return a Promise that resolves to a Contract object. This is the object that has a method for each of our smart contract functions.
Weβre finally ready to deploy our smart contract! Navigate to the command line and run:
You should then see something like:
Please copy and paste this address to save it somewhere, as we will be using this address for later tutorials, so you don't want to lose it.
The From address should match your Metamask account address and the To address will say βContract Creationβ but if we click into the transaction weβll see our contract address in the To field:
Congrats! You just deployed a smart contract to the Ethereum chain π
Here youβll see a handful of JSON-RPC calls that Hardhat/Ethers made under the hood for us when we called the deploy()
function. Two important ones to call out here are eth_sendRawTransaction
, which is the request to actually write our contract onto the Ropsten chain, and eth_getTransactionByHash
which is a request to read information about our transaction given the hash (a typical pattern when sending transactions).
We need an Ethereum account to send and receive transactions. For this tutorial, weβll use Metamask, a virtual wallet in the browser used to manage your Ethereum account address. If you want to understand more about how transactions on Ethereum work, check out from the Ethereum foundation.
You can download and create a Metamask account for free . When you are creating an account, or if you already have an account, make sure to switch over to the βGoerli Test Networkβ in the upper right (so that weβre not dealing with real money).
In order to deploy our smart contract to the test network, weβll need some fake Eth. To get Eth you can go to the and enter your Goerli account address, then click βSend Me Eth.β It may take some time to receive your fake Eth due to network traffic. (At the time of writing this, it took around 30 minutes.) You should see Eth in your Metamask account soon after!
To double check our balance is there, letβs make an request using . This will return the amount of Eth in our wallet. Check out for instructions on how to use the composer tool!
First, weβll need to create a folder for our project. Navigate to your and type:
Now that weβre inside our project folder, weβll use npm init to initialize the project. If you donβt already have npm installed, follow (weβll also need Node.js so download that too!).
Check out this page for more details on .
Open up the hello-world project in your favorite editor (we like ). Smart contracts are written in a language called Solidity which is what we will use to write our HelloWorld.sol smart contract.β
2. Below is a sample Hello World smart contract from the that we will be using for this tutorial. Copy and paste in the contents below into your HelloWorld.sol file, and be sure to read the comments to understand what this contract does:
To learn more about sending transactions, check out on sending transactions using web3.
Follow to export your private key
Ethers.js is a library that makes it easier to interact and make requests to Ethereum by wrapping with more user friendly methods.
Hardhat makes it super easy to integrate for additional tooling and extended functionality. Weβll be taking advantage of the for contract deployment ( has some super clean contract deployment methods).
Hardhat does an amazing job of explaining what each of these lines of code does in their , weβve adopted their explanations here.
If we go to the and search for our contract address we should able to see that it has been deployed successfully. The transaction will look something like this: