Hardhat Env
This section will guide you through deploying an NFT smart contract (ERC-721) on the Glide network using Hardhat.
Hardhat is a developer tool that provides a simple way to deploy, test, and debug smart contracts.
Objectives
By the end of this tutorial, you should be able to do the following:
Setup Hardhat for Glide
Create an NFT smart contract for Glide
Compile a smart contract for Glide
Deploy a smart contract to Glide
Interact with a smart contract deployed on Glide
Prerequisites
Node v18+
This tutorial requires you have Node version 18+ installed.
Download Node v18+
If you are using nvm
to manage your node versions, you can just run nvm install 18
.
Wallet funds
Deploying contracts to the blockchain requires a gas fee. Therefore, you will need to fund your wallet with GLDR to cover those gas fees.
For this tutorial, you will be deploying a contract to the Glide network. You can fund your wallet with Glide GLDR using one of the faucets listed on the Glide Faucets page.
Creating a project
Before you can begin deploying smart contracts to Glide, you need to set up your development environment by creating a Node.js project.
To create a new Node.js project, run:
Next, you will need to install Hardhat and create a new Hardhat project
To install Hardhat, run:
To create a new Hardhat project, run:
Select Create a TypeScript project
then press enter to confirm the project root.
Select y
for both adding a .gitignore
and loading the sample project. It will take a moment for the project setup process to complete.
Configuring Hardhat with Glide
In order to deploy smart contracts to the Glide Protocol, you will need to configure your Hardhat project and add the Glide network.
To configure Hardhat, add Glide as a network to your project's hardhat.config.ts
file:
Install Hardhat toolbox
The above configuration uses the @nomicfoundation/hardhat-toolbox
plugin to bundle all the commonly used packages and Hardhat plugins recommended to start developing with Hardhat.
To install @nomicfoundation/hardhat-toolbox
, run:
Loading environment variables
The above configuration also uses dotenv to load the WALLET_KEY
environment variable from a .env
file to process.env.WALLET_KEY
. You should use a similar method to avoid hardcoding your private keys within your source code.
To install dotenv
, run:
Once you have dotenv
installed, you can create a .env
file with the following content:
Substituting <YOUR_PRIVATE_KEY>
with the private key for your wallet.
Caution
WALLET_KEY
is the private key of the wallet to use when deploying a contract.
Compiling Smart Contract
Below is a simple NFT smart contract (ERC-721) written in the Solidity programming language:
The Solidity code above defines a smart contract named NFT
. The code uses the ERC721
interface provided by the OpenZeppelin Contracts library to create an NFT smart contract. OpenZeppelin allows developers to leverage battle-tested smart contract implementations that adhere to official ERC standards.
To add the OpenZeppelin Contracts library to your project, run:
In your project, delete the contracts/Lock.sol
contract that was generated with the project and add the above code in a new file called contracts/NFT.sol
. (You can also delete the test/Lock.ts
test file, but you should add your own tests ASAP!).
To compile the contract using Hardhat, run:
Deploying the smart contract
Once your contract has been successfully compiled, you can deploy the contract to the Glide network.
To deploy the contract to the Glide network, you'll need to modify the scripts/deploy.ts
in your project:
You'll also need testnet ETH in your wallet. See the prerequisites if you haven't done that yet. Otherwise, the deployment attempt will fail.
Finally, run:
The contract will be deployed on the Glide network. You can view the deployment status and contract by using a block explorer and searching for the address returned by your deploy script. If you've deployed an exact copy of the NFT contract above, it will already be verified and you'll be able to read and write to the contract using the web interface.
Verifying Smart Contract
If you want to interact with your contract on the block explorer, you, or someone, needs to verify it first. The above contract has already been verified, so you should be able to view your version on a block explorer already. For the remainder of this tutorial, we'll walk through how to verify your contract on Base Sepolia testnet.
In hardhat.config.ts
, configure Glide as a custom network. Add the following :
Now, you can verify your contract. Grab the deployed address and run:
You should see an output similar to:
Info
You can't re-verify a contract identical to one that has already been verified. If you attempt to do so, such as verifying the above contract, you'll get an error similar to:
Search for your contract on Glide Explorer to confirm it is verified.
Last updated