Building a Farm-to-Table Tracking Solution on Celo Blockchain Using JavaScript, Solidity, and Hardhat (Part 1)

Building a Farm-to-Table Tracking Solution on Celo Blockchain Using JavaScript, Solidity, and Hardhat (Part 1)
none 0.0 0


One of the most significant shifts in the food and agriculture industry in recent years has been towards the “farm-to-table” concept, emphasizing transparency, sustainability, and local sourcing. Using blockchain technology, it’s possible to take this idea a step further, providing a secure, verifiable record of a food item’s journey from farm to consumer.

Celo, a mobile-first blockchain platform, is an ideal choice for implementing a farm-to-table tracking solution due to its focus on accessibility, scalability, and security. We’ll build this solution using Hardhat, a development environment for compiling, deploying, testing, and debugging Ethereum software, in conjunction with JavaScript and Solidity, a statically-typed programming language for implementing smart contracts on Ethereum and Celo.


Before we start, make sure you have the following installed:

  • Node.js (version 14 or later)
  • npm (comes bundled with Node.js)
  • Hardhat (installed globally via npm)
  • MetaMask or another Web3 provider set up for Celo network

Let’s get started!

Step 1: Setup a New Hardhat Project

Create a new directory for your project and navigate to it in your terminal:

mkdir celo-farm-to-table
cd celo-farm-to-table
mkdir hardhat
cd hardhat

Initialize a new Hardhat project by running:

npx hardhat

You will be prompted to create a sample project. Select “Create a JavaScript Project” and install hardhat locally in your project.

Step 2: Write Your Smart Contract

Create a new file named Trace.sol in the contracts directory and write the following Solidity code:

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

contract Trace {
	struct Product {
    	string name;
    	string farmer;
    	string location;
    	uint timestamp;

	mapping (string => Product) public products;

	function addProduct(string memory _id, string memory _name, string memory _farmer, string memory _location) public {
    	Product memory newProduct = Product({
        	name: _name,
        	farmer: _farmer,
        	location: _location,
        	timestamp: block.timestamp
    	products[_id] = newProduct;

	function getProduct(string memory _id) public view returns (string memory, string memory, string memory, uint) {
    	Product memory product = products[_id];
    	return (, product.farmer, product.location, product.timestamp);

The addProduct function allows us to add a new product identified by an ID with a name, farmer’s name, and location. The getProduct function allows us to fetch a product using its ID.

Step 3: Compile the Contract

Back in the terminal, run the following command to compile the contract:

npx hardhat compile

Step 4: Write Tests

Testing is essential in smart contract development. Let’s write some tests to ensure our contract works as expected. Create a new file named trace-test.js in the test directory and write the following JavaScript code:

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

describe("Trace Contract", function() {
	it("Should return the right product properties when queried", async function() {
    	const Trace = await ethers.getContractFactory("Trace");
    	const trace = await Trace.deploy();
    	await trace.deployed();

    	const tx = await trace.addProduct("1", "Apple", "Farmer Joe", "Location 1");
    	await tx.wait();

    	const product = await


    	expect(product.farmer).to.equal("Farmer Joe");
    	expect(product.location).to.equal("Location 1");

Run the tests with:

npx hardhat test

Step 5: Deploy the Contract to the Celo Network

Finally, we need to deploy our smart contract to the Celo network. This involves writing a deployment script and running the script using Hardhat.

First, install the necessary plugins for deployment:

npm install @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers

Create a scripts directory and a new file named deploy.js inside it with the following content:

async function main() {
	const [deployer] = await ethers.getSigners();

    	"Deploying contracts with the account:",

	const Trace = await ethers.getContractFactory("Trace");
	const trace = await Trace.deploy();
	console.log("Trace contract address:", trace.address);

	.then(() => process.exit(0))
	.catch(error => {

Before running the deployment script, you need to add your Celo network information and private key to hardhat.config.js. Here’s an example:


module.exports = {
	networks: {
    	celo: {
        	url: "",
        	accounts: [`PRIVATE_KEY`]
	solidity: "0.8.4",

Deploy the contract by running the script:

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

With that, we have our smart contract deployed on the Celo network, ready to bring trust and transparency to the farm-to-table process!


The potential of blockchain technology in food and agriculture is vast. The simple farm-to-table tracking solution we’ve built today could be expanded upon to include more detailed product data, different actors in the supply chain, verification of organic or fair-trade status, and much more. Whether you’re a farmer, a restaurateur, or a concerned consumer, blockchain technology could help make the farm-to-table concept a reality.

About Us
Joel Obafemi
A marketer, copywriter, and collab manager for web3 brands. You can connect with me on LinkedIn.


Source Code


Congratulations on your proposal being chosen as a standout this week at Celo Academy! As you prepare your tutorial, please ensure you follow our guidelines found here: Celo Sage Guidelines and Best Practices. Thank you for helping improve the experience of the developers learning at Celo Academy.


I will be reviewing this @Joel

1 Like

I don’t see that this has been approved. Moving back to review. @Joel @ishan.pathak2711

1 Like