Skip to content
Verified contractExactMIT

TrendingPaymentSplitter

Address0xcaaad0bb682fd8bf4ba061309600328e0f4611eb
Balance

0 ETH

 

Transactions

15

 

Compiler

solc 0.8.24

 

Same code

1 more contract

 

Details

Compiler
solc 0.8.24+commit.e11b9ed9
Language
solidity
Optimization
Yes, 200 runs
EVM version
paris
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,126,444
Creation transaction
0x8bad2a413f…0eea513645

src/TrendingPaymentSplitter.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

/*
 * TrendingPaymentSplitter (v2)
 * ----------------------------
 * Receives payments for the trending service and automatically splits them
 * among a fixed set of payees according to shares (basis points).
 *
 * Same design as the v1 already live at 0x6B63937987D75A9a1aa06a5C6E86Fa471dec3227
 * (verified on Etherscan), with two changes:
 *   - distributeToken() uses a safe transfer, so tokens that return no bool
 *     (e.g. USDT on Ethereum) can be split instead of getting stuck forever;
 *   - getPayees() view, so the deploy script can check payees/shares in one call.
 *
 * There is NO owner, NO admin and NO upgrade path: payees and shares are fixed
 * in the constructor and can never change.
 *
 *  - NATIVE COIN (ETH / BNB / MON / AVAX / USDC on ARC / ...):
 *      Fully automatic. Send the payment TO THIS CONTRACT'S ADDRESS and the
 *      receive() function distributes it in the same transaction.
 *      (So the "trending payment wallet" is simply this contract.)
 *
 *  - ERC-20 TOKENS:
 *      The EVM has no hook that fires when a contract receives an ERC-20,
 *      so it cannot be split in the same tx. Instead, after the token lands,
 *      anyone (e.g. your bot) calls distributeToken(token) to split it.
 *
 * Safety: if a payee cannot receive native coin (e.g. it's a reverting
 * contract), that payee's slice is credited to a pull-withdraw ledger instead
 * of reverting the whole payment. Nobody can block the split for the others.
 */

interface IERC20 {
    function balanceOf(address account) external view returns (uint256);
}

contract TrendingPaymentSplitter {
    uint256 public constant TOTAL_SHARES = 10_000; // basis points (100.00%)

    address[] public payees;
    mapping(address => uint256) public shareOf;     // payee => basis points
    mapping(address => uint256) public owedNative;  // payee => pull-withdraw balance

    uint256 private _lock = 1;
    modifier nonReentrant() {
        require(_lock == 1, "reentrant");
        _lock = 2;
        _;
        _lock = 1;
    }

    event PaymentReceived(address indexed from, uint256 amount);
    event NativeReleased(address indexed to, uint256 amount);
    event NativeOwed(address indexed to, uint256 amount);   // send failed -> pull later
    event TokenReleased(address indexed token, address indexed to, uint256 amount);
    event Withdrawn(address indexed to, uint256 amount);

    /**
     * @param _payees  list of recipient addresses
     * @param _shares  share of each payee in basis points; MUST sum to 10_000
     */
    constructor(address[] memory _payees, uint256[] memory _shares) {
        require(_payees.length == _shares.length, "len mismatch");
        require(_payees.length > 0, "no payees");

        uint256 sum;
        for (uint256 i = 0; i < _payees.length; i++) {
            address p = _payees[i];
            require(p != address(0), "zero payee");
            require(shareOf[p] == 0, "dup payee");
            require(_shares[i] > 0, "zero share");

            payees.push(p);
            shareOf[p] = _shares[i];
            sum += _shares[i];
        }
        require(sum == TOTAL_SHARES, "shares != 10000");
    }

    // -------- Native coin: automatic split on arrival --------

    receive() external payable nonReentrant {
        emit PaymentReceived(msg.sender, msg.value);
        _distributeNative(msg.value);
    }

    /// Split any native balance sitting in the contract (e.g. leftover dust,
    /// or coins that arrived without triggering receive()).
    function distributeNative() external nonReentrant {
        uint256 bal = address(this).balance - _totalOwed();
        require(bal > 0, "nothing to split");
        _distributeNative(bal);
    }

    function _distributeNative(uint256 amount) internal {
        uint256 len = payees.length;
        uint256 distributed;

        for (uint256 i = 0; i < len; i++) {
            address p = payees[i];
            // Give any rounding dust to the last payee.
            uint256 slice = (i == len - 1)
                ? amount - distributed
                : (amount * shareOf[p]) / TOTAL_SHARES;
            distributed += slice;
            if (slice == 0) continue;

            (bool ok, ) = payable(p).call{value: slice, gas: 30000}("");
            if (ok) {
                emit NativeReleased(p, slice);
            } else {
                owedNative[p] += slice; // pull-withdraw fallback
                emit NativeOwed(p, slice);
            }
        }
    }

    /// A payee pulls native coin that failed to auto-send.
    function withdraw() external nonReentrant {
        uint256 amount = owedNative[msg.sender];
        require(amount > 0, "nothing owed");
        owedNative[msg.sender] = 0;
        (bool ok, ) = payable(msg.sender).call{value: amount}("");
        require(ok, "withdraw failed");
        emit Withdrawn(msg.sender, amount);
    }

    // -------- ERC-20: split on demand (call from your bot) --------

    /// Split the contract's full balance of `token` among the payees.
    function distributeToken(IERC20 token) external nonReentrant {
        uint256 bal = token.balanceOf(address(this));
        require(bal > 0, "no token balance");

        uint256 len = payees.length;
        uint256 distributed;
        for (uint256 i = 0; i < len; i++) {
            address p = payees[i];
            uint256 slice = (i == len - 1)
                ? bal - distributed
                : (bal * shareOf[p]) / TOTAL_SHARES;
            distributed += slice;
            if (slice == 0) continue;

            _safeTransfer(address(token), p, slice);
            emit TokenReleased(address(token), p, slice);
        }
    }

    /// transfer() that also accepts tokens returning no value (USDT on Ethereum),
    /// and still rejects tokens that return false.
    function _safeTransfer(address token, address to, uint256 amount) private {
        require(token.code.length > 0, "not a token");
        (bool ok, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, amount));
        require(ok && (data.length == 0 || abi.decode(data, (bool))), "token transfer failed");
    }

    // -------- Views --------

    function payeesCount() external view returns (uint256) {
        return payees.length;
    }

    /// All payees and their shares (basis points), in payout order.
    function getPayees() external view returns (address[] memory list, uint256[] memory shares) {
        uint256 len = payees.length;
        list = new address[](len);
        shares = new uint256[](len);
        for (uint256 i = 0; i < len; i++) {
            list[i] = payees[i];
            shares[i] = shareOf[payees[i]];
        }
    }

    function _totalOwed() internal view returns (uint256 total) {
        for (uint256 i = 0; i < payees.length; i++) {
            total += owedNative[payees[i]];
        }
    }
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "paris",
  "remappings": [
    "forge-std/=lib/forge-std/src/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.