Skip to content
Verified contractExactUNLICENSED

DividendTracker

Address0x3bbd5c94193aa844a8e885990dfd3297ae2e6116
Balance

0 ETH

 

Transactions

35

 

Compiler

solc 0.8.26

 

Same code

52 more contracts

 

Details

Compiler
solc 0.8.26+commit.8a97fa7a
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
Via IR
yes
License
UNLICENSED
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
25,822,169
Creation transaction
0xc6b51249c1…bb854adb9b

2 files

src/DividendTracker.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.26;

import {Addr, IERC20Min, ILaunchpadView} from "./Shared.sol";

contract DividendTracker {
    uint256 internal constant MAGNITUDE = 2 ** 128;
    uint256 public constant MIN_DIVIDEND_BALANCE_BASE = 100_000;
    uint256 public constant MAX_BATCH_SIZE = 500;

    address public immutable token;
    address public immutable launchpad;
    uint256 public immutable MIN_DIVIDEND_BALANCE;

    uint256 public totalShares;
    mapping(address => uint256) public shares;
    mapping(address => bool) public excluded;

    uint256 internal magnifiedDividendPerShare;
    mapping(address => int256) internal magnifiedCorrections;
    mapping(address => uint256) public withdrawnDividends;

    uint256 public totalCredited;
    uint256 public totalPaid;
    uint256 public pending;

    address[] public holders;
    mapping(address => uint256) internal holderIndexPlusOne;

    mapping(address => bool) public blacklistOverride;

    bool private _locked;

    event DividendDistributed(uint256 amount);
    event DividendClaimed(address indexed account, address indexed caller, uint256 amount);
    event DividendRecycled(address indexed account, uint256 amount);
    event ExcludedSet(address indexed account, bool excluded);
    event SharesCapped(address indexed account, uint256 requestedDelta, uint256 cappedDelta);
    event BlacklistOverrideSet(address indexed account, bool blacklisted);

    error OnlyToken();
    error Reentrancy();
    error ZeroAccount();
    error NothingToClaim();
    error NothingToSync();
    error DividendTokenTransferFailed();
    error IntOverflow();
    error BelowMinDividendBalance();
    error TooManyAccounts();
    error OnlyLaunchpad();

    modifier onlyToken() {
        if (msg.sender != token) revert OnlyToken();
        _;
    }

    modifier onlyLaunchpad() {
        if (msg.sender != launchpad) revert OnlyLaunchpad();
        _;
    }

    modifier nonReentrant() {
        if (_locked) revert Reentrancy();
        _locked = true;
        _;
        _locked = false;
    }

    constructor(
        address token_,
        address launchpad_,
        uint256 minDividendBalance_,
        address factory_
    ) {
        token = token_;
        launchpad = launchpad_;

        uint256 minAllowed = MIN_DIVIDEND_BALANCE_BASE * 10 ** 18;
        if (minDividendBalance_ < minAllowed) revert BelowMinDividendBalance();
        MIN_DIVIDEND_BALANCE = minDividendBalance_;

        _setExcluded(token_, true);
        _setExcluded(address(this), true);
        _setExcluded(address(0), true);
        _setExcluded(Addr.DEAD, true);
        _setExcluded(Addr.DEFAULT_BURN, true);
        _setExcluded(Addr.V3_NFT_POS_MGR, true);
        _setExcluded(Addr.V3_SWAP_ROUTER_02, true);
        _setExcluded(Addr.V3_FACTORY, true);

        if (factory_ != address(0)) {
            _setExcluded(factory_, true);
        }
        if (launchpad_ != address(0)) {
            _setExcluded(launchpad_, true);
        }
    }

    function distribute(uint256 amount) external nonReentrant {
        if (amount > 0) {
            _pullDividendToken(msg.sender, amount);
        }
        _distribute(amount);
    }

    function syncDividendToken() public returns (uint256 swept) {
        swept = unallocatedDividendToken();
        if (swept == 0 && pending == 0) revert NothingToSync();
        _distribute(swept);
    }

    function unallocatedDividendToken() public view returns (uint256) {
        uint256 reserved = totalCredited - totalPaid + pending;
        uint256 bal = IERC20Min(Addr.SHIB).balanceOf(address(this));
        return bal > reserved ? bal - reserved : 0;
    }

    function _distribute(uint256 amount) internal {
        uint256 total = amount + pending;
        if (total == 0) return;

        uint256 shareSupply = totalShares;
        if (shareSupply == 0) {
            pending = total;
            return;
        }

        uint256 quotient = total / shareSupply;
        uint256 remainder = total % shareSupply;

        uint256 maxSafeQuotient = type(uint256).max / MAGNITUDE;
        uint256 inc;
        uint256 credited;

        if (quotient <= maxSafeQuotient) {
            inc = quotient * MAGNITUDE + (remainder * MAGNITUDE) / shareSupply;
            credited = total;
        } else {
            inc = maxSafeQuotient * MAGNITUDE;
            credited = maxSafeQuotient * shareSupply;
        }

        uint256 maxRemaining = type(uint256).max - magnifiedDividendPerShare;
        if (inc > maxRemaining) {
            inc = maxRemaining;
            credited = (inc / MAGNITUDE) * shareSupply;
            uint256 remInc = inc % MAGNITUDE;
            credited += (remInc * shareSupply) / MAGNITUDE;
        }

        if (inc > 0) {
            pending = total - credited;
            magnifiedDividendPerShare += inc;
            totalCredited += credited;
            emit DividendDistributed(credited);
            return;
        }

        pending = total;
    }

    function _pullDividendToken(address from, uint256 amount) internal {
        bool ok = IERC20Min(Addr.SHIB).transferFrom(from, address(this), amount);
        if (!ok) revert DividendTokenTransferFailed();
    }

    function setBalance(address account, uint256 newBalance) external onlyToken {
        _syncShares(account, newBalance);
    }

    function setExcluded(address account, bool isExcluded_) external onlyToken {
        _setExcluded(account, isExcluded_);
        _syncShares(account, IERC20Min(token).balanceOf(account));
    }

    function syncAccount(address account) public {
        _syncShares(account, IERC20Min(token).balanceOf(account));
    }

    function syncAccounts(address[] calldata accounts) external {
        uint256 len = accounts.length;
        if (len > MAX_BATCH_SIZE) revert TooManyAccounts();
        for (uint256 i = 0; i < len; i++) {
            _syncShares(accounts[i], IERC20Min(token).balanceOf(accounts[i]));
        }
    }

    function setBlacklistOverride(address account, bool blacklisted) external onlyLaunchpad {
        blacklistOverride[account] = blacklisted;
        emit BlacklistOverrideSet(account, blacklisted);
    }

    function _setExcluded(address account, bool isExcluded_) internal {
        excluded[account] = isExcluded_;
        emit ExcludedSet(account, isExcluded_);
    }

    function isExcluded(address account) public view returns (bool) {
        if (excluded[account]) return true;
        return _blacklisted(account);
    }

    function _blacklisted(address account) internal view returns (bool) {
        if (blacklistOverride[account]) return true;
        (bool ok, bytes memory ret) = launchpad.staticcall(
            abi.encodeWithSelector(ILaunchpadView.dividendBlacklist.selector, account)
        );
        if (!ok || ret.length < 32) return true;
        return abi.decode(ret, (bool));
    }

    function _syncShares(address account, uint256 newBalance) internal {
        bool mustExclude = isExcluded(account) || newBalance < MIN_DIVIDEND_BALANCE;
        uint256 newShares = mustExclude ? 0 : newBalance;
        uint256 oldShares = shares[account];
        if (newShares == oldShares) return;

        uint256 mdps = magnifiedDividendPerShare;
        uint256 delta = newShares > oldShares ? newShares - oldShares : oldShares - newShares;

        bool capApplied = false;
        if (mdps != 0 && delta > uint256(type(int256).max) / mdps) {
            uint256 requested = delta;
            delta = uint256(type(int256).max) / mdps;
            capApplied = true;
            if (!mustExclude) {
                newShares = newShares > oldShares ? oldShares + delta : oldShares - delta;
            }
            emit SharesCapped(account, requested, delta);
        }

        int256 correction = _toInt256(mdps * delta);
        if (newShares > oldShares) {
            totalShares += delta;
            magnifiedCorrections[account] -= correction;
        } else {
            totalShares -= delta;
            magnifiedCorrections[account] += correction;
        }

        if (mustExclude && capApplied && newShares != 0) {
            uint256 residual = newShares;
            totalShares -= residual;
            newShares = 0;
        }

        shares[account] = newShares;

        if (oldShares == 0 && newShares > 0) {
            _addHolder(account);
        } else if (oldShares > 0 && newShares == 0) {
            _removeHolder(account);
            _recycle(account);
        }
    }

    function _recycle(address account) internal {
        if (!_blacklisted(account)) return;

        uint256 owed = withdrawableDividendOf(account);
        if (owed == 0) return;

        withdrawnDividends[account] += owed;
        totalPaid += owed;
        emit DividendRecycled(account, owed);

        (bool ok, bytes memory ret) = launchpad.staticcall(
            abi.encodeWithSelector(ILaunchpadView.platformRecipient.selector)
        );
        if (!ok || ret.length < 32) return;
        address recipient = abi.decode(ret, (address));

        bool success = IERC20Min(Addr.SHIB).transfer(recipient, owed);
        if (!success) {
            withdrawnDividends[account] -= owed;
            totalPaid -= owed;
        }
    }

    function _addHolder(address account) internal {
        if (holderIndexPlusOne[account] != 0) return;
        holders.push(account);
        holderIndexPlusOne[account] = holders.length;
    }

    function _removeHolder(address account) internal {
        uint256 idxPlusOne = holderIndexPlusOne[account];
        if (idxPlusOne == 0) return;
        uint256 idx = idxPlusOne - 1;
        uint256 lastIdx = holders.length - 1;
        if (idx != lastIdx) {
            address last = holders[lastIdx];
            holders[idx] = last;
            holderIndexPlusOne[last] = idx + 1;
        }
        holders.pop();
        holderIndexPlusOne[account] = 0;
    }

    function claim() external nonReentrant returns (uint256) {
        return _withdraw(msg.sender);
    }

    function claimFor(address account) external nonReentrant returns (uint256) {
        if (account == address(0)) revert ZeroAccount();
        return _withdraw(account);
    }

    function claimForMany(address[] calldata accounts) external nonReentrant returns (uint256 totalClaimed) {
        uint256 len = accounts.length;
        if (len > MAX_BATCH_SIZE) revert TooManyAccounts();
        for (uint256 i = 0; i < len; i++) {
            if (accounts[i] == address(0)) continue;
            uint256 amount = _tryWithdraw(accounts[i]);
            if (amount > 0) totalClaimed += amount;
        }
    }

    function _withdraw(address account) internal returns (uint256 claimable) {
        claimable = _tryWithdraw(account);
        if (claimable == 0) revert NothingToClaim();
    }

    function _tryWithdraw(address account) internal returns (uint256 claimable) {
        claimable = withdrawableDividendOf(account);
        if (claimable == 0) return 0;

        uint256 bal = IERC20Min(Addr.SHIB).balanceOf(address(this));
        if (claimable > bal) claimable = bal;
        if (claimable == 0) return 0;

        bool success = IERC20Min(Addr.SHIB).transfer(account, claimable);
        if (!success) {
            return 0;
        }

        withdrawnDividends[account] += claimable;
        totalPaid += claimable;

        emit DividendClaimed(account, msg.sender, claimable);
    }

    function holdersCount() external view returns (uint256) {
        return holders.length;
    }

    function holdersSlice(uint256 offset, uint256 limit) external view returns (address[] memory page) {
        uint256 n = holders.length;
        if (offset >= n) return new address[](0);
        uint256 end = offset + limit;
        if (end > n) end = n;
        page = new address[](end - offset);
        for (uint256 i = offset; i < end; i++) {
            page[i - offset] = holders[i];
        }
    }

    function accumulativeDividendOf(address account) public view returns (uint256) {
        uint256 mdps = magnifiedDividendPerShare;
        uint256 s = shares[account];
        int256 corr = magnifiedCorrections[account];

        if (s == 0) {
            return corr > 0 ? uint256(corr) / MAGNITUDE : 0;
        }
        if (mdps == 0) return 0;

        uint256 whole = mdps / MAGNITUDE;
        uint256 rem = mdps % MAGNITUDE;

        uint256 base;
        if (whole == 0) {
            base = 0;
        } else if (whole <= type(uint256).max / s) {
            base = whole * s;
        } else {
            return type(uint256).max;
        }

        uint256 remS;
        if (rem == 0) {
            remS = 0;
        } else if (rem > type(uint256).max / s) {
            return type(uint256).max;
        } else {
            remS = rem * s;
        }

        if (remS > uint256(type(int256).max)) {
            return type(uint256).max;
        }

        int256 numerator = _toInt256(remS) + corr;
        int256 fractional = numerator / int256(MAGNITUDE);

        if (fractional >= 0) {
            uint256 fracU = uint256(fractional);
            return base > type(uint256).max - fracU ? type(uint256).max : base + fracU;
        } else {
            uint256 absFrac = uint256(-fractional);
            return base > absFrac ? base - absFrac : 0;
        }
    }

    function withdrawableDividendOf(address account) public view returns (uint256) {
        uint256 acc = accumulativeDividendOf(account);
        uint256 withdrawn = withdrawnDividends[account];
        return acc > withdrawn ? acc - withdrawn : 0;
    }

    function outstandingDividends() external view returns (uint256) {
        return totalCredited - totalPaid;
    }

    function _toInt256(uint256 x) private pure returns (int256) {
        if (x > uint256(type(int256).max)) revert IntOverflow();
        return int256(x);
    }
}

