Skip to content
Verified contractPartialMIT

ArkBuybackLogic

Address0xee29b48af3d36934cdac9caf395e1db280ae9bfa
Balance

0 ETH

 

Transactions

2

 

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
MIT
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
25,100,064
Creation transaction
0x9d3b628abc…7400fb1aa3

src/buyback/ArkBuybackLogic.sol

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

import {IArkBuybackExecutor} from "../interfaces/IArkBuybackExecutor.sol";
import {IArkFeeRouter} from "../interfaces/IArkFeeRouter.sol";
import {IArkBuybackLogic} from "../interfaces/IArkBuybackLogic.sol";
import {IERC20} from "../interfaces/IERC20.sol";
import {ArkRoutes} from "../libraries/ArkRoutes.sol";
import {ArkRecoverable} from "../security/ArkRecoverable.sol";

contract ArkBuybackLogic is IArkBuybackLogic, ArkRecoverable {
    uint256 private constant BPS = 10_000;

    error ZeroAddress();
    error ZeroAmount();
    error InvalidConfig();
    error UnsupportedAsset();
    error Unauthorized();
    error BuybackNotReady();
    error SlippageExceeded();
    error TransferFailed();
    error HookAlreadySet();
    error VaultAlreadySet();

    event BuybackBudgetNotified(address indexed asset, address indexed caller, uint256 amount);
    event BuybackExecuted(
        address indexed caller, uint256 wethIn, uint256 tokenOut, uint256 nextBuybackBlock
    );
    event HookSet(address indexed hook);
    event VaultSet(address indexed vault);
    event MaxSlippageBpsSet(uint256 maxSlippageBps);

    address public immutable wethAsset;
    address public immutable tokenAsset;
    address public immutable feeRouter;
    IArkBuybackExecutor public immutable executor;
    uint256 public immutable cooldownBlocks;
    uint256 public immutable maxBudgetSpendBps;
    uint256 public immutable maxSlippageBps;

    uint256 public nextBuybackBlock;
    uint256 public totalWethSpent;
    uint256 public totalTokenBought;
    address public hook;
    address public vault;

    constructor(
        address wethAsset_,
        address tokenAsset_,
        address feeRouter_,
        IArkBuybackExecutor executor_,
        address initialOwner,
        uint256 cooldownBlocks_,
        uint256 maxBudgetSpendBps_,
        uint256 maxSlippageBps_
    ) ArkRecoverable(initialOwner) {
        if (wethAsset_ == address(0)) revert ZeroAddress();
        if (tokenAsset_ == address(0)) revert ZeroAddress();
        if (feeRouter_ == address(0)) revert ZeroAddress();
        if (address(executor_) == address(0)) revert ZeroAddress();
        if (wethAsset_ == tokenAsset_) revert InvalidConfig();
        if (cooldownBlocks_ == 0) revert InvalidConfig();
        if (maxBudgetSpendBps_ == 0 || maxBudgetSpendBps_ > 10_000) revert InvalidConfig();
        if (maxSlippageBps_ >= BPS) revert InvalidConfig();

        wethAsset = wethAsset_;
        tokenAsset = tokenAsset_;
        feeRouter = feeRouter_;
        executor = executor_;
        cooldownBlocks = cooldownBlocks_;
        maxBudgetSpendBps = maxBudgetSpendBps_;
        maxSlippageBps = maxSlippageBps_;
        emit MaxSlippageBpsSet(maxSlippageBps_);
    }

    function _isRecoverableToken(address asset) internal view override returns (bool) {
        return asset != wethAsset && asset != tokenAsset;
    }

    modifier onlyFeeRouter() {
        if (msg.sender != feeRouter) revert Unauthorized();
        _;
    }

    modifier onlyHook() {
        if (msg.sender != hook) revert Unauthorized();
        _;
    }

    function setHook(address hook_) external onlyOwner {
        if (hook_ == address(0)) revert ZeroAddress();
        if (hook != address(0)) revert HookAlreadySet();

        hook = hook_;
        emit HookSet(hook_);
    }

    function setVault(address vault_) external onlyOwner {
        if (vault_ == address(0)) revert ZeroAddress();
        if (vault != address(0)) revert VaultAlreadySet();

        vault = vault_;
        emit VaultSet(vault_);
    }

    function notifyBuybackBudget(address asset, uint256 amount) external onlyFeeRouter {
        if (asset != wethAsset) revert UnsupportedAsset();
        if (amount == 0) revert ZeroAmount();

        emit BuybackBudgetNotified(asset, msg.sender, amount);
    }

    function notifyRedirectedLockedReward(address asset, uint256 amount) external {
        if (msg.sender != vault) revert Unauthorized();
        if (asset != wethAsset) revert UnsupportedAsset();
        if (amount == 0) revert ZeroAmount();

        emit BuybackBudgetNotified(asset, msg.sender, amount);
    }

    function executeBuybackFromHook()
        external
        nonReentrant
        onlyHook
        returns (uint256 wethIn, uint256 tokenOut)
    {
        return _executeBuyback(msg.sender);
    }

    function _executeBuyback(address caller) internal returns (uint256 wethIn, uint256 tokenOut) {
        if (block.number < nextBuybackBlock) revert BuybackNotReady();

        wethIn = _availableBuySize();
        if (wethIn == 0) revert BuybackNotReady();

        uint256 wethBefore = IERC20(wethAsset).balanceOf(address(this));
        uint256 tokenBefore = IERC20(tokenAsset).balanceOf(address(this));

        if (!IERC20(wethAsset).approve(address(executor), wethIn)) revert TransferFailed();

        executor.executeBuyback(wethAsset, tokenAsset, wethIn, maxSlippageBps, address(this));

        if (!IERC20(wethAsset).approve(address(executor), 0)) revert TransferFailed();

        uint256 wethSpent = wethBefore - IERC20(wethAsset).balanceOf(address(this));
        if (wethSpent != wethIn) revert TransferFailed();

        tokenOut = IERC20(tokenAsset).balanceOf(address(this)) - tokenBefore;
        if (tokenOut == 0) revert SlippageExceeded();

        nextBuybackBlock = block.number + cooldownBlocks;
        totalWethSpent += wethIn;
        totalTokenBought += tokenOut;

        if (!IERC20(tokenAsset).transfer(feeRouter, tokenOut)) revert TransferFailed();
        IArkFeeRouter(feeRouter).routeReceived(ArkRoutes.BUYBACK_OUTPUT_ROUTE, tokenAsset, tokenOut);

        emit BuybackExecuted(caller, wethIn, tokenOut, nextBuybackBlock);
    }

    function buybackAvailable() external view returns (uint256) {
        if (block.number < nextBuybackBlock) return 0;

        return _availableBuySize();
    }

    function maturedBalance() public view returns (uint256) {
        return IERC20(wethAsset).balanceOf(address(this));
    }

    function accountedWeth() external view returns (uint256) {
        return IERC20(wethAsset).balanceOf(address(this));
    }

    function _availableBuySize() internal view returns (uint256) {
        uint256 balance = IERC20(wethAsset).balanceOf(address(this));
        return balance * maxBudgetSpendBps / 10_000;
    }
}

