Skip to content

Instantly share code, notes, and snippets.

@KBPsystem777
Created November 17, 2024 14:20
Show Gist options
  • Save KBPsystem777/7b9932db9d869cb4576963217b6fa417 to your computer and use it in GitHub Desktop.
Save KBPsystem777/7b9932db9d869cb4576963217b6fa417 to your computer and use it in GitHub Desktop.
CebuFundraiser - A decentralized calamity and fund raising contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
contract CebuFundraiser {
struct Recipient {
address recipientAddress;
string name;
bool isVerified;
}
address public admin;
uint256 public totalDonations;
mapping(address => uint256) public donations;
mapping(address => Recipient) public recipients;
event DonationReceived(address indexed donor, uint256 amount);
event RecipientAdded(address indexed recipient, string name);
event RecipientVerified(address indexed recipient, string name);
event FundsReleased(address indexed recipient, uint256 amount);
event AdminTransferred(address indexed oldAdmin, address indexed newAdmin);
modifier onlyAdmin() {
require(msg.sender == admin, "Only the admin can perform this action");
_;
}
modifier onlyVerifiedRecipient() {
require(
recipients[msg.sender].isVerified,
"Only verified recipients can receive funds"
);
_;
}
constructor() {
admin = msg.sender;
}
// Accept donations
function donate() external payable {
require(msg.value > 0, "Donation amount must be greater than zero");
donations[msg.sender] += msg.value;
totalDonations += msg.value;
emit DonationReceived(msg.sender, msg.value);
}
// Add a new recipient (admin only)
function addRecipient(address _recipientAddress, string calldata _name)
external
onlyAdmin
{
require(
recipients[_recipientAddress].recipientAddress == address(0),
"Recipient already exists"
);
recipients[_recipientAddress] = Recipient({
recipientAddress: _recipientAddress,
name: _name,
isVerified: false
});
emit RecipientAdded(_recipientAddress, _name);
}
// Verify a recipient (admin only)
function verifyRecipient(address _recipientAddress) external onlyAdmin {
require(
recipients[_recipientAddress].recipientAddress != address(0),
"Recipient does not exist"
);
recipients[_recipientAddress].isVerified = true;
emit RecipientVerified(_recipientAddress, recipients[_recipientAddress].name);
}
// Release funds to a verified recipient (admin only)
function releaseFunds(address payable _recipientAddress, uint256 _amount)
external
onlyAdmin
{
require(_amount > 0, "Amount must be greater than zero");
require(
address(this).balance >= _amount,
"Insufficient contract balance"
);
require(
recipients[_recipientAddress].isVerified,
"Recipient is not verified"
);
_recipientAddress.transfer(_amount);
emit FundsReleased(_recipientAddress, _amount);
}
// Transfer admin role
function transferAdmin(address _newAdmin) external onlyAdmin {
require(_newAdmin != address(0), "New admin cannot be the zero address");
address oldAdmin = admin;
admin = _newAdmin;
emit AdminTransferred(oldAdmin, _newAdmin);
}
// Retrieve contract balance
function getContractBalance() external view returns (uint256) {
return address(this).balance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment