ETH Price: $2,949.09 (+1.26%)

Contract

0xD06b4476423873c3337c82c3d30D5818Cc994C0D

Overview

ETH Balance

0 ETH

ETH Value

$0.00

More Info

Private Name Tags

Multichain Info

No addresses found
Transaction Hash
Block
From
To

There are no matching entries

1 Internal Transaction found.

Latest 1 internal transaction

Parent Transaction Hash Block From To
981919872023-06-05 23:33:24963 days ago1686008004  Contract Creation0 ETH

Cross-Chain Transactions
Loading...
Loading

Contract Source Code Verified (Exact Match)

Contract Name:
Vault

Compiler Version
v0.8.17+commit.8df45f5f

Optimization Enabled:
Yes with 200 runs

Other Settings:
default evmVersion
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@vault/IVault.sol";
import "@strategy/IStrategy.sol";
import "@/AddressRegistry.sol";
import "@structs/structs.sol";

/// The Vault contract provides a secure and flexible platform for depositing and withdrawing coins, as well as approving and depositing ETH to strategies.
contract Vault is OwnableUpgradeable, IVault, ERC20Upgradeable {
    using SafeERC20 for IERC20;
    struct DepositWithrawalParams {
        /// Deposit coin position in cpu array
        uint256 coinPositionInCPU;
        /// Deposit amount
        uint256 _amount;
        /// Vault's Coin price usd array
        CoinPriceUSD[] cpu;
        /// Expire time stamp
        uint256 expireTimestamp;
    }

    AddressRegistry public addressRegistry;
    /// USD price cap for coin
    /// only process certain amount of USD per coin
    mapping(address => uint256) public coinCap;
    /// block cap USD for block number
    mapping(uint256 => uint256) public blockCapCounter;
    /// only process certain amount of tx in USD per block
    uint256 public blockCapUSD;
    /// claimable debt amount for routers
    mapping(address => uint256) public debt;
    /// pool ratio denominator for pool ratio calculation
    uint256 constant poolRatioDenominator = 1e18;

    int256 constant weightDenominator = 1e18;

    event SetAddressRegistry(AddressRegistry indexed addressRegistry);
    event SetCoinCap(address indexed coin, uint256 indexed cap);
    event SetBlockCap(uint256 indexed cap);
    event DepositEthToStrategy(
        address indexed strategy,
        uint256 indexed amount
    );
    event Rebalance(
        address indexed destination,
        address indexed coin,
        uint256 indexed amount
    );

    constructor() {
        _disableInitializers();
    }

    receive() external payable {}

    modifier onlyRouter() {
        require(
            msg.sender == addressRegistry.router(),
            "only router has permitted"
        );
        _;
    }

    function init829(
        AddressRegistry _addressRegistry
    ) external payable initializer {
        require(
            address(_addressRegistry) != address(0),
            "_addressRegistry address can't be zero"
        );
        require(msg.value >= 1e15);

        __Ownable_init();
        __ERC20_init("ALP", "ALP");
        addressRegistry = _addressRegistry;
        _mint(msg.sender, msg.value);
        emit SetAddressRegistry(_addressRegistry);
    }

    /// @notice Set addressRegistry
    /// @param _addressRegistry Address registry contract
    function setAddressRegistry(
        AddressRegistry _addressRegistry
    ) external onlyOwner {
        require(
            address(_addressRegistry) != address(0),
            "_addressRegistry address can't be zero"
        );

        addressRegistry = _addressRegistry;

        emit SetAddressRegistry(_addressRegistry);
    }

    /// @notice Set coin cap usd
    /// @param coin Address of coin to set cap
    /// @param cap Amount of cap to set
    function setCoinCapUSD(address coin, uint256 cap) external onlyOwner {
        require(coin != address(0), "invalid coin address");
        coinCap[coin] = cap;
        emit SetCoinCap(coin, cap);
    }

    /// @notice Set block cap for vault
    /// @param cap Amount of cap to set
    function setBlockCap(uint256 cap) external onlyOwner {
        blockCapUSD = cap;
        emit SetBlockCap(cap);
    }

    /// @notice Deposit. Note that the deposit amount is transferred to the vault from the router after checking the amount of ALP minted. If the amount of ALP minted is not correct, the call will revert and the router will refund.
    /// @param params Deposit params
    function deposit(DepositWithrawalParams memory params) external onlyRouter {
        FeeParams memory feeParams = FeeParams({
            cpu: params.cpu,
            vault: this,
            expireTimestamp: params.expireTimestamp,
            position: params.coinPositionInCPU,
            amount: params._amount
        });
        address coin = params.cpu[params.coinPositionInCPU].coin;
        uint256 __decimals = IERC20Metadata(coin).decimals();
        uint256 coinCapValue = ((getAmountAcrossStrategies(coin) +
            params._amount) * params.cpu[params.coinPositionInCPU].price) /
            10 ** __decimals;
        require(coinCapValue < coinCap[coin], "Coin cap reached");

        /// calculate deposit value
        /// formula: depositValue = coinPriceUSD * coinDepositAmount / 10**coinDecimal
        uint256 depositValue = (params.cpu[params.coinPositionInCPU].price *
            params._amount) / 10 ** __decimals;
        require(
            depositValue + blockCapCounter[block.number] < blockCapUSD,
            "Block cap reached"
        );

        /// update blockCapCounter with depositValue
        blockCapCounter[block.number] += depositValue;

        /// Get deposit fee and tvl before deposit
        (int256 fee, , uint256 tvlUSD1e18X) = addressRegistry
            .feeOracle()
            .getDepositFee(feeParams);

        /// vault token mint
        /// poolRatio = depositValue * poolRatioDenominator / tvl
        uint256 poolRatio = (depositValue * poolRatioDenominator) / tvlUSD1e18X;
        /// mintAmountBeforeFee = poolRatio * totalSupply / poolRatioDenominator
        uint256 mintAmountBeforeFee = (poolRatio * totalSupply()) /
            poolRatioDenominator;
        /// mintAmount = mintAmountBeforeFee * (feeDenominator - fee) / feeDenominator
        uint256 mintAmount = (mintAmountBeforeFee *
            uint256(weightDenominator - fee)) / uint256(weightDenominator);
        _mint(msg.sender, mintAmount);
    }

    /// @notice Withdraw. Note that the amount of ALP burned is checked before the router calls `claimDebt` subsequently to claim the token. If the amount of ALP burned is not correct, the call will revert and the router will refund.
    /// @param params Withdraw params
    function withdraw(
        DepositWithrawalParams memory params
    ) external onlyRouter {
        FeeParams memory feeParams = FeeParams({
            cpu: params.cpu,
            vault: this,
            expireTimestamp: params.expireTimestamp,
            position: params.coinPositionInCPU,
            amount: params._amount
        });
        address coin = params.cpu[params.coinPositionInCPU].coin;
        uint256 __decimals = IERC20Metadata(coin).decimals();

        /// calculate withdrawal value
        /// formula: withdrawalValue = coinPriceUSD * withdrawalCoinAmount / 10**coinDecimal
        uint256 withdrawalValue = (params.cpu[params.coinPositionInCPU].price *
            params._amount) / 10 ** __decimals;
        require(
            withdrawalValue + blockCapCounter[block.number] < blockCapUSD,
            "Block cap reached"
        );
        blockCapCounter[block.number] += withdrawalValue;
        // no coin cap check for withdrawal

        /// Get withdrawal fee and tvl before withdraw
        (int256 fee, , uint256 tvlUSD1e18X) = addressRegistry
            .feeOracle()
            .getWithdrawalFee(feeParams);

        /// burn vault token
        /// poolRatio = withdrawalValue * poolRatioDenominator / tvl
        uint256 poolRatio = (withdrawalValue * poolRatioDenominator) /
            tvlUSD1e18X;
        /// burnAmountBeforeFee = poolRatio * totalSupply / poolRatioDenominator
        uint256 burnAmountBeforeFee = (poolRatio * totalSupply()) /
            poolRatioDenominator;
        /// burnAmount = burnAmountBeforeFee * (feeDenominator - fee) / feeDenominator
        uint256 burnAmount = (burnAmountBeforeFee *
            uint256(weightDenominator - fee)) / uint256(weightDenominator);

        _burn(msg.sender, burnAmount);

        /// increase claimable debt amount for withdrawing amount of coin later
        debt[coin] += params._amount;
    }

    /// @notice Claim `amount` debt from vault
    /// @param coin Address of coin to claim
    /// @param amount Amount of debt to claim
    function claimDebt(address coin, uint256 amount) external onlyRouter {
        require(debt[coin] >= amount, "insufficient debt amount for coin");
        debt[coin] -= amount;
        IERC20(coin).safeTransfer(msg.sender, amount);
    }

    /// @notice Approve `amount` of coin for strategy to use
    /// @param strategy Address of Strategy
    /// @param coin Address of coin
    /// @param amount Amount of coin to approve
    function approveStrategy(
        IStrategy strategy,
        address coin,
        uint256 amount
    ) external onlyOwner {
        require(address(strategy) != address(0), "invalid strategy address");
        require(
            addressRegistry.isWhitelistedStrategy(strategy),
            "strategy is not whitelisted"
        );

        /// verify coin is part of the strategy
        IStrategy[] memory strategies = addressRegistry.getCoinToStrategy(coin);
        uint256 i;
        for (; i < strategies.length; i++) {
            if (address(strategies[i]) == address(strategy)) break;
        }
        require(
            i != strategies.length,
            "provided coin is not the part of strategy"
        );

        /// approve coin for strategy
        IERC20(coin).safeApprove(address(strategy), amount);
    }

    /// @notice Deposit `amount` of ETH to strategy because ETH can't be approved. Note that this feature will not likely to be used. Trove mostly will be WETH based.
    /// @param strategy Address of Strategy
    /// @param amount Amount of ETH to deposit
    function depositETHToStrategy(
        IStrategy strategy,
        uint256 amount
    ) external onlyOwner {
        require(
            addressRegistry.isWhitelistedStrategy(strategy),
            "strategy is not whitelisted"
        );
        (bool depositSuccess, ) = address(strategy).call{value: amount}("");
        require(depositSuccess, "Deposit failed");
        emit DepositEthToStrategy(address(strategy), amount);
    }

    /// @notice Rebalance `amount` of coin from vault
    ///         The rebalance contract will be whitelisted by governance and effective after timelock on a case-by-case basis
    ///         typically, there will be 2 types of rebalance contract
    ///             1. on-chain rebalance. use of dexes like uniswap and camelot
    ///             2. otc rebalance. use of cexes or our partnership relationship with projects to acquire tokens
    /// @param destination Address of rebalance contract
    /// @param coin Address of coin
    /// @param amount Amount of coin to withdraw
    function rebalance(
        address destination,
        address coin,
        uint256 amount
    ) external onlyOwner {
        require(destination != address(0), "invalid destination");
        require(
            addressRegistry.isWhitelistedRebalancer(destination),
            "destination is not whitelisted"
        );
        if (coin == address(0)) {
            (bool success, ) = payable(destination).call{value: amount}("");
            require(success, "deposit to destination failed");
        } else {
            IERC20(coin).safeTransfer(destination, amount);
        }
        emit Rebalance(destination, coin, amount);
    }

    /// @notice Get aggregated amount of coin for vault and strategies
    /// @param coin Address of coin
    /// @return value Aggregated amount of coin
    function getAmountAcrossStrategies(
        address coin
    ) public view returns (uint256 value) {
        if (coin == address(0)) {
            value += address(this).balance;
        } else {
            value += IERC20(coin).balanceOf(address(this));
        }
        IStrategy[] memory strategies = addressRegistry.getCoinToStrategy(coin);
        for (uint256 i; i < strategies.length; ) {
            value += strategies[i].getComponentAmount(coin);
            unchecked {
                i++;
            }
        }
    }
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `from` to `to` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) external returns (bool);
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)

pragma solidity ^0.8.0;

import "../IERC20.sol";

/**
 * @dev Interface for the optional metadata functions from the ERC20 standard.
 *
 * _Available since v4.1._
 */
interface IERC20Metadata is IERC20 {
    /**
     * @dev Returns the name of the token.
     */
    function name() external view returns (string memory);

    /**
     * @dev Returns the symbol of the token.
     */
    function symbol() external view returns (string memory);

