Skip to content
Verified contractPartialUNLICENSED

CurveMethods

Address0xf7dbfd129f7b68fcf28ea5e5523d8ff592408f3d
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.21

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.21+commit.d9974bed
Language
solidity
Optimization
Yes, 200 runs
EVM version
paris
Via IR
yes
License
UNLICENSED
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
23,537,647
Creation transaction
0x73b3240fe1…2ced4b9211

contracts/libraries/CurveMethods.sol

/* SPDX-License-Identifier: UNLICENSED */
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import '../curve/ICurvePool.sol';
import '../curve/IGaugeReward.sol';
import '../curve/ILiquidityGaugeV2.sol';
import '../IPositionManager.sol';

library CurveMethods {
    address constant public ETH_COIN = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
    
    function _calcWithdrawOneCoin(address _position, address _pool, address _coin, uint _lpt_amount) public view returns (uint) {
        IPositionManager p = IPositionManager(_position);
        if (p.use_uint_indexes(_pool))
              return ICurvePool(_pool).calc_withdraw_one_coin(_lpt_amount, p.coinIndex(_pool, _coin));
        return ICurvePool(_pool).calc_withdraw_one_coin(_lpt_amount, int128(int(p.coinIndex(_pool, _coin))));
    }

    /// @notice View function to get balance of the position manager in a given token or ETH.
    /// @param _a Address to get balance of position manager.
    /// @return _balance_of balance of the position manager
    function currentBalance(address _a, address _token) public view returns(uint) {
        if (_token == ETH_COIN)
            return _a.balance;
        return IERC20(_token).balanceOf(_a);
    }

    /// @notice View function to get underlyings addresses
    /// @return assets all underlying assets
    function getPositionAssets(address _position, bool _isMultipleCoins, bool _isMetapool) public view returns(address[] memory assets) {
        IPositionManager p = IPositionManager(_position);
        PositionConfig memory position_config = p.position_config();
        assets = new address[](_isMultipleCoins ? p.getCoins(position_config.pool).length : 1);

        if (_isMultipleCoins)
            assets = p.getCoins(position_config.pool);
        else
            assets[0] = position_config.coin;

        for (uint256 i = 0; i < assets.length; i++) {
            if (_isMetapool && assets[i] == p.lpts(position_config.meta_pool.base_pool))
                assets[i] = position_config.meta_pool.base_pool_coin;
            else
                assets[i] = p.getUnderlyingCoin(assets[i]);
        }

        for (uint256 i = 0; i < assets.length; i++)
            if (assets[i] == p.ETH_COIN() && position_config.useWrap)
                assets[i] = p.WNATIVE();
    }

    /// @notice Get underlyings address and amounts by lpt_amount given
    /// @return assets all underlying assets
    /// @return amounts amounts of all underlying assets
    function getUnderlyingAmountsFromLPT(address _position, bool _isMultipleCoins, bool _isMetapool, uint lpt_amount) public view returns (address[] memory assets, uint256[] memory amounts) {
        IPositionManager p = IPositionManager(_position);
        PositionConfig memory position_config = p.position_config();
        assets = getPositionAssets(_position, _isMultipleCoins, _isMetapool);
        amounts = new uint[](assets.length);
        if (lpt_amount != 0) {
            if (_isMultipleCoins) {
                uint total_lpt = IERC20(p.lpts(position_config.pool)).totalSupply();

                for (uint i = 0; i < p.getCoins(position_config.pool).length; i++) {
                    uint balance = ICurvePool(position_config.pool).balances(i);
                    amounts[i] = balance *  lpt_amount / total_lpt;
                    if (_isMetapool && p.lpts(position_config.meta_pool.base_pool) == p.getCoins(position_config.pool)[i])
                        amounts[i] = _calcWithdrawOneCoin(_position, position_config.meta_pool.base_pool, position_config.meta_pool.base_pool_coin, amounts[i]);
                }
            }
            else if (_isMetapool) {
                uint base_lpt_amount = _calcWithdrawOneCoin(_position, position_config.pool, p.lpts(position_config.meta_pool.base_pool), lpt_amount);
                amounts[0] = _calcWithdrawOneCoin(_position, position_config.meta_pool.base_pool, position_config.meta_pool.base_pool_coin, base_lpt_amount);
            }
            else 
                amounts[0] = _calcWithdrawOneCoin(_position, position_config.pool, position_config.coin, lpt_amount);
        }
    }

    /// @notice Get all rewards tokens in the gauge given by position_config
    /// @return rewards_addresses
    function getRewardsTokens(address _position) public view returns(address[] memory rewards_addresses) {
        IPositionManager p = IPositionManager(_position);
        PositionConfig memory position_config = p.position_config();

        uint reward_length = p.getRewardLength();
        rewards_addresses = new address[](reward_length);

        if (reward_length == 0)
            return rewards_addresses;

        rewards_addresses[0] = p.CRV_ADDRESS();
        for (uint i = 1; i < reward_length; i++) 
            rewards_addresses[i] = position_config.gauge_version == GaugeVersion.REWARD ? IGaugeReward(position_config.gauge).rewarded_token() : ILiquidityGaugeV2(position_config.gauge).reward_tokens(i - 1);
    }

    /// @notice Get underlyings address and amounts
    /// @return assets all underlying assets
    /// @return amounts amounts of all underlying assets
    function getUnderlyings(address _position) public view returns(address[] memory assets, uint256[] memory amounts) {
        IPositionManager p = IPositionManager(_position);
        uint lpt_amount = p.totalLPTBalance();
        return p.getUnderlyingAmountsFromLPT(lpt_amount);
    }

    /// @notice Get reward length in the gauge given by position_config
    /// @return reward_length  
    function getRewardLength(address _position) public view returns (uint) {
        PositionConfig memory position_config = IPositionManager(_position).position_config();
        if (position_config.gauge_version == GaugeVersion.NULLGAUGE)
            return 0;

        if (shouldSkipWrongRewardToken(position_config.gauge))
            return 1;

        if (position_config.gauge_version == GaugeVersion.V1)
            return 1;

        if (position_config.gauge_version == GaugeVersion.REWARD) {
            if (IGaugeReward(position_config.gauge).rewarded_token() != address(0))
                return 2;
            return 1;
        }

        if (position_config.gauge_version == GaugeVersion.V4 || position_config.gauge_version == GaugeVersion.VSidechain || position_config.gauge_version == GaugeVersion.V6)
            return 1 + ILiquidityGaugeV2(position_config.gauge).reward_count();

        uint reward_length = 1;    
        for (uint i = 0; i < 8; i++) {
            if (ILiquidityGaugeV2(position_config.gauge).reward_tokens(i) == address(0))
                break;
            reward_length++;
        }
        return reward_length;
    }

    /* TODO: TEMP ignore wrong gauge configuration for Base */
    function shouldSkipWrongRewardToken(address _gauge) public pure returns (bool) {
        return
            _gauge == 0x6616CCfA456F72104452341fa470E9B08A11b1aB ||
            _gauge == 0x93933FA992927284e9d508339153B31eb871e1f4 ||
            _gauge == 0x89289DC2192914a9F0674f1E9A17C56456549b8A ||
            _gauge == 0xE9c898BA654deC2bA440392028D2e7A194E6dc3e ||
            _gauge == 0x79edc58C471Acf2244B8f93d6f425fD06A439407;
    }
}

