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
- Create a .env file
- 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.