    /**
     * @dev Returns the decimals places of the token.
     */
    function decimals() external view returns (uint8);
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/draft-IERC20Permit.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
 * https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
 *
 * Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
 * presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't
 * need to send a transaction, and thus is not required to hold Ether at all.
 */
interface IERC20Permit {
    /**
     * @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens,
     * given ``owner``'s signed approval.
     *
     * IMPORTANT: The same issues {IERC20-approve} has related to transaction
     * ordering also apply here.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `deadline` must be a timestamp in the future.
     * - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner`
     * over the EIP712-formatted function arguments.
     * - the signature must use ``owner``'s current nonce (see {nonces}).
     *
     * For more information on the signature format, see the
     * https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP
     * section].
     */
    function permit(
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external;

    /**
     * @dev Returns the current nonce for `owner`. This value must be
     * included whenever a signature is generated for {permit}.
     *
     * Every successful call to {permit} increases ``owner``'s nonce by one. This
     * prevents a signature from being used multiple times.
     */
    function nonces(address owner) external view returns (uint256);

    /**
     * @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}.
     */
    // solhint-disable-next-line func-name-mixedcase
    function DOMAIN_SEPARATOR() external view returns (bytes32);
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (token/ERC20/utils/SafeERC20.sol)

pragma solidity ^0.8.0;

import "../IERC20.sol";
import "../extensions/draft-IERC20Permit.sol";
import "../../../utils/Address.sol";

/**
 * @title SafeERC20
 * @dev Wrappers around ERC20 operations that throw on failure (when the token
 * contract returns false). Tokens that return no value (and instead revert or
 * throw on failure) are also supported, non-reverting calls are assumed to be
 * successful.
 * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
 * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
 */
library SafeERC20 {
    using Address for address;

    function safeTransfer(
        IERC20 token,
        address to,
        uint256 value
    ) internal {
        _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
    }

    function safeTransferFrom(
        IERC20 token,
        address from,
        address to,
        uint256 value
    ) internal {
        _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
    }

    /**
     * @dev Deprecated. This function has issues similar to the ones found in
     * {IERC20-approve}, and its usage is discouraged.
     *
     * Whenever possible, use {safeIncreaseAllowance} and
     * {safeDecreaseAllowance} instead.
     */
    function safeApprove(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        // safeApprove should only be called when setting an initial allowance,
        // or when resetting it to zero. To increase and decrease it, use
        // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
        require(
            (value == 0) || (token.allowance(address(this), spender) == 0),
            "SafeERC20: approve from non-zero to non-zero allowance"
        );
        _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
    }

    function safeIncreaseAllowance(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        uint256 newAllowance = token.allowance(address(this), spender) + value;
        _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
    }

    function safeDecreaseAllowance(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        unchecked {
            uint256 oldAllowance = token.allowance(address(this), spender);
            require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
            uint256 newAllowance = oldAllowance - value;
            _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
        }
    }

    function safePermit(
        IERC20Permit token,
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) internal {
        uint256 nonceBefore = token.nonces(owner);
        token.permit(owner, spender, value, deadline, v, r, s);
        uint256 nonceAfter = token.nonces(owner);
        require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
    }

    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     */
    function _callOptionalReturn(IERC20 token, bytes memory data) private {
        // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
        // we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that
        // the target address contains contract code and also asserts for success in the low-level call.

        bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
        if (returndata.length > 0) {
            // Return data is optional
            require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
        }
    }
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/Address.sol)

pragma solidity ^0.8.1;

/**
 * @dev Collection of functions related to the address type
 */
library Address {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * [IMPORTANT]
     * ====
     * It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     *
     * Among others, `isContract` will return false for the following
     * types of addresses:
     *
     *  - an externally-owned account
     *  - a contract in construction
     *  - an address where a contract will be created
     *  - an address where a contract lived, but was destroyed
     * ====
     *
     * [IMPORTANT]
     * ====
     * You shouldn't rely on `isContract` to protect against flash loan attacks!
     *
     * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
     * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
     * constructor.
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies on extcodesize/address.code.length, which returns 0
        // for contracts in construction, since the code is only stored at the end
        // of the constructor execution.

        return account.code.length > 0;
    }

    /**
     * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
     * `recipient`, forwarding all available gas and reverting on errors.
     *
     * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
     * of certain opcodes, possibly making contracts go over the 2300 gas limit
     * imposed by `transfer`, making them unable to receive funds via
     * `transfer`. {sendValue} removes this limitation.
     *
     * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
     *
     * IMPORTANT: because control is transferred to `recipient`, care must be
     * taken to not create reentrancy vulnerabilities. Consider using
     * {ReentrancyGuard} or the
     * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
     */
    function sendValue(address payable recipient, uint256 amount) internal {
        require(address(this).balance >= amount, "Address: insufficient balance");

        (bool success, ) = recipient.call{value: amount}("");
        require(success, "Address: unable to send value, recipient may have reverted");
    }

    /**
     * @dev Performs a Solidity function call using a low level `call`. A
     * plain `call` is an unsafe replacement for a function call: use this
     * function instead.
     *
     * If `target` reverts with a revert reason, it is bubbled up by this
     * function (like regular Solidity function calls).
     *
     * Returns the raw returned data. To convert to the expected return value,
     * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
     *
     * Requirements:
     *
     * - `target` must be a contract.
     * - calling `target` with `data` must not revert.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, "Address: low-level call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
     * `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but also transferring `value` wei to `target`.
     *
     * Requirements:
     *
     * - the calling contract must have an ETH balance of at least `value`.
     * - the called Solidity function must be `payable`.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value
    ) internal returns (bytes memory) {
        return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
    }

    /**
     * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
     * with `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value,
        string memory errorMessage
    ) internal returns (bytes memory) {
        require(address(this).balance >= value, "Address: insufficient balance for call");
        (bool success, bytes memory returndata) = target.call{value: value}(data);
        return verifyCallResultFromTarget(target, success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
        return functionStaticCall(target, data, "Address: low-level static call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal view returns (bytes memory) {
        (bool success, bytes memory returndata) = target.staticcall(data);
        return verifyCallResultFromTarget(target, success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionDelegateCall(target, data, "Address: low-level delegate call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        (bool success, bytes memory returndata) = target.delegatecall(data);
        return verifyCallResultFromTarget(target, success, returndata, errorMessage);
    }

    /**
     * @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
     * the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
     *
     * _Available since v4.8._
     */
    function verifyCallResultFromTarget(
        address target,
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal view returns (bytes memory) {
        if (success) {
            if (returndata.length == 0) {
                // only check isContract if the call was successful and the return data is empty
                // otherwise we already know that it was a contract
                require(isContract(target), "Address: call to non-contract");
            }
            return returndata;
        } else {
            _revert(returndata, errorMessage);
        }
    }

    /**
     * @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
     * revert reason or using the provided one.
     *
     * _Available since v4.3._
     */
    function verifyCallResult(
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal pure returns (bytes memory) {
        if (success) {
            return returndata;
        } else {
            _revert(returndata, errorMessage);
        }
    }

    function _revert(bytes memory returndata, string memory errorMessage) private pure {
        // Look for revert reason and bubble it up if present
        if (returndata.length > 0) {
            // The easiest way to bubble the revert reason is using memory via assembly
            /// @solidity memory-safe-assembly
            assembly {
                let returndata_size := mload(returndata)
                revert(add(32, returndata), returndata_size)
            }
        } else {
            revert(errorMessage);
        }
    }
}

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

pragma solidity ^0.8.0;

import "../utils/ContextUpgradeable.sol";
import "../proxy/utils/Initializable.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.
 *
 * By default, the owner account will be the one that deploys the contract. 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 OwnableUpgradeable is Initializable, ContextUpgradeable {
    address private _owner;

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

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    function __Ownable_init() internal onlyInitializing {
        __Ownable_init_unchained();
    }

    function __Ownable_init_unchained() internal onlyInitializing {
        _transferOwnership(_msgSender());
    }

    /**
     * @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 {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing 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 {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _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);
    }

    /**
     * @dev This empty reserved space is put in place to allow future versions to add new
     * variables without shifting down storage in the inheritance chain.
     * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
     */
    uint256[49] private __gap;
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.1) (proxy/utils/Initializable.sol)

pragma solidity ^0.8.2;

import "../../utils/AddressUpgradeable.sol";

/**
 * @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
 * behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an
 * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
 * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
 *
 * The initialization functions use a version number. Once a version number is used, it is consumed and cannot be
 * reused. This mechanism prevents re-execution of each "step" but allows the creation of new initialization steps in
 * case an upgrade adds a module that needs to be initialized.
 *
 * For example:
 *
 * [.hljs-theme-light.nopadding]
 * ```
 * contract MyToken is ERC20Upgradeable {
 *     function initialize() initializer public {
 *         __ERC20_init("MyToken", "MTK");
 *     }
 * }
 * contract MyTokenV2 is MyToken, ERC20PermitUpgradeable {
 *     function initializeV2() reinitializer(2) public {
 *         __ERC20Permit_init("MyToken");
 *     }
 * }
 * ```
 *
 * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
 * possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
 *
 * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
 * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
 *
 * [CAUTION]
 * ====
 * Avoid leaving a contract uninitialized.
 *
 * An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation
 * contract, which may impact the proxy. To prevent the implementation contract from being used, you should invoke
 * the {_disableInitializers} function in the constructor to automatically lock it when it is deployed:
 *
 * [.hljs-theme-light.nopadding]
 * ```
 * /// @custom:oz-upgrades-unsafe-allow constructor
 * constructor() {
 *     _disableInitializers();
 * }
 * ```
 * ====
 */
abstract contract Initializable {
    /**
     * @dev Indicates that the contract has been initialized.
     * @custom:oz-retyped-from bool
     */
    uint8 private _initialized;

    /**
     * @dev Indicates that the contract is in the process of being initialized.
     */
    bool private _initializing;

    /**
     * @dev Triggered when the contract has been initialized or reinitialized.
     */
    event Initialized(uint8 version);

    /**
     * @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope,
     * `onlyInitializing` functions can be used to initialize parent contracts.
     *
     * Similar to `reinitializer(1)`, except that functions marked with `initializer` can be nested in the context of a
     * constructor.
     *
     * Emits an {Initialized} event.
     */
    modifier initializer() {
        bool isTopLevelCall = !_initializing;
        require(
            (isTopLevelCall && _initialized < 1) || (!AddressUpgradeable.isContract(address(this)) && _initialized == 1),
            "Initializable: contract is already initialized"
        );
        _initialized = 1;
        if (isTopLevelCall) {
            _initializing = true;
        }
        _;
        if (isTopLevelCall) {
            _initializing = false;
            emit Initialized(1);
        }
    }

    /**
     * @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the
     * contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be
     * used to initialize parent contracts.
     *
     * A reinitializer may be used after the original initialization step. This is essential to configure modules that
     * are added through upgrades and that require initialization.
     *
     * When `version` is 1, this modifier is similar to `initializer`, except that functions marked with `reinitializer`
     * cannot be nested. If one is invoked in the context of another, execution will revert.
     *
     * Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in
     * a contract, executing them in the right order is up to the developer or operator.
     *
     * WARNING: setting the version to 255 will prevent any future reinitialization.
     *
     * Emits an {Initialized} event.
     */
    modifier reinitializer(uint8 version) {
        require(!_initializing && _initialized < version, "Initializable: contract is already initialized");
        _initialized = version;
        _initializing = true;
        _;
        _initializing = false;
        emit Initialized(version);
    }

    /**
     * @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
     * {initializer} and {reinitializer} modifiers, directly or indirectly.
     */
    modifier onlyInitializing() {
        require(_initializing, "Initializable: contract is not initializing");
        _;
    }

    /**
     * @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call.
     * Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized
     * to any version. It is recommended to use this to lock implementation contracts that are designed to be called
     * through proxies.
     *
     * Emits an {Initialized} event the first time it is successfully executed.
     */
    function _disableInitializers() internal virtual {
        require(!_initializing, "Initializable: contract is initializing");
        if (_initialized < type(uint8).max) {
            _initialized = type(uint8).max;
            emit Initialized(type(uint8).max);
        }
    }

    /**
     * @dev Returns the highest version that has been initialized. See {reinitializer}.
     */
    function _getInitializedVersion() internal view returns (uint8) {
        return _initialized;
    }

    /**
     * @dev Returns `true` if the contract is currently initializing. See {onlyInitializing}.
     */
    function _isInitializing() internal view returns (bool) {
        return _initializing;
    }
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (token/ERC20/ERC20.sol)

pragma solidity ^0.8.0;

import "./IERC20Upgradeable.sol";
import "./extensions/IERC20MetadataUpgradeable.sol";
import "../../utils/ContextUpgradeable.sol";
import "../../proxy/utils/Initializable.sol";

/**
 * @dev Implementation of the {IERC20} interface.
 *
 * This implementation is agnostic to the way tokens are created. This means
 * that a supply mechanism has to be added in a derived contract using {_mint}.
 * For a generic mechanism see {ERC20PresetMinterPauser}.
 *
 * TIP: For a detailed writeup see our guide
 * https://forum.openzeppelin.com/t/how-to-implement-erc20-supply-mechanisms/226[How
 * to implement supply mechanisms].
 *
 * We have followed general OpenZeppelin Contracts guidelines: functions revert
 * instead returning `false` on failure. This behavior is nonetheless
 * conventional and does not conflict with the expectations of ERC20
 * applications.
 *
 * Additionally, an {Approval} event is emitted on calls to {transferFrom}.
 * This allows applications to reconstruct the allowance for all accounts just
 * by listening to said events. Other implementations of the EIP may not emit
 * these events, as it isn't required by the specification.
 *
 * Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
 * functions have been added to mitigate the well-known issues around setting
 * allowances. See {IERC20-approve}.
 */
contract ERC20Upgradeable is Initializable, ContextUpgradeable, IERC20Upgradeable, IERC20MetadataUpgradeable {
    mapping(address => uint256) private _balances;

    mapping(address => mapping(address => uint256)) private _allowances;

    uint256 private _totalSupply;

    string private _name;
    string private _symbol;

    /**
     * @dev Sets the values for {name} and {symbol}.
     *
     * The default value of {decimals} is 18. To select a different value for
     * {decimals} you should overload it.
     *
     * All two of these values are immutable: they can only be set once during
     * construction.
     */
    function __ERC20_init(string memory name_, string memory symbol_) internal onlyInitializing {
        __ERC20_init_unchained(name_, symbol_);
    }

    function __ERC20_init_unchained(string memory name_, string memory symbol_) internal onlyInitializing {
        _name = name_;
        _symbol = symbol_;
    }

    /**
     * @dev Returns the name of the token.
     */
    function name() public view virtual override returns (string memory) {
        return _name;
    }

    /**
     * @dev Returns the symbol of the token, usually a shorter version of the
     * name.
     */
    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }

    /**
     * @dev Returns the number of decimals used to get its user representation.
     * For example, if `decimals` equals `2`, a balance of `505` tokens should
     * be displayed to a user as `5.05` (`505 / 10 ** 2`).
     *
     * Tokens usually opt for a value of 18, imitating the relationship between
     * Ether and Wei. This is the value {ERC20} uses, unless this function is
     * overridden;
     *
     * NOTE: This information is only used for _display_ purposes: it in
     * no way affects any of the arithmetic of the contract, including
     * {IERC20-balanceOf} and {IERC20-transfer}.
     */
    function decimals() public view virtual override returns (uint8) {
        return 18;
    }

    /**
     * @dev See {IERC20-totalSupply}.
     */
    function totalSupply() public view virtual override returns (uint256) {
        return _totalSupply;
    }

    /**
     * @dev See {IERC20-balanceOf}.
     */
    function balanceOf(address account) public view virtual override returns (uint256) {
        return _balances[account];
    }

    /**
     * @dev See {IERC20-transfer}.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - the caller must have a balance of at least `amount`.
     */
    function transfer(address to, uint256 amount) public virtual override returns (bool) {
        address owner = _msgSender();
        _transfer(owner, to, amount);
        return true;
    }

    /**
     * @dev See {IERC20-allowance}.
     */
    function allowance(address owner, address spender) public view virtual override returns (uint256) {
        return _allowances[owner][spender];
    }

    /**
     * @dev See {IERC20-approve}.
     *
     * NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on
     * `transferFrom`. This is semantically equivalent to an infinite approval.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function approve(address spender, uint256 amount) public virtual override returns (bool) {
        address owner = _msgSender();
        _approve(owner, spender, amount);
        return true;
    }

    /**
     * @dev See {IERC20-transferFrom}.
     *
     * Emits an {Approval} event indicating the updated allowance. This is not
     * required by the EIP. See the note at the beginning of {ERC20}.
     *
     * NOTE: Does not update the allowance if the current allowance
     * is the maximum `uint256`.
     *
     * Requirements:
     *
     * - `from` and `to` cannot be the zero address.
     * - `from` must have a balance of at least `amount`.
     * - the caller must have allowance for ``from``'s tokens of at least
     * `amount`.
     */
    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) public virtual override returns (bool) {
        address spender = _msgSender();
        _spendAllowance(from, spender, amount);
        _transfer(from, to, amount);
        return true;
    }

    /**
     * @dev Atomically increases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to {approve} that can be used as a mitigation for
     * problems described in {IERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        address owner = _msgSender();
        _approve(owner, spender, allowance(owner, spender) + addedValue);
        return true;
    }

    /**
     * @dev Atomically decreases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to {approve} that can be used as a mitigation for
     * problems described in {IERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `spender` must have allowance for the caller of at least
     * `subtractedValue`.
     */
    function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
        address owner = _msgSender();
        uint256 currentAllowance = allowance(owner, spender);
        require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
        unchecked {
            _approve(owner, spender, currentAllowance - subtractedValue);
        }

        return true;
    }

    /**
     * @dev Moves `amount` of tokens from `from` to `to`.
     *
     * This internal function is equivalent to {transfer}, and can be used to
     * e.g. implement automatic token fees, slashing mechanisms, etc.
     *
     * Emits a {Transfer} event.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `from` must have a balance of at least `amount`.
     */
    function _transfer(
        address from,
        address to,
        uint256 amount
    ) internal virtual {
        require(from != address(0), "ERC20: transfer from the zero address");
        require(to != address(0), "ERC20: transfer to the zero address");

        _beforeTokenTransfer(from, to, amount);

        uint256 fromBalance = _balances[from];
        require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
        unchecked {
            _balances[from] = fromBalance - amount;
            // Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
            // decrementing then incrementing.
            _balances[to] += amount;
        }

        emit Transfer(from, to, amount);

        _afterTokenTransfer(from, to, amount);
    }

    /** @dev Creates `amount` tokens and assigns them to `account`, increasing
     * the total supply.
     *
     * Emits a {Transfer} event with `from` set to the zero address.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     */
    function _mint(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: mint to the zero address");

        _beforeTokenTransfer(address(0), account, amount);

        _totalSupply += amount;
        unchecked {
            // Overflow not possible: balance + amount is at most totalSupply + amount, which is checked above.
            _balances[account] += amount;
        }
        emit Transfer(address(0), account, amount);

        _afterTokenTransfer(address(0), account, amount);
    }

    /**
     * @dev Destroys `amount` tokens from `account`, reducing the
     * total supply.
     *
     * Emits a {Transfer} event with `to` set to the zero address.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     * - `account` must have at least `amount` tokens.
     */
    function _burn(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: burn from the zero address");

        _beforeTokenTransfer(account, address(0), amount);

        uint256 accountBalance = _balances[account];
        require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
        unchecked {
            _balances[account] = accountBalance - amount;
            // Overflow not possible: amount <= accountBalance <= totalSupply.
            _totalSupply -= amount;
        }

        emit Transfer(account, address(0), amount);

        _afterTokenTransfer(account, address(0), amount);
    }

    /**
     * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
     *
     * This internal function is equivalent to `approve`, and can be used to
     * e.g. set automatic allowances for certain subsystems, etc.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `owner` cannot be the zero address.
     * - `spender` cannot be the zero address.
     */
    function _approve(
        address owner,
        address spender,
        uint256 amount
    ) internal virtual {
        require(owner != address(0), "ERC20: approve from the zero address");
        require(spender != address(0), "ERC20: approve to the zero address");

        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }

    /**
     * @dev Updates `owner` s allowance for `spender` based on spent `amount`.
     *
     * Does not update the allowance amount in case of infinite allowance.
     * Revert if not enough allowance is available.
     *
     * Might emit an {Approval} event.
     */
    function _spendAllowance(
        address owner,
        address spender,
        uint256 amount
    ) internal virtual {
        uint256 currentAllowance = allowance(owner, spender);
        if (currentAllowance != type(uint256).max) {
            require(currentAllowance >= amount, "ERC20: insufficient allowance");
            unchecked {
                _approve(owner, spender, currentAllowance - amount);
            }
        }
    }

    /**
     * @dev Hook that is called before any transfer of tokens. This includes
     * minting and burning.
     *
     * Calling conditions:
     *
     * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
     * will be transferred to `to`.
     * - when `from` is zero, `amount` tokens will be minted for `to`.
     * - when `to` is zero, `amount` of ``from``'s tokens will be burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 amount
    ) internal virtual {}

    /**
     * @dev Hook that is called after any transfer of tokens. This includes
     * minting and burning.
     *
     * Calling conditions:
     *
     * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
     * has been transferred to `to`.
     * - when `from` is zero, `amount` tokens have been minted for `to`.
     * - when `to` is zero, `amount` of ``from``'s tokens have been burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _afterTokenTransfer(
        address from,
        address to,
        uint256 amount
    ) internal virtual {}

    /**
     * @dev This empty reserved space is put in place to allow future versions to add new
     * variables without shifting down storage in the inheritance chain.
     * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
     */
    uint256[45] private __gap;
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20Upgradeable {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `from` to `to` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) external returns (bool);
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)

pragma solidity ^0.8.0;

import "../IERC20Upgradeable.sol";

/**
 * @dev Interface for the optional metadata functions from the ERC20 standard.
 *
 * _Available since v4.1._
 */
interface IERC20MetadataUpgradeable is IERC20Upgradeable {
    /**
     * @dev Returns the name of the token.
     */
    function name() external view returns (string memory);

