// SPDX-License-Identifier: MIT pragma solidity 0.8.28; import {IERC20, ISwapRouter02, SafeTransfer} from "./Interfaces.sol"; /// @title ClapperFeeReserve /// @notice Holds the 2% USDG fee from settled Clapper markets. Once the $CLAPPER token address is set (one time, by /// the deployer), anyone can call buyAndBurn: USDG is swapped through the Robinhood Chain Uniswap router and the /// tokens go straight to 0x...dEaD. Before that, fees only accumulate and buyAndBurn reverts with BuyAndBurnNotLive. /// @dev No withdraw function exists. The only way USDG leaves is a swap whose recipient is the dead address. contract ClapperFeeReserve { using SafeTransfer for IERC20; address public constant DEAD = 0x000000000000000000000000000000000000dEaD; uint256 public constant MAX_PER_BURN = 500e6; // 500 USDG per call limits what one sandwich can take uint256 public constant BURN_COOLDOWN = 1 hours; IERC20 public immutable usdg; ISwapRouter02 public immutable router; address public immutable deployer; address public token; bytes public path; uint256 public lastBurnAt; uint256 public totalUsdgBurned; uint256 public totalTokenBurned; uint256 private _lock = 1; event TokenSet(address indexed token, bytes path); event BoughtAndBurned(address indexed caller, uint256 usdgIn, uint256 tokenOut); error BuyAndBurnNotLive(); error OnlyDeployer(); error TokenAlreadySet(); error BadPath(); error Cooldown(); error NothingToBurn(); error Reentrancy(); error ZeroAddress(); constructor(IERC20 usdg_, ISwapRouter02 router_) { if (address(usdg_) == address(0) || address(router_) == address(0)) revert ZeroAddress(); usdg = usdg_; router = router_; deployer = msg.sender; } /// @notice One-time switch-on. `path_` is a Uniswap v3 path that starts at USDG and ends at `token_`. function setToken(address token_, bytes calldata path_) external { if (msg.sender != deployer) revert OnlyDeployer(); if (token != address(0)) revert TokenAlreadySet(); if (token_ == address(0)) revert ZeroAddress(); if (path_.length < 43 || (path_.length - 20) % 23 != 0) revert BadPath(); if (address(bytes20(path_[0:20])) != address(usdg)) revert BadPath(); if (address(bytes20(path_[path_.length - 20:])) != token_) revert BadPath(); token = token_; path = path_; emit TokenSet(token_, path_); } function isLive() external view returns (bool) { return token != address(0); } function balance() external view returns (uint256) { return usdg.balanceOf(address(this)); } /// @notice Swap up to MAX_PER_BURN USDG into $CLAPPER and send it to the dead address. Anyone can call. function buyAndBurn(uint256 amountIn, uint256 minOut) external returns (uint256 out) { if (_lock != 1) revert Reentrancy(); _lock = 2; if (token == address(0)) revert BuyAndBurnNotLive(); if (lastBurnAt != 0 && block.timestamp < lastBurnAt + BURN_COOLDOWN) revert Cooldown(); uint256 bal = usdg.balanceOf(address(this)); if (amountIn > bal) amountIn = bal; if (amountIn > MAX_PER_BURN) amountIn = MAX_PER_BURN; if (amountIn == 0) revert NothingToBurn(); lastBurnAt = block.timestamp; usdg.safeApprove(address(router), amountIn); out = router.exactInput( ISwapRouter02.ExactInputParams({path: path, recipient: DEAD, amountIn: amountIn, amountOutMinimum: minOut}) ); usdg.safeApprove(address(router), 0); totalUsdgBurned += amountIn; totalTokenBurned += out; emit BoughtAndBurned(msg.sender, amountIn, out); _lock = 1; } }