Created
February 22, 2022 14:30
-
-
Save davidhq/552fa3600f901bca23f1a4f84c17aa1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Setup private variables (only callable from within the contract) | |
struct Funder : | |
sender: address | |
value: uint256 | |
funders: HashMap[int128, Funder] | |
nextFunderIndex: int128 | |
beneficiary: address | |
deadline: public(uint256) | |
goal: public(uint256) | |
refundIndex: int128 | |
timelimit: public(uint256) | |
# Setup global variables | |
@external | |
def __init__(_beneficiary: address, _goal: uint256, _timelimit: uint256): | |
self.beneficiary = _beneficiary | |
self.deadline = block.timestamp + _timelimit | |
self.timelimit = _timelimit | |
self.goal = _goal | |
# Participate in this crowdfunding campaign | |
@external | |
@payable | |
def participate(): | |
assert block.timestamp < self.deadline, "deadline not met (yet)" | |
nfi: int128 = self.nextFunderIndex | |
self.funders[nfi] = Funder({sender: msg.sender, value: msg.value}) | |
self.nextFunderIndex = nfi + 1 | |
# Enough money was raised! Send funds to the beneficiary | |
@external | |
def finalize(): | |
assert block.timestamp >= self.deadline, "deadline has passed" | |
assert self.balance >= self.goal, "the goal has been reached" | |
selfdestruct(self.beneficiary) | |
# Not enough money was raised! Refund everyone (max 30 people at a time | |
# to avoid gas limit issues) | |
@external | |
def refund(): | |
assert block.timestamp >= self.deadline and self.balance < self.goal | |
ind: int128 = self.refundIndex | |
for i in range(ind, ind + 30): | |
if i >= self.nextFunderIndex: | |
self.refundIndex = self.nextFunderIndex | |
return | |
send(self.funders[i].sender, self.funders[i].value) | |
self.funders[i] = empty(Funder) | |
self.refundIndex = ind + 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment