feat: fill up challenge
Some checks failed
Lint / ci (lts/*, ubuntu-latest) (push) Has been cancelled
Some checks failed
Lint / ci (lts/*, ubuntu-latest) (push) Has been cancelled
This commit is contained in:
@@ -10,18 +10,27 @@ contract CrowdFund {
|
||||
/////////////////
|
||||
|
||||
// Errors go here...
|
||||
error NotOpenToWithdraw();
|
||||
error WithdrawTransferFailed(address to, uint256 amount);
|
||||
error TooEarly(uint256 deadline, uint256 currentTimestamp);
|
||||
error AlreadyCompleted();
|
||||
|
||||
//////////////////////
|
||||
/// State Variables //
|
||||
//////////////////////
|
||||
|
||||
FundingRecipient public fundingRecipient;
|
||||
mapping(address => uint256) public balances;
|
||||
bool public openToWithdraw;
|
||||
uint256 public deadline = block.timestamp + 2 hours;
|
||||
uint256 public constant threshold = 1 ether;
|
||||
|
||||
////////////////
|
||||
/// Events /////
|
||||
////////////////
|
||||
|
||||
// Events go here...
|
||||
event Contribution(address, uint256);
|
||||
|
||||
///////////////////
|
||||
/// Modifiers /////
|
||||
@@ -29,6 +38,7 @@ contract CrowdFund {
|
||||
|
||||
modifier notCompleted() {
|
||||
_;
|
||||
if (block.timestamp >= deadline) revert AlreadyCompleted();
|
||||
}
|
||||
|
||||
///////////////////
|
||||
@@ -43,19 +53,42 @@ contract CrowdFund {
|
||||
/// Functions /////
|
||||
///////////////////
|
||||
|
||||
function contribute() public payable {}
|
||||
function contribute() public payable {
|
||||
balances[msg.sender] += msg.value;
|
||||
emit Contribution(msg.sender, msg.value);
|
||||
}
|
||||
|
||||
function withdraw() public {}
|
||||
function withdraw() public {
|
||||
if (!openToWithdraw) revert NotOpenToWithdraw();
|
||||
|
||||
function execute() public {}
|
||||
uint256 amount = balances[msg.sender];
|
||||
balances[msg.sender] = 0;
|
||||
|
||||
receive() external payable {}
|
||||
(bool success,) = msg.sender.call{value: amount}("");
|
||||
if (!success) revert WithdrawTransferFailed(msg.sender, amount);
|
||||
}
|
||||
|
||||
function execute() public {
|
||||
if (block.timestamp < deadline) revert TooEarly(deadline, block.timestamp);
|
||||
|
||||
uint256 balance = address(this).balance;
|
||||
if (balance >= threshold) {
|
||||
fundingRecipient.complete{value: balance}();
|
||||
} else {
|
||||
openToWithdraw = true;
|
||||
}
|
||||
}
|
||||
|
||||
receive() external payable {
|
||||
contribute();
|
||||
}
|
||||
|
||||
////////////////////////
|
||||
/// View Functions /////
|
||||
////////////////////////
|
||||
|
||||
function timeLeft() public view returns (uint256) {
|
||||
return 0;
|
||||
if (block.timestamp >= deadline) return 0;
|
||||
return deadline - block.timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user