-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from elimu-ai/10-sponsorship-cost
feat(backend): set sponsorship cost closes #10
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |