Skip to content
Verified contractPartial

TokenBurner

Address0xd1c87ea0f72a78aa1d3246a2e4998673fa441ee4
Balance

0.060924 ETH

 

Transactions

29

 

Compiler

solc 0.8.26

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.26+commit.8a97fa7a
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
License
Not stated
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
25,293,985
Creation transaction
0x17c7de0f3e…f7f965d893

72 files

The source code is large: one file is shown at a time. Choose a file in the list.

src/TokenBurner.sol

pragma solidity ^0.8.26;

import {IERC20Burnable} from "./utils/IERC20Burnable.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {JrnyHook} from "./JrnyHook.sol";

import {InteractsWithUniversalRouter} from "./utils/InteractsWithUniversalRouter.sol";

contract TokenBurner is Ownable, ReentrancyGuard, InteractsWithUniversalRouter {

    // ── Errors ────────────────────────────────────────────────
    error ProtocolBurnLimitExceeded();
    error ProtocolMinAmountRequirementNotMeet();
    error ProtocolAmountOutMinIsTooLow();
    error AdminValueTooBig();
    error AdminValueTooSmall();
    error AdminWrongValue();
    error AdminValueCannotBeZero();
    error AdminEmergencyWithdrawFailed();

    // ── Events ────────────────────────────────────────────────
    event ProtocolBoughtAndBurned(address indexed sender, uint256 ethAmount, uint256 tokenBurn);
    event AdminUpdatePoolKey();
    event AdminWithdraw(uint256 amount);
    event ProtocolDistributedTip(address indexed user, uint256 amount);
    event AdminTipChanged(uint256 amount);
    event AdminFloorChanged(uint256 amount);
    event AdminCooldownChanged(uint256 amount);
    event AdminFeeDirectionChanged(uint256 direction);
    event AdminIntervalSizeChanged(uint256 interval);
    event AdminIntervalMaxAmountChanged(uint256 amount);
    event AdminTransactionMinAmountChanged(uint256 amount);

    // ── Variables ─────────────────────────────────────────────
    uint8 private constant ENABLE_FEE_SELL = 1 << 1;
    uint8 private constant ENABLE_FEE_BUY = 1;

    address public immutable router;
    PoolKey public poolKey;
    IERC20Burnable public immutable token;
    JrnyHook public immutable hook;

    uint256 private executorTip;
    uint256 private intervalSize;
    uint256 private intervalMaxAmount;
    uint256 private transactionMinAmount;
    uint256 private currentInterval;
    uint256 private currentIntervalAmount;

    uint256 private shockFeesCooldown;
    uint256 private floor;
    uint8 private feeDirection;

    // ── Initialization ────────────────────────────────────────
    constructor(address payable _router, PoolKey memory _poolKey, address tokenAddress, address payable hookAddress) Ownable(msg.sender) {
        router = _router;
        poolKey = _poolKey;
        token = IERC20Burnable(tokenAddress);
        hook = JrnyHook(hookAddress);

        executorTip = 25; // 0.5%
        intervalSize = 3600;
        intervalMaxAmount = 2 ether;
        transactionMinAmount = 0.1 ether;
        shockFeesCooldown = 300;

        floor = 9000;

        feeDirection = ENABLE_FEE_SELL;
    }

    // ── External state-modifying ──────────────────────────────
    // @dev Swap tokens and burn the output
    // @dev burns entire token balance including any pre-existing holdings
    function swapAndBurn(uint256 amountToBurn, uint256 amountOutMin) external nonReentrant {
        if (amountOutMin / amountToBurn < floor) {
            revert ProtocolAmountOutMinIsTooLow();
        }

        uint256 blockInterval = block.timestamp / intervalSize;

        if (blockInterval != currentInterval) {
            currentIntervalAmount = 0;
            currentInterval = blockInterval;
        }

        if (intervalMaxAmount < currentIntervalAmount + amountToBurn) {
            revert ProtocolBurnLimitExceeded();
        }

        if (transactionMinAmount > amountToBurn) {
            revert ProtocolMinAmountRequirementNotMeet();
        }

        currentIntervalAmount += amountToBurn;

        swapExactInputSingle(router, address(token), poolKey, uint128(amountToBurn), uint128(amountOutMin));

        if (shockFeesCooldown != 0) {
            hook.operatorSetTimestampStartEnd(block.timestamp, block.timestamp + shockFeesCooldown, feeDirection);
        }

        uint256 _currentTokenBalance = token.balanceOf(address(this));
        uint256 _tip = _getTip(_currentTokenBalance);

        uint256 actualAmountToBurn = _currentTokenBalance - _tip;

        token.burn(actualAmountToBurn);

        token.transfer(msg.sender, _tip);

        emit ProtocolDistributedTip(msg.sender, _tip);

        emit ProtocolBoughtAndBurned(msg.sender, amountToBurn, actualAmountToBurn);
    }

    // ── External state-modifying (admin) ─────────────────────
    // @dev Set the tip amount for the executor
    function setTip(uint256 _amount) external onlyOwner {
        if (_amount > 500) {
            revert AdminValueTooBig();
        }
        executorTip = _amount;

        emit AdminTipChanged(_amount);
    }

    // @dev Set the cooldown of shock fees, if set to 0, they are disabled
    function setCooldown(uint256 _time) external onlyOwner {
        shockFeesCooldown = _time;

        emit AdminCooldownChanged(_time);
    }

    // @dev Set fee direction
    function setFeeDirection(uint8 _direction) external onlyOwner {
        if (_direction != ENABLE_FEE_BUY && _direction != ENABLE_FEE_SELL && _direction != (ENABLE_FEE_SELL | ENABLE_FEE_BUY)) {
            revert AdminWrongValue();
        }

        feeDirection = _direction;

        emit AdminFeeDirectionChanged(_direction);
    }

    function setFloor(uint256 _floor) external onlyOwner {
        floor = _floor;

        emit AdminFloorChanged(_floor);
    }

    // @dev Set the interval size
    function setIntervalSize(uint256 _size) external onlyOwner {
        if (_size > 3600 * 24) {
            revert AdminValueTooBig();
        }

        if (_size == 0) {
            revert AdminValueCannotBeZero();
        }

        intervalSize = _size;

        emit AdminIntervalSizeChanged(_size);
    }

    // @dev Set the interval max amount that can be spend in the intervalSize
    function setIntervalMaxAmount(uint256 _amount) external onlyOwner {
        if (_amount > 5 ether) {
            revert AdminValueTooBig();
        }

        if (_amount < transactionMinAmount) {
            revert AdminValueTooSmall();
        }

        intervalMaxAmount = _amount;

        emit AdminIntervalMaxAmountChanged(_amount);
    }

    // @dev Set the interval max amount that can be spend in the intervalSize
    function setTransactionMinAmount(uint256 _amount) external onlyOwner {
        if (_amount < 0.01 ether) {
            revert AdminValueTooSmall();
        }

        if (_amount > intervalMaxAmount) {
            revert AdminValueTooBig();
        }

        transactionMinAmount = _amount;

        emit AdminTransactionMinAmountChanged(_amount);
    }

    // @dev Function to update the pool key
    function updatePoolKey(PoolKey calldata _poolKey) external onlyOwner {
        poolKey = _poolKey;

        emit AdminUpdatePoolKey();
    }

    // @dev In the unlikely event that a vulnerability is discovered - allow the owner to withdraw all eth
    function emergencyWithdraw() external onlyOwner {
        uint256 balance = address(this).balance;

        (bool sent,) = payable(owner()).call{value: balance}("");
        if (!sent) {
            revert AdminEmergencyWithdrawFailed();
        }

        emit AdminWithdraw(balance);
    }

    // ── External view ─────────────────────────────────────────
    // @dev Debugging / UI - get the tip percentage in bps
    function getTip() external view returns (uint256) {
        return executorTip;
    }

    // @dev Debugging / UI - get currently available eth amount
    function getAvailableToBurn() external view returns (uint256) {
        uint256 blockInterval = block.timestamp / intervalSize;
        uint256 localIntervalMaxAmount = intervalMaxAmount;
        uint256 localCurrentIntervalAmount = currentIntervalAmount;

        if (blockInterval != currentInterval) {
            return address(this).balance < localIntervalMaxAmount ? address(this).balance : localIntervalMaxAmount;
        }

        uint256 remaining = localIntervalMaxAmount - localCurrentIntervalAmount;

        return address(this).balance < remaining ? address(this).balance : remaining;
    }

    // @dev Debugging / UI - get max eth amount that can be burned
    function getMaxAvailableToBurn() external view returns (uint256) {
        uint256 blockInterval = block.timestamp / intervalSize;

        if (blockInterval != currentInterval) {
            return intervalMaxAmount;
        }

        return intervalMaxAmount - currentIntervalAmount;
    }

    // @dev Debugging / UI - get the current interval
    function getCurrentInterval() external view returns (uint256) {
        return block.timestamp / intervalSize;
    }

    // @dev Debugging / UI - get the currently used amount in the current interval
    function getCurrentIntervalAmount() external view returns (uint256) {
        if(block.timestamp / intervalSize != currentInterval) {
            return 0;
        } else {
            return currentIntervalAmount;
        }
    }

    // @dev Debugging / UI - get the size of intervals (in seconds)
    function getIntervalSize() external view returns (uint256) {
        return intervalSize;
    }

    // @dev Debugging / UI - get maximum interval amount that can be spend in intervalSize
    function getIntervalMaxAmount() external view returns (uint256) {
        return intervalMaxAmount;
    }

    // @dev Debugging / UI - get minimum amount that can be spend in a transaction
    function getIntervalMinAmount() external view returns (uint256) {
        return transactionMinAmount;
    }

    // ── Internal view ─────────────────────────────────────────
    // @dev Calculate the tip of the configured token (active or not).
    function _getTip(uint256 amount) internal view returns (uint256) {
        return amount * executorTip / 10000;
    }

    receive() external payable {}
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "none",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "forge-std/=lib/forge-std/src/",
    "@uniswap/v4-core/=lib/uniswap-hooks/lib/v4-core/",
    "@uniswap/v4-periphery/=lib/uniswap-hooks/lib/v4-periphery/",
    "@uniswap/universal-router/=lib/universal-router/",
    "v4-core/=lib/uniswap-hooks/lib/v4-core/",
    "v4-periphery/=lib/uniswap-hooks/lib/v4-periphery/",
    "@openzeppelin/uniswap-hooks/=lib/uniswap-hooks/",
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
    "hookmate/=lib/hookmate/src/",
    "permit2/=lib/uniswap-hooks/lib/v4-periphery/lib/permit2/",
    "solmate/=lib/uniswap-hooks/lib/v4-core/lib/solmate/",
    "@ensdomains/=lib/uniswap-hooks/lib/v4-core/node_modules/@ensdomains/",
    "@uniswap/v2-core/=lib/universal-router/node_modules/@uniswap/v2-core/",
    "@uniswap/v3-core/=lib/universal-router/node_modules/@uniswap/v3-core/",
    "@uniswap/v3-periphery/=lib/universal-router/lib/v3-periphery/",
    "base64-sol/=lib/universal-router/node_modules/base64-sol/",
    "ds-test/=lib/universal-router/lib/solmate/lib/ds-test/src/",
    "erc4626-tests/=lib/openzeppelin-contracts-upgradeable/lib/erc4626-tests/",
    "forge-gas-snapshot/=lib/universal-router/lib/permit2/lib/forge-gas-snapshot/src/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts-upgradeable/lib/halmos-cheatcodes/src/",
    "hardhat/=lib/universal-router/node_modules/hardhat/",
    "openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "uniswap-hooks/=lib/uniswap-hooks/src/",
    "universal-router/=lib/universal-router/",
    "v3-periphery/=lib/universal-router/lib/v3-periphery/contracts/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.