    /**
     * @dev Returns the symbol of the token.
     */
    function symbol() external view returns (string memory);

    /**
     * @dev Returns the decimals places of the token.
     */
    function decimals() external view returns (uint8);
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/Address.sol)

pragma solidity ^0.8.1;

/**
 * @dev Collection of functions related to the address type
 */
library AddressUpgradeable {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * [IMPORTANT]
     * ====
     * It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     *
     * Among others, `isContract` will return false for the following
     * types of addresses:
     *
     *  - an externally-owned account
     *  - a contract in construction
     *  - an address where a contract will be created
     *  - an address where a contract lived, but was destroyed
     * ====
     *
     * [IMPORTANT]
     * ====
     * You shouldn't rely on `isContract` to protect against flash loan attacks!
     *
     * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
     * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
     * constructor.
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies on extcodesize/address.code.length, which returns 0
        // for contracts in construction, since the code is only stored at the end
        // of the constructor execution.

        return account.code.length > 0;
    }

    /**
     * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
     * `recipient`, forwarding all available gas and reverting on errors.
     *
     * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
     * of certain opcodes, possibly making contracts go over the 2300 gas limit
     * imposed by `transfer`, making them unable to receive funds via
     * `transfer`. {sendValue} removes this limitation.
     *
     * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
     *
     * IMPORTANT: because control is transferred to `recipient`, care must be
     * taken to not create reentrancy vulnerabilities. Consider using
     * {ReentrancyGuard} or the
     * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
     */
    function sendValue(address payable recipient, uint256 amount) internal {
        require(address(this).balance >= amount, "Address: insufficient balance");

        (bool success, ) = recipient.call{value: amount}("");
        require(success, "Address: unable to send value, recipient may have reverted");
    }

