Skip to content

Commit

Permalink
Merge pull request #15 from elimu-ai/14-sponsorship-queue
Browse files Browse the repository at this point in the history
refactor(backend): sponsorship queue
  • Loading branch information
jo-elimu authored Jun 27, 2024
2 parents 2f834db + 6c0752e commit 5c55251
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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);
}
}
50 changes: 0 additions & 50 deletions backend/test/SponsorshipProgram.ts

This file was deleted.

76 changes: 76 additions & 0 deletions backend/test/SponsorshipQueue.ts
Original file line number Diff line number Diff line change
@@ -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"));
});
});
});

0 comments on commit 5c55251

Please sign in to comment.