Introduction
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.
Prerequisites
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.name, 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
trace.getProduct("1");
expect(product.name).to.equal("Apple");
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();
console.log(
"Deploying contracts with the account:",
deployer.address
);
const Trace = await ethers.getContractFactory("Trace");
const trace = await Trace.deploy();
console.log("Trace contract address:", trace.address);
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
Before running the deployment script, you need to add your Celo network information and private key to hardhat.config.js
. Here’s an example:
require("@nomiclabs/hardhat-waffle");
module.exports = {
networks: {
celo: {
url: "https://alfajores-forno.celo-testnet.org",
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!
Conclusion
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.