    /**
     * @dev Performs a Solidity function call using a low level `call`. A
     * plain `call` is an unsafe replacement for a function call: use this
     * function instead.
     *
     * If `target` reverts with a revert reason, it is bubbled up by this
     * function (like regular Solidity function calls).
     *
     * Returns the raw returned data. To convert to the expected return value,
     * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
     *
     * Requirements:
     *
     * - `target` must be a contract.
     * - calling `target` with `data` must not revert.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, "Address: low-level call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
     * `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but also transferring `value` wei to `target`.
     *
     * Requirements:
     *
     * - the calling contract must have an ETH balance of at least `value`.
     * - the called Solidity function must be `payable`.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value
    ) internal returns (bytes memory) {
        return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
    }

    /**
     * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
     * with `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value,
        string memory errorMessage
    ) internal returns (bytes memory) {
        require(address(this).balance >= value, "Address: insufficient balance for call");
        (bool success, bytes memory returndata) = target.call{value: value}(data);
        return verifyCallResultFromTarget(target, success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
        return functionStaticCall(target, data, "Address: low-level static call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal view returns (bytes memory) {
        (bool success, bytes memory returndata) = target.staticcall(data);
        return verifyCallResultFromTarget(target, success, returndata, errorMessage);
    }

    /**
     * @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
     * the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
     *
     * _Available since v4.8._
     */
    function verifyCallResultFromTarget(
        address target,
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal view returns (bytes memory) {
        if (success) {
            if (returndata.length == 0) {
                // only check isContract if the call was successful and the return data is empty
                // otherwise we already know that it was a contract
                require(isContract(target), "Address: call to non-contract");
            }
            return returndata;
        } else {
            _revert(returndata, errorMessage);
        }
    }

    /**
     * @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
     * revert reason or using the provided one.
     *
     * _Available since v4.3._
     */
    function verifyCallResult(
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal pure returns (bytes memory) {
        if (success) {
            return returndata;
        } else {
            _revert(returndata, errorMessage);
        }
    }

    function _revert(bytes memory returndata, string memory errorMessage) private pure {
        // Look for revert reason and bubble it up if present
        if (returndata.length > 0) {
            // The easiest way to bubble the revert reason is using memory via assembly
            /// @solidity memory-safe-assembly
            assembly {
                let returndata_size := mload(returndata)
                revert(add(32, returndata), returndata_size)
            }
        } else {
            revert(errorMessage);
        }
    }
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

pragma solidity ^0.8.0;
import "../proxy/utils/Initializable.sol";

/**
 * @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 ContextUpgradeable is Initializable {
    function __Context_init() internal onlyInitializing {
    }

    function __Context_init_unchained() internal onlyInitializing {
    }
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

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

    /**
     * @dev This empty reserved space is put in place to allow future versions to add new
     * variables without shifting down storage in the inheritance chain.
     * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
     */
    uint256[50] private __gap;
}

// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@strategy/IStrategy.sol";
import "@vault/FeeOracle.sol";

contract AddressRegistry is OwnableUpgradeable {
    /// FeeOracle
    FeeOracle public feeOracle;
    /// Router
    address public router;
    /// Mapping for coin and its support strategy
    mapping(address => IStrategy[]) public coinToStrategy;
    /// Mapping for strategy and its whitelisted status indicated by timestamp
    mapping(IStrategy => uint256) public strategyWhitelist;
    /// Mapping for rebalancer and its whitelisted status indicated by timestamp
    mapping(address => uint256) public rebalancerWhitelist;
    /// Array of supported coins
    address[] public supportedCoinAddresses;

    event SetRouter(address indexed router);
    event AddStrategy(IStrategy indexed strategy, address[] indexed coins);
    event AddRebalancer(address indexed rebalancer);
    event RemoveStrategy(IStrategy indexed strategy);
    event RemoveRebalancer(address indexed rebalancer);
    event Initialized(address indexed feeOracle, address indexed router);

    constructor() {
        _disableInitializers();
    }

    function init(FeeOracle _feeOracle, address _router) external initializer {
        require(
            address(_feeOracle) != address(0),
            "_feeOracle address can't be zero"
        );
        require(_router != address(0), "_router address can't be zero");

        __Ownable_init();
        feeOracle = _feeOracle;
        router = _router;
        emit Initialized(address(_feeOracle), _router);
    }

    function getSupportedCoinAddresses()
        external
        view
        returns (address[] memory)
    {
        return supportedCoinAddresses;
    }

    /// @notice Set router
    /// @param _router address of router
    function setRouter(address _router) external onlyOwner {
        require(_router != address(0), "_router address can't be zero");
        router = _router;

        emit SetRouter(_router);
    }

    /// @notice Add strategy for given coins
    /// @param strategy address of strategy
    /// @param coins array of coins that strategy supports
    function addStrategy(
        IStrategy strategy,
        address[] calldata coins
    ) external onlyOwner {
        require(
            strategyWhitelist[strategy] == 0,
            "Strategy already whitelisted"
        );
        for (uint256 i; i < coins.length; ) {
            uint256 j;
            /// add strategy
            coinToStrategy[coins[i]].push(strategy);

            uint256 supportedCoinLength = supportedCoinAddresses.length;
            for (j = 0; j < supportedCoinLength; j++) {
                if (supportedCoinAddresses[j] == coins[i]) {
                    break;
                }
            }
            if (j == supportedCoinLength) {
                supportedCoinAddresses.push(coins[i]);
            }

            unchecked {
                i++;
            }
        }
        strategyWhitelist[strategy] = block.timestamp + 1 days;

        emit AddStrategy(strategy, coins);
    }

    /// @notice Add rebalancer
    /// @param rebalancer address of rebalancer
    function addRebalancer(address rebalancer) external onlyOwner {
        require(
            rebalancerWhitelist[rebalancer] == 0,
            "Rebalancer already whitelisted"
        );
        rebalancerWhitelist[rebalancer] = block.timestamp + 1 days;

        emit AddRebalancer(rebalancer);
    }

    /// @notice Remove strategy and remove all coins that supported by removed strategy
    /// @param strategy address of strategy
    function removeStrategy(IStrategy strategy) external onlyOwner {
        require(strategyWhitelist[strategy] != 0, "Strategy not whitelisted");
        strategyWhitelist[strategy] = 0;

        address[] memory coins = supportedCoinAddresses;
        uint256 coinLength = coins.length;
        uint256 emptyCoinCount;
        for (uint8 i; i < coinLength; i++) {
            IStrategy[] storage _strategies = coinToStrategy[coins[i]];
            uint256 strategyLength = _strategies.length;
            uint8 j;
            for (; j < strategyLength; j++) {
                if (_strategies[j] == strategy) {
                    uint256 lastElementIndex = _strategies.length - 1;
                    IStrategy lastElement = _strategies[lastElementIndex];
                    _strategies[j] = lastElement;
                    _strategies.pop();
                    break;
                }
            }

            /// Count support coin address when there is no strategy available
            if (j != strategyLength && strategyLength == 1) {
                emptyCoinCount += 1;
            }
        }
        if (emptyCoinCount != 0) {
            address[] memory newCoins = new address[](
                coinLength - emptyCoinCount
            );
            uint256 newCoinIndex = 0;

            for (uint8 i; i < coinLength; i++) {
                IStrategy[] memory _strategies = coinToStrategy[coins[i]];
                if (_strategies.length != 0) {
                    newCoins[newCoinIndex] = coins[i];
                    newCoinIndex++;
                }
            }

            supportedCoinAddresses = newCoins;
        }

        emit RemoveStrategy(strategy);
    }

    /// @notice Remove rebalancer
    /// @param rebalancer address of rebalancer to be removed
    function removeRebalancer(address rebalancer) external onlyOwner {
        require(
            rebalancerWhitelist[rebalancer] != 0,
            "Rebalancer not whitelisted"
        );
        rebalancerWhitelist[rebalancer] = 0;

        emit RemoveRebalancer(rebalancer);
    }

    /// @notice Get all supported strategies for given coin address
    /// @param coin address of coin
    function getCoinToStrategy(
        address coin
    ) external view returns (IStrategy[] memory strategies) {
        uint256 activeStrategies = 0;
        uint256 strategyLengthForCoin = coinToStrategy[coin].length;
        IStrategy[] memory strategiesForCoin = coinToStrategy[coin];
        // count active strategies
        for (uint256 i; i < strategyLengthForCoin; i++) {
            if (
                strategyWhitelist[strategiesForCoin[i]] < block.timestamp &&
                strategyWhitelist[strategiesForCoin[i]] != 0
            ) {
                activeStrategies++;
            }
        }
        // create array of active strategies
        uint j = 0;
        strategies = new IStrategy[](activeStrategies);
        for (uint256 i; i < strategyLengthForCoin; i++) {
            if (
                strategyWhitelist[strategiesForCoin[i]] < block.timestamp &&
                strategyWhitelist[strategiesForCoin[i]] != 0
            ) {
                strategies[j] = strategiesForCoin[i];
                j++;
            }
        }
    }

    /// @notice Get whitelisted status of given strategy
    /// @param strategy address of strategy
    function isWhitelistedStrategy(
        IStrategy strategy
    ) external view returns (bool) {
        return
            block.timestamp >= strategyWhitelist[strategy] &&
            strategyWhitelist[strategy] != 0;
    }

    /// @notice Get whitelisted status of given rebalancer
    /// @param rebalancer address of rebalancer
    function isWhitelistedRebalancer(
        address rebalancer
    ) external view returns (bool) {
        return
            block.timestamp >= rebalancerWhitelist[rebalancer] &&
            rebalancerWhitelist[rebalancer] != 0;
    }
}

// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

/// Strategy Interface
interface IStrategy {
    function getComponentAmount(address coin) external view returns (uint256);
}

// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import "@vault/IVault.sol";
import "@structs/structs.sol";

/// Fee oracle contract that provides deposit and withdrawal fees to be used by vault contract
/// Find the formulas here: https://docs.google.com/spreadsheets/d/1K-x2kDNVfSKCEjaOtOS_gbQu5PcrkRccioJP1UBuLhs/edit#gid=0
/// The fees are based on the current weight of a coin in the vault compared to its target
contract FeeOracle is OwnableUpgradeable {
    /// targets
    CoinWeight[50] public targets;
    /// length of coin weight targets
    uint256 public targetsLength;
    /// max fee
    uint256 public maxFee;
    /// max bonus
    uint256 constant maxBonus = 0;
    /// weight denominator for weight calculation
    uint256 constant weightDenominator = 1e18;

    event SetTargets(CoinWeight[] indexed coinWeights);
    event SetMaxFee(uint256 indexed maxFee);
    event Initialized(uint256 indexed maxFee);

    constructor() {
        _disableInitializers();
    }

    function init(uint256 _maxFee) external initializer {
        require(_maxFee <= 0.5e18, "_maxFee can't be greater than 0.5e18");

        __Ownable_init();
        maxFee = _maxFee;

        emit Initialized(_maxFee);
    }

    function setMaxFee(uint256 _maxFee) external onlyOwner {
        require(_maxFee <= 0.5e18, "_maxFee can't be greater than 0.5e18");
        maxFee = _maxFee;
        emit SetMaxFee(_maxFee);
    }

    /// @notice Set target coin weights
    /// @param weights Coin weights to set
    function setTargets(CoinWeight[] memory weights) external onlyOwner {
        targetsLength = weights.length;
        require(weights.length <= 50, "too many weights");
        for (uint8 i; i < weights.length; ) {
            targets[i] = weights[i];
            unchecked {
                ++i;
            }
        }
        isNormalizedWeightArray(weights);
        emit SetTargets(weights);
    }

    function isInTarget(address coin) external view returns (bool) {
        uint256 _targetsLength = targetsLength;
        for (uint8 i; i < _targetsLength; ) {
            if (targets[i].coin == coin) return true;
            unchecked {
                ++i;
            }
        }
        return false;
    }

    /// @notice Get deposit fee
    /// @param params Deposit fee params
    /// @return fee Deposit fee
    /// @return weights Latest coin weights for vault before deposit
    /// @return tvlUSD1e18X Latest tvl for vault before deposit
    function getDepositFee(
        FeeParams memory params
    )
        external
        view
        returns (int256 fee, CoinWeight[] memory weights, uint256 tvlUSD1e18X)
    {
        CoinWeightsParams memory coinWeightParams = CoinWeightsParams({
            cpu: params.cpu,
            vault: params.vault,
            expireTimestamp: params.expireTimestamp
        });
        (weights, tvlUSD1e18X) = getCoinWeights(coinWeightParams);
        CoinWeight memory target = targets[params.position];
        CoinWeight memory currentCoinWeight = weights[params.position];
        uint256 __decimals = target.coin == address(0)
            ? 18
            : IERC20Metadata(target.coin).decimals();

        /// new weight calc
        /// formula: depositValue = depositAmount * depositPrice / 10**decimals
        uint256 depositValueUSD1e18X = (params.amount *
            params.cpu[params.position].price) / 10 ** __decimals;

        /// formula: currentCoinValue = currentCoinWeight * tvl / weightDenominator
        uint256 currentCoinValue = (currentCoinWeight.weight * tvlUSD1e18X) /
            weightDenominator;

        /// formula: newWeight = (currentCoinValue + depositValue) * weightDenominator / (tvl + depositValue)
        uint256 newWeight = ((currentCoinValue + depositValueUSD1e18X) *
            weightDenominator) / (tvlUSD1e18X + depositValueUSD1e18X);

        /// calculate distance
        /// calculate original distance
        /// formula: originalDistance = abs(currentWeight - targetWeight) / targetWeight
        uint256 originalDistance = getDistance(
            target.weight,
            currentCoinWeight.weight,
            false
        );
        /// calculate new distance
        /// formula: newDistance = abs(newWeight - targetWeight) / targetWeight
        uint256 newDistance = getDistance(target.weight, newWeight, false);
        require(newDistance < weightDenominator, "Too far away from target");
        if (originalDistance > newDistance) {
            // bonus
            uint256 improvement = originalDistance - newDistance;
            fee =
                (int256(improvement * maxBonus) * -1) /
                int256(weightDenominator);
        } else {
            // penalty
            uint256 deterioration = newDistance - originalDistance;
            fee = int256(deterioration * maxFee) / int256(weightDenominator);
        }
    }

    /// @notice Get withdrawal fee
    /// @param params Withdrawal fee params
    /// @return fee Withdrawal fee
    /// @return weights Latest coin weight for vault before withdraw
    /// @return tvlUSD1e18X Latest tvl for vault before withdraw
    function getWithdrawalFee(
        FeeParams memory params
    )
        external
        view
        returns (int256 fee, CoinWeight[] memory weights, uint256 tvlUSD1e18X)
    {
        CoinWeightsParams memory coinWeightParams = CoinWeightsParams({
            cpu: params.cpu,
            vault: params.vault,
            expireTimestamp: params.expireTimestamp
        });
        (weights, tvlUSD1e18X) = getCoinWeights(coinWeightParams);
        CoinWeight memory target = targets[params.position];
        CoinWeight memory currentCoinWeight = weights[params.position];
        uint256 __decimals = target.coin == address(0)
            ? 18
            : IERC20Metadata(target.coin).decimals();

        /// new weight calc
        /// formula: withdrawalValue = withdrawalAmount * withdrawalPrice / 10**decimals
        uint256 withdrawalValueUSD1e18X = (params.amount *
            params.cpu[params.position].price) / 10 ** __decimals;

        /// formula: currentCoinValue = currentCoinWeight * tvl / weightDenominator
        uint256 currentCoinValue = (currentCoinWeight.weight * tvlUSD1e18X) /
            weightDenominator;

        /// formula: newWeight = (currentCoinValue - withdrawalValue) * weightDenominator / (tvl - withdrawalValue)
        uint256 newWeight = ((currentCoinValue - withdrawalValueUSD1e18X) *
            weightDenominator) / (tvlUSD1e18X - withdrawalValueUSD1e18X);

        // calculate distance
        /// calculate original distance
        /// formula: originalDistance = abs(currentWeight - targetWeight) / targetWeight
        uint256 originalDistance = getDistance(
            target.weight,
            currentCoinWeight.weight,
            true
        );
        /// calculate new distance
        /// formula: newDistance = abs(newWeight - targetWeight) / targetWeight
        uint256 newDistance = getDistance(target.weight, newWeight, true);
        require(newDistance < weightDenominator, "Too far away from target");
        if (originalDistance > newDistance) {
            // bonus
            uint256 improvement = originalDistance - newDistance;
            fee = int256(improvement * maxBonus) / int256(weightDenominator);
        } else {
            // penalty
            uint256 deterioration = newDistance - originalDistance;
            fee =
                (int256(deterioration * maxFee) * -1) /
                int256(weightDenominator);
        }
    }

    /// @notice Get targets
    /// @return targets coin weights
    function getTargets() external view returns (CoinWeight[] memory) {
        uint256 _targetsLength = targetsLength;
        CoinWeight[] memory _targets = new CoinWeight[](_targetsLength);
        for (uint8 i; i < _targetsLength; ) {
            _targets[i] = targets[i];
            unchecked {
                ++i;
            }
        }
        return _targets;
    }

    /// @notice Get current coin weights and tvl for given params
    /// @param params CoinWeightsPrams for get coin weights
    /// @return weights Current coin weights for given params
    /// @return tvlUSD1e18X TVL for given vault
    function getCoinWeights(
        CoinWeightsParams memory params
    ) public view returns (CoinWeight[] memory weights, uint256 tvlUSD1e18X) {
        require(
            block.timestamp < params.expireTimestamp,
            "Execution window passed"
        );
        uint256 _targetsLength = targetsLength;
        weights = new CoinWeight[](_targetsLength);
        require(params.cpu.length == _targetsLength, "Oracle length error");
        CoinWeight[50] memory _targets = targets;
        for (uint8 i; i < _targetsLength; ) {
            require(
                params.cpu[i].coin == _targets[i].coin,
                "Oracle order error"
            );
            /// Get available amount of coin for the vault per every coin
            /// formula: coinVaultAmount + coinStrategiesAmount - coinDebtAmount
            uint256 amount = params.vault.getAmountAcrossStrategies(
                _targets[i].coin
            ) - params.vault.debt(_targets[i].coin);
            /// Initialize coinWeight with available amount of coin
            weights[i] = CoinWeight(params.cpu[i].coin, amount);
            unchecked {
                i++;
            }
        }

        /// Calc tvl
        uint8[] memory __decimals = new uint8[](_targetsLength);
        for (uint8 i; i < _targetsLength; ) {
            __decimals[i] = _targets[i].coin == address(0)
                ? 18
                : IERC20Metadata(_targets[i].coin).decimals();
            /// Calculate tvl over the coin weights
            /// Set weight with every coin value
            /// formula: coinValue = coinAmount * coinPriceUSD / 10**coinDecimal
            weights[i].weight =
                (weights[i].weight * params.cpu[i].price) /
                10 ** __decimals[i];
            /// formula: tvl += coinValue
            tvlUSD1e18X += weights[i].weight;
            unchecked {
                i++;
            }
        }

        /// Normalize
        for (uint8 i; i < _targetsLength; ) {
            /// Normalize coin weights
            /// formula: weight = coinValue * weightDenominator / tvl
            weights[i].weight =
                (weights[i].weight * weightDenominator) /
                tvlUSD1e18X;
            unchecked {
                i++;
            }
        }
        isNormalizedWeightArray(weights);
    }

    /// @notice Check if weights array is normalized or not
    /// @param weights Coin weight array that needs to be checked
    function isNormalizedWeightArray(
        CoinWeight[] memory weights
    ) internal pure {
        uint256 totalWeight = 0;
        for (uint8 i; i < weights.length; ) {
            totalWeight += weights[i].weight;
            unchecked {
                i++;
            }
        }
        // compensate for rounding errors
        require(
            totalWeight >= weightDenominator - weights.length,
            "Weight error"
        );
        require(totalWeight <= weightDenominator, "Weight error 2");
    }

    /// @notice Get distance between two weights. The "distance" is calculated as a percentage change of the new weight compared to the target weight.
    /// @param targetWeight Standard weight that calculate distance
    /// @param comparedWeight Compared weight that calculate distance
    /// @param method deposit or withdraw
    /// @return distance
    function getDistance(
        uint256 targetWeight,
        uint256 comparedWeight,
        bool method
    ) internal pure returns (uint256) {
        /// formula: distance = abs(targetWeight - comparedWeight) * weightDenominator / targetWeight
        if (targetWeight == 0) return method ? 0 : weightDenominator;
        return
            targetWeight >= comparedWeight
                ? ((targetWeight - comparedWeight) * weightDenominator) /
                    targetWeight
                : ((comparedWeight - targetWeight) * weightDenominator) /
                    targetWeight;
    }
}

// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

/// Vault Interface
interface IVault {
    function getAmountAcrossStrategies(
        address coin
    ) external view returns (uint256 value);

    function debt(address coin) external view returns (uint256 value);

    function rebalance(
        address destination,
        address coin,
        uint256 amount
    ) external;
}

File 18 of 18 : structs.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

import "@vault/IVault.sol";

enum PairType {
    USDC,
    WETH
}

struct CoinPriceUSD {
    address coin;
    uint256 price;
}

struct CoinWeight {
    address coin;
    uint256 weight;
}

struct CoinValue {
    address coin;
    uint256 value;
}

struct CoinWeightsParams {
    CoinPriceUSD[] cpu;
    IVault vault;
    uint256 expireTimestamp;
}

struct FeeParams {
    CoinPriceUSD[] cpu;
    IVault vault;
    uint256 expireTimestamp;
    uint256 position;
    uint256 amount;
}

Settings
{
  "remappings": [
    "@/=src/contracts/",
    "@farm/=src/contracts/farm/",
    "@mocks/=src/mocks/",
    "@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "@strategy/=src/contracts/strategy/",
    "@structs/=src/structs/",
    "@tokens/=src/contracts/tokens/",
    "@vault/=src/contracts/vault/",
    "ds-test/=lib/ds-test/src/",
    "forge-std/=lib/forge-std/src/",
    "openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/"
  ],
  "optimizer": {
    "enabled": true,
    "runs": 200
  },
  "metadata": {
    "bytecodeHash": "ipfs"
  },
  "outputSelection": {
    "*": {
      "*": [
        "evm.bytecode",
        "evm.deployedBytecode",
        "devdoc",
        "userdoc",
        "metadata",
        "abi"
      ]
    }
  },
  "evmVersion": "london",
  "libraries": {}
}

Contract Security Audit

Contract ABI

API
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"strategy","type":"address"},{"indexed":true,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"DepositEthToStrategy","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint8","name":"version","type":"uint8"}],"name":"Initialized","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"destination","type":"address"},{"indexed":true,"internalType":"address","name":"coin","type":"address"},{"indexed":true,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Rebalance","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract AddressRegistry","name":"addressRegistry","type":"address"}],"name":"SetAddressRegistry","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"cap","type":"uint256"}],"name":"SetBlockCap","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"coin","type":"address"},{"indexed":true,"internalType":"uint256","name":"cap","type":"uint256"}],"name":"SetCoinCap","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[],"name":"addressRegistry","outputs":[{"internalType":"contract AddressRegistry","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IStrategy","name":"strategy","type":"address"},{"internalType":"address","name":"coin","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approveStrategy","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"blockCapCounter","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"blockCapUSD","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"coin","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"claimDebt","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"coinCap","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"debt","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"coinPositionInCPU","type":"uint256"},{"internalType":"uint256","name":"_amount","type":"uint256"},{"components":[{"internalType":"address","name":"coin","type":"address"},{"internalType":"uint256","name":"price","type":"uint256"}],"internalType":"struct CoinPriceUSD[]","name":"cpu","type":"tuple[]"},{"internalType":"uint256","name":"expireTimestamp","type":"uint256"}],"internalType":"struct Vault.DepositWithrawalParams","name":"params","type":"tuple"}],"name":"deposit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IStrategy","name":"strategy","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"depositETHToStrategy","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"coin","type":"address"}],"name":"getAmountAcrossStrategies","outputs":[{"internalType":"uint256","name":"value","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract AddressRegistry","name":"_addressRegistry","type":"address"}],"name":"init829","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"destination","type":"address"},{"internalType":"address","name":"coin","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"rebalance","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract AddressRegistry","name":"_addressRegistry","type":"address"}],"name":"setAddressRegistry","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"cap","type":"uint256"}],"name":"setBlockCap","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"coin","type":"address"},{"internalType":"uint256","name":"cap","type":"uint256"}],"name":"setCoinCapUSD","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"coinPositionInCPU","type":"uint256"},{"internalType":"uint256","name":"_amount","type":"uint256"},{"components":[{"internalType":"address","name":"coin","type":"address"},{"internalType":"uint256","name":"price","type":"uint256"}],"internalType":"struct CoinPriceUSD[]","name":"cpu","type":"tuple[]"},{"internalType":"uint256","name":"expireTimestamp","type":"uint256"}],"internalType":"struct Vault.DepositWithrawalParams","name":"params","type":"tuple"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]

