Skip to content

Instantly share code, notes, and snippets.

@rosyanxone
Last active September 4, 2024 08:31
Show Gist options
  • Save rosyanxone/c556472be57aa111c349b33397b7edd7 to your computer and use it in GitHub Desktop.
Save rosyanxone/c556472be57aa111c349b33397b7edd7 to your computer and use it in GitHub Desktop.
Crowdfunding Smart Contract
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.9;
contract BeriEther {
struct Program {
address owner;
address payable recipient;
string title;
string description;
uint256 deadline;
uint256 target;
uint256 amountCollected;
string image;
address[] donators;
uint256[] donations;
string[] messages;
bool isFinish;
}
mapping(uint256 => Program) private programs;
uint256 public totalPrograms = 0;
struct Report {
string title;
string story;
string image;
uint256 createdAt;
}
mapping(uint256 => Report) private reports;
function createProgram(
address payable _recipient,
string memory _title,
string memory _description,
uint256 _target,
uint256 _deadline,
string memory _image
) external {
Program storage program = programs[totalPrograms];
require(
program.deadline < block.timestamp,
"The deadline should be a date in the future."
);
program.owner = msg.sender;
program.recipient = _recipient;
program.title = _title;
program.description = _description;
program.target = _target;
program.deadline = _deadline;
program.amountCollected = 0;
program.image = _image;
program.isFinish = false;
totalPrograms++;
}
function donateToProgram(uint256 _id, string memory _message)
public
payable
{
Program storage program = programs[_id];
require(!program.isFinish, "The program has ended.");
require(program.deadline > 0, "The program does not exist.");
uint256 amount = msg.value;
require(amount > 0, "Donation amount must be greater than 0.");
program.donators.push(msg.sender);
program.donations.push(amount);
program.messages.push(_message);
program.amountCollected = program.amountCollected + amount;
}
function getDonators(uint256 _id)
public
view
returns (
address[] memory,
uint256[] memory,
string[] memory
)
{
Program storage program = programs[_id];
return (program.donators, program.donations, program.messages);
}
function getPrograms() public view returns (Program[] memory) {
Program[] memory allPrograms = new Program[](totalPrograms);
for (uint256 i = 0; i < totalPrograms; i++) {
Program storage item = programs[i];
allPrograms[i] = item;
}
return allPrograms;
}
function getProgram(uint256 _id) public view returns (Program memory) {
Program storage program = programs[_id];
require(program.deadline > 0, "The program does not exist.");
return program;
}
function getDonation(uint256 _id) public payable {
Program storage program = programs[_id];
require(
msg.sender == program.owner,
"You're not the owner of program."
);
require(!program.isFinish, "The program has ended.");
require(
program.amountCollected >= program.target ||
program.deadline < block.timestamp,
"The program has not reached its deadline."
);
(bool sent, ) = payable(program.recipient).call{
value: program.amountCollected
}("");
if (sent) {
program.isFinish = true;
}
}
function createReport(
uint256 _id,
string memory _title,
string memory _story,
string memory _image
) external {
require(
msg.sender == programs[_id].owner,
"You're not the owner of program."
);
require(programs[_id].isFinish, "The program is not end yet.");
Report storage report = reports[_id];
require(report.createdAt == 0, "The report has been created.");
report.title = _title;
report.story = _story;
report.image = _image;
report.createdAt = block.timestamp;
}
function getReport(uint256 _id) public view returns (Report memory) {
Report storage report = reports[_id];
require(report.createdAt > 0, "The report does not exist.");
return report;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment