From 1ed48db34bea070e3ad2db14c6035c4025587696 Mon Sep 17 00:00:00 2001 From: James Duncombe Date: Mon, 19 Feb 2024 17:48:56 +0000 Subject: [PATCH] Adds a check for overfunding. --- contracts/fast/Crowdfund.sol | 4 ++++ test/fast/Crowdfund.test.ts | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/contracts/fast/Crowdfund.sol b/contracts/fast/Crowdfund.sol index 5960b063..2aaca558 100644 --- a/contracts/fast/Crowdfund.sol +++ b/contracts/fast/Crowdfund.sol @@ -26,6 +26,8 @@ contract Crowdfund { error TokenContractError(); /// @notice Happens when there are insufficient funds somewhere. error InsufficientFunds(uint256 amount); + /// @notice Happens when overfunding occurs. + error CapExceeded(); /// @notice Happens when an address is not an issuer member. error RequiresIssuerMemberCaller(); @@ -148,6 +150,8 @@ contract Crowdfund { function pledge(uint256 amount) public onlyDuring(Phase.Funding) onlyFastMember { // Make sure the amount is non-zero. if (amount == 0) revert InconsistentParameter("amount"); + // Make sure this will not result in overfunding. + if (isCapped() && collected + amount > params.cap) revert CapExceeded(); // Make sure that the message sender gave us allowance for at least this amount. uint256 allowance = params.token.allowance(msg.sender, address(this)); if (allowance < amount) revert InsufficientFunds(amount - allowance); diff --git a/test/fast/Crowdfund.test.ts b/test/fast/Crowdfund.test.ts index 497cc856..af482540 100644 --- a/test/fast/Crowdfund.test.ts +++ b/test/fast/Crowdfund.test.ts @@ -359,6 +359,11 @@ describe("Crowdfunds", () => { await expect(subject).to.have.revertedWith("InconsistentParameter"); }); + it("requires that the pledged amount + collected amount does not go over the cap", async () => { + const subject = crowdfund.connect(alice).pledge(32_000_000_001); + await expect(subject).to.have.revertedWith("CapExceeded"); + }); + it("checks the allowance of the crowdfunding contract with the ERC20 contract", async () => { erc20.allowance.returns(100_000); erc20.transferFrom.returns(true);