Skip to content

Commit

Permalink
Refactor project donation handling
Browse files Browse the repository at this point in the history
  • Loading branch information
naiirad committed Nov 28, 2023
1 parent fe00288 commit 1e28973
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 80 deletions.
97 changes: 25 additions & 72 deletions packages/hardhat/contracts/KindKoin_DonationHub.sol
Original file line number Diff line number Diff line change
@@ -1,125 +1,78 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

// Useful for debugging. Remove when deploying to a live network.
// Hardhat console for debugging purposes. Remove this import for production deployment.
import "hardhat/console.sol";

// Use openzeppelin to inherit battle-tested implementations (ERC20, ERC721, etc)
// Importing OpenZeppelin's Ownable contract for access control functionalities.
import "@openzeppelin/contracts/access/Ownable.sol";

// The KindKoin_DonationHub contract for handling project donations.
contract KindKoin_DonationHub is Ownable {
// Structure to store information about each project
// Project struct to hold each project's wallet address and total donations received.
struct Project {
address payable wallet; // Wallet address to receive donations
uint totalDonations; // Total amount of donations received
address payable wallet;
uint totalDonations;
}

// Service fee percentage, initialized to 1%
uint public serviceFeePercentage = 1;
// Variable to store the service fee percentage. Initialized to 1% (10 basis points).
uint public serviceFeePercentage = 10; // 1% = 10 basis points

// Mapping from project ID to project details
// Mapping to store project details by their ID.
mapping(uint => Project) private projects;
// Mapping to track total donations made by each user
// Mapping to track the total donations made by each user.
mapping(address => uint) private userTotalDonations;

// Event emitted when a donation is made
// Event to log donation activities.
event DonationMade(address indexed donor, uint indexed projectId, uint amount);

// Function to add a new project or update an existing one to the platform
// Function to add a new project. Existing projects cannot be updated (wallet address is immutable).
function setProject(uint projectId, address payable projectWallet) public onlyOwner {
require(projects[projectId].wallet == address(0), "Project already exists");
projects[projectId] = Project(projectWallet, 0);
}

// Function to remove a project from the platform
// Function to remove a project from the platform.
function removeProject(uint projectId) public onlyOwner {
delete projects[projectId];
}

// Function to adjust the service fee percentage
// Function to adjust the service fee percentage in 0.1% steps.
function adjustServiceFee(uint newFeePercentage) public onlyOwner {
require(newFeePercentage >= 0 && newFeePercentage <= 3, "Fee must be between 0% and 3%");
require(newFeePercentage >= 0 && newFeePercentage <= 30, "Fee must be between 0% and 3%");
serviceFeePercentage = newFeePercentage;
}

// Function to handle donation transactions
// Function to handle donation transactions to a specific project.
function donate(uint projectId) public payable {
require(msg.value > 0, "Donation must be greater than 0");
require(projects[projectId].wallet != address(0), "Project does not exist");

// Calculate the service fee and the actual donation amount
uint fee = (msg.value * serviceFeePercentage) / 100;
uint fee = (msg.value * serviceFeePercentage) / 1000; // Calculating the fee
uint donationAmount = msg.value - fee;

// Transfer the donation to the project's wallet
projects[projectId].wallet.transfer(donationAmount);
// Update total donations for the project
projects[projectId].totalDonations += donationAmount;
// Update total donations made by the user

userTotalDonations[msg.sender] += donationAmount;

// Transfer the fee to the contract owner
payable(owner()).transfer(fee);

// Emit an event for the donation
emit DonationMade(msg.sender, projectId, donationAmount);
}

// Function to donate evenly across all projects
function donateToAllProjects() public payable {
require(msg.value > 0, "Donation must be greater than 0");

// Calculate the service fee
uint fee = (msg.value * serviceFeePercentage) / 100;
uint remainingAmount = msg.value - fee;

// Calculate the number of projects
uint projectCount = 0;
for (uint i = 0; ; i++) {
if (projects[i].wallet != address(0)) {
projectCount++;
} else {
break;
}
}

require(projectCount > 0, "No projects to donate to");

// Calculate the donation amount for each project
uint donationPerProject = remainingAmount / projectCount;

// Distribute the donations to each project
for (uint i = 0; i < projectCount; i++) {
if (projects[i].wallet != address(0)) {
projects[i].wallet.transfer(donationPerProject);
projects[i].totalDonations += donationPerProject;
}
}

// Update total donations made by the user
userTotalDonations[msg.sender] += remainingAmount;

// Transfer the fee to the contract owner
payable(owner()).transfer(fee);

// Emit an event for each donation made to a project
for (uint i = 0; i < projectCount; i++) {
if (projects[i].wallet != address(0)) {
emit DonationMade(msg.sender, i, donationPerProject);
}
}
}

// Function to get the total donations for a specific project
// Function to get the total donations for a specific project.
function getTotalDonations(uint projectId) public view returns (uint) {
return projects[projectId].totalDonations;
}

// Function to get the total amount donated by a specific user
// Function to get the total amount donated by a specific user.
function getUserTotalDonations(address user) public view returns (uint) {
return userTotalDonations[user];
}
// Function to retrieve the wallet address of a project based on its ID

// Function to retrieve the wallet address of a project based on its ID.
function getWalletAddressByProjectId(uint projectId) public view returns (address) {
return projects[projectId].wallet;
}
}
}
9 changes: 1 addition & 8 deletions packages/nextjs/contracts/deployedContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { GenericContractsDeclaration } from "~~/utils/scaffold-eth/contract";
const deployedContracts = {
31337: {
KindKoin_DonationHub: {
address: "0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9",
address: "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9",
abi: [
{
anonymous: false,
Expand Down Expand Up @@ -79,13 +79,6 @@ const deployedContracts = {
stateMutability: "payable",
type: "function",
},
{
inputs: [],
name: "donateToAllProjects",
outputs: [],
stateMutability: "payable",
type: "function",
},
{
inputs: [
{
Expand Down

0 comments on commit 1e28973

Please sign in to comment.