Revenue-Sharing Smart Agreement

What it does:
A smart contract that automatically distributes incoming revenue among multiple stakeholders based on predefined, transparent percentages.

Why it matters:
Revenue sharing in traditional businesses is slow, opaque, and trust-based. This contract guarantees instant, trustless, and auditable payouts with zero manual intervention.

How it works:

  • Stakeholders and their revenue shares are defined on-chain.

  • Revenue is sent directly to the contract.

  • Funds are split automatically according to agreed percentages.

  • Each stakeholder can withdraw their share at any time.

  • All distributions are recorded immutably on-chain.

      // SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

/**
 * @title RevenueSharingAgreement
 * @author Nam
 * @notice Automatically distributes revenue among stakeholders
 */
contract RevenueSharingAgreement {

    // -------------------- DATA STRUCTURES --------------------

    struct Stakeholder {
        uint256 share;    // Percentage share (e.g. 25 = 25%)
        uint256 balance;  // Withdrawable revenue
        bool active;
    }

    mapping(address => Stakeholder) public stakeholders;
    address[] public stakeholderList;

    address public owner;
    uint256 public totalShares;

    // -------------------- EVENTS --------------------

    event StakeholderAdded(address indexed stakeholder, uint256 share);
    event RevenueReceived(uint256 amount);
    event RevenueAllocated(uint256 amount);
    event Withdrawal(address indexed stakeholder, uint256 amount);

    // -------------------- MODIFIERS --------------------

    modifier onlyOwner() {
        require(msg.sender == owner, "Not owner");
        _;
    }

    modifier onlyStakeholder() {
        require(stakeholders[msg.sender].active, "Not stakeholder");
        _;
    }

    // -------------------- CONSTRUCTOR --------------------

    constructor() {
        owner = msg.sender;
    }

    // -------------------- STAKEHOLDER MANAGEMENT --------------------

    /**
     * @notice Add a new stakeholder
     * @param _stakeholder Stakeholder address
     * @param _share Revenue share percentage
     */
    function addStakeholder(address _stakeholder, uint256 _share)
        external
        onlyOwner
    {
        require(_stakeholder != address(0), "Invalid address");
        require(!stakeholders[_stakeholder].active, "Already added");
        require(_share > 0, "Invalid share");

        stakeholders[_stakeholder] = Stakeholder({
            share: _share,
            balance: 0,
            active: true
        });

        stakeholderList.push(_stakeholder);
        totalShares += _share;

        emit StakeholderAdded(_stakeholder, _share);
    }

    /**
     * @notice Receive revenue (ETH)
     */
    receive() external payable {
        require(msg.value > 0, "No ETH received");
        emit RevenueReceived(msg.value);

        _allocateRevenue(msg.value);
    }

    /**
     * @notice Internal revenue allocation logic
     */
    function _allocateRevenue(uint256 _amount) internal {
        for (uint256 i = 0; i  0, "Nothing to withdraw");

        stakeholders[msg.sender].balance = 0;
        payable(msg.sender).transfer(amount);

        emit Withdrawal(msg.sender, amount);
    }

    // -------------------- VIEW FUNCTIONS --------------------

    /**
     * @notice Get stakeholder count
     */
    function stakeholderCount() external view returns (uint256) {
        return stakeholderList.length;
    }

    /**
     * @notice Get stakeholder info
     */
    function getStakeholder(address _stakeholder)
        external
        view
        returns (
            uint256 share,
            uint256 balance,
            bool active
        )
    {
        Stakeholder memory s = stakeholders[_stakeholder];
        return (
            s.share,
            s.balance,
            s.active
        );
    }
}