Getting Started with Hardhat on Celo: A Beginner's Guide

Getting Started with Hardhat on Celo: A Beginner's Guide https://celo.academy/uploads/default/optimized/2X/5/505251f11981221f433866a0d8f08cd155932e28_2_1024x576.jpeg
none 0.0 0

Introduction

Celo is a blockchain platform that aims to provide a decentralized financial infrastructure to millions of people across the globe. It is built on top of the Ethereum network and utilizes smart contracts to enable secure, fast, and low-cost transactions.

In this beginner’s guide, we’ll cover the basics of Celo and walk you through the process of setting up your development environment and building your first Celo smart contract.

Prerequistes

Before proceeding, you should read this articles:

Requirements

Before we dive into the tutorial, you will need the following tools and accounts:

  • Visual Studio Code (VSCode) or any other code editor
  • Node.js and npm (Node Package Manager) installed on your machine
  • A Celo account with testnet funds (You can obtain testnet funds from the Celo Faucet)

Getting Started:

Now, let’s get started with building your first Celo smart contract.

In this section, we’ll walk you through the process of building a simple smart contract that enables users to send and receive Celo.

Step 1: Create a new project directory and initialize it with npm:

mkdir First-Celo-Smart-Contract
cd First-Celo-Smart-Contract
npm init

Step 2: Next, initialize a new Node.js project and install Hardhat along with other necessary dependencies:

npm init -y
npm install --save-dev hardhat @nomiclabs/hardhat-waffle ethereum-waffle chai ethers
npm install dotenv

Now, create a new Hardhat project:


npx hardhat

When prompted, select “Create a JavaScript Project”.

Step 3: Writing a Smart Contract

With our development environment set up, we can start writing a smart contract. In the contracts directory, create a file named HelloWorld.sol:

Open the file and add the following Solidity code:

// File: contracts/HelloWorld.sol

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

// Define the smart contract
contract HelloWorld {
	// Declare a public string variable named "message"
	string public message;

	// Define a constructor that sets the initial message
	constructor(string memory initMessage) {
    	message = initMessage;
	}

	// Define a function that updates the message
	function updateMessage(string memory newMessage) public {
    	message = newMessage;
	}
}

This is a simple smart contract with a public message string variable and a function to update the message. The constructor function initializes the message when the contract is deployed.

Step 4: Create a new file called “deploy.js” and add the following code:

// File: scripts/deploy.js

// Define an asynchronous function for deployment
async function main() {
  // Get a contract factory for the HelloWorld contract
  const HelloWorld = await ethers.getContractFactory("HelloWorld");

  // Deploy the HelloWorld contract with the initial message
  const helloWorld = await HelloWorld.deploy("Hello, Celo!");

  // Wait for the transaction to be mined
  await helloWorld.deployed();

  // Log the address of the deployed contract
  console.log("HelloWorld deployed to:", helloWorld.address);
}

// Call the main function and handle errors
main()
  .then(() => process.exit(0)) // On success, exit with code 0
  .catch(error => { // On error...
	console.error(error); // Log the error
	process.exit(1); // Exit with code 1
  });

This script deploys the HelloWorld contract and logs its address.

Step 5: Configuring Hardhat for Celo

In order to deploy the contract to the Celo network, we need to configure Hardhat to connect to the network. Open hardhat.config.js and add the following configuration:

require("@nomiclabs/hardhat-waffle");
require("dotenv").config({ path: ".env" });

// Add the alfajores network to the configuration
module.exports = {
  solidity: "0.8.0",
  networks: {
    alfajores: {
      url: "https://alfajores-forno.celo-testnet.org",
      accounts: {
        mnemonic: process.env.MNEMONIC_KEY,
        path: "m/44'/52752'/0'/0",
      },
      chainId: 44787,
    },
  },
};

In your mnemonic, just copy and paste the seed phrase with the spaces. Also, please don’t share with anyone

Use environment variables (.env) file to store your mnemonic

  1. Create a .env file
  2. Use dotenv package to read environment variables within Hardhat
// .env file
MNEMONIC_KEY="run deer open shut ....."

Step 6: Deploy your smart contract to the Celo network:

Once the smart contract is written, it’s time to compile & deploy. Simply run:

npx hardhat compile
npx hardhat run scripts/deploy.js --network alfajores

Congratulations! You’ve just deployed your first Celo smart contract.

Conclusion

In this beginner’s guide, we covered the basics of Celo and walked you through the process of setting up your development environment, building and deploying your first Celo smart contract.

Next Steps

Now that you have deployed your first Celo smart contract, you can go ahead and build more advanced applications on the Celo blockchain. Click here to learn more.

About the Author

Joshua Obafemi

I’m a Web3 Fullstack Developer and Technical Writer. You can connect with me on GitHub, Twitter, Linkedin.

References

Source Code

2 Likes

@Celo_Academy

Kindly review this article, I’d like to know if I should edit the header

1 Like

i will be reviewing this.

Okay thanks, let me know if any changes are needed

1 Like

I can’t see any smart contract which you trying to deploy in tutorial also can you add folder structure like which file will be in which folder

1 Like

my contract.js and deploycontract.js are same files also can you add screenshots of running this code .

1 Like

Okay, I’ll make the screenshots, also where do I add images for the articles as I have code snippets

1 Like

you can add running code screenshot where you tell them to run script .after that add the screenshot.

1 Like

ANy updates??
Waiting from two days

1 Like

My apologies, kindly check the edits

1 Like

Very Insightful

Gladden to come across the educative piece. :partying_face:

1 Like