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 Apr 22, 2024
1 parent dd3cb69 commit 83732e2
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 @@ -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();
Expand Down Expand Up @@ -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);
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 @@ -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);
Expand Down

0 comments on commit 83732e2

Please sign in to comment.