Building and Deploying Upgradable ERC20 Token on Celo Network with Hardhat and OpenZeppelin Contracts Upgradeable

Building and Deploying Upgradable ERC20 Token on Celo Network with Hardhat and OpenZeppelin Contracts Upgradeable

Celo Network is a decentralized blockchain platform that aims to provide financial tools and services to people with limited access to traditional financial institutions. One of the key features of Celo is its compatibility with Ethereum-based smart contracts, which makes it easier for developers to deploy decentralized applications on the network.

In this article, we will explore how to deploy an Upgradable ERC20 Token on the Celo network using Hardhat and OpenZeppelin Contracts Upgradeable. We will cover the following topics:

  • Setting up the development environment

  • Writing and testing the smart contract

  • Deploying the smart contract on the Celo network using Hardhat

Setting up the Development Environment

To get started, you will need to have the following tools installed on your system:

  • Node.js and npm (or yarn)

  • Hardhat

  • Celo Extension

You can install Hardhat and Celo Extension by running the following commands in your terminal:

npm install --save-dev hardhat @nomiclabs/hardhat-celo 

Next, you will need to create a new Hardhat project by running the following command:

npx hardhat init 

This will create a new Hardhat project with some basic files and configurations. You can now install the OpenZeppelin Contracts Upgradeable library by running the following command:

npm install @openzeppelin/contracts-upgradeable 

Writing and Testing the Smart Contract

In this example, we will write a basic Upgradable ERC20 token contract that will allow users to transfer tokens between accounts. To get started, create a new file named MyToken.sol in the contracts directory of your Hardhat project and paste the following code:

// SPDX-License-Identifier: MIT 
pragma solidity ^0.8.0; 

import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol"; 
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; 
contract MyToken is Initializable, ERC20Upgradeable { 
  function initialize() initializer public { 
    __ERC20_init("MyToken", "MTK"); 
    _mint(msg.sender, 1000000 * (10 ** uint256(decimals()))); 
  } 
} 

This contract extends the ERC20Upgradeable contract from the OpenZeppelin Contracts Upgradeable library and initializes the token with a name, symbol, and initial supply of 1,000,000 tokens.

Next, create a new file named MyToken.test.js in the test directory of your Hardhat project and paste the following code:

const { expect } = require("chai"); 
const { ethers } = require("hardhat"); 

describe("MyToken", function () { 
  it("Should return the correct name and symbol", async function () { 
    const MyToken = await ethers.getContractFactory("MyToken"); 
    const myToken = await MyToken.deploy(); 
    await myToken.deployed(); 
    expect(await myToken.name()).to.equal("MyToken"); 
    expect(await myToken.symbol()).to.equal("MTK"); 
  }); 
}); 

This test verifies that the MyToken contract returns the correct name and symbol values. You can run the test by running the following command in your terminal:

npx hardhat test 

Deploying the Smart Contract on the Celo Network

Now that we have written and tested our smart contract, we can deploy it on the Celo network using Hardhat. To do this, we will need to create a new deployment script in the scripts directory of our Hardhat project.

Create a new file named deploy.js in the scripts directory and paste the following code:

const { ethers, upgrades } = require("hardhat"); 

async function main() { 
  const MyToken = await ethers.getContractFactory("MyToken"); 
  const myToken = await upgrades.deployProxy(MyToken, [], { initializer: "initialize" }); 
  await myToken.deployed(); console.log("MyToken deployed to:", myToken.address); 
} 

main().then(() => process.exit(0))
.catch(error => { 
  console.error(error); 
  process.exit(1); 
}); 

This script uses the upgrades.deployProxy() method from the Hardhat Upgrades plugin to deploy the MyToken contract as an upgradable contract. The initializer option is used to specify the name of the initialization function in the smart contract.

Next, you will need to add the Celo network configuration to your hardhat.config.js file. You can do this by adding the following lines to the networks section of the file:

celo: { 
  url: "https://forno.celo.org", // URL of the Celo network RPC endpoint 
  accounts: [process.env.PRIVATE_KEY], // Array of private keys for the accounts to use 
  gasPrice: 1000000000, // Gas price in wei 
  gas: 6000000, // Gas limit 
  timeout: 1000000 // Timeout in milliseconds 
} 

Replace process.env.PRIVATE_KEY with the private key of your Celo account. You can also set the gas price, gas limit, and timeout values to suit your needs.

To deploy the MyToken contract on the Celo network, run the following command in your terminal:

npx hardhat run --network celo scripts/deploy.js 

This will deploy the MyToken contract on the Celo network and output the address of the deployed contract in the terminal.

Using the Upgradable ERC20 Token

Now that we have deployed the Upgradable ERC20 Token on the Celo network, we can use it in our DApp by interacting with the contract address.

To interact with the contract using the Celo CLI, you can run the following commands in your terminal:

npx celocli contract:call <contract_address> name 
npx celocli contract:call <contract_address> symbol 

Replace <contract_address> with the address of the deployed contract. These commands will output the name and symbol values of the contract in the terminal.

To interact with the contract using a Celo-compatible wallet such as Valora, you can add the contract address to your wallet and use the built-in token transfer functionality to send and receive tokens.

Conclusion

In this article, we have explored how to deploy an Upgradable ERC20 Token on the Celo network using Hardhat and OpenZeppelin Contracts Upgradeable. We have covered the process of setting up the development environment, writing and testing the smart contract, and deploying the contract on the Celo network using Hardhat.

By following the steps outlined in this article, you should now be able to deploy your own upgradable ERC20 token on the Celo network and use it in your DApp.

< Our development centers >