// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; /// @title ReferralRewards /// @notice Minimal reentrancy guard (inlined — keeps the contract dependency-free). abstract contract ReentrancyGuard { uint256 private constant _NOT_ENTERED = 1; uint256 private constant _ENTERED = 2; uint256 private _status = _NOT_ENTERED; error ReentrancyGuardReentrantCall(); modifier nonReentrant() { if (_status == _ENTERED) revert ReentrancyGuardReentrantCall(); _status = _ENTERED; _; _status = _NOT_ENTERED; } } /// @notice Minimal BEP-20 interface (transfer / balanceOf only — all this contract needs). interface IERC20 { function transfer(address to, uint256 amount) external returns (bool); function balanceOf(address account) external view returns (uint256); } /// @notice Minimal BAD token interface (the launch gate). interface IBadToken { function tradingEnabled() external view returns (bool); } /// @title ReferralRewards /// @notice Trustless referral-reward pool for Bad Decisions (BAD). /// @dev Design — deliberately the smallest possible trust surface: /// 1. The founder deploys this contract, funds it with BAD, verifies the /// source on BscScan, then renounces ownership. That is the whole setup. /// 2. The deployer (owner) has exactly ONE privileged function: /// renounceOwnership(). There is NO withdraw, NO sweep, NO rescue, /// NO signer change, NO pause, NO amount/cap change — those functions /// do not exist in this contract at all, before or after renouncing. /// 3. A referral reward (fixed 50,000 BAD) is paid only against an /// EIP-712 voucher signed by the immutable `signer` (the app backend's /// verification key, set once in the constructor). The voucher binds /// one referrer to one referred account id (an opaque bytes32 hash of /// the app account's unique id, computed off-chain). /// 4. Anyone can relay a claim (claimFor) — the BAD always goes to the /// voucher's referrer, never to the caller. Claiming yourself is the /// same call with you as the referrer (claim). /// 5. Rules enforced on-chain, forever: each referredId pays out at most /// once, each referrer can be paid for at most 10 referrals, and the /// lifetime total paid can never exceed the 1B BAD pool cap — /// regardless of the contract's balance. /// 6. Claims only open once BAD trading is live (tradingEnabled) — BAD's /// own transfer gate blocks payouts before launch anyway. /// No proxy, no upgrade, no backdoor — immutable by design. contract ReferralRewards is ReentrancyGuard { /// @notice The BAD token contract. IERC20 public immutable badToken; /// @notice EIP-712 voucher signer (app verification key). Set once, forever. address public immutable signer; /// @notice The deployer. Its ONLY power is renounceOwnership(). address public owner; /// @notice Fixed reward per verified referral: 50,000 BAD. uint256 public constant REWARD_PER_REFERRAL = 50_000 * 1e18; /// @notice Max paid referrals per referrer. uint256 public constant MAX_PAID_PER_REFERRER = 10; /// @notice Lifetime payout cap: 1B BAD. totalPaid can never exceed this. uint256 public constant POOL_CAP = 1_000_000_000 * 1e18; /// @notice EIP-712 domain separator (name "BadDecisionsReferralRewards", version "1"). bytes32 private immutable _domainSeparator; /// @notice EIP-712 type hash for ReferralClaim(address referrer, bytes32 referredId). bytes32 private constant _CLAIM_TYPEHASH = keccak256("ReferralClaim(address referrer,bytes32 referredId)"); /// @notice secp256k1 group order / 2 — signatures with s above this are invalid (EIP-2). uint256 private constant _SECP256K1N_HALF = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0; /// @notice Lifetime BAD paid out (wei). Never exceeds POOL_CAP. uint256 public totalPaid; /// @notice Paid referral count per referrer (never exceeds MAX_PAID_PER_REFERRER). mapping(address => uint256) private _paidCount; /// @notice Whether a referred account id has already been paid out. mapping(bytes32 => bool) private _referredClaimed; event Claimed(address indexed referrer, bytes32 indexed referredId, uint256 amount); event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); error ZeroAddress(); error NotOwner(); error TradingNotLive(); error InvalidSignature(); error ReferredAlreadyClaimed(); error ReferrerCapReached(); error PoolCapExceeded(); error InsufficientBalance(); error TransferFailed(); /// @param _badToken BAD token contract address. /// @param _signer EIP-712 voucher signer address (immutable). constructor(address _badToken, address _signer) { if (_badToken == address(0) || _signer == address(0)) revert ZeroAddress(); badToken = IERC20(_badToken); signer = _signer; owner = msg.sender; _domainSeparator = keccak256( abi.encode( keccak256( "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)" ), keccak256(bytes("BadDecisionsReferralRewards")), keccak256(bytes("1")), block.chainid, address(this) ) ); emit OwnershipTransferred(address(0), msg.sender); } /// @notice The owner's ONLY privileged function: give up ownership forever. /// @dev After this call owner() is address(0) and nobody holds any privilege. /// Claims keep working exactly as before — they never depended on the owner. function renounceOwnership() external { if (msg.sender != owner) revert NotOwner(); address previous = owner; owner = address(0); emit OwnershipTransferred(previous, address(0)); } /// @notice Claim YOUR referral reward: the voucher's referrer must be you. /// @param referredId Hash of the referred app account's unique id. /// @param signature EIP-712 signature from `signer` over ReferralClaim(you, referredId). function claim(bytes32 referredId, bytes calldata signature) external { _claim(msg.sender, referredId, signature); } /// @notice Relay a claim for someone else. The BAD goes ONLY to the referrer — /// the caller can never receive or redirect it. /// @param referrer The voucher's referrer (the payee). /// @param referredId Hash of the referred app account's unique id. /// @param signature EIP-712 signature from `signer` over ReferralClaim(referrer, referredId). function claimFor(address referrer, bytes32 referredId, bytes calldata signature) external { _claim(referrer, referredId, signature); } /// @dev Shared claim path. Checks → signature → effects → interaction. function _claim(address referrer, bytes32 referredId, bytes calldata signature) internal nonReentrant { if (referrer == address(0)) revert ZeroAddress(); if (!IBadToken(address(badToken)).tradingEnabled()) revert TradingNotLive(); if (_referredClaimed[referredId]) revert ReferredAlreadyClaimed(); if (_paidCount[referrer] >= MAX_PAID_PER_REFERRER) revert ReferrerCapReached(); if (totalPaid + REWARD_PER_REFERRAL > POOL_CAP) revert PoolCapExceeded(); if (badToken.balanceOf(address(this)) < REWARD_PER_REFERRAL) { revert InsufficientBalance(); } bytes32 structHash = keccak256(abi.encode(_CLAIM_TYPEHASH, referrer, referredId)); bytes32 digest = keccak256(abi.encodePacked("\x19\x01", _domainSeparator, structHash)); if (_recover(digest, signature) != signer) revert InvalidSignature(); // Effects before interaction — a re-entrant or repeated call finds // the referredId already spent and the counters already moved. _referredClaimed[referredId] = true; _paidCount[referrer] += 1; totalPaid += REWARD_PER_REFERRAL; if (!badToken.transfer(referrer, REWARD_PER_REFERRAL)) revert TransferFailed(); emit Claimed(referrer, referredId, REWARD_PER_REFERRAL); } /// @dev ecrecover with strict signature hygiene: 65-byte signature, /// v in {27, 28}, s in the lower half-order (no malleability). function _recover(bytes32 digest, bytes calldata signature) internal pure returns (address) { if (signature.length != 65) revert InvalidSignature(); bytes32 r; bytes32 s; uint8 v; assembly { r := calldataload(signature.offset) s := calldataload(add(signature.offset, 32)) v := byte(0, calldataload(add(signature.offset, 64))) } if (v != 27 && v != 28) revert InvalidSignature(); if (uint256(s) > _SECP256K1N_HALF) revert InvalidSignature(); address recovered = ecrecover(digest, v, r, s); if (recovered == address(0)) revert InvalidSignature(); return recovered; } /// @notice Paid referral count for a referrer. function paidCountOf(address referrer) external view returns (uint256) { return _paidCount[referrer]; } /// @notice Whether a referred account id has already been paid out. function isReferredClaimed(bytes32 referredId) external view returns (bool) { return _referredClaimed[referredId]; } /// @notice BAD still payable from the pool (POOL_CAP − totalPaid). function remainingPool() external view returns (uint256) { return POOL_CAP - totalPaid; } /// @notice The EIP-712 domain separator for this contract (for off-chain voucher tooling). function domainSeparator() external view returns (bytes32) { return _domainSeparator; } }