Estimated time to complete this guide: ~15 minutes
If you are new to blockchain development and donât know where to start, or if you just want to understand how to deploy and interact with smart contracts, this guide is for you. We will walk through creating and deploying a simple smart contract on the Goerli test network using a virtual wallet (Metamask), Solidity, Hardhat, and Alchemy (donât worry if you donât understand what any of this means yet, we will explain it!).
If you have questions at any point feel free to reach out in the Alchemy Discord!
Create and Deploy your Smart Contract
Step 1: Connect to the Ethereum network
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.
If you donât already have an Alchemy account, sign up for free here.
Step 2: Create your app (and API key)
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 this guide.
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.
Step 3: Create an Ethereum account (address)
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 this page from the Ethereum foundation.
You can download and create a Metamask account for free here. 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).
Step 4: Add ether from a Faucet
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 Goerli faucet 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!
Step 5: Check your Balance
To double check our balance is there, letâs make an eth_getBalance request using Alchemyâs composer tool. This will return the amount of Eth in our wallet. Check out this video for instructions on how to use the composer tool!
After you input your Metamask account address and click âSend Requestâ, you should see a response that looks like this:
-- CODE language-js line-numbers --{"jsonrpc": "2.0", "id": 0, "result": "0x2B5E3AF16B1880000"}
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 đ¤.
Step 6: Initialize our project
First, weâll need to create a folder for our project. Navigate to your command line 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 these instructions (weâll also need Node.js so download that too!).
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!
Step 7: Download Hardhat
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.
Check out this page for more details on installation instructions.
Step 8: Create Hardhat project
You should then see a welcome message and option to select what you want to do. Select âcreate an empty hardhat.config.jsâ:
Step 9: Add project folders
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
Step 10: Write our contract
You might be asking yourself, when the heck are we going to write code?? Well, here we are, on Step 10 đ
Open up the hello-world project in your favorite editor (we like VSCode). Smart contracts are written in a language called Solidity which is what we will use to write our HelloWorld.sol smart contract.â
1. Navigate to the âcontractsâ folder and create a new file called HelloWorld.sol
2. Below is a sample Hello World smart contract from the Ethereum Foundation 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:
Step 11: Connect Metamask & Alchemy to your project
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.
To learn more about sending transactions, check out this tutorial on sending transactions using web3.
First, install the dotenv package in your project directory:
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.
- Follow these instructions to export your private key
- See below to get HTTP Alchemy API URL
To actually connect these to our code, weâll reference these variables in our hardhat.config.js file on step 13.
Step 12: Install Ethers.js
Ethers.js is a library that makes it easier to interact and make requests to Ethereum by wrapping standard JSON-RPC methods with more user friendly methods.
Hardhat makes it super easy to integrate Plugins for additional tooling and extended functionality. Weâll be taking advantage of the Ethers plugin for contract deployment (Ethers.js has some super clean contract deployment methods).
In your project directory type:
Step 13: Update hardhat.config.js
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.
Step 14: Compile our contract
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:
If not, you can always message in the Alchemy discord.
Step 15: Write our deploy script
Now that our contract is written and our configuration file is good to go, itâs time to write our contract deploy script.
Hardhat does an amazing job of explaining what each of these lines of code does in their Contracts tutorial, weâve adopted their explanations here.
Step 16: Deploy our contract
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.
If we go to the Goerli etherscan and search for our contract address we should able to see that it has been deployed successfully. The transaction will look something like this:
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 đ
To understand whatâs going on under the hood, letâs navigate to the Explorer tab in our Alchemy dashboard . If you have multiple Alchemy apps make sure to filter by app and select âHello Worldâ.
To learn more about sending transactions, check out this tutorial on sending transactions using Web3.
Once you complete this tutorial, let us know how your experience was or if you have any feedback by tagging us on Twitter @alchemyplatform!