diff --git a/backend/contracts/SponsorshipProgram.sol b/backend/contracts/SponsorshipQueue.sol similarity index 59% rename from backend/contracts/SponsorshipProgram.sol rename to backend/contracts/SponsorshipQueue.sol index 6e09220..94cfe88 100644 --- a/backend/contracts/SponsorshipProgram.sol +++ b/backend/contracts/SponsorshipQueue.sol @@ -1,15 +1,20 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.24; -/** - * Education sponsorship program, for delivering education to out-of-school children. - */ -contract SponsorshipProgram { +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(); @@ -34,4 +39,15 @@ contract SponsorshipProgram { 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/SponsorshipProgram.ts b/backend/test/SponsorshipProgram.ts deleted file mode 100644 index 4a19cb8..0000000 --- a/backend/test/SponsorshipProgram.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { - loadFixture, -} from "@nomicfoundation/hardhat-toolbox/network-helpers"; -import { expect } from "chai"; -import hre from "hardhat"; - -describe("SponsorshipProgram", 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. - async function deployFixture() { - // Contracts are deployed using the first signer/account by default - const [owner, otherAccount] = await hre.ethers.getSigners(); - - const estimatedCost = hre.ethers.parseUnits("0.02"); - - const SponsorshipProgram = await hre.ethers.getContractFactory("SponsorshipProgram"); - const sponsorshipProgram = await SponsorshipProgram.deploy(estimatedCost); - - return { sponsorshipProgram, owner, otherAccount }; - } - - describe("Deployment", function () { - it("Should set the right estimated cost", async function () { - const { sponsorshipProgram } = await loadFixture(deployFixture); - - const expectedValue = hre.ethers.parseUnits("0.02"); - console.log("expectedValue:", expectedValue); - expect(await sponsorshipProgram.estimatedCost()).to.equal(expectedValue); - }); - - it("Should set the right owner", async function () { - const { sponsorshipProgram, owner } = await loadFixture(deployFixture); - - expect(await sponsorshipProgram.owner()).to.equal(owner.address); - }); - }); - - describe("EstimatedCost", function () { - it("Should emit an event on update", async function () { - const { sponsorshipProgram } = await loadFixture(deployFixture); - - const newEstimatedCost = hre.ethers.parseUnits("0.03"); - console.log("newEstimatedCost:", newEstimatedCost); - await expect(sponsorshipProgram.updateEstimatedCost(newEstimatedCost)) - .to.emit(sponsorshipProgram, "EstimatedCostUpdated") - .withArgs(newEstimatedCost); - }); - }); -}); diff --git a/backend/test/SponsorshipQueue.ts b/backend/test/SponsorshipQueue.ts new file mode 100644 index 0000000..a0951eb --- /dev/null +++ b/backend/test/SponsorshipQueue.ts @@ -0,0 +1,76 @@ +import { + loadFixture, +} from "@nomicfoundation/hardhat-toolbox/network-helpers"; +import { expect } from "chai"; +import hre from "hardhat"; + +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. + async function deployFixture() { + // Contracts are deployed using the first signer/account by default + 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, firstAccount, otherAccount }; + } + + describe("Deployment", function () { + it("Should set the right estimated cost", async function () { + const { sponsorshipQueue } = await loadFixture(deployFixture); + + const expectedValue = hre.ethers.parseUnits("0.02"); + console.log("expectedValue:", expectedValue); + expect(await sponsorshipQueue.estimatedCost()).to.equal(expectedValue); + }); + + it("Should set the right owner", async function () { + const { sponsorshipQueue, firstAccount } = await loadFixture(deployFixture); + + expect(await sponsorshipQueue.owner()).to.equal(firstAccount.address); + }); + }); + + describe("EstimatedCost", function () { + it("Should emit an event on update", async function () { + const { sponsorshipQueue } = await loadFixture(deployFixture); + + const newEstimatedCost = hre.ethers.parseUnits("0.03"); + console.log("newEstimatedCost:", newEstimatedCost); + await expect(sponsorshipQueue.updateEstimatedCost(newEstimatedCost)) + .to.emit(sponsorshipQueue, "EstimatedCostUpdated") + .withArgs(newEstimatedCost); + }); + }); + + describe("Sponsorships", function () { + it("Should emit an event on addSponsorship", async function () { + const { sponsorshipQueue, firstAccount } = await loadFixture(deployFixture); + + const firstAccountBalance = await hre.ethers.provider.getBalance(firstAccount.address); + console.log("firstAccountBalance:", firstAccountBalance); + + 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")); + }); + }); +});