@openzeppelin/contracts/token/ERC20/IERC20.sol

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

pragma solidity >=0.4.16;

/**
 * @dev Interface of the ERC-20 standard as defined in the ERC.
 */
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 value of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

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

    /**
     * @dev Moves a `value` amount of 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 value) 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 a `value` amount of tokens 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 value) external returns (bool);

    /**
     * @dev Moves a `value` amount of tokens from `from` to `to` using the
     * allowance mechanism. `value` 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 value) external returns (bool);
}

contracts/IPositionManager.sol

/* SPDX-License-Identifier: UNLICENSED */
pragma solidity ^0.8.0;

import './convex/IConvexRewards.sol';

struct MetaPool {
    bool is_meta_pool;
    address base_pool;
    address base_pool_coin;
}

enum GaugeVersion { V1, V2, V3, REWARD, VSidechain, V4, V6, NULLGAUGE }
    
struct PositionConfig {
    address pool;
    address gauge;
    GaugeVersion gauge_version;
    address coin;
    MetaPool meta_pool;
    bool useWrap;
}

interface IPositionManager {
  function cvx_booster() external view returns (address);

  function pool_cvx_id() external view returns (uint);

  function currentBalance(address _token) external view returns (uint);

  function CRV_ADDRESS() external view returns (address);

  function CVX_ADDRESS() external view returns (address);

  function ETH_COIN() external view returns (address);

  function WNATIVE() external view returns (address);

  function lpts(address _pool) external view returns (address);
  function getCoins(address _pool) external view returns (address[] memory);
  function use_uint_indexes(address _pool) external view returns (bool);

  function coinIndex(address _pool, address _coin) external view returns (uint);

  function position_config() external view returns (PositionConfig memory);

  function getUnderlyingCoin(address _coin) external view returns (address);

