Online Course Revenue Sharing

What it does:
Automatically splits revenue from online course sales among educators, platform, and collaborators based on predefined percentages.

Why it matters:
Ensures fair, transparent, and automated revenue distribution, reduces disputes, and incentivizes contributors and collaborators to create quality courses.

How it works:

  • Course creator sets up revenue sharing percentages for educators, collaborators, and platform

  • Students purchase courses by sending funds to the smart contract

  • Contract calculates each party’s share automatically

  • Funds are released immediately or scheduled according to the revenue plan

  • All revenue transactions and distributions are logged on-chain

  • Collaborators can verify payments without relying on the platform

  • Contracts can handle multiple courses and update revenue shares as needed

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

/**
 * @title OnlineCourseRevenueSharing
 * @author Nam
 * @notice Splits course revenue automatically among educators and collaborators
 */
contract OnlineCourseRevenueSharing {

    // -------------------- STRUCTS --------------------

    struct Course {
        address creator;
        address[] collaborators;
        uint256[] collaboratorShares; // percentage in basis points (10000 = 100%)
        uint256 platformShare; // in basis points
        bool active;
    }

    mapping(uint256 => Course) public courses;
    uint256 public courseCount;

    address public platform;
    uint256 public constant TOTAL_SHARE = 10000; // 100%

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

    event CourseCreated(uint256 indexed courseId, address creator);
    event RevenueReceived(uint256 indexed courseId, uint256 amount);
    event RevenueDistributed(uint256 indexed courseId, uint256 totalAmount);

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

    constructor(address _platform) {
        require(_platform != address(0), "Invalid platform address");
        platform = _platform;
    }

    // -------------------- COURSE MANAGEMENT --------------------

    function createCourse(
        address[] calldata _collaborators,
        uint256[] calldata _collaboratorShares,
        uint256 _platformShare
    ) external {
        require(_collaborators.length == _collaboratorShares.length, "Length mismatch");

        uint256 totalCollaboratorShares = 0;
        for (uint256 i = 0; i < _collaboratorShares.length; i++) {
            totalCollaboratorShares += _collaboratorShares[i];
        }

        require(totalCollaboratorShares + _platformShare  0, "No payment sent");

        emit RevenueReceived(_courseId, msg.value);

        // Distribute platform share
        uint256 platformAmount = (msg.value * c.platformShare) / TOTAL_SHARE;
        if (platformAmount > 0) {
            payable(platform).transfer(platformAmount);
        }

        // Distribute collaborator shares
        for (uint256 i = 0; i  0) {
                payable(c.collaborators[i]).transfer(shareAmount);
            }
        }

        // Remaining goes to creator
        uint256 distributed = platformAmount;
        for (uint256 i = 0; i < c.collaboratorShares.length; i++) {
            distributed += (msg.value * c.collaboratorShares[i]) / TOTAL_SHARE;
        }

        uint256 creatorAmount = msg.value - distributed;
        payable(c.creator).transfer(creatorAmount);

        emit RevenueDistributed(_courseId, msg.value);
    }

    // -------------------- FUNDING RECEIVED --------------------

    receive() external payable {}
}