Building Back End Decentralised Payment App on Celo using JavaScript and Solidity (Part2)

Building Back End Decentralised Payment App on Celo using JavaScript and Solidity (Part2) https://celo.academy/uploads/default/optimized/2X/9/993229ed204ef2187e928e87ee660c3518ace856_2_1024x576.png
none 0.0 0

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:

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

Reference

GitHub

7 Likes

This seems to be in the wrong folder. If its a new request, please move it to Proposals > New Request

6 Likes

thank you for the correction. I have just done that

4 Likes

Congratulations on being among the highest voted proposals for this week! I’m moving this to todo so that you can get started on this project. :mortar_board: :seedling:

5 Likes

I’ll be reviewing this @olusegun

5 Likes

can i know why this was moved in to publish section

5 Likes

Hello @ishan.pathak2711 What errors would you like me to correct?

3 Likes

First please tell me , were you said to move the article in the publish section by the reviewer?

4 Likes

I did not move the article. I don’t think I am supposed to do that.

The reviewer moved the article after reviewing it.

Is there any problem or changes you noticed I should work on @ishan.pathak2711?

4 Likes

No need

2 Likes

Okay.

3 Likes

Is there any existing article in the community that solves this. If none, can you create a proposal and interested folks like can vote it.

4 Likes

You can read the front end resources article here.

3 Likes

The link to the part 1 should be inside your tutorial itself. It makes things easier.

Great content sir.

1 Like

It’s linked in the requirements EmiriDbest

3 Likes

Nice one sir…apologies for not noticing it.
I suggest you put it in introduction and make it more obvious that its the first part of the work…probably through a more detailed sentence.

Thanks for your efforts.

1 Like

@olusegun this is cool, :+1:

I look forward to more complex implementation.

8 Likes