From 83732e292b2d3edf1fb3f4e5db9cf0bd614f52a8 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 b880513..01843eb 100644 --- a/contracts/fast/Crowdfund.sol +++ b/contracts/fast/Crowdfund.sol @@ -28,6 +28,8 @@ contract Crowdfund is AHasContext { 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(); @@ -162,6 +164,8 @@ contract Crowdfund is AHasContext { 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(_msgSender(), address(this)); if (allowance < amount) revert InsufficientFunds(amount - allowance); diff --git a/test/fast/Crowdfund.test.ts b/test/fast/Crowdfund.test.ts index 849eb8c..ab47e04 100644 --- a/test/fast/Crowdfund.test.ts +++ b/test/fast/Crowdfund.test.ts @@ -380,6 +380,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);