🌎 Hello World Smart Contract on Polygon
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 Polygon Mumbai 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 using Hardhat
Step 1: Connect to the Polygon network
There are many ways to make requests to the Polygon chain. For simplicity, we’ll use a free account on Alchemy, a blockchain developer platform and API that allows us to communicate with the Polygon 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, you can 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 Polygon Mumbai 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), click "Polygon" for the Chain, and choose “Polygon Mumbai” for your network.
Click “Create app” and that’s it! Your app should appear in the table below.
Step 3: Create an wallet address
Since Polygon is a Layer-2 scaling solution for Ethereum, we need to get an Ethereum wallet and add a custom Polygon URL to send and receive transactions on the Polygon network. For this tutorial, we’ll use Metamask, a virtual wallet in the browser used to manage your wallet address. If you want to understand more about how transactions on Ethereum work, check out this page from the Ethereum foundation.
To get your customer Polygon RPC URL from Alchemy, go to your "Hello World" app in your Alchemy dashboard and click "View Key" in the top right corner. Then go ahead and copy your Alchemy HTTP API key!
You can download and create a Metamask account for free here. Once you've created an account, follow these steps to set up the Polygon network on your wallet.
Select “Settings” from the drop down menu on the top right corner of your Metamask wallet.
Select “Networks” from the menu to the left.
Connect your wallet to the Mumbai Testnet using the following parameters.
Network Name: Polygon Mumbai Testnet
Step 4: Add Polygon Mumbai Test MATIC from a Faucet
In order to deploy our smart contract to the test network, we’ll need some fake MATIC. To get MATIC, you can go to the Polygon Mumbai Faucet, select "Mumbai", choose "MATIC Token", and enter your Polygon wallet address, then click “Submit.” 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. Select "Polygon" as the chain, "Polygon Mumbai" as the network, "eth_getBalance" as the method, and input your address. This will return the amount of MATIC 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:
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 0xde0b6b3a7640000 to decimal we get 1*10^18 which equals 1 eth, which can be mapped to 1 MATIC based on denomination.
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!
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.
hello-world project run:
Check out this page for more details on installation instructions.
Step 8: Create Hardhat project
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).
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:
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
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.
- Navigate to the “contracts” folder and create a new file called
- 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:
This is a super simple smart contract that stores a message upon creation and can be updated by calling the
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:
Then, create a
.env file in the root directory of our project, and add your Metamask private key and HTTP Alchemy API URL to it.
Your environment file must be named
.env or it won't be recognized as an environment file.
Do not name it
.env-custom or anything else.
WARNING: If you are using version control system like git to manage your project, please DO NOT track the .env file. Add .env to your .gitignore file so that you don't accidentally publish your secrets to the world
- Follow these instructions to export your private key
- To get your Alchemy HTTP API Key (RPC URL), go to your "Hello World" app in your Alchemy dashboard and click "View Key" in the top right corner. Then go ahead and copy your Alchemy HTTP API key!
.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.
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:
We’ll also require ethers in our
hardhat.config.js in the next step.
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.
hardhat.config.js to look like this:
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:
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! 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.
Navigate to the
scripts/ folder and create a new file called
deploy.js , adding the following contents to it:
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.
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
Contract, instances are connected to the first signer (owner) by default.
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.
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 Polygon Mumbai explorer and search for our contract address we should able to see that it has been deployed successfully.
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
Congrats! You just deployed a smart contract to the Polygon 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”.
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 Polygon chain, and
eth_getTransactionByHash which is a request to read information about our transaction given the hash (a typical pattern when sending transactions).
That’s all for this tutorial! Once you complete this tutorial, let us know how your experience was or if you have any feedback by tagging us on Twitter @alchemyplatform!