Skip to content

Commit

Permalink
feat(backend): set sponsorship cost
Browse files Browse the repository at this point in the history
refs #10
  • Loading branch information
jo-elimu committed Jun 26, 2024
1 parent efa1ee9 commit f0ba348
Show file tree
Hide file tree
Showing 2 changed files with 69 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;

/**
* A system for sponsoring the education of one individual child.
*/
contract SponsorshipProgram {
address public owner;
uint256 public sponsorshipCost;

event SponsorshipCostUpdated(uint256 amount);

error OnlyOwner();

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

constructor(uint256 _sponsorshipCost) {
owner = msg.sender;
sponsorshipCost = _sponsorshipCost;
}

function updateSponsorshipCost(uint256 amount) public onlyOwner {
sponsorshipCost = amount;
emit SponsorshipCostUpdated(amount);
}
}
38 changes: 38 additions & 0 deletions backend/test/SponsorshipProgram.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 sponsorshipCost = hre.ethers.parseUnits("0.02");

const SponsorshipProgram = await hre.ethers.getContractFactory("SponsorshipProgram");
const sponsorshipProgram = await SponsorshipProgram.deploy(sponsorshipCost);

return { sponsorshipProgram, owner, otherAccount };
}

describe("Deployment", function () {
it("Should set the right sponshorship cost", async function () {
const { sponsorshipProgram } = await loadFixture(deployFixture);

const expectedValue = hre.ethers.parseUnits("0.02");
console.log("expectedValue:", expectedValue);
expect(await sponsorshipProgram.sponsorshipCost()).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);
});
});
});

0 comments on commit f0ba348

Please sign in to comment.