lib/v4-core/lib/openzeppelin-contracts/contracts/access/Ownable.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)

pragma solidity ^0.8.20;

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

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * The initial owner is set to the address provided by the deployer. This can
 * later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    /**
     * @dev The caller account is not authorized to perform an operation.
     */
    error OwnableUnauthorizedAccount(address account);

    /**
     * @dev The owner is not a valid owner account. (eg. `address(0)`)
     */
    error OwnableInvalidOwner(address owner);

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the address provided by the deployer as the initial owner.
     */
    constructor(address initialOwner) {
        if (initialOwner == address(0)) {
            revert OwnableInvalidOwner(address(0));
        }
        _transferOwnership(initialOwner);
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        if (owner() != _msgSender()) {
            revert OwnableUnauthorizedAccount(_msgSender());
        }
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby disabling any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        if (newOwner == address(0)) {
            revert OwnableInvalidOwner(address(0));
        }
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

lib/v4-core/lib/openzeppelin-contracts/contracts/access/Ownable2Step.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable2Step.sol)

pragma solidity ^0.8.20;

import {Ownable} from "./Ownable.sol";

/**
 * @dev Contract module which provides access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * The initial owner is specified at deployment time in the constructor for `Ownable`. This
 * can later be changed with {transferOwnership} and {acceptOwnership}.
 *
 * This module is used through inheritance. It will make available all functions
 * from parent (Ownable).
 */
abstract contract Ownable2Step is Ownable {
    address private _pendingOwner;

    event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Returns the address of the pending owner.
     */
    function pendingOwner() public view virtual returns (address) {
        return _pendingOwner;
    }

    /**
     * @dev Starts the ownership transfer of the contract to a new account. Replaces the pending transfer if there is one.
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual override onlyOwner {
        _pendingOwner = newOwner;
        emit OwnershipTransferStarted(owner(), newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`) and deletes any pending owner.
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual override {
        delete _pendingOwner;
        super._transferOwnership(newOwner);
    }

    /**
     * @dev The new owner accepts the ownership transfer.
     */
    function acceptOwnership() public virtual {
        address sender = _msgSender();
        if (pendingOwner() != sender) {
            revert OwnableUnauthorizedAccount(sender);
        }
        _transferOwnership(sender);
    }
}

lib/v4-core/lib/openzeppelin-contracts/contracts/utils/Context.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)

pragma solidity ^0.8.20;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }

    function _contextSuffixLength() internal view virtual returns (uint256) {
        return 0;
    }
}

src/interfaces/IArkBuybackExecutor.sol

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

interface IArkBuybackExecutor {
    function executeBuyback(
        address wethAsset,
        address tokenAsset,
        uint256 wethIn,
        uint256 maxSlippageBps,
        address receiver
    ) external returns (uint256 tokenOut);

    function previewBuyback(
        address wethAsset,
        address tokenAsset,
        uint256 wethIn,
        uint256 maxSlippageBps
    ) external view returns (uint256 minTokenOut);
}

src/interfaces/IArkBuybackLogic.sol

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