60806040523480156200001157600080fd5b506200001c62000022565b620000e4565b600054610100900460ff16156200008f5760405162461bcd60e51b815260206004820152602760248201527f496e697469616c697a61626c653a20636f6e747261637420697320696e697469604482015266616c697a696e6760c81b606482015260840160405180910390fd5b60005460ff9081161015620000e2576000805460ff191660ff9081179091556040519081527f7f26b83ff96e1f2b6a682f133852f6798a09c465da95921460cefb38474024989060200160405180910390a15b565b61314d80620000f46000396000f3fe6080604052600436106101d15760003560e01c80638da5cb5b116100f7578063b36bf47111610095578063ee2eb60011610064578063ee2eb60014610565578063eee950e514610585578063f2fde38b1461059b578063f3ad65f4146105bb57600080fd5b8063b36bf471146104d8578063b3f865f7146104f8578063caa186b714610518578063dd62ed3e1461054557600080fd5b8063a457c2d7116100d1578063a457c2d714610458578063a9059cbb14610478578063aa13378d14610498578063aa492e68146104b857600080fd5b80638da5cb5b146103e457806395d89b41146104165780639b6c56ec1461042b57600080fd5b80632ec9dc161161016f578063645061501161013e57806364506150146103595780636d8623321461037957806370a0823114610399578063715018a6146103cf57600080fd5b80632ec9dc16146102d0578063313ce567146102fd57806339509351146103195780634a174c351461033957600080fd5b80630e6d55e7116101ab5780630e6d55e71461026657806318160ddd1461027b57806323b872dd1461029057806327c7812c146102b057600080fd5b806306fdde03146101dd578063095ea7b3146102085780630a7908381461023857600080fd5b366101d857005b600080fd5b3480156101e957600080fd5b506101f26105db565b6040516101ff91906127c3565b60405180910390f35b34801561021457600080fd5b5061022861022336600461280b565b61066d565b60405190151581526020016101ff565b34801561024457600080fd5b50610258610253366004612837565b610687565b6040519081526020016101ff565b610279610274366004612837565b610841565b005b34801561028757600080fd5b50606754610258565b34801561029c57600080fd5b506102286102ab36600461285b565b610a2b565b3480156102bc57600080fd5b506102796102cb366004612837565b610a4f565b3480156102dc57600080fd5b506102586102eb36600461289c565b60996020526000908152604090205481565b34801561030957600080fd5b50604051601281526020016101ff565b34801561032557600080fd5b5061022861033436600461280b565b610ac7565b34801561034557600080fd5b5061027961035436600461280b565b610ae9565b34801561036557600080fd5b5061027961037436600461289c565b610b85565b34801561038557600080fd5b5061027961039436600461280b565b610bc0565b3480156103a557600080fd5b506102586103b4366004612837565b6001600160a01b031660009081526065602052604090205490565b3480156103db57600080fd5b50610279610d51565b3480156103f057600080fd5b506033546001600160a01b03165b6040516001600160a01b0390911681526020016101ff565b34801561042257600080fd5b506101f2610d65565b34801561043757600080fd5b50610258610446366004612837565b609b6020526000908152604090205481565b34801561046457600080fd5b5061022861047336600461280b565b610d74565b34801561048457600080fd5b5061022861049336600461280b565b610def565b3480156104a457600080fd5b506102796104b336600461285b565b610dfd565b3480156104c457600080fd5b506102796104d336600461296c565b611058565b3480156104e457600080fd5b506102796104f336600461296c565b6114de565b34801561050457600080fd5b5061027961051336600461285b565b6118e8565b34801561052457600080fd5b50610258610533366004612837565b60986020526000908152604090205481565b34801561055157600080fd5b50610258610560366004612a80565b611b07565b34801561057157600080fd5b5061027961058036600461280b565b611b32565b34801561059157600080fd5b50610258609a5481565b3480156105a757600080fd5b506102796105b6366004612837565b611c8d565b3480156105c757600080fd5b506097546103fe906001600160a01b031681565b6060606880546105ea90612ab9565b80601f016020809104026020016040519081016040528092919081815260200182805461061690612ab9565b80156106635780601f1061063857610100808354040283529160200191610663565b820191906000526020600020905b81548152906001019060200180831161064657829003601f168201915b5050505050905090565b60003361067b818585611d06565b60019150505b92915050565b60006001600160a01b0382166106a8576106a14782612b09565b905061071d565b6040516370a0823160e01b81523060048201526001600160a01b038316906370a0823190602401602060405180830381865afa1580156106ec573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906107109190612b1c565b61071a9082612b09565b90505b609754604051630f8486b760e11b81526001600160a01b0384811660048301526000921690631f090d6e90602401600060405180830381865afa158015610768573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526107909190810190612b35565b905060005b815181101561083a578181815181106107b0576107b0612bc4565b602090810291909101015160405163da0b206360e01b81526001600160a01b0386811660048301529091169063da0b206390602401602060405180830381865afa158015610802573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906108269190612b1c565b6108309084612b09565b9250600101610795565b5050919050565b600054610100900460ff16158080156108615750600054600160ff909116105b8061087b5750303b15801561087b575060005460ff166001145b6108e35760405162461bcd60e51b815260206004820152602e60248201527f496e697469616c697a61626c653a20636f6e747261637420697320616c72656160448201526d191e481a5b9a5d1a585b1a5e995960921b60648201526084015b60405180910390fd5b6000805460ff191660011790558015610906576000805461ff0019166101001790555b6001600160a01b03821661092c5760405162461bcd60e51b81526004016108da90612bda565b66038d7ea4c6800034101561094057600080fd5b610948611e2a565b610988604051806040016040528060038152602001620414c560ec1b815250604051806040016040528060038152602001620414c560ec1b815250611e59565b609780546001600160a01b0319166001600160a01b0384161790556109ad3334611e8a565b6040516001600160a01b038316907f6b166bc981591e4d37f661da896b9fe0c8ed4c8306068fb296ec9c3cf2cf17e090600090a28015610a27576000805461ff0019169055604051600181527f7f26b83ff96e1f2b6a682f133852f6798a09c465da95921460cefb38474024989060200160405180910390a15b5050565b600033610a39858285611f4b565b610a44858585611fc5565b506001949350505050565b610a57612170565b6001600160a01b038116610a7d5760405162461bcd60e51b81526004016108da90612bda565b609780546001600160a01b0319166001600160a01b0383169081179091556040517f6b166bc981591e4d37f661da896b9fe0c8ed4c8306068fb296ec9c3cf2cf17e090600090a250565b60003361067b818585610ada8383611b07565b610ae49190612b09565b611d06565b610af1612170565b6001600160a01b038216610b3e5760405162461bcd60e51b8152602060048201526014602482015273696e76616c696420636f696e206164647265737360601b60448201526064016108da565b6001600160a01b038216600081815260986020526040808220849055518392917f0effed52874c24c39a6db6c1cbf7fccfb0171900f623ddf1463d142bdd65719c91a35050565b610b8d612170565b609a81905560405181907f9ddfcb9cd791bfaecb571c9b1b91e5542b76f96bdfcd288551351c5564e8d3ad90600090a250565b610bc8612170565b609754604051631432585160e31b81526001600160a01b0384811660048301529091169063a192c28890602401602060405180830381865afa158015610c12573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610c369190612c20565b610c825760405162461bcd60e51b815260206004820152601b60248201527f7374726174656779206973206e6f742077686974656c6973746564000000000060448201526064016108da565b6000826001600160a01b03168260405160006040518083038185875af1925050503d8060008114610ccf576040519150601f19603f3d011682016040523d82523d6000602084013e610cd4565b606091505b5050905080610d165760405162461bcd60e51b815260206004820152600e60248201526d11195c1bdcda5d0819985a5b195960921b60448201526064016108da565b60405182906001600160a01b038516907f9b931226d89fe9ed52bb22b49e697228de3f6d78c4f7fa69092763ded428119f90600090a3505050565b610d59612170565b610d6360006121ca565b565b6060606980546105ea90612ab9565b60003381610d828286611b07565b905083811015610de25760405162461bcd60e51b815260206004820152602560248201527f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f77604482015264207a65726f60d81b60648201526084016108da565b610a448286868403611d06565b60003361067b818585611fc5565b610e05612170565b6001600160a01b038316610e5b5760405162461bcd60e51b815260206004820152601860248201527f696e76616c69642073747261746567792061646472657373000000000000000060448201526064016108da565b609754604051631432585160e31b81526001600160a01b0385811660048301529091169063a192c28890602401602060405180830381865afa158015610ea5573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610ec99190612c20565b610f155760405162461bcd60e51b815260206004820152601b60248201527f7374726174656779206973206e6f742077686974656c6973746564000000000060448201526064016108da565b609754604051630f8486b760e11b81526001600160a01b0384811660048301526000921690631f090d6e90602401600060405180830381865afa158015610f60573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610f889190810190612b35565b905060005b8151811015610fdb57846001600160a01b0316828281518110610fb257610fb2612bc4565b60200260200101516001600160a01b03160315610fdb5780610fd381612c42565b915050610f8d565b8151810361103d5760405162461bcd60e51b815260206004820152602960248201527f70726f766964656420636f696e206973206e6f74207468652070617274206f6660448201526820737472617465677960b81b60648201526084016108da565b6110516001600160a01b038516868561221c565b5050505050565b609760009054906101000a90046001600160a01b03166001600160a01b031663f887ea406040518163ffffffff1660e01b8152600401602060405180830381865afa1580156110ab573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906110cf9190612c5b565b6001600160a01b0316336001600160a01b0316146110ff5760405162461bcd60e51b81526004016108da90612c78565b60006040518060a0016040528083604001518152602001306001600160a01b031681526020018360600151815260200183600001518152602001836020015181525090506000826040015183600001518151811061115f5761115f612bc4565b60200260200101516000015190506000816001600160a01b031663313ce5676040518163ffffffff1660e01b8152600401602060405180830381865afa1580156111ad573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906111d19190612caf565b60ff16905060006111e382600a612db6565b60408601518651815181106111fa576111fa612bc4565b602002602001015160200151866020015161121486610687565b61121e9190612b09565b6112289190612dc2565b6112329190612dd9565b6001600160a01b038416600090815260986020526040902054909150811061128f5760405162461bcd60e51b815260206004820152601060248201526f10dbda5b8818d85c081c995858da195960821b60448201526064016108da565b600061129c83600a612db6565b602087015160408801518851815181106112b8576112b8612bc4565b6020026020010151602001516112ce9190612dc2565b6112d89190612dd9565b609a5443600090815260996020526040902054919250906112f99083612b09565b1061133a5760405162461bcd60e51b8152602060048201526011602482015270109b1bd8dac818d85c081c995858da1959607a1b60448201526064016108da565b4360009081526099602052604081208054839290611359908490612b09565b92505081905550600080609760009054906101000a90046001600160a01b03166001600160a01b031663500b19e76040518163ffffffff1660e01b8152600401602060405180830381865afa1580156113b6573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906113da9190612c5b565b6001600160a01b0316631d6573dc886040518263ffffffff1660e01b81526004016114059190612dfb565b600060405180830381865afa158015611422573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f1916820160405261144a9190810190612e94565b9250509150600081670de0b6b3a7640000856114669190612dc2565b6114709190612dd9565b90506000670de0b6b3a764000061148660675490565b6114909084612dc2565b61149a9190612dd9565b90506000670de0b6b3a76400006114b18682612f68565b6114bb9084612dc2565b6114c59190612dd9565b90506114d13382611e8a565b5050505050505050505050565b609760009054906101000a90046001600160a01b03166001600160a01b031663f887ea406040518163ffffffff1660e01b8152600401602060405180830381865afa158015611531573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906115559190612c5b565b6001600160a01b0316336001600160a01b0316146115855760405162461bcd60e51b81526004016108da90612c78565b60006040518060a0016040528083604001518152602001306001600160a01b03168152602001836060015181526020018360000151815260200183602001518152509050600082604001518360000151815181106115e5576115e5612bc4565b60200260200101516000015190506000816001600160a01b031663313ce5676040518163ffffffff1660e01b8152600401602060405180830381865afa158015611633573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906116579190612caf565b60ff169050600061166982600a612db6565b6020860151604087015187518151811061168557611685612bc4565b60200260200101516020015161169b9190612dc2565b6116a59190612dd9565b609a5443600090815260996020526040902054919250906116c69083612b09565b106117075760405162461bcd60e51b8152602060048201526011602482015270109b1bd8dac818d85c081c995858da1959607a1b60448201526064016108da565b4360009081526099602052604081208054839290611726908490612b09565b92505081905550600080609760009054906101000a90046001600160a01b03166001600160a01b031663500b19e76040518163ffffffff1660e01b8152600401602060405180830381865afa158015611783573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906117a79190612c5b565b6001600160a01b031663879ce416876040518263ffffffff1660e01b81526004016117d29190612dfb565b600060405180830381865afa1580156117ef573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526118179190810190612e94565b9250509150600081670de0b6b3a7640000856118339190612dc2565b61183d9190612dd9565b90506000670de0b6b3a764000061185360675490565b61185d9084612dc2565b6118679190612dd9565b90506000670de0b6b3a764000061187e8682612f68565b6118889084612dc2565b6118929190612dd9565b905061189e3382612369565b8960200151609b60008a6001600160a01b03166001600160a01b0316815260200190815260200160002060008282546118d79190612b09565b909155505050505050505050505050565b6118f0612170565b6001600160a01b03831661193c5760405162461bcd60e51b815260206004820152601360248201527234b73b30b634b2103232b9ba34b730ba34b7b760691b60448201526064016108da565b609754604051630550ff9360e01b81526001600160a01b03858116600483015290911690630550ff9390602401602060405180830381865afa158015611986573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906119aa9190612c20565b6119f65760405162461bcd60e51b815260206004820152601e60248201527f64657374696e6174696f6e206973206e6f742077686974656c6973746564000060448201526064016108da565b6001600160a01b038216611aad576000836001600160a01b03168260405160006040518083038185875af1925050503d8060008114611a51576040519150601f19603f3d011682016040523d82523d6000602084013e611a56565b606091505b5050905080611aa75760405162461bcd60e51b815260206004820152601d60248201527f6465706f73697420746f2064657374696e6174696f6e206661696c656400000060448201526064016108da565b50611ac1565b611ac16001600160a01b038316848361249d565b80826001600160a01b0316846001600160a01b03167fb0850b8e0f9e8315dde3c9f9f31138283e6bbe16cd29e8552eb1dcdf9fac9e3b60405160405180910390a4505050565b6001600160a01b03918216600090815260666020908152604080832093909416825291909152205490565b609760009054906101000a90046001600160a01b03166001600160a01b031663f887ea406040518163ffffffff1660e01b8152600401602060405180830381865afa158015611b85573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611ba99190612c5b565b6001600160a01b0316336001600160a01b031614611bd95760405162461bcd60e51b81526004016108da90612c78565b6001600160a01b0382166000908152609b6020526040902054811115611c4b5760405162461bcd60e51b815260206004820152602160248201527f696e73756666696369656e74206465627420616d6f756e7420666f7220636f696044820152603760f91b60648201526084016108da565b6001600160a01b0382166000908152609b602052604081208054839290611c73908490612f8f565b90915550610a2790506001600160a01b038316338361249d565b611c95612170565b6001600160a01b038116611cfa5760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b60648201526084016108da565b611d03816121ca565b50565b6001600160a01b038316611d685760405162461bcd60e51b8152602060048201526024808201527f45524332303a20617070726f76652066726f6d20746865207a65726f206164646044820152637265737360e01b60648201526084016108da565b6001600160a01b038216611dc95760405162461bcd60e51b815260206004820152602260248201527f45524332303a20617070726f766520746f20746865207a65726f206164647265604482015261737360f01b60648201526084016108da565b6001600160a01b0383811660008181526066602090815260408083209487168084529482529182902085905590518481527f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925910160405180910390a3505050565b600054610100900460ff16611e515760405162461bcd60e51b81526004016108da90612fa2565b610d636124cd565b600054610100900460ff16611e805760405162461bcd60e51b81526004016108da90612fa2565b610a2782826124fd565b6001600160a01b038216611ee05760405162461bcd60e51b815260206004820152601f60248201527f45524332303a206d696e7420746f20746865207a65726f20616464726573730060448201526064016108da565b8060676000828254611ef29190612b09565b90915550506001600160a01b0382166000818152606560209081526040808320805486019055518481527fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef910160405180910390a35050565b6000611f578484611b07565b90506000198114611fbf5781811015611fb25760405162461bcd60e51b815260206004820152601d60248201527f45524332303a20696e73756666696369656e7420616c6c6f77616e636500000060448201526064016108da565b611fbf8484848403611d06565b50505050565b6001600160a01b0383166120295760405162461bcd60e51b815260206004820152602560248201527f45524332303a207472616e736665722066726f6d20746865207a65726f206164604482015264647265737360d81b60648201526084016108da565b6001600160a01b03821661208b5760405162461bcd60e51b815260206004820152602360248201527f45524332303a207472616e7366657220746f20746865207a65726f206164647260448201526265737360e81b60648201526084016108da565b6001600160a01b038316600090815260656020526040902054818110156121035760405162461bcd60e51b815260206004820152602660248201527f45524332303a207472616e7366657220616d6f756e7420657863656564732062604482015265616c616e636560d01b60648201526084016108da565b6001600160a01b0380851660008181526065602052604080822086860390559286168082529083902080548601905591517fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef906121639086815260200190565b60405180910390a3611fbf565b6033546001600160a01b03163314610d635760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657260448201526064016108da565b603380546001600160a01b038381166001600160a01b0319831681179093556040519116919082907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a35050565b8015806122965750604051636eb1769f60e11b81523060048201526001600160a01b03838116602483015284169063dd62ed3e90604401602060405180830381865afa158015612270573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906122949190612b1c565b155b6123015760405162461bcd60e51b815260206004820152603660248201527f5361666545524332303a20617070726f76652066726f6d206e6f6e2d7a65726f60448201527520746f206e6f6e2d7a65726f20616c6c6f77616e636560501b60648201526084016108da565b6040516001600160a01b03831660248201526044810182905261236490849063095ea7b360e01b906064015b60408051601f198184030181529190526020810180516001600160e01b03166001600160e01b03199093169290921790915261253d565b505050565b6001600160a01b0382166123c95760405162461bcd60e51b815260206004820152602160248201527f45524332303a206275726e2066726f6d20746865207a65726f206164647265736044820152607360f81b60648201526084016108da565b6001600160a01b0382166000908152606560205260409020548181101561243d5760405162461bcd60e51b815260206004820152602260248201527f45524332303a206275726e20616d6f756e7420657863656564732062616c616e604482015261636560f01b60648201526084016108da565b6001600160a01b03831660008181526065602090815260408083208686039055606780548790039055518581529192917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef910160405180910390a3505050565b6040516001600160a01b03831660248201526044810182905261236490849063a9059cbb60e01b9060640161232d565b600054610100900460ff166124f45760405162461bcd60e51b81526004016108da90612fa2565b610d63336121ca565b600054610100900460ff166125245760405162461bcd60e51b81526004016108da90612fa2565b6068612530838261303b565b506069612364828261303b565b6000612592826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b031661260f9092919063ffffffff16565b80519091501561236457808060200190518101906125b09190612c20565b6123645760405162461bcd60e51b815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e6044820152691bdd081cdd58d8d9595960b21b60648201526084016108da565b606061261e8484600085612626565b949350505050565b6060824710156126875760405162461bcd60e51b815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f6044820152651c8818d85b1b60d21b60648201526084016108da565b600080866001600160a01b031685876040516126a391906130fb565b60006040518083038185875af1925050503d80600081146126e0576040519150601f19603f3d011682016040523d82523d6000602084013e6126e5565b606091505b50915091506126f687838387612701565b979650505050505050565b60608315612770578251600003612769576001600160a01b0385163b6127695760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e747261637400000060448201526064016108da565b508161261e565b61261e83838151156127855781518083602001fd5b8060405162461bcd60e51b81526004016108da91906127c3565b60005b838110156127ba5781810151838201526020016127a2565b50506000910152565b60208152600082518060208401526127e281604085016020870161279f565b601f01601f19169190910160400192915050565b6001600160a01b0381168114611d0357600080fd5b6000806040838503121561281e57600080fd5b8235612829816127f6565b946020939093013593505050565b60006020828403121561284957600080fd5b8135612854816127f6565b9392505050565b60008060006060848603121561287057600080fd5b833561287b816127f6565b9250602084013561288b816127f6565b929592945050506040919091013590565b6000602082840312156128ae57600080fd5b5035919050565b634e487b7160e01b600052604160045260246000fd5b6040516080810167ffffffffffffffff811182821017156128ee576128ee6128b5565b60405290565b6040805190810167ffffffffffffffff811182821017156128ee576128ee6128b5565b604051601f8201601f1916810167ffffffffffffffff81118282101715612940576129406128b5565b604052919050565b600067ffffffffffffffff821115612962576129626128b5565b5060051b60200190565b6000602080838503121561297f57600080fd5b823567ffffffffffffffff8082111561299757600080fd5b90840190608082870312156129ab57600080fd5b6129b36128cb565b823581528383013584820152604080840135838111156129d257600080fd5b84019250601f830188136129e557600080fd5b82356129f86129f382612948565b612917565b81815260069190911b8401860190868101908a831115612a1757600080fd5b948701945b82861015612a605783868c031215612a345760008081fd5b612a3c6128f4565b8635612a47816127f6565b8152868901358982015282529483019490870190612a1c565b808486015250505050606083013560608201528094505050505092915050565b60008060408385031215612a9357600080fd5b8235612a9e816127f6565b91506020830135612aae816127f6565b809150509250929050565b600181811c90821680612acd57607f821691505b602082108103612aed57634e487b7160e01b600052602260045260246000fd5b50919050565b634e487b7160e01b600052601160045260246000fd5b8082018082111561068157610681612af3565b600060208284031215612b2e57600080fd5b5051919050565b60006020808385031215612b4857600080fd5b825167ffffffffffffffff811115612b5f57600080fd5b8301601f81018513612b7057600080fd5b8051612b7e6129f382612948565b81815260059190911b82018301908381019087831115612b9d57600080fd5b928401925b828410156126f6578351612bb5816127f6565b82529284019290840190612ba2565b634e487b7160e01b600052603260045260246000fd5b60208082526026908201527f5f61646472657373526567697374727920616464726573732063616e2774206260408201526565207a65726f60d01b606082015260800190565b600060208284031215612c3257600080fd5b8151801515811461285457600080fd5b600060018201612c5457612c54612af3565b5060010190565b600060208284031215612c6d57600080fd5b8151612854816127f6565b60208082526019908201527f6f6e6c7920726f7574657220686173207065726d697474656400000000000000604082015260600190565b600060208284031215612cc157600080fd5b815160ff8116811461285457600080fd5b600181815b80851115612d0d578160001904821115612cf357612cf3612af3565b80851615612d0057918102915b93841c9390800290612cd7565b509250929050565b600082612d2457506001610681565b81612d3157506000610681565b8160018114612d475760028114612d5157612d6d565b6001915050610681565b60ff841115612d6257612d62612af3565b50506001821b610681565b5060208310610133831016604e8410600b8410161715612d90575081810a610681565b612d9a8383612cd2565b8060001904821115612dae57612dae612af3565b029392505050565b60006128548383612d15565b808202811582820484141761068157610681612af3565b600082612df657634e487b7160e01b600052601260045260246000fd5b500490565b6020808252825160a083830152805160c0840181905260009291820190839060e08601905b80831015612e5457835180516001600160a01b03168352850151858301529284019260019290920191604090910190612e20565b50928601516001600160a01b0381166040870152926040870151606087015260608701516080870152608087015160a08701528094505050505092915050565b600080600060608486031215612ea957600080fd5b8351925060208085015167ffffffffffffffff811115612ec857600080fd5b8501601f81018713612ed957600080fd5b8051612ee76129f382612948565b81815260069190911b82018301908381019089831115612f0657600080fd5b928401925b82841015612f52576040848b031215612f245760008081fd5b612f2c6128f4565b8451612f37816127f6565b81528486015186820152825260409093019290840190612f0b565b8096505050505050604084015190509250925092565b8181036000831280158383131683831282161715612f8857612f88612af3565b5092915050565b8181038181111561068157610681612af3565b6020808252602b908201527f496e697469616c697a61626c653a20636f6e7472616374206973206e6f74206960408201526a6e697469616c697a696e6760a81b606082015260800190565b601f82111561236457600081815260208120601f850160051c810160208610156130145750805b601f850160051c820191505b8181101561303357828155600101613020565b505050505050565b815167ffffffffffffffff811115613055576130556128b5565b613069816130638454612ab9565b84612fed565b602080601f83116001811461309e57600084156130865750858301515b600019600386901b1c1916600185901b178555613033565b600085815260208120601f198616915b828110156130cd578886015182559484019460019091019084016130ae565b50858210156130eb5787850151600019600388901b60f8161c191681555b5050505050600190811b01905550565b6000825161310d81846020870161279f565b919091019291505056fea26469706673582212208795b03491acca6e48ba35debb934f455745f88c9cb21df42864d1037c612b8764736f6c63430008110033

