Real Estate Commission Automation

What it does:
Automatically calculates and distributes real estate commissions to agents and brokers once a property transaction is successfully completed.

Why it matters:
Eliminates commission disputes, manual calculations, and delayed payouts by enforcing transparent, pre-agreed commission rules on-chain.

How it works:

  • Commission rates for each agent or broker are defined at deployment

  • Sale proceeds are deposited into the smart contract

  • Contract verifies that the transaction is completed

  • Commission amounts are calculated automatically

  • Agents receive their payouts instantly

  • Remaining funds are released to the seller

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

/**
 * @title RealEstateCommissionAutomation
 * @author Nam
 * @notice Automates commission calculation and payout for real estate transactions
 */
contract RealEstateCommissionAutomation {

    // -------------------- ROLES --------------------

    address public seller;
    address public buyer;

    // -------------------- COMMISSION STRUCT --------------------

    struct Agent {
        address payable wallet;
        uint256 commissionRate; // basis points (e.g. 300 = 3%)
    }

    Agent[] public agents;

    // -------------------- SALE TERMS --------------------

    uint256 public salePrice;
    bool public saleCompleted;

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

    event SaleDeposited(address indexed buyer, uint256 amount);
    event CommissionPaid(address indexed agent, uint256 amount);
    event SaleFinalized(uint256 sellerAmount);

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

    modifier onlyBuyer() {
        require(msg.sender == buyer, "Not buyer");
        _;
    }

    modifier onlySeller() {
        require(msg.sender == seller, "Not seller");
        _;
    }

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

    constructor(
        address _buyer,
        Agent[] memory _agents
    ) {
        require(_buyer != address(0), "Invalid buyer");

        seller = msg.sender;
        buyer = _buyer;

        uint256 totalRate;
        for (uint256 i = 0; i  0, "Invalid rate");

            agents.push(_agents[i]);
            totalRate += _agents[i].commissionRate;
        }

        require(totalRate  0, "Invalid sale amount");

        salePrice = msg.value;
        emit SaleDeposited(msg.sender, msg.value);
    }

    /**
     * @notice Finalize sale and distribute commissions
     */
    function finalizeSale() external onlySeller {
        require(salePrice > 0, "No funds deposited");
        require(!saleCompleted, "Already finalized");

        saleCompleted = true;

        uint256 remaining = salePrice;

        for (uint256 i = 0; i < agents.length; i++) {
            uint256 commission =
                (salePrice * agents[i].commissionRate) / 10000;
            remaining -= commission;
            agents[i].wallet.transfer(commission);

            emit CommissionPaid(agents[i].wallet, commission);
        }

        payable(seller).transfer(remaining);
        emit SaleFinalized(remaining);
    }
}