Skip to content

Commit

Permalink
Merge pull request #11 from elimu-ai/10-sponsorship-cost
Browse files Browse the repository at this point in the history
feat(backend): set sponsorship cost

closes #10
  • Loading branch information
jo-elimu authored Jun 27, 2024
2 parents efa1ee9 + 56d98fc commit e2dbb9c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
31 changes: 31 additions & 0 deletions backend/contracts/SponsorshipProgram.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

/**
* Education sponsorship program, for delivering education to out-of-school children.
*/
contract SponsorshipProgram {
address public owner;
uint256 public estimatedCost;

event EstimatedCostUpdated(uint256 amount);

error OnlyOwner();

modifier onlyOwner() {
if (msg.sender != owner) {
revert OnlyOwner();
}
_;
}

constructor(uint256 _estimatedCost) {
owner = msg.sender;
estimatedCost = _estimatedCost;
}

function updateEstimatedCost(uint256 amount) public onlyOwner {
estimatedCost = amount;
emit EstimatedCostUpdated(amount);
}
}
50 changes: 50 additions & 0 deletions backend/test/SponsorshipProgram.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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);
});
});
});

0 comments on commit e2dbb9c

Please sign in to comment.