// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
contract FlexClub003 is ReentrancyGuard {
struct Goal {
string name;
string description;
uint256 pooled;
uint256 target;
uint256 deadline;
uint256 contributors;
}
mapping(address => uint256) public balances;
mapping(address => bool) private hasContributed;
Goal public goal;
address public donationWallet;
bool public fundsWithdrawn;
event Deposit(address indexed user, uint256 amount);
event Withdrawal(address indexed recipient, uint256 amount);
event GoalInfoUpdated(
string name,
string description,
uint256 pooled,
uint256 target,
uint256 deadline,
uint256 contributors
);
constructor(
address _donationWallet,
uint256 _deadline,
string memory _goalName,
string memory _goalDescription,
uint256 _targetAmount
) {
require(_donationWallet != address(0), "Invalid donation wallet address");
require(_deadline > block.timestamp, "Deadline must be in the future");
donationWallet = _donationWallet;
goal = Goal(_goalName, _goalDescription, 0, _targetAmount, _deadline, 0);
fundsWithdrawn = false;
}
receive() external payable {
_processDeposit();
}
function deposit() public payable {
_processDeposit();
}
function _processDeposit() private nonReentrant {
require(block.timestamp < goal.deadline, "The deadline has passed");
require(msg.value > 0, "Deposit amount must be greater than zero");
require(!fundsWithdrawn, "Funds have already been withdrawn");
balances[msg.sender] += msg.value;
goal.pooled += msg.value;
// Update unique contributor count
if (!hasContributed[msg.sender]) {
hasContributed[msg.sender] = true;
goal.contributors += 1;
}
emit Deposit(msg.sender, msg.value);
emit GoalInfoUpdated(
goal.name,
goal.description,
goal.pooled,
goal.target,
goal.deadline,
goal.contributors
);
}
function withdraw() public nonReentrant {
require(msg.sender == donationWallet, "Only the donation wallet can withdraw");
require(
block.timestamp >= goal.deadline || goal.pooled >= goal.target,
"Cannot withdraw before deadline or target is reached"
);
require(!fundsWithdrawn, "Funds have already been withdrawn");
require(goal.pooled > 0, "No funds to withdraw");
fundsWithdrawn = true;
uint256 amount = address(this).balance;
// Transfer the pooled ETH to the donation wallet
(bool success, ) = donationWallet.call{value: amount}("");
require(success, "Transfer failed");
emit Withdrawal(donationWallet, amount);
emit GoalInfoUpdated(
goal.name,
goal.description,
0, // Reset pooled amount to zero
goal.target,
goal.deadline,
goal.contributors
);
}
/**
* @notice Allows users to refund their contribution and withdraw their deposited amount
* provided the donation wallet hasn't withdrawn the funds yet.
*/
function refund() public nonReentrant {
require(!fundsWithdrawn, "Funds have already been withdrawn");
uint256 userBalance = balances[msg.sender];
require(userBalance > 0, "No balance to withdraw");
// Update state before transferring to prevent reentrancy attacks
balances[msg.sender] = 0;
goal.pooled -= userBalance;
// Transfer the user's ETH back
(bool success, ) = msg.sender.call{value: userBalance}("");
require(success, "Transfer failed");
emit Withdrawal(msg.sender, userBalance);
emit GoalInfoUpdated(
goal.name,
goal.description,
goal.pooled,
goal.target,
goal.deadline,
goal.contributors
);
}
function getGoalInfo()
public
view
returns (
string memory name,
string memory description,
uint256 pooled,
uint256 target,
uint256 deadline,
uint256 contributors
)
{
return (
goal.name,
goal.description,
goal.pooled,
goal.target,
goal.deadline,
goal.contributors
);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/ReentrancyGuard.sol)
pragma solidity ^0.8.20;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant NOT_ENTERED = 1;
uint256 private constant ENTERED = 2;
uint256 private _status;
/**
* @dev Unauthorized reentrant call.
*/
error ReentrancyGuardReentrantCall();
constructor() {
_status = NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be NOT_ENTERED
if (_status == ENTERED) {
revert ReentrancyGuardReentrantCall();
}
// Any calls to nonReentrant after this point will fail
_status = ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == ENTERED;
}
}
{
"compilationTarget": {
"contracts/Flexclub003.sol": "FlexClub003"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": false,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_donationWallet","type":"address"},{"internalType":"uint256","name":"_deadline","type":"uint256"},{"internalType":"string","name":"_goalName","type":"string"},{"internalType":"string","name":"_goalDescription","type":"string"},{"internalType":"uint256","name":"_targetAmount","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"ReentrancyGuardReentrantCall","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"string","name":"name","type":"string"},{"indexed":false,"internalType":"string","name":"description","type":"string"},{"indexed":false,"internalType":"uint256","name":"pooled","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"target","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"deadline","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"contributors","type":"uint256"}],"name":"GoalInfoUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Withdrawal","type":"event"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balances","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"deposit","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"donationWallet","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"fundsWithdrawn","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getGoalInfo","outputs":[{"internalType":"string","name":"name","type":"string"},{"internalType":"string","name":"description","type":"string"},{"internalType":"uint256","name":"pooled","type":"uint256"},{"internalType":"uint256","name":"target","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint256","name":"contributors","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"goal","outputs":[{"internalType":"string","name":"name","type":"string"},{"internalType":"string","name":"description","type":"string"},{"internalType":"uint256","name":"pooled","type":"uint256"},{"internalType":"uint256","name":"target","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint256","name":"contributors","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"refund","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]