  function getUnderlyingAmountsFromLPT(uint _lpt_amount) external view returns (address[] memory, uint256[] memory);

  function stakedLPTBalance() external view returns (uint);

  function totalLPTBalance() external view returns (uint);

  function rewardsAddress() external view returns (address);

  function rewardsContract() external view returns (IConvexRewards);

  function getRewardLength() external view returns (uint);
}

contracts/convex/IConvexRewards.sol

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


//sample convex reward contracts interface
interface IConvexRewards{
    function withdrawAndUnwrap(uint256 _amount, bool _claim) external returns(bool);

    function getReward(address _account, bool _claimExtras) external returns(bool);

    function rewardToken() external view returns (address);

    function extraRewardsLength() external view returns (uint);

    function extraRewards(uint _index) external view returns (address _child_rewards_contract);

    /* Sidechain */

    function withdraw(uint256 _amount, bool _claim) external returns(bool);

    function getReward(address _account) external;

    function rewardLength() external view returns (uint);

    struct RewardType {
        address reward_token;
        uint256 reward_integral;
        uint256 reward_remaining;
    }

    function rewards(uint _index) external view returns (RewardType memory);
}

contracts/curve/ICurvePool.sol

/* SPDX-License-Identifier: UNLICENSED */
pragma solidity ^0.8.0;

interface ICurvePool {
  function calc_token_amount(uint[2] memory _amounts, bool _is_deposit) external view returns(uint);
  function calc_token_amount(uint[3] memory _amounts, bool _is_deposit) external view returns(uint);
  function calc_token_amount(uint[4] memory _amounts, bool _is_deposit) external view returns(uint);
  function balances(uint) external view returns(uint);
  function coins(uint) external view returns(address);

  function add_liquidity(uint[2] memory _amounts, uint _min_mint_amount) payable external;
  function add_liquidity(uint[3] memory _amounts, uint _min_mint_amount) payable external;
  function add_liquidity(uint[4] memory _amounts, uint _min_mint_amount) payable external;
  function add_liquidity(uint[2] memory _amounts, uint _min_mint_amount, bool _use_underlying) payable external;
  function add_liquidity(uint[3] memory _amounts, uint _min_mint_amount, bool _use_underlying) payable external;
  function add_liquidity(uint[4] memory _amounts, uint _min_mint_amount, bool _use_underlying) payable external;

  function remove_liquidity_one_coin(uint _token_amount, int128 i, uint _min_amount) external;
  function remove_liquidity_one_coin(uint _token_amount, uint i, uint _min_amount) external;
  function remove_liquidity_one_coin(uint _token_amount, int128 i, uint _min_amount, bool _use_underlying) external;
  function remove_liquidity_one_coin(uint _token_amount, uint i, uint _min_amount, bool _use_underlying) external;

  function remove_liquidity(uint _amount, uint[2] memory _min_amounts) external;
  function remove_liquidity(uint _amount, uint[3] memory _min_amounts) external;
  function remove_liquidity(uint _amount, uint[4] memory _min_amounts) external;
  function remove_liquidity(uint _amount, uint[2] memory _min_amounts, bool _use_underlying) external;
  function remove_liquidity(uint _amount, uint[3] memory _min_amounts, bool _use_underlying) external;
  function remove_liquidity(uint _amount, uint[4] memory _min_amounts, bool _use_underlying) external;

  function calc_withdraw_one_coin(uint _token_amount, int128 i) external view returns(uint);
  function calc_withdraw_one_coin(uint _token_amount, uint i) external view returns(uint);

  function get_dy(int128 i, int128 j, uint dx) external view returns(uint);
  function get_dy(uint i, uint j, uint dx) external view returns(uint);

  function exchange(int128 i, int128 j, uint dx, uint min_dy) external;
  function exchange(uint i, uint j, uint dx, uint min_dy) external;
}

contracts/curve/IGaugeReward.sol

/* SPDX-License-Identifier: UNLICENSED */
pragma solidity ^0.8.0;

interface IGaugeReward {
  function rewarded_token() external view returns(address);
}

contracts/curve/ILiquidityGaugeV2.sol

/* SPDX-License-Identifier: UNLICENSED */
pragma solidity ^0.8.0;

interface ILiquidityGaugeV2 {
  function deposit(uint) external;

  function withdraw(uint) external;

  function claim_rewards(address) external;
  
  function claim_rewards() external;

  function reward_tokens(uint) external view returns(address);
  
  function reward_count() external view returns(uint);
  
  function crv_token() external view returns(address);
}
Compiler settingsShow
{
  "viaIR": true,
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "paris"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.