Deployed Bytecode

0x6080604052600436106101d15760003560e01c80638da5cb5b116100f7578063b36bf47111610095578063ee2eb60011610064578063ee2eb60014610565578063eee950e514610585578063f2fde38b1461059b578063f3ad65f4146105bb57600080fd5b8063b36bf471146104d8578063b3f865f7146104f8578063caa186b714610518578063dd62ed3e1461054557600080fd5b8063a457c2d7116100d1578063a457c2d714610458578063a9059cbb14610478578063aa13378d14610498578063aa492e68146104b857600080fd5b80638da5cb5b146103e457806395d89b41146104165780639b6c56ec1461042b57600080fd5b80632ec9dc161161016f578063645061501161013e57806364506150146103595780636d8623321461037957806370a0823114610399578063715018a6146103cf57600080fd5b80632ec9dc16146102d0578063313ce567146102fd57806339509351146103195780634a174c351461033957600080fd5b80630e6d55e7116101ab5780630e6d55e71461026657806318160ddd1461027b57806323b872dd1461029057806327c7812c146102b057600080fd5b806306fdde03146101dd578063095ea7b3146102085780630a7908381461023857600080fd5b366101d857005b600080fd5b3480156101e957600080fd5b506101f26105db565b6040516101ff91906127c3565b60405180910390f35b34801561021457600080fd5b5061022861022336600461280b565b61066d565b60405190151581526020016101ff565b34801561024457600080fd5b50610258610253366004612837565b610687565b6040519081526020016101ff565b610279610274366004612837565b610841565b005b34801561028757600080fd5b50606754610258565b34801561029c57600080fd5b506102286102ab36600461285b565b610a2b565b3480156102bc57600080fd5b506102796102cb366004612837565b610a4f565b3480156102dc57600080fd5b506102586102eb36600461289c565b60996020526000908152604090205481565b34801561030957600080fd5b50604051601281526020016101ff565b34801561032557600080fd5b5061022861033436600461280b565b610ac7565b34801561034557600080fd5b5061027961035436600461280b565b610ae9565b34801561036557600080fd5b5061027961037436600461289c565b610b85565b34801561038557600080fd5b5061027961039436600461280b565b610bc0565b3480156103a557600080fd5b506102586103b4366004612837565b6001600160a01b031660009081526065602052604090205490565b3480156103db57600080fd5b50610279610d51565b3480156103f057600080fd5b506033546001600160a01b03165b6040516001600160a01b0390911681526020016101ff565b34801561042257600080fd5b506101f2610d65565b34801561043757600080fd5b50610258610446366004612837565b609b6020526000908152604090205481565b34801561046457600080fd5b5061022861047336600461280b565b610d74565b34801561048457600080fd5b5061022861049336600461280b565b610def565b3480156104a457600080fd5b506102796104b336600461285b565b610dfd565b3480156104c457600080fd5b506102796104d336600461296c565b611058565b3480156104e457600080fd5b506102796104f336600461296c565b6114de565b34801561050457600080fd5b5061027961051336600461285b565b6118e8565b34801561052457600080fd5b50610258610533366004612837565b60986020526000908152604090205481565b34801561055157600080fd5b50610258610560366004612a80565b611b07565b34801561057157600080fd5b5061027961058036600461280b565b611b32565b34801561059157600080fd5b50610258609a5481565b3480156105a757600080fd5b506102796105b6366004612837565b611c8d565b3480156105c757600080fd5b506097546103fe906001600160a01b031681565b6060606880546105ea90612ab9565b80601f016020809104026020016040519081016040528092919081815260200182805461061690612ab9565b80156106635780601f1061063857610100808354040283529160200191610663565b820191906000526020600020905b81548152906001019060200180831161064657829003601f168201915b5050505050905090565b60003361067b818585611d06565b60019150505b92915050565b60006001600160a01b0382166106a8576106a14782612b09565b905061071d565b6040516370a0823160e01b81523060048201526001600160a01b038316906370a0823190602401602060405180830381865afa1580156106ec573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906107109190612b1c565b61071a9082612b09565b90505b609754604051630f8486b760e11b81526001600160a01b0384811660048301526000921690631f090d6e90602401600060405180830381865afa158015610768573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526107909190810190612b35565b905060005b815181101561083a578181815181106107b0576107b0612bc4565b602090810291909101015160405163da0b206360e01b81526001600160a01b0386811660048301529091169063da0b206390602401602060405180830381865afa158015610802573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906108269190612b1c565b6108309084612b09565b9250600101610795565b5050919050565b600054610100900460ff16158080156108615750600054600160ff909116105b8061087b5750303b15801561087b575060005460ff166001145b6108e35760405162461bcd60e51b815260206004820152602e60248201527f496e697469616c697a61626c653a20636f6e747261637420697320616c72656160448201526d191e481a5b9a5d1a585b1a5e995960921b60648201526084015b60405180910390fd5b6000805460ff191660011790558015610906576000805461ff0019166101001790555b6001600160a01b03821661092c5760405162461bcd60e51b81526004016108da90612bda565b66038d7ea4c6800034101561094057600080fd5b610948611e2a565b610988604051806040016040528060038152602001620414c560ec1b815250604051806040016040528060038152602001620414c560ec1b815250611e59565b609780546001600160a01b0319166001600160a01b0384161790556109ad3334611e8a565b6040516001600160a01b038316907f6b166bc981591e4d37f661da896b9fe0c8ed4c8306068fb296ec9c3cf2cf17e090600090a28015610a27576000805461ff0019169055604051600181527f7f26b83ff96e1f2b6a682f133852f6798a09c465da95921460cefb38474024989060200160405180910390a15b5050565b600033610a39858285611f4b565b610a44858585611fc5565b506001949350505050565b610a57612170565b6001600160a01b038116610a7d5760405162461bcd60e51b81526004016108da90612bda565b609780546001600160a01b0319166001600160a01b0383169081179091556040517f6b166bc981591e4d37f661da896b9fe0c8ed4c8306068fb296ec9c3cf2cf17e090600090a250565b60003361067b818585610ada8383611b07565b610ae49190612b09565b611d06565b610af1612170565b6001600160a01b038216610b3e5760405162461bcd60e51b8152602060048201526014602482015273696e76616c696420636f696e206164647265737360601b60448201526064016108da565b6001600160a01b038216600081815260986020526040808220849055518392917f0effed52874c24c39a6db6c1cbf7fccfb0171900f623ddf1463d142bdd65719c91a35050565b610b8d612170565b609a81905560405181907f9ddfcb9cd791bfaecb571c9b1b91e5542b76f96bdfcd288551351c5564e8d3ad90600090a250565b610bc8612170565b609754604051631432585160e31b81526001600160a01b0384811660048301529091169063a192c28890602401602060405180830381865afa158015610c12573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610c369190612c20565b610c825760405162461bcd60e51b815260206004820152601b60248201527f7374726174656779206973206e6f742077686974656c6973746564000000000060448201526064016108da565b6000826001600160a01b03168260405160006040518083038185875af1925050503d8060008114610ccf576040519150601f19603f3d011682016040523d82523d6000602084013e610cd4565b606091505b5050905080610d165760405162461bcd60e51b815260206004820152600e60248201526d11195c1bdcda5d0819985a5b195960921b60448201526064016108da565b60405182906001600160a01b038516907f9b931226d89fe9ed52bb22b49e697228de3f6d78c4f7fa69092763ded428119f90600090a3505050565b610d59612170565b610d6360006121ca565b565b6060606980546105ea90612ab9565b60003381610d828286611b07565b905083811015610de25760405162461bcd60e51b815260206004820152602560248201527f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f77604482015264207a65726f60d81b60648201526084016108da565b610a448286868403611d06565b60003361067b818585611fc5565b610e05612170565b6001600160a01b038316610e5b5760405162461bcd60e51b815260206004820152601860248201527f696e76616c69642073747261746567792061646472657373000000000000000060448201526064016108da565b609754604051631432585160e31b81526001600160a01b0385811660048301529091169063a192c28890602401602060405180830381865afa158015610ea5573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610ec99190612c20565b610f155760405162461bcd60e51b815260206004820152601b60248201527f7374726174656779206973206e6f742077686974656c6973746564000000000060448201526064016108da565b609754604051630f8486b760e11b81526001600160a01b0384811660048301526000921690631f090d6e90602401600060405180830381865afa158015610f60573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610f889190810190612b35565b905060005b8151811015610fdb57846001600160a01b0316828281518110610fb257610fb2612bc4565b60200260200101516001600160a01b03160315610fdb5780610fd381612c42565b915050610f8d565b8151810361103d5760405162461bcd60e51b815260206004820152602960248201527f70726f766964656420636f696e206973206e6f74207468652070617274206f6660448201526820737472617465677960b81b60648201526084016108da565b6110516001600160a01b038516868561221c565b5050505050565b609760009054906101000a90046001600160a01b03166001600160a01b031663f887ea406040518163ffffffff1660e01b8152600401602060405180830381865afa1580156110ab573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906110cf9190612c5b565b6001600160a01b0316336001600160a01b0316146110ff5760405162461bcd60e51b81526004016108da90612c78565b60006040518060a0016040528083604001518152602001306001600160a01b031681526020018360600151815260200183600001518152602001836020015181525090506000826040015183600001518151811061115f5761115f612bc4565b60200260200101516000015190506000816001600160a01b031663313ce5676040518163ffffffff1660e01b8152600401602060405180830381865afa1580156111ad573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906111d19190612caf565b60ff16905060006111e382600a612db6565b60408601518651815181106111fa576111fa612bc4565b602002602001015160200151866020015161121486610687565b61121e9190612b09565b6112289190612dc2565b6112329190612dd9565b6001600160a01b038416600090815260986020526040902054909150811061128f5760405162461bcd60e51b815260206004820152601060248201526f10dbda5b8818d85c081c995858da195960821b60448201526064016108da565b600061129c83600a612db6565b602087015160408801518851815181106112b8576112b8612bc4565b6020026020010151602001516112ce9190612dc2565b6112d89190612dd9565b609a5443600090815260996020526040902054919250906112f99083612b09565b1061133a5760405162461bcd60e51b8152602060048201526011602482015270109b1bd8dac818d85c081c995858da1959607a1b60448201526064016108da565b4360009081526099602052604081208054839290611359908490612b09565b92505081905550600080609760009054906101000a90046001600160a01b03166001600160a01b031663500b19e76040518163ffffffff1660e01b8152600401602060405180830381865afa1580156113b6573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906113da9190612c5b565b6001600160a01b0316631d6573dc886040518263ffffffff1660e01b81526004016114059190612dfb565b600060405180830381865afa158015611422573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f1916820160405261144a9190810190612e94565b9250509150600081670de0b6b3a7640000856114669190612dc2565b6114709190612dd9565b90506000670de0b6b3a764000061148660675490565b6114909084612dc2565b61149a9190612dd9565b90506000670de0b6b3a76400006114b18682612f68565b6114bb9084612dc2565b6114c59190612dd9565b90506114d13382611e8a565b5050505050505050505050565b609760009054906101000a90046001600160a01b03166001600160a01b031663f887ea406040518163ffffffff1660e01b8152600401602060405180830381865afa158015611531573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906115559190612c5b565b6001600160a01b0316336001600160a01b0316146115855760405162461bcd60e51b81526004016108da90612c78565b60006040518060a0016040528083604001518152602001306001600160a01b03168152602001836060015181526020018360000151815260200183602001518152509050600082604001518360000151815181106115e5576115e5612bc4565b60200260200101516000015190506000816001600160a01b031663313ce5676040518163ffffffff1660e01b8152600401602060405180830381865afa158015611633573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906116579190612caf565b60ff169050600061166982600a612db6565b6020860151604087015187518151811061168557611685612bc4565b60200260200101516020015161169b9190612dc2565b6116a59190612dd9565b609a5443600090815260996020526040902054919250906116c69083612b09565b106117075760405162461bcd60e51b8152602060048201526011602482015270109b1bd8dac818d85c081c995858da1959607a1b60448201526064016108da565b4360009081526099602052604081208054839290611726908490612b09565b92505081905550600080609760009054906101000a90046001600160a01b03166001600160a01b031663500b19e76040518163ffffffff1660e01b8152600401602060405180830381865afa158015611783573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906117a79190612c5b565b6001600160a01b031663879ce416876040518263ffffffff1660e01b81526004016117d29190612dfb565b600060405180830381865afa1580156117ef573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526118179190810190612e94565b9250509150600081670de0b6b3a7640000856118339190612dc2565b61183d9190612dd9565b90506000670de0b6b3a764000061185360675490565b61185d9084612dc2565b6118679190612dd9565b90506000670de0b6b3a764000061187e8682612f68565b6118889084612dc2565b6118929190612dd9565b905061189e3382612369565b8960200151609b60008a6001600160a01b03166001600160a01b0316815260200190815260200160002060008282546118d79190612b09565b909155505050505050505050505050565b6118f0612170565b6001600160a01b03831661193c5760405162461bcd60e51b815260206004820152601360248201527234b73b30b634b2103232b9ba34b730ba34b7b760691b60448201526064016108da565b609754604051630550ff9360e01b81526001600160a01b03858116600483015290911690630550ff9390602401602060405180830381865afa158015611986573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906119aa9190612c20565b6119f65760405162461bcd60e51b815260206004820152601e60248201527f64657374696e6174696f6e206973206e6f742077686974656c6973746564000060448201526064016108da565b6001600160a01b038216611aad576000836001600160a01b03168260405160006040518083038185875af1925050503d8060008114611a51576040519150601f19603f3d011682016040523d82523d6000602084013e611a56565b606091505b5050905080611aa75760405162461bcd60e51b815260206004820152601d60248201527f6465706f73697420746f2064657374696e6174696f6e206661696c656400000060448201526064016108da565b50611ac1565b611ac16001600160a01b038316848361249d565b80826001600160a01b0316846001600160a01b03167fb0850b8e0f9e8315dde3c9f9f31138283e6bbe16cd29e8552eb1dcdf9fac9e3b60405160405180910390a4505050565b6001600160a01b03918216600090815260666020908152604080832093909416825291909152205490565b609760009054906101000a90046001600160a01b03166001600160a01b031663f887ea406040518163ffffffff1660e01b8152600401602060405180830381865afa158015611b85573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611ba99190612c5b565b6001600160a01b0316336001600160a01b031614611bd95760405162461bcd60e51b81526004016108da90612c78565b6001600160a01b0382166000908152609b6020526040902054811115611c4b5760405162461bcd60e51b815260206004820152602160248201527f696e73756666696369656e74206465627420616d6f756e7420666f7220636f696044820152603760f91b60648201526084016108da565b6001600160a01b0382166000908152609b602052604081208054839290611c73908490612f8f565b90915550610a2790506001600160a01b038316338361249d565b611c95612170565b6001600160a01b038116611cfa5760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b60648201526084016108da565b611d03816121ca565b50565b6001600160a01b038316611d685760405162461bcd60e51b8152602060048201526024808201527f45524332303a20617070726f76652066726f6d20746865207a65726f206164646044820152637265737360e01b60648201526084016108da565b6001600160a01b038216611dc95760405162461bcd60e51b815260206004820152602260248201527f45524332303a20617070726f766520746f20746865207a65726f206164647265604482015261737360f01b60648201526084016108da565b6001600160a01b0383811660008181526066602090815260408083209487168084529482529182902085905590518481527f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925910160405180910390a3505050565b600054610100900460ff16611e515760405162461bcd60e51b81526004016108da90612fa2565b610d636124cd565b600054610100900460ff16611e805760405162461bcd60e51b81526004016108da90612fa2565b610a2782826124fd565b6001600160a01b038216611ee05760405162461bcd60e51b815260206004820152601f60248201527f45524332303a206d696e7420746f20746865207a65726f20616464726573730060448201526064016108da565b8060676000828254611ef29190612b09565b90915550506001600160a01b0382166000818152606560209081526040808320805486019055518481527fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef910160405180910390a35050565b6000611f578484611b07565b90506000198114611fbf5781811015611fb25760405162461bcd60e51b815260206004820152601d60248201527f45524332303a20696e73756666696369656e7420616c6c6f77616e636500000060448201526064016108da565b611fbf8484848403611d06565b50505050565b6001600160a01b0383166120295760405162461bcd60e51b815260206004820152602560248201527f45524332303a207472616e736665722066726f6d20746865207a65726f206164604482015264647265737360d81b60648201526084016108da565b6001600160a01b03821661208b5760405162461bcd60e51b815260206004820152602360248201527f45524332303a207472616e7366657220746f20746865207a65726f206164647260448201526265737360e81b60648201526084016108da565b6001600160a01b038316600090815260656020526040902054818110156121035760405162461bcd60e51b815260206004820152602660248201527f45524332303a207472616e7366657220616d6f756e7420657863656564732062604482015265616c616e636560d01b60648201526084016108da565b6001600160a01b0380851660008181526065602052604080822086860390559286168082529083902080548601905591517fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef906121639086815260200190565b60405180910390a3611fbf565b6033546001600160a01b03163314610d635760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657260448201526064016108da565b603380546001600160a01b038381166001600160a01b0319831681179093556040519116919082907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a35050565b8015806122965750604051636eb1769f60e11b81523060048201526001600160a01b03838116602483015284169063dd62ed3e90604401602060405180830381865afa158015612270573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906122949190612b1c565b155b6123015760405162461bcd60e51b815260206004820152603660248201527f5361666545524332303a20617070726f76652066726f6d206e6f6e2d7a65726f60448201527520746f206e6f6e2d7a65726f20616c6c6f77616e636560501b60648201526084016108da565b6040516001600160a01b03831660248201526044810182905261236490849063095ea7b360e01b906064015b60408051601f198184030181529190526020810180516001600160e01b03166001600160e01b03199093169290921790915261253d565b505050565b6001600160a01b0382166123c95760405162461bcd60e51b815260206004820152602160248201527f45524332303a206275726e2066726f6d20746865207a65726f206164647265736044820152607360f81b60648201526084016108da565b6001600160a01b0382166000908152606560205260409020548181101561243d5760405162461bcd60e51b815260206004820152602260248201527f45524332303a206275726e20616d6f756e7420657863656564732062616c616e604482015261636560f01b60648201526084016108da565b6001600160a01b03831660008181526065602090815260408083208686039055606780548790039055518581529192917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef910160405180910390a3505050565b6040516001600160a01b03831660248201526044810182905261236490849063a9059cbb60e01b9060640161232d565b600054610100900460ff166124f45760405162461bcd60e51b81526004016108da90612fa2565b610d63336121ca565b600054610100900460ff166125245760405162461bcd60e51b81526004016108da90612fa2565b6068612530838261303b565b506069612364828261303b565b6000612592826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b031661260f9092919063ffffffff16565b80519091501561236457808060200190518101906125b09190612c20565b6123645760405162461bcd60e51b815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e6044820152691bdd081cdd58d8d9595960b21b60648201526084016108da565b606061261e8484600085612626565b949350505050565b6060824710156126875760405162461bcd60e51b815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f6044820152651c8818d85b1b60d21b60648201526084016108da565b600080866001600160a01b031685876040516126a391906130fb565b60006040518083038185875af1925050503d80600081146126e0576040519150601f19603f3d011682016040523d82523d6000602084013e6126e5565b606091505b50915091506126f687838387612701565b979650505050505050565b60608315612770578251600003612769576001600160a01b0385163b6127695760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e747261637400000060448201526064016108da565b508161261e565b61261e83838151156127855781518083602001fd5b8060405162461bcd60e51b81526004016108da91906127c3565b60005b838110156127ba5781810151838201526020016127a2565b50506000910152565b60208152600082518060208401526127e281604085016020870161279f565b601f01601f19169190910160400192915050565b6001600160a01b0381168114611d0357600080fd5b6000806040838503121561281e57600080fd5b8235612829816127f6565b946020939093013593505050565b60006020828403121561284957600080fd5b8135612854816127f6565b9392505050565b60008060006060848603121561287057600080fd5b833561287b816127f6565b9250602084013561288b816127f6565b929592945050506040919091013590565b6000602082840312156128ae57600080fd5b5035919050565b634e487b7160e01b600052604160045260246000fd5b6040516080810167ffffffffffffffff811182821017156128ee576128ee6128b5565b60405290565b6040805190810167ffffffffffffffff811182821017156128ee576128ee6128b5565b604051601f8201601f1916810167ffffffffffffffff81118282101715612940576129406128b5565b604052919050565b600067ffffffffffffffff821115612962576129626128b5565b5060051b60200190565b6000602080838503121561297f57600080fd5b823567ffffffffffffffff8082111561299757600080fd5b90840190608082870312156129ab57600080fd5b6129b36128cb565b823581528383013584820152604080840135838111156129d257600080fd5b84019250601f830188136129e557600080fd5b82356129f86129f382612948565b612917565b81815260069190911b8401860190868101908a831115612a1757600080fd5b948701945b82861015612a605783868c031215612a345760008081fd5b612a3c6128f4565b8635612a47816127f6565b8152868901358982015282529483019490870190612a1c565b808486015250505050606083013560608201528094505050505092915050565b60008060408385031215612a9357600080fd5b8235612a9e816127f6565b91506020830135612aae816127f6565b809150509250929050565b600181811c90821680612acd57607f821691505b602082108103612aed57634e487b7160e01b600052602260045260246000fd5b50919050565b634e487b7160e01b600052601160045260246000fd5b8082018082111561068157610681612af3565b600060208284031215612b2e57600080fd5b5051919050565b60006020808385031215612b4857600080fd5b825167ffffffffffffffff811115612b5f57600080fd5b8301601f81018513612b7057600080fd5b8051612b7e6129f382612948565b81815260059190911b82018301908381019087831115612b9d57600080fd5b928401925b828410156126f6578351612bb5816127f6565b82529284019290840190612ba2565b634e487b7160e01b600052603260045260246000fd5b60208082526026908201527f5f61646472657373526567697374727920616464726573732063616e2774206260408201526565207a65726f60d01b606082015260800190565b600060208284031215612c3257600080fd5b8151801515811461285457600080fd5b600060018201612c5457612c54612af3565b5060010190565b600060208284031215612c6d57600080fd5b8151612854816127f6565b60208082526019908201527f6f6e6c7920726f7574657220686173207065726d697474656400000000000000604082015260600190565b600060208284031215612cc157600080fd5b815160ff8116811461285457600080fd5b600181815b80851115612d0d578160001904821115612cf357612cf3612af3565b80851615612d0057918102915b93841c9390800290612cd7565b509250929050565b600082612d2457506001610681565b81612d3157506000610681565b8160018114612d475760028114612d5157612d6d565b6001915050610681565b60ff841115612d6257612d62612af3565b50506001821b610681565b5060208310610133831016604e8410600b8410161715612d90575081810a610681565b612d9a8383612cd2565b8060001904821115612dae57612dae612af3565b029392505050565b60006128548383612d15565b808202811582820484141761068157610681612af3565b600082612df657634e487b7160e01b600052601260045260246000fd5b500490565b6020808252825160a083830152805160c0840181905260009291820190839060e08601905b80831015612e5457835180516001600160a01b03168352850151858301529284019260019290920191604090910190612e20565b50928601516001600160a01b0381166040870152926040870151606087015260608701516080870152608087015160a08701528094505050505092915050565b600080600060608486031215612ea957600080fd5b8351925060208085015167ffffffffffffffff811115612ec857600080fd5b8501601f81018713612ed957600080fd5b8051612ee76129f382612948565b81815260069190911b82018301908381019089831115612f0657600080fd5b928401925b82841015612f52576040848b031215612f245760008081fd5b612f2c6128f4565b8451612f37816127f6565b81528486015186820152825260409093019290840190612f0b565b8096505050505050604084015190509250925092565b8181036000831280158383131683831282161715612f8857612f88612af3565b5092915050565b8181038181111561068157610681612af3565b6020808252602b908201527f496e697469616c697a61626c653a20636f6e7472616374206973206e6f74206960408201526a6e697469616c697a696e6760a81b606082015260800190565b601f82111561236457600081815260208120601f850160051c810160208610156130145750805b601f850160051c820191505b8181101561303357828155600101613020565b505050505050565b815167ffffffffffffffff811115613055576130556128b5565b613069816130638454612ab9565b84612fed565b602080601f83116001811461309e57600084156130865750858301515b600019600386901b1c1916600185901b178555613033565b600085815260208120601f198616915b828110156130cd578886015182559484019460019091019084016130ae565b50858210156130eb5787850151600019600388901b60f8161c191681555b5050505050600190811b01905550565b6000825161310d81846020870161279f565b919091019291505056fea26469706673582212208795b03491acca6e48ba35debb934f455745f88c9cb21df42864d1037c612b8764736f6c63430008110033

Block Transaction Difficulty Gas Used Reward
View All Blocks Produced

Block Uncle Number Difficulty Gas Used Reward
View All Uncles
Loading...
Loading
Loading...
Loading
Loading...
Loading

Validator Index Block Amount
View All Withdrawals

Transaction Hash Block Value Eth2 PubKey Valid
View All Deposits
Loading...
Loading
[ Download: CSV Export  ]

A contract address hosts a smart contract, which is a set of code stored on the blockchain that runs when predetermined conditions are met. Learn more about addresses in our Knowledge Base.