StabilityNexus / FairFund

5 stars 8 forks source link

Implement platform fee. #43

Closed adityabhattad2021 closed 2 months ago

adityabhattad2021 commented 2 months ago

Overview

To sustain the development of the FairFund project, a certain percentage of platform fees will be charged on every fund transfer to a proposal.

Changes

/**
 * @dev Distributes the funds to the proposals
 * @notice Can only be called after the tally date has passed
 */
function distributeFunds() external nonReentrant tallyDatePassed {
    if (s_fundsDistributed) {
        revert FundingVault__AlreadyDistributedFunds();
    }
    s_fundsDistributed = true;
    uint256 platformFeePercentage = i_deployer.getPlatformFee();
    uint256 feeAmount = 0;

    for (uint256 i = 1; i <= s_proposalIdCounter; i++) {
        uint256 amount = calculateFundingToBeReceived(i);
        if (amount > 0) {
            Proposal memory proposal = s_proposals[i];
            uint256 fee = (amount * platformFeePercentage) / 100;
            amount -= fee;
            feeAmount += fee;
            bool success = i_fundingToken.transfer(proposal.recipient, amount);
            if (!success) {
                revert FundingVault__TransferFailed();
            }
            s_totalFundsDistributed += amount + fee;
            emit FundsDistributed(i, proposal.recipient, amount);
        }
    }
    if (feeAmount > 0) {
        bool success = i_fundingToken.transfer(address(i_deployer), feeAmount);
        if (!success) {
            revert FundingVault__TransferFailed();
        }
        emit PlatformFeeSubmitted(address(i_deployer), feeAmount);
    }
}