Blockchain technology is rapidly gaining traction in various sectors, one of which is the financial industry. Celo, a mobile-first blockchain platform, aims to provide efficient and affordable financial tools for everyone, thereby contributing significantly to the decentralized financial world.
In this article, we will delve into building a back-end decentralized payment application using Celo, JavaScript, and Solidity.
Requirements:
- Build the Frontend of the Decentralised Payment App
- Node.js and npm installed on your machine
- A basic understanding of JavaScript and Solidity
- The Hardhat development environment for Ethereum
- A Celo wallet
Setting Up the Development Environment
We’ll start by initializing a new node.js project and installing Hardhat:
mkdir celo-payment-app
cd celo-payment-app
npm init -y
npm install --save-dev hardhat
Then, we create a new Hardhat project:
npx hardhat
During the setup, choose ‘Create a JavaScript project’.
Contract Development
We’ll write our smart contract in Solidity. Let’s create a new file in the contracts
directory called PaymentProcessor.sol
Our contract will accept payments and store the necessary transaction data.
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract PaymentProcessor {
address public admin;
mapping(address => uint256) public balances;
constructor(address _admin) {
admin = _admin;
}
function pay() external payable {
require(msg.value > 0, "Payment must be positive");
balances[msg.sender] += msg.value;
emit PaymentDone(msg.sender, msg.value);
}
event PaymentDone(address payer, uint256 amount);
}
Here, we’re creating a simple contract that allows users to send payments and tracks their balances. Whenever a payment is received, a PaymentDone
event is emitted.
Compiling the Contract
Hardhat makes it easy to compile contracts and manage different Solidity versions. Let’s compile our contract:
npx hardhat compile
Testing the Contract
Writing tests for our smart contract is crucial. Let’s write a test in the test
directory:
mkdir test
touch test/PaymentProcessor.js
In test/PaymentProcessor.js
:
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("PaymentProcessor", function () {
it("Should receive and record payments", async function () {
const PaymentProcessor = await ethers.getContractFactory("PaymentProcessor");
const paymentProcessor = await PaymentProcessor.deploy();
await paymentProcessor.deployed();
await paymentProcessor.pay({value: ethers.utils.parseEther("1.0")});
expect(await paymentProcessor.balances(owner.address)).to.equal(ethers.utils.parseEther("1.0"));
});
});
Now, run the tests:
$ npx hardhat test
Deployment
Hardhat also aids in contract deployment. In the scripts
directory:
$ mkdir scripts
$ touch scripts/deploy.js
And in scripts/deploy.js
:
const hre = require("hardhat");
async function main() {
const PaymentProcessor = await hre.ethers.getContractFactory("PaymentProcessor");
const paymentProcessor = await PaymentProcessor.deploy();
await paymentProcessor.deployed();
console.log("PaymentProcessor deployed to:", paymentProcessor.address);
}
main()
.catch((error)
=> {
console.error(error);
process.exit(1);
});
Now, we can deploy the contract:
$ npx hardhat run scripts/deploy.js
Integrating with Celo
To allow our DApp to interact with Celo, we need to connect our contract to a Celo network. Hardhat makes it easy to switch between different networks. We’ll add a network configuration in the hardhat.config.js
file:
module.exports = {
solidity: "0.8.0",
networks: {
celo: {
url: "https://alfajores-forno.celo-testnet.org",
accounts: [process.env.CELO_PRIVATE_KEY]
},
},
};
We are now able to deploy our contract on the Celo network using:
$ npx hardhat run --network celo scripts/deploy.js
Now that we’ve created and deployed our smart contract, the next step is to build a front-end application that interacts with it. But that’s a topic for another article.
Conclusion
In summary, this guide should provide a clear insight into how you can leverage JavaScript, Solidity, and Hardhat to build a back-end decentralized payment application on Celo. It’s fascinating to see how these tools work together to deliver powerful, decentralized applications, and I encourage you to explore further.
About the Author
Aborode Prime Olusegun is a Growth Marketer and Data Analyst. He’s got a huge interest in Web3 and the decentralisation that it offers. Twitter