编译器
0.8.17+commit.8df45f5f
文件 1 的 34:Address.sol
pragma solidity ^0.8.1;
library Address {
function isContract(address account) internal view returns (bool) {
return account.code.length > 0;
}
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");
}
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, "Address: low-level call failed");
}
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
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");
}
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);
}
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
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);
}
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
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);
}
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata,
string memory errorMessage
) internal view returns (bytes memory) {
if (success) {
if (returndata.length == 0) {
require(isContract(target), "Address: call to non-contract");
}
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
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 {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
文件 2 的 34:ArrayExtensions.sol
pragma solidity 0.8.17;
library ArrayExtensions {
function copy(uint256[] memory array) internal pure returns (uint256[] memory) {
uint256[] memory copy_ = new uint256[](array.length);
for (uint256 i = 0; i < array.length; i++) {
copy_[i] = array[i];
}
return copy_;
}
}
文件 3 的 34:ConicPoolV2.sol
pragma solidity 0.8.17;
import "Ownable.sol";
import "ERC20.sol";
import "Address.sol";
import "EnumerableSet.sol";
import "EnumerableMap.sol";
import "IERC20.sol";
import "SafeERC20.sol";
import "IERC20Metadata.sol";
import "IConicPool.sol";
import "IRewardManager.sol";
import "ICurveHandler.sol";
import "ICurveRegistryCache.sol";
import "IInflationManager.sol";
import "ILpTokenStaker.sol";
import "IConvexHandler.sol";
import "IOracle.sol";
import "IBaseRewardPool.sol";
import "LpToken.sol";
import "RewardManagerV2.sol";
import "ScaledMath.sol";
import "ArrayExtensions.sol";
contract ConicPoolV2 is IConicPool, Ownable {
using ArrayExtensions for uint256[];
using EnumerableSet for EnumerableSet.AddressSet;
using EnumerableMap for EnumerableMap.AddressToUintMap;
using SafeERC20 for IERC20;
using SafeERC20 for IERC20Metadata;
using SafeERC20 for ILpToken;
using ScaledMath for uint256;
using Address for address;
struct DepositVars {
uint256 exchangeRate;
uint256 underlyingBalanceIncrease;
uint256 mintableUnderlyingAmount;
uint256 lpReceived;
uint256 underlyingBalanceBefore;
uint256 allocatedBalanceBefore;
uint256[] allocatedPerPoolBefore;
uint256 underlyingBalanceAfter;
uint256 allocatedBalanceAfter;
uint256[] allocatedPerPoolAfter;
}
uint256 internal constant _IDLE_RATIO_UPPER_BOUND = 0.2e18;
uint256 internal constant _MIN_DEPEG_THRESHOLD = 0.01e18;
uint256 internal constant _MAX_DEPEG_THRESHOLD = 0.1e18;
uint256 internal constant _MAX_DEVIATION_UPPER_BOUND = 0.2e18;
uint256 internal constant _DEPEG_UNDERLYING_MULTIPLIER = 2;
uint256 internal constant _TOTAL_UNDERLYING_CACHE_EXPIRY = 3 days;
uint256 internal constant _MAX_USD_LP_VALUE_FOR_REMOVING_CURVE_POOL = 100e18;
IERC20 public immutable CVX;
IERC20 public immutable CRV;
IERC20 public constant CNC = IERC20(0x9aE380F0272E2162340a5bB646c354271c0F5cFC);
address internal constant _WETH_ADDRESS = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
IERC20Metadata public immutable override underlying;
ILpToken public immutable override lpToken;
IRewardManager public immutable rewardManager;
IController public immutable controller;
uint256 public maxDeviation = 0.02e18;
uint256 public maxIdleCurveLpRatio = 0.05e18;
bool public isShutdown;
uint256 public depegThreshold = 0.03e18;
uint256 internal _cacheUpdatedTimestamp;
uint256 internal _cachedTotalUnderlying;
bool public rebalancingRewardActive;
EnumerableSet.AddressSet internal _curvePools;
EnumerableMap.AddressToUintMap internal weights;
uint256 public totalDeviationAfterWeightUpdate;
mapping(address => uint256) _cachedPrices;
modifier onlyController() {
require(msg.sender == address(controller), "not authorized");
_;
}
constructor(
address _underlying,
IRewardManager _rewardManager,
address _controller,
string memory _lpTokenName,
string memory _symbol,
address _cvx,
address _crv
) {
require(
_underlying != _cvx && _underlying != _crv && _underlying != address(CNC),
"invalid underlying"
);
underlying = IERC20Metadata(_underlying);
controller = IController(_controller);
uint8 decimals = IERC20Metadata(_underlying).decimals();
lpToken = new LpToken(address(this), decimals, _lpTokenName, _symbol);
rewardManager = _rewardManager;
CVX = IERC20(_cvx);
CRV = IERC20(_crv);
CVX.safeApprove(address(_rewardManager), type(uint256).max);
CRV.safeApprove(address(_rewardManager), type(uint256).max);
CNC.safeApprove(address(_rewardManager), type(uint256).max);
}
receive() external payable {
require(address(underlying) == _WETH_ADDRESS, "not WETH pool");
}
function depositFor(
address account,
uint256 underlyingAmount,
uint256 minLpReceived,
bool stake
) public override returns (uint256) {
DepositVars memory vars;
require(!isShutdown, "pool is shutdown");
require(underlyingAmount > 0, "deposit amount cannot be zero");
uint256 underlyingPrice_ = controller.priceOracle().getUSDPrice(address(underlying));
(
vars.underlyingBalanceBefore,
vars.allocatedBalanceBefore,
vars.allocatedPerPoolBefore
) = _getTotalAndPerPoolUnderlying(underlyingPrice_);
vars.exchangeRate = _exchangeRate(vars.underlyingBalanceBefore);
underlying.safeTransferFrom(msg.sender, address(this), underlyingAmount);
_depositToCurve(
vars.allocatedBalanceBefore,
vars.allocatedPerPoolBefore,
underlying.balanceOf(address(this))
);
(
vars.underlyingBalanceAfter,
vars.allocatedBalanceAfter,
vars.allocatedPerPoolAfter
) = _getTotalAndPerPoolUnderlying(underlyingPrice_);
vars.underlyingBalanceIncrease = vars.underlyingBalanceAfter - vars.underlyingBalanceBefore;
vars.mintableUnderlyingAmount = _min(underlyingAmount, vars.underlyingBalanceIncrease);
vars.lpReceived = vars.mintableUnderlyingAmount.divDown(vars.exchangeRate);
require(vars.lpReceived >= minLpReceived, "too much slippage");
if (stake) {
lpToken.mint(address(this), vars.lpReceived);
ILpTokenStaker lpTokenStaker = controller.lpTokenStaker();
lpToken.safeApprove(address(lpTokenStaker), vars.lpReceived);
lpTokenStaker.stakeFor(vars.lpReceived, address(this), account);
} else {
lpToken.mint(account, vars.lpReceived);
}
_handleRebalancingRewards(
account,
vars.allocatedBalanceBefore,
vars.allocatedPerPoolBefore,
vars.allocatedBalanceAfter,
vars.allocatedPerPoolAfter
);
_cachedTotalUnderlying = vars.underlyingBalanceAfter;
_cacheUpdatedTimestamp = block.timestamp;
emit Deposit(msg.sender, account, underlyingAmount, vars.lpReceived);
return vars.lpReceived;
}
function deposit(
uint256 underlyingAmount,
uint256 minLpReceived
) external override returns (uint256) {
return depositFor(msg.sender, underlyingAmount, minLpReceived, true);
}
function deposit(
uint256 underlyingAmount,
uint256 minLpReceived,
bool stake
) external override returns (uint256) {
return depositFor(msg.sender, underlyingAmount, minLpReceived, stake);
}
function _depositToCurve(
uint256 totalUnderlying_,
uint256[] memory allocatedPerPool,
uint256 underlyingAmount_
) internal {
uint256 depositsRemaining_ = underlyingAmount_;
uint256 totalAfterDeposit_ = totalUnderlying_ + underlyingAmount_;
uint256[] memory allocatedPerPoolCopy = allocatedPerPool.copy();
while (depositsRemaining_ > 0) {
(uint256 curvePoolIndex_, uint256 maxDeposit_) = _getDepositPool(
totalAfterDeposit_,
allocatedPerPoolCopy
);
if (depositsRemaining_ < maxDeposit_ + 1e2) {
maxDeposit_ = depositsRemaining_;
}
address curvePool_ = _curvePools.at(curvePoolIndex_);
uint256 toDeposit_ = _min(depositsRemaining_, maxDeposit_);
_depositToCurvePool(curvePool_, toDeposit_);
depositsRemaining_ -= toDeposit_;
allocatedPerPoolCopy[curvePoolIndex_] += toDeposit_;
}
}
function _getDepositPool(
uint256 totalUnderlying_,
uint256[] memory allocatedPerPool
) internal view returns (uint256 poolIndex, uint256 maxDepositAmount) {
uint256 curvePoolCount_ = allocatedPerPool.length;
int256 iPoolIndex = -1;
for (uint256 i; i < curvePoolCount_; i++) {
address curvePool_ = _curvePools.at(i);
uint256 allocatedUnderlying_ = allocatedPerPool[i];
uint256 targetAllocation_ = totalUnderlying_.mulDown(weights.get(curvePool_));
if (allocatedUnderlying_ >= targetAllocation_) continue;
uint256 maxBalance_ = targetAllocation_ + targetAllocation_.mulDown(_getMaxDeviation());
uint256 maxDepositAmount_ = maxBalance_ - allocatedUnderlying_;
if (maxDepositAmount_ <= maxDepositAmount) continue;
maxDepositAmount = maxDepositAmount_;
iPoolIndex = int256(i);
}
require(iPoolIndex > -1, "error retrieving deposit pool");
poolIndex = uint256(iPoolIndex);
}
function _depositToCurvePool(address curvePool_, uint256 underlyingAmount_) internal {
if (underlyingAmount_ == 0) return;
controller.curveHandler().functionDelegateCall(
abi.encodeWithSignature(
"deposit(address,address,uint256)",
curvePool_,
underlying,
underlyingAmount_
)
);
uint256 idleCurveLpBalance_ = _idleCurveLpBalance(curvePool_);
uint256 totalCurveLpBalance_ = _stakedCurveLpBalance(curvePool_) + idleCurveLpBalance_;
if (idleCurveLpBalance_.divDown(totalCurveLpBalance_) >= maxIdleCurveLpRatio) {
controller.convexHandler().functionDelegateCall(
abi.encodeWithSignature("deposit(address,uint256)", curvePool_, idleCurveLpBalance_)
);
}
}
function totalUnderlying() public view virtual returns (uint256) {
(uint256 totalUnderlying_, , ) = getTotalAndPerPoolUnderlying();
return totalUnderlying_;
}
function _exchangeRate(uint256 totalUnderlying_) internal view returns (uint256) {
uint256 lpSupply = lpToken.totalSupply();
if (lpSupply == 0 || totalUnderlying_ == 0) return ScaledMath.ONE;
return totalUnderlying_.divDown(lpSupply);
}
function exchangeRate() public view virtual override returns (uint256) {
return _exchangeRate(totalUnderlying());
}
function usdExchangeRate() external view virtual override returns (uint256) {
uint256 underlyingPrice = controller.priceOracle().getUSDPrice(address(underlying));
return _exchangeRate(_cachedTotalUnderlying).mulDown(underlyingPrice);
}
function unstakeAndWithdraw(
uint256 conicLpAmount,
uint256 minUnderlyingReceived
) external returns (uint256) {
controller.lpTokenStaker().unstakeFrom(conicLpAmount, msg.sender);
return withdraw(conicLpAmount, minUnderlyingReceived);
}
function withdraw(
uint256 conicLpAmount,
uint256 minUnderlyingReceived
) public override returns (uint256) {
require(lpToken.balanceOf(msg.sender) >= conicLpAmount, "insufficient balance");
uint256 underlyingBalanceBefore_ = underlying.balanceOf(address(this));
(
uint256 totalUnderlying_,
uint256 allocatedUnderlying_,
uint256[] memory allocatedPerPool
) = getTotalAndPerPoolUnderlying();
uint256 underlyingToReceive_ = conicLpAmount.mulDown(_exchangeRate(totalUnderlying_));
{
if (underlyingBalanceBefore_ < underlyingToReceive_) {
uint256 underlyingToWithdraw_ = underlyingToReceive_ - underlyingBalanceBefore_;
_withdrawFromCurve(allocatedUnderlying_, allocatedPerPool, underlyingToWithdraw_);
}
}
uint256 underlyingWithdrawn_ = _min(
underlying.balanceOf(address(this)),
underlyingToReceive_
);
require(underlyingWithdrawn_ >= minUnderlyingReceived, "too much slippage");
lpToken.burn(msg.sender, conicLpAmount);
underlying.safeTransfer(msg.sender, underlyingWithdrawn_);
_cachedTotalUnderlying = totalUnderlying_ - underlyingWithdrawn_;
_cacheUpdatedTimestamp = block.timestamp;
emit Withdraw(msg.sender, underlyingWithdrawn_);
return underlyingWithdrawn_;
}
function _withdrawFromCurve(
uint256 totalUnderlying_,
uint256[] memory allocatedPerPool,
uint256 amount_
) internal {
uint256 withdrawalsRemaining_ = amount_;
uint256 totalAfterWithdrawal_ = totalUnderlying_ - amount_;
uint256[] memory allocatedPerPoolCopy = allocatedPerPool.copy();
while (withdrawalsRemaining_ > 0) {
(uint256 curvePoolIndex_, uint256 maxWithdrawal_) = _getWithdrawPool(
totalAfterWithdrawal_,
allocatedPerPoolCopy
);
address curvePool_ = _curvePools.at(curvePoolIndex_);
uint256 toWithdraw_ = _min(withdrawalsRemaining_, maxWithdrawal_);
_withdrawFromCurvePool(curvePool_, toWithdraw_);
withdrawalsRemaining_ -= toWithdraw_;
allocatedPerPoolCopy[curvePoolIndex_] -= toWithdraw_;
}
}
function _getWithdrawPool(
uint256 totalUnderlying_,
uint256[] memory allocatedPerPool
) internal view returns (uint256 withdrawPoolIndex, uint256 maxWithdrawalAmount) {
uint256 curvePoolCount_ = allocatedPerPool.length;
int256 iWithdrawPoolIndex = -1;
for (uint256 i; i < curvePoolCount_; i++) {
address curvePool_ = _curvePools.at(i);
uint256 weight_ = weights.get(curvePool_);
uint256 allocatedUnderlying_ = allocatedPerPool[i];
if (weight_ == 0) {
uint256 price_ = controller.priceOracle().getUSDPrice(address(underlying));
uint256 allocatedUsd = (price_ * allocatedUnderlying_) /
10 ** underlying.decimals();
if (allocatedUsd >= _MAX_USD_LP_VALUE_FOR_REMOVING_CURVE_POOL / 2) {
return (uint256(i), allocatedUnderlying_);
}
}
uint256 targetAllocation_ = totalUnderlying_.mulDown(weight_);
if (allocatedUnderlying_ <= targetAllocation_) continue;
uint256 minBalance_ = targetAllocation_ - targetAllocation_.mulDown(_getMaxDeviation());
uint256 maxWithdrawalAmount_ = allocatedUnderlying_ - minBalance_;
if (maxWithdrawalAmount_ <= maxWithdrawalAmount) continue;
maxWithdrawalAmount = maxWithdrawalAmount_;
iWithdrawPoolIndex = int256(i);
}
require(iWithdrawPoolIndex > -1, "error retrieving withdraw pool");
withdrawPoolIndex = uint256(iWithdrawPoolIndex);
}
function _withdrawFromCurvePool(address curvePool_, uint256 underlyingAmount_) internal {
ICurveRegistryCache registryCache_ = controller.curveRegistryCache();
address curveLpToken_ = registryCache_.lpToken(curvePool_);
uint256 lpToWithdraw_ = _underlyingToCurveLp(curveLpToken_, underlyingAmount_);
if (lpToWithdraw_ == 0) return;
uint256 idleCurveLpBalance_ = _idleCurveLpBalance(curvePool_);
address rewardPool = registryCache_.getRewardPool(curvePool_);
uint256 stakedLpBalance = IBaseRewardPool(rewardPool).balanceOf(address(this));
uint256 totalAvailableLp = idleCurveLpBalance_ + stakedLpBalance;
if (totalAvailableLp < lpToWithdraw_) {
lpToWithdraw_ = totalAvailableLp;
}
if (lpToWithdraw_ > idleCurveLpBalance_) {
controller.convexHandler().functionDelegateCall(
abi.encodeWithSignature(
"withdraw(address,uint256)",
curvePool_,
lpToWithdraw_ - idleCurveLpBalance_
)
);
}
controller.curveHandler().functionDelegateCall(
abi.encodeWithSignature(
"withdraw(address,address,uint256)",
curvePool_,
underlying,
lpToWithdraw_
)
);
}
function allCurvePools() external view override returns (address[] memory) {
return _curvePools.values();
}
function curvePoolsCount() external view override returns (uint256) {
return _curvePools.length();
}
function getCurvePoolAtIndex(uint256 _index) external view returns (address) {
return _curvePools.at(_index);
}
function isRegisteredCurvePool(address _pool) public view returns (bool) {
return _curvePools.contains(_pool);
}
function getPoolWeight(address _pool) external view returns (uint256) {
(, uint256 _weight) = weights.tryGet(_pool);
return _weight;
}
function addCurvePool(address _pool) external override onlyOwner {
require(!_curvePools.contains(_pool), "pool already added");
ICurveRegistryCache curveRegistryCache_ = controller.curveRegistryCache();
curveRegistryCache_.initPool(_pool);
bool supported_ = curveRegistryCache_.hasCoinAnywhere(_pool, address(underlying));
require(supported_, "coin not in pool");
address curveLpToken = curveRegistryCache_.lpToken(_pool);
require(controller.priceOracle().isTokenSupported(curveLpToken), "cannot price LP Token");
address booster = controller.convexBooster();
IERC20(curveLpToken).safeApprove(booster, type(uint256).max);
if (!weights.contains(_pool)) weights.set(_pool, 0);
require(_curvePools.add(_pool), "failed to add pool");
emit CurvePoolAdded(_pool);
}
function removeCurvePool(address _pool) external override onlyOwner {
require(_curvePools.contains(_pool), "pool not added");
require(_curvePools.length() > 1, "cannot remove last pool");
address curveLpToken = controller.curveRegistryCache().lpToken(_pool);
uint256 lpTokenPrice = controller.priceOracle().getUSDPrice(curveLpToken);
uint256 usdLpValue = totalCurveLpBalance(_pool).mulDown(lpTokenPrice);
require(usdLpValue < _MAX_USD_LP_VALUE_FOR_REMOVING_CURVE_POOL, "pool has allocated funds");
uint256 weight = weights.get(_pool);
IERC20(curveLpToken).safeApprove(controller.convexBooster(), 0);
require(weight == 0, "pool has weight set");
require(_curvePools.remove(_pool), "pool not removed");
require(weights.remove(_pool), "weight not removed");
emit CurvePoolRemoved(_pool);
}
function updateWeights(PoolWeight[] memory poolWeights) external onlyController {
require(poolWeights.length == _curvePools.length(), "invalid pool weights");
uint256 total;
for (uint256 i; i < poolWeights.length; i++) {
address pool = poolWeights[i].poolAddress;
require(isRegisteredCurvePool(pool), "pool is not registered");
uint256 newWeight = poolWeights[i].weight;
weights.set(pool, newWeight);
emit NewWeight(pool, newWeight);
total += newWeight;
}
require(total == ScaledMath.ONE, "weights do not sum to 1");
(
uint256 totalUnderlying_,
uint256 totalAllocated,
uint256[] memory allocatedPerPool
) = getTotalAndPerPoolUnderlying();
uint256 totalDeviation = _computeTotalDeviation(totalUnderlying_, allocatedPerPool);
totalDeviationAfterWeightUpdate = totalDeviation;
rebalancingRewardActive = !_isBalanced(allocatedPerPool, totalAllocated);
_updatePriceCache();
}
function _updatePriceCache() internal {
uint256 length_ = _curvePools.length();
IOracle priceOracle_ = controller.priceOracle();
for (uint256 i; i < length_; i++) {
address lpToken_ = controller.curveRegistryCache().lpToken(_curvePools.at(i));
_cachedPrices[lpToken_] = priceOracle_.getUSDPrice(lpToken_);
}
address underlying_ = address(underlying);
_cachedPrices[underlying_] = priceOracle_.getUSDPrice(underlying_);
}
function shutdownPool() external override onlyController {
require(!isShutdown, "pool already shutdown");
isShutdown = true;
emit Shutdown();
}
function updateDepegThreshold(uint256 newDepegThreshold_) external onlyOwner {
require(newDepegThreshold_ >= _MIN_DEPEG_THRESHOLD, "invalid depeg threshold");
require(newDepegThreshold_ <= _MAX_DEPEG_THRESHOLD, "invalid depeg threshold");
depegThreshold = newDepegThreshold_;
emit DepegThresholdUpdated(newDepegThreshold_);
}
function handleDepeggedCurvePool(address curvePool_) external override {
require(isRegisteredCurvePool(curvePool_), "pool is not registered");
require(weights.get(curvePool_) != 0, "pool weight already 0");
require(!_isDepegged(address(underlying)), "underlying is depegged");
address lpToken_ = controller.curveRegistryCache().lpToken(curvePool_);
require(_isDepegged(lpToken_), "pool is not depegged");
_setWeightToZero(curvePool_);
rebalancingRewardActive = true;
emit HandledDepeggedCurvePool(curvePool_);
}
function _setWeightToZero(address curvePool_) internal {
uint256 weight_ = weights.get(curvePool_);
if (weight_ == 0) return;
require(weight_ != ScaledMath.ONE, "can't remove last pool");
uint256 scaleUp_ = ScaledMath.ONE.divDown(ScaledMath.ONE - weights.get(curvePool_));
uint256 curvePoolLength_ = _curvePools.length();
for (uint256 i; i < curvePoolLength_; i++) {
address pool_ = _curvePools.at(i);
uint256 newWeight_ = pool_ == curvePool_ ? 0 : weights.get(pool_).mulDown(scaleUp_);
weights.set(pool_, newWeight_);
emit NewWeight(pool_, newWeight_);
}
(
uint256 totalUnderlying_,
,
uint256[] memory allocatedPerPool
) = getTotalAndPerPoolUnderlying();
uint256 totalDeviation = _computeTotalDeviation(totalUnderlying_, allocatedPerPool);
totalDeviationAfterWeightUpdate = totalDeviation;
}
function _isDepegged(address asset_) internal view returns (bool) {
uint256 depegThreshold_ = depegThreshold;
if (asset_ == address(underlying)) depegThreshold_ *= _DEPEG_UNDERLYING_MULTIPLIER;
uint256 cachedPrice_ = _cachedPrices[asset_];
uint256 currentPrice_ = controller.priceOracle().getUSDPrice(asset_);
uint256 priceDiff_ = cachedPrice_.absSub(currentPrice_);
uint256 priceDiffPercent_ = priceDiff_.divDown(cachedPrice_);
return priceDiffPercent_ > depegThreshold_;
}
function handleInvalidConvexPid(address curvePool_) external {
require(isRegisteredCurvePool(curvePool_), "curve pool not registered");
ICurveRegistryCache registryCache_ = controller.curveRegistryCache();
uint256 pid = registryCache_.getPid(curvePool_);
require(registryCache_.isShutdownPid(pid), "convex pool pid is shutdown");
_setWeightToZero(curvePool_);
emit HandledInvalidConvexPid(curvePool_, pid);
}
function setMaxIdleCurveLpRatio(uint256 maxIdleCurveLpRatio_) external onlyOwner {
require(maxIdleCurveLpRatio != maxIdleCurveLpRatio_, "same as current");
require(maxIdleCurveLpRatio_ <= _IDLE_RATIO_UPPER_BOUND, "ratio exceeds upper bound");
maxIdleCurveLpRatio = maxIdleCurveLpRatio_;
emit NewMaxIdleCurveLpRatio(maxIdleCurveLpRatio_);
}
function setMaxDeviation(uint256 maxDeviation_) external onlyOwner {
require(maxDeviation != maxDeviation_, "same as current");
require(maxDeviation_ <= _MAX_DEVIATION_UPPER_BOUND, "deviation exceeds upper bound");
maxDeviation = maxDeviation_;
emit MaxDeviationUpdated(maxDeviation_);
}
function getWeight(address curvePool) external view returns (uint256) {
return weights.get(curvePool);
}
function getWeights() external view override returns (PoolWeight[] memory) {
uint256 length_ = _curvePools.length();
PoolWeight[] memory weights_ = new PoolWeight[](length_);
for (uint256 i; i < length_; i++) {
(address pool_, uint256 weight_) = weights.at(i);
weights_[i] = PoolWeight(pool_, weight_);
}
return weights_;
}
function getAllocatedUnderlying() external view override returns (PoolWithAmount[] memory) {
PoolWithAmount[] memory perPoolAllocated = new PoolWithAmount[](_curvePools.length());
(, , uint256[] memory allocated) = getTotalAndPerPoolUnderlying();
for (uint256 i; i < perPoolAllocated.length; i++) {
perPoolAllocated[i] = PoolWithAmount(_curvePools.at(i), allocated[i]);
}
return perPoolAllocated;
}
function computeTotalDeviation() external view override returns (uint256) {
(
,
uint256 allocatedUnderlying_,
uint256[] memory perPoolUnderlying
) = getTotalAndPerPoolUnderlying();
return _computeTotalDeviation(allocatedUnderlying_, perPoolUnderlying);
}
function computeDeviationRatio() external view returns (uint256) {
(
,
uint256 allocatedUnderlying_,
uint256[] memory perPoolUnderlying
) = getTotalAndPerPoolUnderlying();
if (allocatedUnderlying_ == 0) return 0;
uint256 deviation = _computeTotalDeviation(allocatedUnderlying_, perPoolUnderlying);
return deviation.divDown(allocatedUnderlying_);
}
function cachedTotalUnderlying() external view virtual override returns (uint256) {
if (block.timestamp > _cacheUpdatedTimestamp + _TOTAL_UNDERLYING_CACHE_EXPIRY) {
return totalUnderlying();
}
return _cachedTotalUnderlying;
}
function getTotalAndPerPoolUnderlying()
public
view
returns (
uint256 totalUnderlying_,
uint256 totalAllocated_,
uint256[] memory perPoolUnderlying_
)
{
uint256 underlyingPrice_ = controller.priceOracle().getUSDPrice(address(underlying));
return _getTotalAndPerPoolUnderlying(underlyingPrice_);
}
function totalCurveLpBalance(address curvePool_) public view returns (uint256) {
return _stakedCurveLpBalance(curvePool_) + _idleCurveLpBalance(curvePool_);
}
function isBalanced() external view override returns (bool) {
(
,
uint256 allocatedUnderlying_,
uint256[] memory allocatedPerPool_
) = getTotalAndPerPoolUnderlying();
return _isBalanced(allocatedPerPool_, allocatedUnderlying_);
}
function _getTotalAndPerPoolUnderlying(
uint256 underlyingPrice_
)
internal
view
returns (
uint256 totalUnderlying_,
uint256 totalAllocated_,
uint256[] memory perPoolUnderlying_
)
{
uint256 curvePoolsLength_ = _curvePools.length();
perPoolUnderlying_ = new uint256[](curvePoolsLength_);
for (uint256 i; i < curvePoolsLength_; i++) {
address curvePool_ = _curvePools.at(i);
uint256 poolUnderlying_ = _curveLpToUnderlying(
controller.curveRegistryCache().lpToken(curvePool_),
totalCurveLpBalance(curvePool_),
underlyingPrice_
);
perPoolUnderlying_[i] = poolUnderlying_;
totalAllocated_ += poolUnderlying_;
}
totalUnderlying_ = totalAllocated_ + underlying.balanceOf(address(this));
}
function _stakedCurveLpBalance(address pool_) internal view returns (uint256) {
return
IBaseRewardPool(IConvexHandler(controller.convexHandler()).getRewardPool(pool_))
.balanceOf(address(this));
}
function _idleCurveLpBalance(address curvePool_) internal view returns (uint256) {
return IERC20(controller.curveRegistryCache().lpToken(curvePool_)).balanceOf(address(this));
}
function _curveLpToUnderlying(
address curveLpToken_,
uint256 curveLpAmount_,
uint256 underlyingPrice_
) internal view returns (uint256) {
return
curveLpAmount_
.mulDown(controller.priceOracle().getUSDPrice(curveLpToken_))
.divDown(underlyingPrice_)
.convertScale(18, underlying.decimals());
}
function _underlyingToCurveLp(
address curveLpToken_,
uint256 underlyingAmount_
) internal view returns (uint256) {
return
underlyingAmount_
.mulDown(controller.priceOracle().getUSDPrice(address(underlying)))
.divDown(controller.priceOracle().getUSDPrice(curveLpToken_))
.convertScale(underlying.decimals(), 18);
}
function _computeTotalDeviation(
uint256 allocatedUnderlying_,
uint256[] memory perPoolAllocations_
) internal view returns (uint256) {
uint256 totalDeviation;
for (uint256 i; i < perPoolAllocations_.length; i++) {
uint256 weight = weights.get(_curvePools.at(i));
uint256 targetAmount = allocatedUnderlying_.mulDown(weight);
totalDeviation += targetAmount.absSub(perPoolAllocations_[i]);
}
return totalDeviation;
}
function _handleRebalancingRewards(
address account,
uint256 allocatedBalanceBefore_,
uint256[] memory allocatedPerPoolBefore,
uint256 allocatedBalanceAfter_,
uint256[] memory allocatedPerPoolAfter
) internal {
if (!rebalancingRewardActive) return;
uint256 deviationBefore = _computeTotalDeviation(
allocatedBalanceBefore_,
allocatedPerPoolBefore
);
uint256 deviationAfter = _computeTotalDeviation(
allocatedBalanceAfter_,
allocatedPerPoolAfter
);
controller.inflationManager().handleRebalancingRewards(
account,
deviationBefore,
deviationAfter
);
if (_isBalanced(allocatedPerPoolAfter, allocatedBalanceAfter_)) {
rebalancingRewardActive = false;
}
}
function _min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
function _isBalanced(
uint256[] memory allocatedPerPool_,
uint256 totalAllocated_
) internal view returns (bool) {
if (totalAllocated_ == 0) return true;
for (uint256 i; i < allocatedPerPool_.length; i++) {
uint256 weight_ = weights.get(_curvePools.at(i));
uint256 currentAllocated_ = allocatedPerPool_[i];
if (weight_ == 0) {
uint256 price_ = controller.priceOracle().getUSDPrice(address(underlying));
uint256 allocatedUsd_ = (price_ * currentAllocated_) / 10 ** underlying.decimals();
if (allocatedUsd_ >= _MAX_USD_LP_VALUE_FOR_REMOVING_CURVE_POOL / 2) {
return false;
}
continue;
}
uint256 targetAmount = totalAllocated_.mulDown(weight_);
uint256 deviation = targetAmount.absSub(currentAllocated_);
uint256 deviationRatio = deviation.divDown(targetAmount);
if (deviationRatio > maxDeviation) return false;
}
return true;
}
function _getMaxDeviation() internal view returns (uint256) {
return rebalancingRewardActive ? 0 : maxDeviation;
}
}
文件 4 的 34:Context.sol
pragma solidity ^0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
文件 5 的 34:CurvePoolUtils.sol
pragma solidity 0.8.17;
import "ICurvePoolV2.sol";
import "ICurvePoolV1.sol";
import "ScaledMath.sol";
library CurvePoolUtils {
using ScaledMath for uint256;
uint256 internal constant _DEFAULT_IMBALANCE_THRESHOLD = 0.02e18;
enum AssetType {
USD,
ETH,
BTC,
OTHER,
CRYPTO
}
struct PoolMeta {
address pool;
uint256 numberOfCoins;
AssetType assetType;
uint256[] decimals;
uint256[] prices;
uint256[] thresholds;
}
function ensurePoolBalanced(PoolMeta memory poolMeta) internal view {
uint256 fromDecimals = poolMeta.decimals[0];
uint256 fromBalance = 10 ** fromDecimals;
uint256 fromPrice = poolMeta.prices[0];
for (uint256 i = 1; i < poolMeta.numberOfCoins; i++) {
uint256 toDecimals = poolMeta.decimals[i];
uint256 toPrice = poolMeta.prices[i];
uint256 toExpectedUnscaled = (fromBalance * fromPrice) / toPrice;
uint256 toExpected = toExpectedUnscaled.convertScale(
uint8(fromDecimals),
uint8(toDecimals)
);
uint256 toActual;
if (poolMeta.assetType == AssetType.CRYPTO) {
toActual = ICurvePoolV2(poolMeta.pool).get_dy(0, i, fromBalance);
} else {
toActual = ICurvePoolV1(poolMeta.pool).get_dy(0, int128(uint128(i)), fromBalance);
}
require(
_isWithinThreshold(toExpected, toActual, poolMeta.thresholds[i]),
"pool is not balanced"
);
}
}
function _isWithinThreshold(
uint256 a,
uint256 b,
uint256 imbalanceTreshold
) internal pure returns (bool) {
if (imbalanceTreshold == 0) imbalanceTreshold = _DEFAULT_IMBALANCE_THRESHOLD;
if (a > b) return (a - b).divDown(a) <= imbalanceTreshold;
return (b - a).divDown(b) <= imbalanceTreshold;
}
}
文件 6 的 34:ERC20.sol
pragma solidity ^0.8.0;
import "IERC20.sol";
import "IERC20Metadata.sol";
import "Context.sol";
contract ERC20 is Context, IERC20, IERC20Metadata {
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
uint256 private _totalSupply;
string private _name;
string private _symbol;
constructor(string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
}
function name() public view virtual override returns (string memory) {
return _name;
}
function symbol() public view virtual override returns (string memory) {
return _symbol;
}
function decimals() public view virtual override returns (uint8) {
return 18;
}
function totalSupply() public view virtual override returns (uint256) {
return _totalSupply;
}
function balanceOf(address account) public view virtual override returns (uint256) {
return _balances[account];
}
function transfer(address to, uint256 amount) public virtual override returns (bool) {
address owner = _msgSender();
_transfer(owner, to, amount);
return true;
}
function allowance(address owner, address spender) public view virtual override returns (uint256) {
return _allowances[owner][spender];
}
function approve(address spender, uint256 amount) public virtual override returns (bool) {
address owner = _msgSender();
_approve(owner, spender, amount);
return true;
}
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;
}
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
address owner = _msgSender();
_approve(owner, spender, allowance(owner, spender) + addedValue);
return true;
}
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;
}
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;
_balances[to] += amount;
}
emit Transfer(from, to, amount);
_afterTokenTransfer(from, to, amount);
}
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 {
_balances[account] += amount;
}
emit Transfer(address(0), account, amount);
_afterTokenTransfer(address(0), account, amount);
}
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;
_totalSupply -= amount;
}
emit Transfer(account, address(0), amount);
_afterTokenTransfer(account, address(0), amount);
}
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);
}
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);
}
}
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
function _afterTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
}
文件 7 的 34:EnumerableMap.sol
pragma solidity ^0.8.0;
import "EnumerableSet.sol";
library EnumerableMap {
using EnumerableSet for EnumerableSet.Bytes32Set;
struct Bytes32ToBytes32Map {
EnumerableSet.Bytes32Set _keys;
mapping(bytes32 => bytes32) _values;
}
function set(
Bytes32ToBytes32Map storage map,
bytes32 key,
bytes32 value
) internal returns (bool) {
map._values[key] = value;
return map._keys.add(key);
}
function remove(Bytes32ToBytes32Map storage map, bytes32 key) internal returns (bool) {
delete map._values[key];
return map._keys.remove(key);
}
function contains(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bool) {
return map._keys.contains(key);
}
function length(Bytes32ToBytes32Map storage map) internal view returns (uint256) {
return map._keys.length();
}
function at(Bytes32ToBytes32Map storage map, uint256 index) internal view returns (bytes32, bytes32) {
bytes32 key = map._keys.at(index);
return (key, map._values[key]);
}
function tryGet(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bool, bytes32) {
bytes32 value = map._values[key];
if (value == bytes32(0)) {
return (contains(map, key), bytes32(0));
} else {
return (true, value);
}
}
function get(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bytes32) {
bytes32 value = map._values[key];
require(value != 0 || contains(map, key), "EnumerableMap: nonexistent key");
return value;
}
function get(
Bytes32ToBytes32Map storage map,
bytes32 key,
string memory errorMessage
) internal view returns (bytes32) {
bytes32 value = map._values[key];
require(value != 0 || contains(map, key), errorMessage);
return value;
}
struct UintToUintMap {
Bytes32ToBytes32Map _inner;
}
function set(
UintToUintMap storage map,
uint256 key,
uint256 value
) internal returns (bool) {
return set(map._inner, bytes32(key), bytes32(value));
}
function remove(UintToUintMap storage map, uint256 key) internal returns (bool) {
return remove(map._inner, bytes32(key));
}
function contains(UintToUintMap storage map, uint256 key) internal view returns (bool) {
return contains(map._inner, bytes32(key));
}
function length(UintToUintMap storage map) internal view returns (uint256) {
return length(map._inner);
}
function at(UintToUintMap storage map, uint256 index) internal view returns (uint256, uint256) {
(bytes32 key, bytes32 value) = at(map._inner, index);
return (uint256(key), uint256(value));
}
function tryGet(UintToUintMap storage map, uint256 key) internal view returns (bool, uint256) {
(bool success, bytes32 value) = tryGet(map._inner, bytes32(key));
return (success, uint256(value));
}
function get(UintToUintMap storage map, uint256 key) internal view returns (uint256) {
return uint256(get(map._inner, bytes32(key)));
}
function get(
UintToUintMap storage map,
uint256 key,
string memory errorMessage
) internal view returns (uint256) {
return uint256(get(map._inner, bytes32(key), errorMessage));
}
struct UintToAddressMap {
Bytes32ToBytes32Map _inner;
}
function set(
UintToAddressMap storage map,
uint256 key,
address value
) internal returns (bool) {
return set(map._inner, bytes32(key), bytes32(uint256(uint160(value))));
}
function remove(UintToAddressMap storage map, uint256 key) internal returns (bool) {
return remove(map._inner, bytes32(key));
}
function contains(UintToAddressMap storage map, uint256 key) internal view returns (bool) {
return contains(map._inner, bytes32(key));
}
function length(UintToAddressMap storage map) internal view returns (uint256) {
return length(map._inner);
}
function at(UintToAddressMap storage map, uint256 index) internal view returns (uint256, address) {
(bytes32 key, bytes32 value) = at(map._inner, index);
return (uint256(key), address(uint160(uint256(value))));
}
function tryGet(UintToAddressMap storage map, uint256 key) internal view returns (bool, address) {
(bool success, bytes32 value) = tryGet(map._inner, bytes32(key));
return (success, address(uint160(uint256(value))));
}
function get(UintToAddressMap storage map, uint256 key) internal view returns (address) {
return address(uint160(uint256(get(map._inner, bytes32(key)))));
}
function get(
UintToAddressMap storage map,
uint256 key,
string memory errorMessage
) internal view returns (address) {
return address(uint160(uint256(get(map._inner, bytes32(key), errorMessage))));
}
struct AddressToUintMap {
Bytes32ToBytes32Map _inner;
}
function set(
AddressToUintMap storage map,
address key,
uint256 value
) internal returns (bool) {
return set(map._inner, bytes32(uint256(uint160(key))), bytes32(value));
}
function remove(AddressToUintMap storage map, address key) internal returns (bool) {
return remove(map._inner, bytes32(uint256(uint160(key))));
}
function contains(AddressToUintMap storage map, address key) internal view returns (bool) {
return contains(map._inner, bytes32(uint256(uint160(key))));
}
function length(AddressToUintMap storage map) internal view returns (uint256) {
return length(map._inner);
}
function at(AddressToUintMap storage map, uint256 index) internal view returns (address, uint256) {
(bytes32 key, bytes32 value) = at(map._inner, index);
return (address(uint160(uint256(key))), uint256(value));
}
function tryGet(AddressToUintMap storage map, address key) internal view returns (bool, uint256) {
(bool success, bytes32 value) = tryGet(map._inner, bytes32(uint256(uint160(key))));
return (success, uint256(value));
}
function get(AddressToUintMap storage map, address key) internal view returns (uint256) {
return uint256(get(map._inner, bytes32(uint256(uint160(key)))));
}
function get(
AddressToUintMap storage map,
address key,
string memory errorMessage
) internal view returns (uint256) {
return uint256(get(map._inner, bytes32(uint256(uint160(key))), errorMessage));
}
struct Bytes32ToUintMap {
Bytes32ToBytes32Map _inner;
}
function set(
Bytes32ToUintMap storage map,
bytes32 key,
uint256 value
) internal returns (bool) {
return set(map._inner, key, bytes32(value));
}
function remove(Bytes32ToUintMap storage map, bytes32 key) internal returns (bool) {
return remove(map._inner, key);
}
function contains(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool) {
return contains(map._inner, key);
}
function length(Bytes32ToUintMap storage map) internal view returns (uint256) {
return length(map._inner);
}
function at(Bytes32ToUintMap storage map, uint256 index) internal view returns (bytes32, uint256) {
(bytes32 key, bytes32 value) = at(map._inner, index);
return (key, uint256(value));
}
function tryGet(Bytes32ToUintMap storage map, bytes32 key) internal view returns (bool, uint256) {
(bool success, bytes32 value) = tryGet(map._inner, key);
return (success, uint256(value));
}
function get(Bytes32ToUintMap storage map, bytes32 key) internal view returns (uint256) {
return uint256(get(map._inner, key));
}
function get(
Bytes32ToUintMap storage map,
bytes32 key,
string memory errorMessage
) internal view returns (uint256) {
return uint256(get(map._inner, key, errorMessage));
}
}
文件 8 的 34:EnumerableSet.sol
pragma solidity ^0.8.0;
library EnumerableSet {
struct Set {
bytes32[] _values;
mapping(bytes32 => uint256) _indexes;
}
function _add(Set storage set, bytes32 value) private returns (bool) {
if (!_contains(set, value)) {
set._values.push(value);
set._indexes[value] = set._values.length;
return true;
} else {
return false;
}
}
function _remove(Set storage set, bytes32 value) private returns (bool) {
uint256 valueIndex = set._indexes[value];
if (valueIndex != 0) {
uint256 toDeleteIndex = valueIndex - 1;
uint256 lastIndex = set._values.length - 1;
if (lastIndex != toDeleteIndex) {
bytes32 lastValue = set._values[lastIndex];
set._values[toDeleteIndex] = lastValue;
set._indexes[lastValue] = valueIndex;
}
set._values.pop();
delete set._indexes[value];
return true;
} else {
return false;
}
}
function _contains(Set storage set, bytes32 value) private view returns (bool) {
return set._indexes[value] != 0;
}
function _length(Set storage set) private view returns (uint256) {
return set._values.length;
}
function _at(Set storage set, uint256 index) private view returns (bytes32) {
return set._values[index];
}
function _values(Set storage set) private view returns (bytes32[] memory) {
return set._values;
}
struct Bytes32Set {
Set _inner;
}
function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
return _add(set._inner, value);
}
function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
return _remove(set._inner, value);
}
function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
return _contains(set._inner, value);
}
function length(Bytes32Set storage set) internal view returns (uint256) {
return _length(set._inner);
}
function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
return _at(set._inner, index);
}
function values(Bytes32Set storage set) internal view returns (bytes32[] memory) {
bytes32[] memory store = _values(set._inner);
bytes32[] memory result;
assembly {
result := store
}
return result;
}
struct AddressSet {
Set _inner;
}
function add(AddressSet storage set, address value) internal returns (bool) {
return _add(set._inner, bytes32(uint256(uint160(value))));
}
function remove(AddressSet storage set, address value) internal returns (bool) {
return _remove(set._inner, bytes32(uint256(uint160(value))));
}
function contains(AddressSet storage set, address value) internal view returns (bool) {
return _contains(set._inner, bytes32(uint256(uint160(value))));
}
function length(AddressSet storage set) internal view returns (uint256) {
return _length(set._inner);
}
function at(AddressSet storage set, uint256 index) internal view returns (address) {
return address(uint160(uint256(_at(set._inner, index))));
}
function values(AddressSet storage set) internal view returns (address[] memory) {
bytes32[] memory store = _values(set._inner);
address[] memory result;
assembly {
result := store
}
return result;
}
struct UintSet {
Set _inner;
}
function add(UintSet storage set, uint256 value) internal returns (bool) {
return _add(set._inner, bytes32(value));
}
function remove(UintSet storage set, uint256 value) internal returns (bool) {
return _remove(set._inner, bytes32(value));
}
function contains(UintSet storage set, uint256 value) internal view returns (bool) {
return _contains(set._inner, bytes32(value));
}
function length(UintSet storage set) internal view returns (uint256) {
return _length(set._inner);
}
function at(UintSet storage set, uint256 index) internal view returns (uint256) {
return uint256(_at(set._inner, index));
}
function values(UintSet storage set) internal view returns (uint256[] memory) {
bytes32[] memory store = _values(set._inner);
uint256[] memory result;
assembly {
result := store
}
return result;
}
}
文件 9 的 34:IBaseRewardPool.sol
pragma solidity 0.8.17;
interface IBaseRewardPool {
function stakeFor(address, uint256) external;
function stake(uint256) external;
function stakeAll() external returns (bool);
function withdraw(uint256 amount, bool claim) external returns (bool);
function withdrawAndUnwrap(uint256 amount, bool claim) external returns (bool);
function earned(address account) external view returns (uint256);
function getReward() external;
function getReward(address _account, bool _claimExtras) external;
function extraRewardsLength() external view returns (uint256);
function extraRewards(uint256 _pid) external view returns (address);
function rewardToken() external view returns (address);
function balanceOf(address account) external view returns (uint256);
}
文件 10 的 34:IBooster.sol
pragma solidity 0.8.17;
interface IBooster {
function poolInfo(
uint256 pid
)
external
view
returns (
address lpToken,
address token,
address gauge,
address crvRewards,
address stash,
bool shutdown
);
function poolLength() external view returns (uint256);
function deposit(uint256 _pid, uint256 _amount, bool _stake) external returns (bool);
function withdraw(uint256 _pid, uint256 _amount) external returns (bool);
function withdrawAll(uint256 _pid) external returns (bool);
function depositAll(uint256 _pid, bool _stake) external returns (bool);
function earmarkRewards(uint256 _pid) external returns (bool);
function isShutdown() external view returns (bool);
}
文件 11 的 34:ICNCLockerV2.sol
pragma solidity 0.8.17;
import "MerkleProof.sol";
interface ICNCLockerV2 {
event Locked(address indexed account, uint256 amount, uint256 unlockTime, bool relocked);
event UnlockExecuted(address indexed account, uint256 amount);
event Relocked(address indexed account, uint256 amount);
event KickExecuted(address indexed account, address indexed kicker, uint256 amount);
event FeesReceived(address indexed sender, uint256 crvAmount, uint256 cvxAmount);
event FeesClaimed(address indexed claimer, uint256 crvAmount, uint256 cvxAmount);
event AirdropBoostClaimed(address indexed claimer, uint256 amount);
event Shutdown();
event TokenRecovered(address indexed token);
struct VoteLock {
uint256 amount;
uint64 unlockTime;
uint128 boost;
uint64 id;
}
function lock(uint256 amount, uint64 lockTime) external;
function lock(uint256 amount, uint64 lockTime, bool relock) external;
function lockFor(uint256 amount, uint64 lockTime, bool relock, address account) external;
function relock(uint64 lockId, uint64 lockTime) external;
function relock(uint64 lockTime) external;
function relockMultiple(uint64[] calldata lockIds, uint64 lockTime) external;
function totalBoosted() external view returns (uint256);
function shutDown() external;
function recoverToken(address token) external;
function executeAvailableUnlocks() external returns (uint256);
function executeAvailableUnlocksFor(address dst) external returns (uint256);
function executeUnlocks(address dst, uint64[] calldata lockIds) external returns (uint256);
function claimAirdropBoost(uint256 amount, MerkleProof.Proof calldata proof) external;
function balanceOf(address user) external view returns (uint256);
function unlockableBalance(address user) external view returns (uint256);
function unlockableBalanceBoosted(address user) external view returns (uint256);
function kick(address user, uint64 lockId) external;
function receiveFees(uint256 amountCrv, uint256 amountCvx) external;
function claimableFees(
address account
) external view returns (uint256 claimableCrv, uint256 claimableCvx);
function claimFees() external returns (uint256 crvAmount, uint256 cvxAmount);
function computeBoost(uint128 lockTime) external view returns (uint128);
function airdropBoost(address account) external view returns (uint256);
function claimedAirdrop(address account) external view returns (bool);
function totalVoteBoost(address account) external view returns (uint256);
function totalRewardsBoost(address account) external view returns (uint256);
function userLocks(address account) external view returns (VoteLock[] memory);
}
文件 12 的 34:IConicPool.sol
pragma solidity 0.8.17;
import "ILpToken.sol";
import "IRewardManager.sol";
import "IOracle.sol";
import "IController.sol";
interface IConicPool {
event Deposit(
address indexed sender,
address indexed receiver,
uint256 depositedAmount,
uint256 lpReceived
);
event Withdraw(address indexed account, uint256 amount);
event NewWeight(address indexed curvePool, uint256 newWeight);
event NewMaxIdleCurveLpRatio(uint256 newRatio);
event ClaimedRewards(uint256 claimedCrv, uint256 claimedCvx);
event HandledDepeggedCurvePool(address curvePool_);
event HandledInvalidConvexPid(address curvePool_, uint256 pid_);
event CurvePoolAdded(address curvePool_);
event CurvePoolRemoved(address curvePool_);
event Shutdown();
event DepegThresholdUpdated(uint256 newThreshold);
event MaxDeviationUpdated(uint256 newMaxDeviation);
struct PoolWeight {
address poolAddress;
uint256 weight;
}
struct PoolWithAmount {
address poolAddress;
uint256 amount;
}
function underlying() external view returns (IERC20Metadata);
function lpToken() external view returns (ILpToken);
function rewardManager() external view returns (IRewardManager);
function controller() external view returns (IController);
function depegThreshold() external view returns (uint256);
function maxIdleCurveLpRatio() external view returns (uint256);
function getPoolWeight(address curvePool) external view returns (uint256);
function setMaxIdleCurveLpRatio(uint256 value) external;
function updateDepegThreshold(uint256 value) external;
function computeDeviationRatio() external view returns (uint256);
function depositFor(
address _account,
uint256 _amount,
uint256 _minLpReceived,
bool stake
) external returns (uint256);
function deposit(uint256 _amount, uint256 _minLpReceived) external returns (uint256);
function deposit(
uint256 _amount,
uint256 _minLpReceived,
bool stake
) external returns (uint256);
function exchangeRate() external view returns (uint256);
function usdExchangeRate() external view returns (uint256);
function allCurvePools() external view returns (address[] memory);
function curvePoolsCount() external view returns (uint256);
function getCurvePoolAtIndex(uint256 _index) external view returns (address);
function unstakeAndWithdraw(uint256 _amount, uint256 _minAmount) external returns (uint256);
function withdraw(uint256 _amount, uint256 _minAmount) external returns (uint256);
function updateWeights(PoolWeight[] memory poolWeights) external;
function getWeight(address curvePool) external view returns (uint256);
function getWeights() external view returns (PoolWeight[] memory);
function getAllocatedUnderlying() external view returns (PoolWithAmount[] memory);
function removeCurvePool(address pool) external;
function addCurvePool(address pool) external;
function totalCurveLpBalance(address curvePool_) external view returns (uint256);
function rebalancingRewardActive() external view returns (bool);
function totalDeviationAfterWeightUpdate() external view returns (uint256);
function computeTotalDeviation() external view returns (uint256);
function totalUnderlying() external view returns (uint256);
function getTotalAndPerPoolUnderlying()
external
view
returns (
uint256 totalUnderlying_,
uint256 totalAllocated_,
uint256[] memory perPoolUnderlying_
);
function cachedTotalUnderlying() external view returns (uint256);
function handleInvalidConvexPid(address pool) external;
function shutdownPool() external;
function isShutdown() external view returns (bool);
function handleDepeggedCurvePool(address curvePool_) external;
function isBalanced() external view returns (bool);
}
文件 13 的 34:IController.sol
pragma solidity 0.8.17;
import "IConicPool.sol";
import "IOracle.sol";
import "IInflationManager.sol";
import "ILpTokenStaker.sol";
import "ICurveRegistryCache.sol";
interface IController {
event PoolAdded(address indexed pool);
event PoolRemoved(address indexed pool);
event PoolShutdown(address indexed pool);
event ConvexBoosterSet(address convexBooster);
event CurveHandlerSet(address curveHandler);
event ConvexHandlerSet(address convexHandler);
event CurveRegistryCacheSet(address curveRegistryCache);
event InflationManagerSet(address inflationManager);
event PriceOracleSet(address priceOracle);
event WeightUpdateMinDelaySet(uint256 weightUpdateMinDelay);
struct WeightUpdate {
address conicPoolAddress;
IConicPool.PoolWeight[] weights;
}
function inflationManager() external view returns (IInflationManager);
function setInflationManager(address manager) external;
function curveRegistryCache() external view returns (ICurveRegistryCache);
function setLpTokenStaker(address _lpTokenStaker) external;
function lpTokenStaker() external view returns (ILpTokenStaker);
function priceOracle() external view returns (IOracle);
function setPriceOracle(address oracle) external;
function listPools() external view returns (address[] memory);
function listActivePools() external view returns (address[] memory);
function isPool(address poolAddress) external view returns (bool);
function isActivePool(address poolAddress) external view returns (bool);
function addPool(address poolAddress) external;
function shutdownPool(address poolAddress) external;
function removePool(address poolAddress) external;
function cncToken() external view returns (address);
function lastWeightUpdate(address poolAddress) external view returns (uint256);
function updateWeights(WeightUpdate memory update) external;
function updateAllWeights(WeightUpdate[] memory weights) external;
function convexBooster() external view returns (address);
function curveHandler() external view returns (address);
function convexHandler() external view returns (address);
function setConvexBooster(address _convexBooster) external;
function setCurveHandler(address _curveHandler) external;
function setConvexHandler(address _convexHandler) external;
function setCurveRegistryCache(address curveRegistryCache_) external;
function emergencyMinter() external view returns (address);
function setWeightUpdateMinDelay(uint256 delay) external;
}
文件 14 的 34:IConvexHandler.sol
pragma solidity 0.8.17;
interface IConvexHandler {
function deposit(address _curvePool, uint256 _amount) external;
function claimBatchEarnings(address[] memory _curvePools, address _conicPool) external;
function getRewardPool(address _curvePool) external view returns (address);
function getCrvEarned(address _account, address _curvePool) external view returns (uint256);
function getCrvEarnedBatch(
address _account,
address[] memory _curvePools
) external view returns (uint256);
function computeClaimableConvex(uint256 crvAmount) external view returns (uint256);
}
文件 15 的 34:ICurveHandler.sol
pragma solidity 0.8.17;
interface ICurveHandler {
function deposit(address _curvePool, address _token, uint256 _amount) external;
function withdraw(address _curvePool, address _token, uint256 _amount) external;
}
文件 16 的 34:ICurvePoolV1.sol
pragma solidity 0.8.17;
interface ICurvePoolV1 {
function get_virtual_price() external view returns (uint256);
function add_liquidity(uint256[8] calldata amounts, uint256 min_mint_amount) external;
function add_liquidity(uint256[7] calldata amounts, uint256 min_mint_amount) external;
function add_liquidity(uint256[6] calldata amounts, uint256 min_mint_amount) external;
function add_liquidity(uint256[5] calldata amounts, uint256 min_mint_amount) external;
function add_liquidity(uint256[4] calldata amounts, uint256 min_mint_amount) external;
function add_liquidity(uint256[3] calldata amounts, uint256 min_mint_amount) external;
function add_liquidity(uint256[2] calldata amounts, uint256 min_mint_amount) external;
function remove_liquidity_imbalance(
uint256[4] calldata amounts,
uint256 max_burn_amount
) external;
function remove_liquidity_imbalance(
uint256[3] calldata amounts,
uint256 max_burn_amount
) external;
function remove_liquidity_imbalance(
uint256[2] calldata amounts,
uint256 max_burn_amount
) external;
function lp_token() external view returns (address);
function A_PRECISION() external view returns (uint256);
function A_precise() external view returns (uint256);
function remove_liquidity(uint256 _amount, uint256[3] calldata min_amounts) external;
function exchange(
int128 from,
int128 to,
uint256 _from_amount,
uint256 _min_to_amount
) external;
function coins(uint256 i) external view returns (address);
function balances(uint256 i) external view returns (uint256);
function get_dy(int128 i, int128 j, uint256 _dx) external view returns (uint256);
function calc_token_amount(
uint256[4] calldata amounts,
bool deposit
) external view returns (uint256);
function calc_token_amount(
uint256[3] calldata amounts,
bool deposit
) external view returns (uint256);
function calc_token_amount(
uint256[2] calldata amounts,
bool deposit
) external view returns (uint256);
function calc_withdraw_one_coin(
uint256 _token_amount,
int128 i
) external view returns (uint256);
function remove_liquidity_one_coin(
uint256 _token_amount,
int128 i,
uint256 min_amount
) external;
}
文件 17 的 34:ICurvePoolV2.sol
pragma solidity 0.8.17;
interface ICurvePoolV2 {
function token() external view returns (address);
function coins(uint256 i) external view returns (address);
function factory() external view returns (address);
function exchange(
uint256 i,
uint256 j,
uint256 dx,
uint256 min_dy,
bool use_eth,
address receiver
) external returns (uint256);
function exchange_underlying(
uint256 i,
uint256 j,
uint256 dx,
uint256 min_dy,
address receiver
) external returns (uint256);
function add_liquidity(
uint256[2] memory amounts,
uint256 min_mint_amount,
bool use_eth,
address receiver
) external returns (uint256);
function add_liquidity(
uint256[2] memory amounts,
uint256 min_mint_amount
) external returns (uint256);
function add_liquidity(
uint256[3] memory amounts,
uint256 min_mint_amount,
bool use_eth,
address receiver
) external returns (uint256);
function add_liquidity(
uint256[3] memory amounts,
uint256 min_mint_amount
) external returns (uint256);
function remove_liquidity(
uint256 _amount,
uint256[2] memory min_amounts,
bool use_eth,
address receiver
) external;
function remove_liquidity(uint256 _amount, uint256[2] memory min_amounts) external;
function remove_liquidity(
uint256 _amount,
uint256[3] memory min_amounts,
bool use_eth,
address receiver
) external;
function remove_liquidity(uint256 _amount, uint256[3] memory min_amounts) external;
function remove_liquidity_one_coin(
uint256 token_amount,
uint256 i,
uint256 min_amount,
bool use_eth,
address receiver
) external returns (uint256);
function get_dy(uint256 i, uint256 j, uint256 dx) external view returns (uint256);
function calc_token_amount(uint256[] memory amounts) external view returns (uint256);
function calc_withdraw_one_coin(
uint256 token_amount,
uint256 i
) external view returns (uint256);
function get_virtual_price() external view returns (uint256);
}
文件 18 的 34:ICurveRegistryCache.sol
pragma solidity 0.8.17;
import "IBooster.sol";
import "CurvePoolUtils.sol";
interface ICurveRegistryCache {
function BOOSTER() external view returns (IBooster);
function initPool(address pool_) external;
function initPool(address pool_, uint256 pid_) external;
function lpToken(address pool_) external view returns (address);
function assetType(address pool_) external view returns (CurvePoolUtils.AssetType);
function isRegistered(address pool_) external view returns (bool);
function hasCoinDirectly(address pool_, address coin_) external view returns (bool);
function hasCoinAnywhere(address pool_, address coin_) external view returns (bool);
function basePool(address pool_) external view returns (address);
function coinIndex(address pool_, address coin_) external view returns (int128);
function nCoins(address pool_) external view returns (uint256);
function coinIndices(
address pool_,
address from_,
address to_
) external view returns (int128, int128, bool);
function decimals(address pool_) external view returns (uint256[] memory);
function interfaceVersion(address pool_) external view returns (uint256);
function poolFromLpToken(address lpToken_) external view returns (address);
function coins(address pool_) external view returns (address[] memory);
function getPid(address _pool) external view returns (uint256);
function getRewardPool(address _pool) external view returns (address);
function isShutdownPid(uint256 pid_) external view returns (bool);
}
文件 19 的 34:IERC20.sol
pragma solidity ^0.8.0;
interface IERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(
address from,
address to,
uint256 amount
) external returns (bool);
}
文件 20 的 34:IERC20Metadata.sol
pragma solidity ^0.8.0;
import "IERC20.sol";
interface IERC20Metadata is IERC20 {
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
}
文件 21 的 34:IInflationManager.sol
pragma solidity 0.8.17;
interface IInflationManager {
event TokensClaimed(address indexed pool, uint256 cncAmount);
event RebalancingRewardHandlerAdded(address indexed pool, address indexed handler);
event RebalancingRewardHandlerRemoved(address indexed pool, address indexed handler);
event PoolWeightsUpdated();
function executeInflationRateUpdate() external;
function updatePoolWeights() external;
function computePoolWeights()
external
view
returns (address[] memory _pools, uint256[] memory poolWeights, uint256 totalUSDValue);
function computePoolWeight(
address pool
) external view returns (uint256 poolWeight, uint256 totalUSDValue);
function currentInflationRate() external view returns (uint256);
function getCurrentPoolInflationRate(address pool) external view returns (uint256);
function handleRebalancingRewards(
address account,
uint256 deviationBefore,
uint256 deviationAfter
) external;
function addPoolRebalancingRewardHandler(
address poolAddress,
address rebalancingRewardHandler
) external;
function removePoolRebalancingRewardHandler(
address poolAddress,
address rebalancingRewardHandler
) external;
function rebalancingRewardHandlers(
address poolAddress
) external view returns (address[] memory);
function hasPoolRebalancingRewardHandlers(
address poolAddress,
address handler
) external view returns (bool);
}
文件 22 的 34:ILpToken.sol
pragma solidity 0.8.17;
import "IERC20Metadata.sol";
interface ILpToken is IERC20Metadata {
function mint(address account, uint256 amount) external returns (uint256);
function burn(address _owner, uint256 _amount) external returns (uint256);
}
文件 23 的 34:ILpTokenStaker.sol
pragma solidity 0.8.17;
interface ILpTokenStaker {
event LpTokenStaked(address indexed account, uint256 amount);
event LpTokenUnstaked(address indexed account, uint256 amount);
event TokensClaimed(address indexed pool, uint256 cncAmount);
event Shutdown();
function stake(uint256 amount, address conicPool) external;
function unstake(uint256 amount, address conicPool) external;
function stakeFor(uint256 amount, address conicPool, address account) external;
function unstakeFor(uint256 amount, address conicPool, address account) external;
function unstakeFrom(uint256 amount, address account) external;
function getUserBalanceForPool(
address conicPool,
address account
) external view returns (uint256);
function getBalanceForPool(address conicPool) external view returns (uint256);
function updateBoost(address user) external;
function claimCNCRewardsForPool(address pool) external;
function claimableCnc(address pool) external view returns (uint256);
function checkpoint(address pool) external returns (uint256);
function shutdown() external;
function getBoost(address user) external view returns (uint256);
}
文件 24 的 34:IOracle.sol
pragma solidity 0.8.17;
interface IOracle {
event TokenUpdated(address indexed token, address feed, uint256 maxDelay, bool isEthPrice);
function getUSDPrice(address token) external view returns (uint256);
function isTokenSupported(address token) external view returns (bool);
}
文件 25 的 34:IRewardManager.sol
pragma solidity 0.8.17;
interface IRewardManager {
event ClaimedRewards(uint256 claimedCrv, uint256 claimedCvx);
event SoldRewardTokens(uint256 targetTokenReceived);
event ExtraRewardAdded(address reward);
event ExtraRewardRemoved(address reward);
event ExtraRewardsCurvePoolSet(address extraReward, address curvePool);
event FeesSet(uint256 feePercentage);
event FeesEnabled(uint256 feePercentage);
event EarningsClaimed(
address indexed claimedBy,
uint256 cncEarned,
uint256 crvEarned,
uint256 cvxEarned
);
function accountCheckpoint(address account) external;
function poolCheckpoint() external returns (bool);
function addExtraReward(address reward) external returns (bool);
function addBatchExtraRewards(address[] memory rewards) external;
function pool() external view returns (address);
function setFeePercentage(uint256 _feePercentage) external;
function claimableRewards(
address account
) external view returns (uint256 cncRewards, uint256 crvRewards, uint256 cvxRewards);
function claimEarnings() external returns (uint256, uint256, uint256);
function claimPoolEarningsAndSellRewardTokens() external;
}
文件 26 的 34:Initializable.sol
pragma solidity ^0.8.2;
import "Address.sol";
abstract contract Initializable {
uint8 private _initialized;
bool private _initializing;
event Initialized(uint8 version);
modifier initializer() {
bool isTopLevelCall = !_initializing;
require(
(isTopLevelCall && _initialized < 1) || (!Address.isContract(address(this)) && _initialized == 1),
"Initializable: contract is already initialized"
);
_initialized = 1;
if (isTopLevelCall) {
_initializing = true;
}
_;
if (isTopLevelCall) {
_initializing = false;
emit Initialized(1);
}
}
modifier reinitializer(uint8 version) {
require(!_initializing && _initialized < version, "Initializable: contract is already initialized");
_initialized = version;
_initializing = true;
_;
_initializing = false;
emit Initialized(version);
}
modifier onlyInitializing() {
require(_initializing, "Initializable: contract is not initializing");
_;
}
function _disableInitializers() internal virtual {
require(!_initializing, "Initializable: contract is initializing");
if (_initialized < type(uint8).max) {
_initialized = type(uint8).max;
emit Initialized(type(uint8).max);
}
}
function _getInitializedVersion() internal view returns (uint8) {
return _initialized;
}
function _isInitializing() internal view returns (bool) {
return _initializing;
}
}
文件 27 的 34:LpToken.sol
pragma solidity 0.8.17;
import "ERC20.sol";
import "ILpToken.sol";
import "IConicPool.sol";
contract LpToken is ILpToken, ERC20 {
address public immutable minter;
modifier onlyMinter() {
require(msg.sender == minter, "not authorized");
_;
}
uint8 private __decimals;
constructor(
address _minter,
uint8 _decimals,
string memory name,
string memory symbol
) ERC20(name, symbol) {
minter = _minter;
__decimals = _decimals;
}
function decimals() public view virtual override(ERC20, IERC20Metadata) returns (uint8) {
return __decimals;
}
function mint(
address _account,
uint256 _amount
) external override onlyMinter returns (uint256) {
_mint(_account, _amount);
return _amount;
}
function burn(address _owner, uint256 _amount) external override onlyMinter returns (uint256) {
_burn(_owner, _amount);
return _amount;
}
}
文件 28 的 34:MerkleProof.sol
pragma solidity 0.8.17;
library MerkleProof {
struct Proof {
uint16 nodeIndex;
bytes32[] hashes;
}
function isValid(
Proof memory proof,
bytes32 node,
bytes32 merkleRoot
) internal pure returns (bool) {
uint256 length = proof.hashes.length;
uint16 nodeIndex = proof.nodeIndex;
for (uint256 i = 0; i < length; i++) {
if (nodeIndex % 2 == 0) {
node = keccak256(abi.encodePacked(node, proof.hashes[i]));
} else {
node = keccak256(abi.encodePacked(proof.hashes[i], node));
}
nodeIndex /= 2;
}
return node == merkleRoot;
}
}
文件 29 的 34:Ownable.sol
pragma solidity ^0.8.0;
import "Context.sol";
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor() {
_transferOwnership(_msgSender());
}
modifier onlyOwner() {
_checkOwner();
_;
}
function owner() public view virtual returns (address) {
return _owner;
}
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
文件 30 的 34:RewardManagerV2.sol
pragma solidity 0.8.17;
import "Ownable.sol";
import "Initializable.sol";
import "EnumerableSet.sol";
import "SafeERC20.sol";
import "IERC20Metadata.sol";
import "IConicPool.sol";
import "ILpToken.sol";
import "IRewardManager.sol";
import "IConvexHandler.sol";
import "ICurveHandler.sol";
import "IController.sol";
import "IOracle.sol";
import "IInflationManager.sol";
import "ILpTokenStaker.sol";
import "ICNCLockerV2.sol";
import "ICurvePoolV2.sol";
import "UniswapRouter02.sol";
import "ScaledMath.sol";
contract RewardManagerV2 is IRewardManager, Ownable, Initializable {
using ScaledMath for uint256;
using SafeERC20 for IERC20;
using EnumerableSet for EnumerableSet.AddressSet;
struct RewardMeta {
uint256 earnedIntegral;
uint256 lastHoldings;
mapping(address => uint256) accountIntegral;
mapping(address => uint256) accountShare;
}
IERC20 public constant CVX = IERC20(0x4e3FBD56CD56c3e72c1403e103b45Db9da5B9D2B);
IERC20 public constant CRV = IERC20(0xD533a949740bb3306d119CC777fa900bA034cd52);
IERC20 public constant WETH = IERC20(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
IERC20 public constant CNC = IERC20(0x9aE380F0272E2162340a5bB646c354271c0F5cFC);
UniswapRouter02 public constant SUSHISWAP =
UniswapRouter02(0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F);
ICurvePoolV2 public constant CNC_ETH_POOL =
ICurvePoolV2(0x838af967537350D2C44ABB8c010E49E32673ab94);
uint256 public constant MAX_FEE_PERCENTAGE = 3e17;
uint256 public constant SLIPPAGE_THRESHOLD = 0.95e18;
bytes32 internal constant _CNC_KEY = "cnc";
bytes32 internal constant _CRV_KEY = "crv";
bytes32 internal constant _CVX_KEY = "cvx";
address public override pool;
ILpToken public lpToken;
IERC20 public immutable underlying;
IController public immutable controller;
ICNCLockerV2 public immutable locker;
bool internal _claimingCNC;
EnumerableSet.AddressSet internal _extraRewards;
mapping(address => address) public extraRewardsCurvePool;
mapping(bytes32 => RewardMeta) internal _rewardsMeta;
bool public feesEnabled;
uint256 public feePercentage;
constructor(address _controller, address _underlying, address cncLocker) {
underlying = IERC20(_underlying);
controller = IController(_controller);
WETH.safeApprove(address(CNC_ETH_POOL), type(uint256).max);
locker = ICNCLockerV2(cncLocker);
}
function initialize(address _pool) external onlyOwner initializer {
pool = _pool;
lpToken = IConicPool(_pool).lpToken();
}
function poolCheckpoint() public override returns (bool) {
IConvexHandler convexHandler = IConvexHandler(controller.convexHandler());
(uint256 crvHoldings, uint256 cvxHoldings, uint256 cncHoldings) = _getHoldings(
convexHandler
);
uint256 crvEarned = crvHoldings - _rewardsMeta[_CRV_KEY].lastHoldings;
uint256 cvxEarned = cvxHoldings - _rewardsMeta[_CVX_KEY].lastHoldings;
uint256 cncEarned = cncHoldings - _rewardsMeta[_CNC_KEY].lastHoldings;
uint256 crvFee;
uint256 cvxFee;
if (feesEnabled) {
crvFee = crvEarned.mulDown(feePercentage);
cvxFee = cvxEarned.mulDown(feePercentage);
crvEarned -= crvFee;
cvxEarned -= cvxFee;
crvHoldings -= crvFee;
cvxHoldings -= cvxFee;
}
uint256 _totalStaked = controller.lpTokenStaker().getBalanceForPool(pool);
if (_totalStaked > 0) {
_updateEarned(_CVX_KEY, cvxHoldings, cvxEarned, _totalStaked);
_updateEarned(_CRV_KEY, crvHoldings, crvEarned, _totalStaked);
_updateEarned(_CNC_KEY, cncHoldings, cncEarned, _totalStaked);
}
if (!feesEnabled) {
return false;
}
bool rewardsClaimed = false;
if (crvFee > CRV.balanceOf(pool) || cvxFee > CVX.balanceOf(pool)) {
_claimPoolEarningsAndSellRewardTokens();
rewardsClaimed = true;
}
CRV.safeTransferFrom(pool, address(this), crvFee);
CVX.safeTransferFrom(pool, address(this), cvxFee);
CRV.safeApprove(address(locker), crvFee);
CVX.safeApprove(address(locker), cvxFee);
locker.receiveFees(crvFee, cvxFee);
return rewardsClaimed;
}
function _updateEarned(
bytes32 key,
uint256 holdings,
uint256 earned,
uint256 _totalSupply
) internal {
_rewardsMeta[key].earnedIntegral += earned.divDown(_totalSupply);
_rewardsMeta[key].lastHoldings = holdings;
}
function _getEarnedRewards()
internal
view
returns (uint256 crvEarned, uint256 cvxEarned, uint256 cncEarned)
{
IConvexHandler convexHandler = IConvexHandler(controller.convexHandler());
return _getEarnedRewards(convexHandler);
}
function _getHoldings(
IConvexHandler convexHandler
) internal view returns (uint256 crvHoldings, uint256 cvxHoldings, uint256 cncHoldings) {
address[] memory curvePools = IConicPool(pool).allCurvePools();
uint256 claimableCRV = convexHandler.getCrvEarnedBatch(pool, curvePools);
crvHoldings = CRV.balanceOf(pool) + claimableCRV;
uint256 claimableCVX = convexHandler.computeClaimableConvex(claimableCRV);
cvxHoldings = CVX.balanceOf(pool) + claimableCVX;
cncHoldings = CNC.balanceOf(pool);
if (!_claimingCNC) {
cncHoldings += controller.lpTokenStaker().claimableCnc(pool);
}
}
function _getEarnedRewards(
IConvexHandler convexHandler
) internal view returns (uint256 crvEarned, uint256 cvxEarned, uint256 cncEarned) {
(
uint256 currentHoldingsCRV,
uint256 currentHoldingsCVX,
uint256 currentHoldingsCNC
) = _getHoldings(convexHandler);
crvEarned = currentHoldingsCRV - _rewardsMeta[_CRV_KEY].lastHoldings;
cvxEarned = currentHoldingsCVX - _rewardsMeta[_CVX_KEY].lastHoldings;
cncEarned = currentHoldingsCNC - _rewardsMeta[_CNC_KEY].lastHoldings;
}
function accountCheckpoint(address account) external {
_accountCheckpoint(account);
}
function _accountCheckpoint(address account) internal {
uint256 accountBalance = controller.lpTokenStaker().getUserBalanceForPool(pool, account);
poolCheckpoint();
_updateAccountRewardsMeta(_CNC_KEY, account, accountBalance);
_updateAccountRewardsMeta(_CRV_KEY, account, accountBalance);
_updateAccountRewardsMeta(_CVX_KEY, account, accountBalance);
}
function _updateAccountRewardsMeta(bytes32 key, address account, uint256 balance) internal {
RewardMeta storage meta = _rewardsMeta[key];
uint256 share = balance.mulDown(meta.earnedIntegral - meta.accountIntegral[account]);
meta.accountShare[account] += share;
meta.accountIntegral[account] = meta.earnedIntegral;
}
function claimEarnings() public override returns (uint256, uint256, uint256) {
_accountCheckpoint(msg.sender);
uint256 crvAmount = _rewardsMeta[_CRV_KEY].accountShare[msg.sender];
uint256 cvxAmount = _rewardsMeta[_CVX_KEY].accountShare[msg.sender];
uint256 cncAmount = _rewardsMeta[_CNC_KEY].accountShare[msg.sender];
if (
crvAmount > CRV.balanceOf(pool) ||
cvxAmount > CVX.balanceOf(pool) ||
cncAmount > CNC.balanceOf(pool)
) {
_claimPoolEarningsAndSellRewardTokens();
}
_rewardsMeta[_CNC_KEY].accountShare[msg.sender] = 0;
_rewardsMeta[_CVX_KEY].accountShare[msg.sender] = 0;
_rewardsMeta[_CRV_KEY].accountShare[msg.sender] = 0;
CRV.safeTransferFrom(pool, msg.sender, crvAmount);
CVX.safeTransferFrom(pool, msg.sender, cvxAmount);
CNC.safeTransferFrom(pool, msg.sender, cncAmount);
(
uint256 currentHoldingsCRV,
uint256 currentHoldingsCVX,
uint256 currentHoldingsCNC
) = _getHoldings(IConvexHandler(controller.convexHandler()));
_rewardsMeta[_CRV_KEY].lastHoldings = currentHoldingsCRV;
_rewardsMeta[_CVX_KEY].lastHoldings = currentHoldingsCVX;
_rewardsMeta[_CNC_KEY].lastHoldings = currentHoldingsCNC;
emit EarningsClaimed(msg.sender, cncAmount, crvAmount, cvxAmount);
return (cncAmount, crvAmount, cvxAmount);
}
function claimPoolEarningsAndSellRewardTokens() external override {
if (!poolCheckpoint()) {
_claimPoolEarningsAndSellRewardTokens();
}
}
function _claimPoolEarningsAndSellRewardTokens() internal {
_claimPoolEarnings();
uint256 cncBalanceBefore_ = CNC.balanceOf(pool);
_sellRewardTokens();
uint256 receivedCnc_ = CNC.balanceOf(pool) - cncBalanceBefore_;
uint256 _totalStaked = controller.lpTokenStaker().getBalanceForPool(pool);
if (_totalStaked > 0)
_rewardsMeta[_CNC_KEY].earnedIntegral += receivedCnc_.divDown(_totalStaked);
emit SoldRewardTokens(receivedCnc_);
}
function _claimPoolEarnings() internal {
_claimingCNC = true;
controller.lpTokenStaker().claimCNCRewardsForPool(pool);
_claimingCNC = false;
uint256 cvxBalance = CVX.balanceOf(pool);
uint256 crvBalance = CRV.balanceOf(pool);
address convexHandler = controller.convexHandler();
IConvexHandler(convexHandler).claimBatchEarnings(IConicPool(pool).allCurvePools(), pool);
uint256 claimedCvx = CVX.balanceOf(pool) - cvxBalance;
uint256 claimedCrv = CRV.balanceOf(pool) - crvBalance;
emit ClaimedRewards(claimedCrv, claimedCvx);
}
function _sellRewardTokens() internal {
uint256 extraRewardsLength_ = _extraRewards.length();
if (extraRewardsLength_ == 0) return;
for (uint256 i; i < extraRewardsLength_; i++) {
_swapRewardTokenForWeth(_extraRewards.at(i));
}
_swapWethForCNC();
}
function listExtraRewards() external view returns (address[] memory) {
return _extraRewards.values();
}
function addExtraReward(address reward) public override onlyOwner returns (bool) {
require(reward != address(0), "invalid address");
require(
reward != address(CVX) &&
reward != address(CRV) &&
reward != address(underlying) &&
reward != address(CNC),
"token not allowed"
);
address[] memory curvePools_ = IConicPool(pool).allCurvePools();
for (uint256 i; i < curvePools_.length; i++) {
address curveLpToken_ = controller.curveRegistryCache().lpToken(curvePools_[i]);
require(reward != curveLpToken_, "token not allowed");
}
IERC20(reward).safeApprove(address(SUSHISWAP), 0);
IERC20(reward).safeApprove(address(SUSHISWAP), type(uint256).max);
emit ExtraRewardAdded(reward);
return _extraRewards.add(reward);
}
function addBatchExtraRewards(address[] memory _rewards) external override onlyOwner {
for (uint256 i; i < _rewards.length; i++) {
addExtraReward(_rewards[i]);
}
}
function removeExtraReward(address tokenAddress) external onlyOwner {
_extraRewards.remove(tokenAddress);
emit ExtraRewardRemoved(tokenAddress);
}
function setExtraRewardsCurvePool(address extraReward_, address curvePool_) external onlyOwner {
require(curvePool_ != extraRewardsCurvePool[extraReward_], "must be different to current");
if (curvePool_ != address(0)) {
IERC20(extraReward_).safeApprove(curvePool_, 0);
IERC20(extraReward_).safeApprove(curvePool_, type(uint256).max);
}
extraRewardsCurvePool[extraReward_] = curvePool_;
emit ExtraRewardsCurvePoolSet(extraReward_, curvePool_);
}
function setFeePercentage(uint256 _feePercentage) external override onlyOwner {
require(_feePercentage < MAX_FEE_PERCENTAGE, "cannot set fee percentage to more than 30%");
require(locker.totalBoosted() > 0);
feePercentage = _feePercentage;
feesEnabled = true;
emit FeesSet(feePercentage);
}
function claimableRewards(
address account
) external view returns (uint256 cncRewards, uint256 crvRewards, uint256 cvxRewards) {
uint256 _totalStaked = controller.lpTokenStaker().getBalanceForPool(pool);
if (_totalStaked == 0) return (0, 0, 0);
(uint256 crvEarned, uint256 cvxEarned, uint256 cncEarned) = _getEarnedRewards();
uint256 userBalance = controller.lpTokenStaker().getUserBalanceForPool(pool, account);
cncRewards = _getClaimableReward(
account,
_CNC_KEY,
cncEarned,
userBalance,
_totalStaked,
false
);
crvRewards = _getClaimableReward(
account,
_CRV_KEY,
crvEarned,
userBalance,
_totalStaked,
feesEnabled
);
cvxRewards = _getClaimableReward(
account,
_CVX_KEY,
cvxEarned,
userBalance,
_totalStaked,
feesEnabled
);
}
function _getClaimableReward(
address account,
bytes32 key,
uint256 earned,
uint256 userBalance,
uint256 _totalSupply,
bool deductFee
) internal view returns (uint256) {
RewardMeta storage meta = _rewardsMeta[key];
uint256 integral = meta.earnedIntegral;
if (deductFee) {
integral += earned.divDown(_totalSupply).mulDown(ScaledMath.ONE - feePercentage);
} else {
integral += earned.divDown(_totalSupply);
}
return
meta.accountShare[account] +
userBalance.mulDown(integral - meta.accountIntegral[account]);
}
function _swapRewardTokenForWeth(address rewardToken_) internal {
uint256 tokenBalance_ = IERC20(rewardToken_).balanceOf(address(this));
if (tokenBalance_ == 0) return;
ICurvePoolV2 curvePool_ = ICurvePoolV2(extraRewardsCurvePool[rewardToken_]);
if (address(curvePool_) != address(0)) {
(int128 i, int128 j, ) = controller.curveRegistryCache().coinIndices(
address(curvePool_),
rewardToken_,
address(WETH)
);
(uint256 from_, uint256 to_) = (uint256(uint128(i)), uint256(uint128(j)));
curvePool_.exchange(
from_,
to_,
tokenBalance_,
_minAmountOut(address(rewardToken_), address(WETH), tokenBalance_),
false,
address(this)
);
return;
}
address[] memory path_ = new address[](2);
path_[0] = rewardToken_;
path_[1] = address(WETH);
SUSHISWAP.swapExactTokensForTokens(
tokenBalance_,
_minAmountOut(address(rewardToken_), address(WETH), tokenBalance_),
path_,
address(this),
block.timestamp
);
}
function _swapWethForCNC() internal {
uint256 wethBalance_ = WETH.balanceOf(address(this));
if (wethBalance_ == 0) return;
CNC_ETH_POOL.exchange(
0,
1,
wethBalance_,
_minAmountOut(address(WETH), address(CNC), wethBalance_),
false,
pool
);
}
function _minAmountOut(
address tokenIn_,
address tokenOut_,
uint256 amountIn_
) internal view returns (uint256) {
IOracle oracle_ = controller.priceOracle();
if (tokenIn_ == tokenOut_) {
return amountIn_;
}
if (!oracle_.isTokenSupported(tokenIn_) || !oracle_.isTokenSupported(tokenOut_)) {
return 0;
}
return
amountIn_
.mulDown(oracle_.getUSDPrice(tokenIn_))
.divDown(oracle_.getUSDPrice(tokenOut_))
.convertScale(
IERC20Metadata(tokenIn_).decimals(),
IERC20Metadata(tokenOut_).decimals()
)
.mulDown(SLIPPAGE_THRESHOLD);
}
}
文件 31 的 34:SafeERC20.sol
pragma solidity ^0.8.0;
import "IERC20.sol";
import "draft-IERC20Permit.sol";
import "Address.sol";
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));
}
function safeApprove(
IERC20 token,
address spender,
uint256 value
) internal {
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");
}
function _callOptionalReturn(IERC20 token, bytes memory data) private {
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
if (returndata.length > 0) {
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
文件 32 的 34:ScaledMath.sol
pragma solidity 0.8.17;
library ScaledMath {
uint256 internal constant DECIMALS = 18;
uint256 internal constant ONE = 10 ** DECIMALS;
function mulDown(uint256 a, uint256 b) internal pure returns (uint256) {
return (a * b) / ONE;
}
function mulDown(uint256 a, uint256 b, uint256 decimals) internal pure returns (uint256) {
return (a * b) / (10 ** decimals);
}
function divDown(uint256 a, uint256 b) internal pure returns (uint256) {
return (a * ONE) / b;
}
function divDown(uint256 a, uint256 b, uint256 decimals) internal pure returns (uint256) {
return (a * 10 ** decimals) / b;
}
function divUp(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
return ((a * ONE) - 1) / b + 1;
}
function mulDown(int256 a, int256 b) internal pure returns (int256) {
return (a * b) / int256(ONE);
}
function mulDownUint128(uint128 a, uint128 b) internal pure returns (uint128) {
return (a * b) / uint128(ONE);
}
function mulDown(int256 a, int256 b, uint256 decimals) internal pure returns (int256) {
return (a * b) / int256(10 ** decimals);
}
function divDown(int256 a, int256 b) internal pure returns (int256) {
return (a * int256(ONE)) / b;
}
function divDownUint128(uint128 a, uint128 b) internal pure returns (uint128) {
return (a * uint128(ONE)) / b;
}
function divDown(int256 a, int256 b, uint256 decimals) internal pure returns (int256) {
return (a * int256(10 ** decimals)) / b;
}
function convertScale(
uint256 a,
uint8 fromDecimals,
uint8 toDecimals
) internal pure returns (uint256) {
if (fromDecimals == toDecimals) return a;
if (fromDecimals > toDecimals) return downscale(a, fromDecimals, toDecimals);
return upscale(a, fromDecimals, toDecimals);
}
function convertScale(
int256 a,
uint8 fromDecimals,
uint8 toDecimals
) internal pure returns (int256) {
if (fromDecimals == toDecimals) return a;
if (fromDecimals > toDecimals) return downscale(a, fromDecimals, toDecimals);
return upscale(a, fromDecimals, toDecimals);
}
function upscale(
uint256 a,
uint8 fromDecimals,
uint8 toDecimals
) internal pure returns (uint256) {
return a * (10 ** (toDecimals - fromDecimals));
}
function downscale(
uint256 a,
uint8 fromDecimals,
uint8 toDecimals
) internal pure returns (uint256) {
return a / (10 ** (fromDecimals - toDecimals));
}
function upscale(
int256 a,
uint8 fromDecimals,
uint8 toDecimals
) internal pure returns (int256) {
return a * int256(10 ** (toDecimals - fromDecimals));
}
function downscale(
int256 a,
uint8 fromDecimals,
uint8 toDecimals
) internal pure returns (int256) {
return a / int256(10 ** (fromDecimals - toDecimals));
}
function intPow(uint256 a, uint256 n) internal pure returns (uint256) {
uint256 result = ONE;
for (uint256 i; i < n; ) {
result = mulDown(result, a);
unchecked {
++i;
}
}
return result;
}
function absSub(uint256 a, uint256 b) internal pure returns (uint256) {
unchecked {
return a >= b ? a - b : b - a;
}
}
}
文件 33 的 34:UniswapRouter02.sol
pragma solidity 0.8.17;
interface UniswapRouter02 {
function swapTokensForExactTokens(
uint256 amountOut,
uint256 amountInMax,
address[] calldata path,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
function swapExactTokensForTokens(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
function swapETHForExactTokens(
uint256 amountOut,
address[] calldata path,
address to,
uint256 deadline
) external payable returns (uint256[] memory amounts);
function swapExactTokensForETH(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
function getAmountIn(
uint256 amountOut,
uint256 reserveIn,
uint256 reserveOut
) external returns (uint256 amountIn);
function getAmountOut(
uint256 amountIn,
uint256 reserveIn,
uint256 reserveOut
) external view returns (uint256 amountOut);
function getAmountsIn(
uint256 amountOut,
address[] calldata path
) external view returns (uint256[] memory amounts);
function getAmountsOut(
uint256 amountIn,
address[] memory path
) external view returns (uint256[] memory amounts);
function getReserves(
address factory,
address tokenA,
address tokenB
) external view returns (uint256 reserveA, uint256 reserveB);
function WETH() external pure returns (address);
}
interface UniswapV2Pair {
function getReserves()
external
view
returns (uint112 _reserve0, uint112 _reserve1, uint32 _blockTimestampLast);
}
interface UniswapV2Factory {
function getPair(address tokenA, address tokenB) external view returns (address pair);
}
文件 34 的 34:draft-IERC20Permit.sol
pragma solidity ^0.8.0;
interface IERC20Permit {
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
function nonces(address owner) external view returns (uint256);
function DOMAIN_SEPARATOR() external view returns (bytes32);
}
{
"compilationTarget": {
"ConicPoolV2.sol": "ConicPoolV2"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_underlying","type":"address"},{"internalType":"contract IRewardManager","name":"_rewardManager","type":"address"},{"internalType":"address","name":"_controller","type":"address"},{"internalType":"string","name":"_lpTokenName","type":"string"},{"internalType":"string","name":"_symbol","type":"string"},{"internalType":"address","name":"_cvx","type":"address"},{"internalType":"address","name":"_crv","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"claimedCrv","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"claimedCvx","type":"uint256"}],"name":"ClaimedRewards","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"curvePool_","type":"address"}],"name":"CurvePoolAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"curvePool_","type":"address"}],"name":"CurvePoolRemoved","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"newThreshold","type":"uint256"}],"name":"DepegThresholdUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"receiver","type":"address"},{"indexed":false,"internalType":"uint256","name":"depositedAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"lpReceived","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"curvePool_","type":"address"}],"name":"HandledDepeggedCurvePool","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"curvePool_","type":"address"},{"indexed":false,"internalType":"uint256","name":"pid_","type":"uint256"}],"name":"HandledInvalidConvexPid","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"newMaxDeviation","type":"uint256"}],"name":"MaxDeviationUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"newRatio","type":"uint256"}],"name":"NewMaxIdleCurveLpRatio","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"curvePool","type":"address"},{"indexed":false,"internalType":"uint256","name":"newWeight","type":"uint256"}],"name":"NewWeight","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":[],"name":"Shutdown","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Withdraw","type":"event"},{"inputs":[],"name":"CNC","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"CRV","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"CVX","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_pool","type":"address"}],"name":"addCurvePool","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"allCurvePools","outputs":[{"internalType":"address[]","name":"","type":"address[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"cachedTotalUnderlying","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"computeDeviationRatio","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"computeTotalDeviation","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"controller","outputs":[{"internalType":"contract IController","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"curvePoolsCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"depegThreshold","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"underlyingAmount","type":"uint256"},{"internalType":"uint256","name":"minLpReceived","type":"uint256"},{"internalType":"bool","name":"stake","type":"bool"}],"name":"deposit","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"underlyingAmount","type":"uint256"},{"internalType":"uint256","name":"minLpReceived","type":"uint256"}],"name":"deposit","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"uint256","name":"underlyingAmount","type":"uint256"},{"internalType":"uint256","name":"minLpReceived","type":"uint256"},{"internalType":"bool","name":"stake","type":"bool"}],"name":"depositFor","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"exchangeRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getAllocatedUnderlying","outputs":[{"components":[{"internalType":"address","name":"poolAddress","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"internalType":"struct IConicPool.PoolWithAmount[]","name":"","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_index","type":"uint256"}],"name":"getCurvePoolAtIndex","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_pool","type":"address"}],"name":"getPoolWeight","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getTotalAndPerPoolUnderlying","outputs":[{"internalType":"uint256","name":"totalUnderlying_","type":"uint256"},{"internalType":"uint256","name":"totalAllocated_","type":"uint256"},{"internalType":"uint256[]","name":"perPoolUnderlying_","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"curvePool","type":"address"}],"name":"getWeight","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getWeights","outputs":[{"components":[{"internalType":"address","name":"poolAddress","type":"address"},{"internalType":"uint256","name":"weight","type":"uint256"}],"internalType":"struct IConicPool.PoolWeight[]","name":"","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"curvePool_","type":"address"}],"name":"handleDepeggedCurvePool","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"curvePool_","type":"address"}],"name":"handleInvalidConvexPid","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"isBalanced","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_pool","type":"address"}],"name":"isRegisteredCurvePool","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"isShutdown","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"lpToken","outputs":[{"internalType":"contract ILpToken","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"maxDeviation","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"maxIdleCurveLpRatio","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"rebalancingRewardActive","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_pool","type":"address"}],"name":"removeCurvePool","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"rewardManager","outputs":[{"internalType":"contract IRewardManager","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"maxDeviation_","type":"uint256"}],"name":"setMaxDeviation","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"maxIdleCurveLpRatio_","type":"uint256"}],"name":"setMaxIdleCurveLpRatio","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"shutdownPool","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"curvePool_","type":"address"}],"name":"totalCurveLpBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalDeviationAfterWeightUpdate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalUnderlying","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"underlying","outputs":[{"internalType":"contract IERC20Metadata","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"conicLpAmount","type":"uint256"},{"internalType":"uint256","name":"minUnderlyingReceived","type":"uint256"}],"name":"unstakeAndWithdraw","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newDepegThreshold_","type":"uint256"}],"name":"updateDepegThreshold","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"poolAddress","type":"address"},{"internalType":"uint256","name":"weight","type":"uint256"}],"internalType":"struct IConicPool.PoolWeight[]","name":"poolWeights","type":"tuple[]"}],"name":"updateWeights","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"usdExchangeRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"conicLpAmount","type":"uint256"},{"internalType":"uint256","name":"minUnderlyingReceived","type":"uint256"}],"name":"withdraw","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]