Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
naiirad committed Nov 28, 2023
1 parent edd11a8 commit fa49eb8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions packages/hardhat/contracts/KindKoin_DonationHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ contract KindKoin_DonationHub is Ownable, Pausable, ReentrancyGuard {
// Add the third project
setProject(2, payable(address(0x90F79bf6EB2c4f870365E785982E1f101E93b906)), "Humanitas in Centro");
}

// Modifier to check if a project exists.
modifier projectExists(uint projectId) {
require(projects[projectId].wallet != address(0), "Project does not exist");
_;
}

// Allows the owner to add a new project.
function setProject(uint projectId, address payable projectWallet, string memory projectName) public onlyOwner whenNotPaused returns (bool) {
require(projectId >= 0 && projectId < maxProjectCount, "Invalid project ID or exceeds max limit");
Expand All @@ -73,8 +80,7 @@ contract KindKoin_DonationHub is Ownable, Pausable, ReentrancyGuard {
}

// Allows anyone to donate DFI to a project.
function donateDFI(uint projectId) public payable whenNotPaused nonReentrant {
require(projects[projectId].wallet != address(0), "Project does not exist");
function donateDFI(uint projectId) public payable whenNotPaused nonReentrant projectExists(projectId) {
require(msg.value > 0, "Donation must be greater than 0");

uint fee = (msg.value * serviceFeeBasisPoints) / 1000;
Expand All @@ -91,8 +97,7 @@ contract KindKoin_DonationHub is Ownable, Pausable, ReentrancyGuard {
}

// Allows donations in supported ERC20 tokens.
function donateWithToken(uint projectId, address tokenAddress, uint amount) public whenNotPaused nonReentrant {
require(projects[projectId].wallet != address(0), "Project does not exist");
function donateWithToken(uint projectId, address tokenAddress, uint amount) public whenNotPaused nonReentrant projectExists(projectId) {
require(amount > 0, "Donation must be greater than 0");
require(IERC20(tokenAddress).transferFrom(msg.sender, address(this), amount), "Transfer failed");

Expand All @@ -109,15 +114,15 @@ contract KindKoin_DonationHub is Ownable, Pausable, ReentrancyGuard {
withdrawTokenDonations(projectId, tokenAddress);
}

// Allows a project to withdraw their DFI donations.
// Automatically transfers DFI donations to the project after a donation is made.
function withdrawDFIDonations(uint projectId) private {
Project storage project = projects[projectId];
uint amount = project.pendingWithdrawals;
project.pendingWithdrawals = 0;
project.wallet.transfer(amount);
}

// Allows a project to withdraw their token donations.
// Automatically transfers Token donations to the project after a donation is made.
function withdrawTokenDonations(uint projectId, address tokenAddress) private {
Project storage project = projects[projectId];
uint amount = project.pendingWithdrawals;
Expand Down
2 changes: 1 addition & 1 deletion 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: "0x322813Fd9A801c5507c9de605d63CEA4f2CE6c44",
address: "0x4A679253410272dd5232B3Ff7cF5dbB88f295319",
abi: [
{
inputs: [],
Expand Down

0 comments on commit fa49eb8

Please sign in to comment.