src/Shared.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.26;

library Addr {
    address internal constant DEAD = 0x000000000000000000000000000000000000dEaD;
    address internal constant DEFAULT_BURN = 0xdEAD000000000000000042069420694206942069;

    address internal constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
    address internal constant SHIB = 0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE;

    address internal constant V3_FACTORY         = 0x1F98431c8aD98523631AE4a59f267346ea31F984;
    address internal constant V3_SWAP_ROUTER_02  = 0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45;
    address internal constant V3_NFT_POS_MGR     = 0xC36442b4a4522E871399CD717aBDD847Ab11FE88;
}

interface IERC20Min {
    function balanceOf(address account) external view returns (uint256);
    function transfer(address to, uint256 amount) external returns (bool);
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
    function decimals() external view returns (uint8);
}

interface ISwapRouter02 {
    struct ExactInputSingleParams {
        address tokenIn;
        address tokenOut;
        uint24 fee;
        address recipient;
        uint256 amountIn;
        uint256 amountOutMinimum;
        uint160 sqrtPriceLimitX96;
    }

    function exactInputSingle(ExactInputSingleParams calldata params)
        external
        payable
        returns (uint256 amountOut);
}

interface INonfungiblePositionManager {
    struct MintParams {
        address token0;
        address token1;
        uint24 fee;
        int24 tickLower;
        int24 tickUpper;
        uint256 amount0Desired;
        uint256 amount1Desired;
        uint256 amount0Min;
        uint256 amount1Min;
        address recipient;
        uint256 deadline;
    }

    struct CollectParams {
        uint256 tokenId;
        address recipient;
        uint128 amount0Max;
        uint128 amount1Max;
    }

    function createAndInitializePoolIfNecessary(
        address token0,
        address token1,
        uint24 fee,
        uint160 sqrtPriceX96
    ) external payable returns (address pool);

    function mint(MintParams calldata params)
        external
        payable
        returns (
            uint256 tokenId,
            uint128 liquidity,
            uint256 amount0,
            uint256 amount1
        );

    function refundETH() external payable;

    function collect(CollectParams calldata params)
        external
        payable
        returns (
            uint256 amount0,
            uint256 amount1
        );
}

interface IWETH {
    function deposit() external payable;
    function withdraw(uint256 amount) external;
    function balanceOf(address account) external view returns (uint256);
    function transfer(address to, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
}

interface ILaunchpadView {
    function dividendBlacklist(address account) external view returns (bool);
    function platformRecipient() external view returns (address);
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "forge-std/=lib/forge-std/src/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.