interface IArkBuybackLogic {
    function notifyBuybackBudget(address asset, uint256 amount) external;
    function notifyRedirectedLockedReward(address asset, uint256 amount) external;
}

src/interfaces/IArkFeeRouter.sol

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

interface IArkFeeRouter {
    function routeReceived(bytes32 routeId, address asset, uint256 amount) external;
}

src/interfaces/IERC20.sol

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

interface IERC20 {
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);

    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function allowance(address owner, address spender) external view returns (uint256);
    function transfer(address to, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
}

src/libraries/ArkRoutes.sol

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

library ArkRoutes {
    bytes32 internal constant EXIT_FEE_ROUTE = keccak256("ARK_EXIT_FEE");
    bytes32 internal constant HOOK_FEE_ROUTE = keccak256("ARK_HOOK_FEE");
    bytes32 internal constant BUYBACK_OUTPUT_ROUTE = keccak256("ARK_BUYBACK_OUTPUT");
}

src/security/ArkOwnable.sol

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

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {Ownable2Step} from "@openzeppelin/contracts/access/Ownable2Step.sol";

abstract contract ArkOwnable is Ownable2Step {
    error OwnershipRenounceDisabled();

    constructor(address initialOwner) Ownable(initialOwner) {}

    function renounceOwnership() public view override onlyOwner {
        revert OwnershipRenounceDisabled();
    }
}

src/security/ArkRecoverable.sol

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

import {IERC20} from "../interfaces/IERC20.sol";
import {ArkOwnable} from "./ArkOwnable.sol";
import {ArkReentrancyGuard} from "./ArkReentrancyGuard.sol";

abstract contract ArkRecoverable is ArkOwnable, ArkReentrancyGuard {
    error NoRecoverableBalance();
    error ProtectedRecoveryAsset();
    error RecoveryTransferFailed();

    event NativeRecovered(address indexed recipient, uint256 amount);
    event TokenRecovered(address indexed asset, address indexed recipient, uint256 amount);

    constructor(address initialOwner) ArkOwnable(initialOwner) {}

    function recoverNative() external nonReentrant onlyOwner returns (uint256 amount) {
        amount = address(this).balance;
        if (amount == 0) revert NoRecoverableBalance();

        address recipient = owner();
        (bool success,) = payable(recipient).call{value: amount}("");
        if (!success) revert RecoveryTransferFailed();

        emit NativeRecovered(recipient, amount);
    }

    function recoverToken(address asset) external nonReentrant onlyOwner returns (uint256 amount) {
        if (asset == address(0)) revert ProtectedRecoveryAsset();
        if (!_isRecoverableToken(asset)) revert ProtectedRecoveryAsset();

        amount = IERC20(asset).balanceOf(address(this));
        if (amount == 0) revert NoRecoverableBalance();

        address recipient = owner();
        if (!IERC20(asset).transfer(recipient, amount)) revert RecoveryTransferFailed();

        emit TokenRecovered(asset, recipient, amount);
    }

    function _isRecoverableToken(address) internal view virtual returns (bool) {
        return true;
    }
}

src/security/ArkReentrancyGuard.sol

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

abstract contract ArkReentrancyGuard {
    error ReentrantCall();

    uint256 private constant ENTERED = 1;
    bytes32 private constant REENTRANCY_GUARD_SLOT =
        0x3a6f8673abc4ff4f17461e886ecbdb6f0f20d51c49db9bac70360d14b8040ec4;

    modifier nonReentrant() {
        _nonReentrantBefore();
        _;
        _nonReentrantAfter();
    }

    function _nonReentrantBefore() private {
        bytes32 slot = REENTRANCY_GUARD_SLOT;
        uint256 status;
        assembly ("memory-safe") {
            status := tload(slot)
        }
        if (status == ENTERED) revert ReentrantCall();
        assembly ("memory-safe") {
            tstore(slot, ENTERED)
        }
    }

    function _nonReentrantAfter() private {
        bytes32 slot = REENTRANCY_GUARD_SLOT;
        assembly ("memory-safe") {
            tstore(slot, 0)
        }
    }
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": false,
    "bytecodeHash": "none",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "@ensdomains/=lib/v4-core/node_modules/@ensdomains/",
    "@openzeppelin/=lib/v4-core/lib/openzeppelin-contracts/",
    "@uniswap/v4-core/=lib/v4-periphery/lib/v4-core/",
    "ds-test/=lib/v4-core/lib/forge-std/lib/ds-test/src/",
    "erc4626-tests/=lib/v4-core/lib/openzeppelin-contracts/lib/erc4626-tests/",
    "forge-gas-snapshot/=lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/src/",
    "forge-std/=lib/v4-core/lib/forge-std/src/",
    "hardhat/=lib/v4-core/node_modules/hardhat/",
    "openzeppelin-contracts/=lib/v4-core/lib/openzeppelin-contracts/",
    "permit2/=lib/v4-periphery/lib/permit2/",
    "solmate/=lib/v4-core/lib/solmate/",
    "v4-core/=lib/v4-core/src/",
    "v4-periphery/=lib/v4-periphery/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.