From bc15a2765ea277f8763bc155d794affb0df2b82b Mon Sep 17 00:00:00 2001 From: jo-elimu <1451036+jo-elimu@users.noreply.github.com> Date: Thu, 27 Jun 2024 13:32:01 +0700 Subject: [PATCH 1/3] refactor(backend): sponsorship queue refs #14 --- ...orshipProgram.sol => SponsorshipQueue.sol} | 5 +---- ...nsorshipProgram.ts => SponsorshipQueue.ts} | 22 +++++++++---------- 2 files changed, 12 insertions(+), 15 deletions(-) rename backend/contracts/{SponsorshipProgram.sol => SponsorshipQueue.sol} (86%) rename backend/test/{SponsorshipProgram.ts => SponsorshipQueue.ts} (59%) diff --git a/backend/contracts/SponsorshipProgram.sol b/backend/contracts/SponsorshipQueue.sol similarity index 86% rename from backend/contracts/SponsorshipProgram.sol rename to backend/contracts/SponsorshipQueue.sol index 6e09220..fee240f 100644 --- a/backend/contracts/SponsorshipProgram.sol +++ b/backend/contracts/SponsorshipQueue.sol @@ -1,10 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.24; -/** - * Education sponsorship program, for delivering education to out-of-school children. - */ -contract SponsorshipProgram { +contract SponsorshipQueue { address public owner; uint256 public estimatedCost; diff --git a/backend/test/SponsorshipProgram.ts b/backend/test/SponsorshipQueue.ts similarity index 59% rename from backend/test/SponsorshipProgram.ts rename to backend/test/SponsorshipQueue.ts index 4a19cb8..70adce8 100644 --- a/backend/test/SponsorshipProgram.ts +++ b/backend/test/SponsorshipQueue.ts @@ -4,7 +4,7 @@ import { import { expect } from "chai"; import hre from "hardhat"; -describe("SponsorshipProgram", function () { +describe("SponsorshipQueue", function () { // We define a fixture to reuse the same setup in every test. // We use loadFixture to run this setup once, snapshot that state, // and reset Hardhat Network to that snapshot in every test. @@ -14,36 +14,36 @@ describe("SponsorshipProgram", function () { const estimatedCost = hre.ethers.parseUnits("0.02"); - const SponsorshipProgram = await hre.ethers.getContractFactory("SponsorshipProgram"); - const sponsorshipProgram = await SponsorshipProgram.deploy(estimatedCost); + const SponsorshipQueue = await hre.ethers.getContractFactory("SponsorshipQueue"); + const sponsorshipQueue = await SponsorshipQueue.deploy(estimatedCost); - return { sponsorshipProgram, owner, otherAccount }; + return { sponsorshipQueue, owner, otherAccount }; } describe("Deployment", function () { it("Should set the right estimated cost", async function () { - const { sponsorshipProgram } = await loadFixture(deployFixture); + const { sponsorshipQueue } = await loadFixture(deployFixture); const expectedValue = hre.ethers.parseUnits("0.02"); console.log("expectedValue:", expectedValue); - expect(await sponsorshipProgram.estimatedCost()).to.equal(expectedValue); + expect(await sponsorshipQueue.estimatedCost()).to.equal(expectedValue); }); it("Should set the right owner", async function () { - const { sponsorshipProgram, owner } = await loadFixture(deployFixture); + const { sponsorshipQueue, owner } = await loadFixture(deployFixture); - expect(await sponsorshipProgram.owner()).to.equal(owner.address); + expect(await sponsorshipQueue.owner()).to.equal(owner.address); }); }); describe("EstimatedCost", function () { it("Should emit an event on update", async function () { - const { sponsorshipProgram } = await loadFixture(deployFixture); + const { sponsorshipQueue } = await loadFixture(deployFixture); const newEstimatedCost = hre.ethers.parseUnits("0.03"); console.log("newEstimatedCost:", newEstimatedCost); - await expect(sponsorshipProgram.updateEstimatedCost(newEstimatedCost)) - .to.emit(sponsorshipProgram, "EstimatedCostUpdated") + await expect(sponsorshipQueue.updateEstimatedCost(newEstimatedCost)) + .to.emit(sponsorshipQueue, "EstimatedCostUpdated") .withArgs(newEstimatedCost); }); }); From 379a810eb2f0404a85f3caf58929912f0b6d6575 Mon Sep 17 00:00:00 2001 From: jo-elimu <1451036+jo-elimu@users.noreply.github.com> Date: Thu, 27 Jun 2024 16:32:27 +0700 Subject: [PATCH 2/3] feat(backend): sponsorship queue refs #14 --- backend/contracts/SponsorshipQueue.sol | 19 +++++++++++++++++++ backend/test/SponsorshipQueue.ts | 9 +++++++++ 2 files changed, 28 insertions(+) diff --git a/backend/contracts/SponsorshipQueue.sol b/backend/contracts/SponsorshipQueue.sol index fee240f..94cfe88 100644 --- a/backend/contracts/SponsorshipQueue.sol +++ b/backend/contracts/SponsorshipQueue.sol @@ -1,12 +1,20 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.24; +struct Sponsorship { + uint256 estimatedCost; + uint256 timestamp; + address sponsor; +} + contract SponsorshipQueue { address public owner; uint256 public estimatedCost; + Sponsorship[] public sponsorships; event OwnerUpdated(address owner); event EstimatedCostUpdated(uint256 estimatedCost); + event SponsorshipAdded(Sponsorship sponsorship); error OnlyOwner(); @@ -31,4 +39,15 @@ contract SponsorshipQueue { estimatedCost = _estimatedCost; emit EstimatedCostUpdated(_estimatedCost); } + + function addSponsorship() public payable { + payable(address(this)).send(msg.value); + Sponsorship memory sponsorship = Sponsorship( + msg.value, + block.timestamp, + msg.sender + ); + sponsorships.push(sponsorship); + emit SponsorshipAdded(sponsorship); + } } diff --git a/backend/test/SponsorshipQueue.ts b/backend/test/SponsorshipQueue.ts index 70adce8..af3d9ac 100644 --- a/backend/test/SponsorshipQueue.ts +++ b/backend/test/SponsorshipQueue.ts @@ -47,4 +47,13 @@ describe("SponsorshipQueue", function () { .withArgs(newEstimatedCost); }); }); + + describe("Sponsorships", function () { + it("Should emit an event on addSponsorship", async function () { + const { sponsorshipQueue } = await loadFixture(deployFixture); + + await expect(sponsorshipQueue.addSponsorship()) + .to.emit(sponsorshipQueue, "SponsorshipAdded"); + }); + }); }); From 6c0752eef7cbe8240e1d1353a5450c8852d9efe3 Mon Sep 17 00:00:00 2001 From: jo-elimu <1451036+jo-elimu@users.noreply.github.com> Date: Thu, 27 Jun 2024 17:06:21 +0700 Subject: [PATCH 3/3] test(backend): sponsorship queue refs #14 --- backend/test/SponsorshipQueue.ts | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/backend/test/SponsorshipQueue.ts b/backend/test/SponsorshipQueue.ts index af3d9ac..a0951eb 100644 --- a/backend/test/SponsorshipQueue.ts +++ b/backend/test/SponsorshipQueue.ts @@ -10,14 +10,14 @@ describe("SponsorshipQueue", function () { // and reset Hardhat Network to that snapshot in every test. async function deployFixture() { // Contracts are deployed using the first signer/account by default - const [owner, otherAccount] = await hre.ethers.getSigners(); + const [firstAccount, otherAccount] = await hre.ethers.getSigners(); const estimatedCost = hre.ethers.parseUnits("0.02"); const SponsorshipQueue = await hre.ethers.getContractFactory("SponsorshipQueue"); const sponsorshipQueue = await SponsorshipQueue.deploy(estimatedCost); - return { sponsorshipQueue, owner, otherAccount }; + return { sponsorshipQueue, firstAccount, otherAccount }; } describe("Deployment", function () { @@ -30,9 +30,9 @@ describe("SponsorshipQueue", function () { }); it("Should set the right owner", async function () { - const { sponsorshipQueue, owner } = await loadFixture(deployFixture); + const { sponsorshipQueue, firstAccount } = await loadFixture(deployFixture); - expect(await sponsorshipQueue.owner()).to.equal(owner.address); + expect(await sponsorshipQueue.owner()).to.equal(firstAccount.address); }); }); @@ -50,10 +50,27 @@ describe("SponsorshipQueue", function () { describe("Sponsorships", function () { it("Should emit an event on addSponsorship", async function () { - const { sponsorshipQueue } = await loadFixture(deployFixture); + const { sponsorshipQueue, firstAccount } = await loadFixture(deployFixture); + + const firstAccountBalance = await hre.ethers.provider.getBalance(firstAccount.address); + console.log("firstAccountBalance:", firstAccountBalance); - await expect(sponsorshipQueue.addSponsorship()) + await expect(sponsorshipQueue.addSponsorship({ value: hre.ethers.parseUnits("0.02") })) .to.emit(sponsorshipQueue, "SponsorshipAdded"); }); + + it("Should increase contract balance on addSponsorship", async function () { + const { sponsorshipQueue, firstAccount } = await loadFixture(deployFixture); + + const firstAccountBalance = await hre.ethers.provider.getBalance(firstAccount.address); + console.log("firstAccountBalance:", firstAccountBalance); + + console.log("sponsorshipQueue.target:", sponsorshipQueue.target); + + await sponsorshipQueue.addSponsorship({ value: hre.ethers.parseUnits("0.02") }); + const contractBalance = await hre.ethers.provider.getBalance(sponsorshipQueue.target); + console.log("contractBalance:", contractBalance); + expect(contractBalance).to.equal(hre.ethers.parseUnits("0.02")); + }); }); });