Skip to content

Commit

Permalink
Adds a check for overfunding.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesduncombe committed Feb 19, 2024
1 parent 0e364de commit 1ed48db
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions contracts/fast/Crowdfund.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions test/fast/Crowdfund.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1ed48db

Please sign in to comment.