|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 functionCall(target, data, "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");
|
|
require(isContract(target), "Address: call to non-contract");
|
|
|
|
(bool success, bytes memory returndata) = target.call{value: value}(data);
|
|
return verifyCallResult(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) {
|
|
require(isContract(target), "Address: static call to non-contract");
|
|
|
|
(bool success, bytes memory returndata) = target.staticcall(data);
|
|
return verifyCallResult(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) {
|
|
require(isContract(target), "Address: delegate call to non-contract");
|
|
|
|
(bool success, bytes memory returndata) = target.delegatecall(data);
|
|
return verifyCallResult(success, returndata, errorMessage);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function verifyCallResult(
|
|
bool success,
|
|
bytes memory returndata,
|
|
string memory errorMessage
|
|
) internal pure returns (bytes memory) {
|
|
if (success) {
|
|
return returndata;
|
|
} else {
|
|
|
|
if (returndata.length > 0) {
|
|
|
|
|
|
assembly {
|
|
let returndata_size := mload(returndata)
|
|
revert(add(32, returndata), returndata_size)
|
|
}
|
|
} else {
|
|
revert(errorMessage);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
pragma solidity >=0.6.0 <0.9.0;
|
|
|
|
interface IUniswapRouterETH {
|
|
function addLiquidity(
|
|
address tokenA,
|
|
address tokenB,
|
|
uint amountADesired,
|
|
uint amountBDesired,
|
|
uint amountAMin,
|
|
uint amountBMin,
|
|
address to,
|
|
uint deadline
|
|
) external returns (uint amountA, uint amountB, uint liquidity);
|
|
|
|
function addLiquidityETH(
|
|
address token,
|
|
uint amountTokenDesired,
|
|
uint amountTokenMin,
|
|
uint amountETHMin,
|
|
address to,
|
|
uint deadline
|
|
) external payable returns (uint amountToken, uint amountETH, uint liquidity);
|
|
|
|
function removeLiquidity(
|
|
address tokenA,
|
|
address tokenB,
|
|
uint liquidity,
|
|
uint amountAMin,
|
|
uint amountBMin,
|
|
address to,
|
|
uint deadline
|
|
) external returns (uint amountA, uint amountB);
|
|
|
|
function removeLiquidityETH(
|
|
address token,
|
|
uint liquidity,
|
|
uint amountTokenMin,
|
|
uint amountETHMin,
|
|
address to,
|
|
uint deadline
|
|
) external returns (uint amountToken, uint amountETH);
|
|
|
|
function swapExactTokensForTokens(
|
|
uint amountIn,
|
|
uint amountOutMin,
|
|
address[] calldata path,
|
|
address to,
|
|
uint deadline
|
|
) external returns (uint[] memory amounts);
|
|
|
|
function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
|
|
external
|
|
payable
|
|
returns (uint[] memory amounts);
|
|
|
|
function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
|
|
external
|
|
returns (uint[] memory amounts);
|
|
|
|
function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
|
|
}
|
|
|
|
|
|
|
|
|
|
pragma solidity >=0.6.0 <0.9.0;
|
|
|
|
interface IWrappedNative {
|
|
function deposit() external payable;
|
|
|
|
function withdraw(uint256 wad) external;
|
|
}
|
|
|
|
|
|
|
|
|
|
pragma solidity >=0.6.0 <0.9.0;
|
|
|
|
interface IConvexBooster {
|
|
function deposit(uint256 pid, uint256 amount, bool stake) external returns (bool);
|
|
function earmarkRewards(uint256 _pid) external;
|
|
function poolInfo(uint256 pid) external view returns (
|
|
address lptoken,
|
|
address token,
|
|
address gauge,
|
|
address crvRewards,
|
|
address stash,
|
|
bool shutdown
|
|
);
|
|
}
|
|
|
|
interface IConvexRewardPool {
|
|
function balanceOf(address account) external view returns (uint256);
|
|
function earned(address account) external view returns (uint256);
|
|
function periodFinish() external view returns (uint256);
|
|
function getReward() external;
|
|
function getReward(address _account, bool _claimExtras) external;
|
|
function withdrawAndUnwrap(uint256 _amount, bool claim) external;
|
|
function withdrawAllAndUnwrap(bool claim) external;
|
|
}
|
|
|
|
|
|
|
|
|
|
pragma solidity >=0.6.0;
|
|
|
|
interface ICurveSwap {
|
|
function remove_liquidity_one_coin(uint256 token_amount, int128 i, uint256 min_amount) external;
|
|
function calc_withdraw_one_coin(uint256 tokenAmount, int128 i) external view returns (uint256);
|
|
function coins(uint256 arg0) external view returns (address);
|
|
|
|
function add_liquidity(uint256[2] memory amounts, uint256 min_mint_amount) external payable;
|
|
function add_liquidity(uint256[2] memory amounts, uint256 min_mint_amount, bool _use_underlying) external;
|
|
function add_liquidity(address _pool, uint256[2] memory amounts, uint256 min_mint_amount) external;
|
|
|
|
function add_liquidity(uint256[3] memory amounts, uint256 min_mint_amount) external payable;
|
|
function add_liquidity(uint256[3] memory amounts, uint256 min_mint_amount, bool _use_underlying) external payable;
|
|
function add_liquidity(address _pool, uint256[3] memory amounts, uint256 min_mint_amount) external payable;
|
|
|
|
function add_liquidity(uint256[4] memory amounts, uint256 min_mint_amount) external payable;
|
|
function add_liquidity(address _pool, uint256[4] memory amounts, uint256 min_mint_amount) external payable;
|
|
|
|
function add_liquidity(uint256[5] memory amounts, uint256 min_mint_amount) external payable;
|
|
function add_liquidity(address _pool, uint256[5] memory amounts, uint256 min_mint_amount) external payable;
|
|
|
|
function get_dy(int128 i, int128 j, uint256 dx) external view returns (uint256);
|
|
function exchange(int128 i, int128 j, uint256 dx, uint256 min_dy) external;
|
|
}
|
|
|
|
|
|
|
|
|
|
pragma solidity >=0.6.0 <0.9.0;
|
|
|
|
interface ICurveSwap256 {
|
|
function exchange(uint256 i, uint256 j, uint256 dx, uint256 min_dy) external;
|
|
}
|
|
|
|
|
|
|
|
|
|
pragma solidity >=0.6.0;
|
|
|
|
interface IGaugeFactory {
|
|
function mint(address _gauge) external;
|
|
}
|
|
|
|
|
|
|
|
|
|
pragma solidity >=0.6.0 <0.9.0;
|
|
|
|
interface IRewardsGauge {
|
|
function balanceOf(address account) external view returns (uint256);
|
|
function claimable_reward(address _addr, address _token) external view returns (uint256);
|
|
function claim_rewards(address _addr) external;
|
|
function deposit(uint256 _value) external;
|
|
function withdraw(uint256 _value) external;
|
|
function reward_contract() external view returns (address);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.1;
|
|
|
|
|
|
|
|
|
|
library AddressUpgradeable {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 functionCall(target, data, "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");
|
|
require(isContract(target), "Address: call to non-contract");
|
|
|
|
(bool success, bytes memory returndata) = target.call{value: value}(data);
|
|
return verifyCallResult(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) {
|
|
require(isContract(target), "Address: static call to non-contract");
|
|
|
|
(bool success, bytes memory returndata) = target.staticcall(data);
|
|
return verifyCallResult(success, returndata, errorMessage);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function verifyCallResult(
|
|
bool success,
|
|
bytes memory returndata,
|
|
string memory errorMessage
|
|
) internal pure returns (bytes memory) {
|
|
if (success) {
|
|
return returndata;
|
|
} else {
|
|
|
|
if (returndata.length > 0) {
|
|
|
|
|
|
assembly {
|
|
let returndata_size := mload(returndata)
|
|
revert(add(32, returndata), returndata_size)
|
|
}
|
|
} else {
|
|
revert(errorMessage);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.2;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract Initializable {
|
|
|
|
|
|
|
|
|
|
uint8 private _initialized;
|
|
|
|
|
|
|
|
|
|
bool private _initializing;
|
|
|
|
|
|
|
|
|
|
event Initialized(uint8 version);
|
|
|
|
|
|
|
|
|
|
|
|
modifier initializer() {
|
|
bool isTopLevelCall = !_initializing;
|
|
require(
|
|
(isTopLevelCall && _initialized < 1) || (!AddressUpgradeable.isContract(address(this)) && _initialized == 1),
|
|
"Initializable: contract is already initialized"
|
|
);
|
|
_initialized = 1;
|
|
if (isTopLevelCall) {
|
|
_initializing = true;
|
|
}
|
|
_;
|
|
if (isTopLevelCall) {
|
|
_initializing = false;
|
|
emit Initialized(1);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract ContextUpgradeable is Initializable {
|
|
function __Context_init() internal onlyInitializing {
|
|
}
|
|
|
|
function __Context_init_unchained() internal onlyInitializing {
|
|
}
|
|
function _msgSender() internal view virtual returns (address) {
|
|
return msg.sender;
|
|
}
|
|
|
|
function _msgData() internal view virtual returns (bytes calldata) {
|
|
return msg.data;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint256[50] private __gap;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract OwnableUpgradeable is Initializable, ContextUpgradeable {
|
|
address private _owner;
|
|
|
|
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
|
|
|
|
|
|
|
|
|
|
function __Ownable_init() internal onlyInitializing {
|
|
__Ownable_init_unchained();
|
|
}
|
|
|
|
function __Ownable_init_unchained() internal onlyInitializing {
|
|
_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);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint256[49] private __gap;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract PausableUpgradeable is Initializable, ContextUpgradeable {
|
|
|
|
|
|
|
|
event Paused(address account);
|
|
|
|
|
|
|
|
|
|
event Unpaused(address account);
|
|
|
|
bool private _paused;
|
|
|
|
|
|
|
|
|
|
function __Pausable_init() internal onlyInitializing {
|
|
__Pausable_init_unchained();
|
|
}
|
|
|
|
function __Pausable_init_unchained() internal onlyInitializing {
|
|
_paused = false;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
modifier whenNotPaused() {
|
|
_requireNotPaused();
|
|
_;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
modifier whenPaused() {
|
|
_requirePaused();
|
|
_;
|
|
}
|
|
|
|
|
|
|
|
|
|
function paused() public view virtual returns (bool) {
|
|
return _paused;
|
|
}
|
|
|
|
|
|
|
|
|
|
function _requireNotPaused() internal view virtual {
|
|
require(!paused(), "Pausable: paused");
|
|
}
|
|
|
|
|
|
|
|
|
|
function _requirePaused() internal view virtual {
|
|
require(paused(), "Pausable: not paused");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _pause() internal virtual whenNotPaused {
|
|
_paused = true;
|
|
emit Paused(_msgSender());
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _unpause() internal virtual whenPaused {
|
|
_paused = false;
|
|
emit Unpaused(_msgSender());
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint256[49] private __gap;
|
|
}
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
interface IFeeConfig {
|
|
struct FeeCategory {
|
|
uint256 total;
|
|
uint256 beefy;
|
|
uint256 call;
|
|
uint256 strategist;
|
|
string label;
|
|
bool active;
|
|
}
|
|
struct AllFees {
|
|
FeeCategory performance;
|
|
uint256 deposit;
|
|
uint256 withdraw;
|
|
}
|
|
function getFees(address strategy) external view returns (FeeCategory memory);
|
|
function stratFeeId(address strategy) external view returns (uint256);
|
|
function setStratFeeId(uint256 feeId) external;
|
|
}
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
contract StratFeeManagerInitializable is OwnableUpgradeable, PausableUpgradeable {
|
|
|
|
struct CommonAddresses {
|
|
address vault;
|
|
address unirouter;
|
|
address keeper;
|
|
address strategist;
|
|
address beefyFeeRecipient;
|
|
address beefyFeeConfig;
|
|
}
|
|
|
|
|
|
address public vault;
|
|
address public unirouter;
|
|
address public keeper;
|
|
address public strategist;
|
|
address public beefyFeeRecipient;
|
|
IFeeConfig public beefyFeeConfig;
|
|
|
|
uint256 constant DIVISOR = 1 ether;
|
|
uint256 constant public WITHDRAWAL_FEE_CAP = 50;
|
|
uint256 constant public WITHDRAWAL_MAX = 10000;
|
|
uint256 internal withdrawalFee;
|
|
|
|
event SetStratFeeId(uint256 feeId);
|
|
event SetWithdrawalFee(uint256 withdrawalFee);
|
|
event SetVault(address vault);
|
|
event SetUnirouter(address unirouter);
|
|
event SetKeeper(address keeper);
|
|
event SetStrategist(address strategist);
|
|
event SetBeefyFeeRecipient(address beefyFeeRecipient);
|
|
event SetBeefyFeeConfig(address beefyFeeConfig);
|
|
|
|
function __StratFeeManager_init(CommonAddresses calldata _commonAddresses) internal onlyInitializing {
|
|
__Ownable_init();
|
|
__Pausable_init();
|
|
vault = _commonAddresses.vault;
|
|
unirouter = _commonAddresses.unirouter;
|
|
keeper = _commonAddresses.keeper;
|
|
strategist = _commonAddresses.strategist;
|
|
beefyFeeRecipient = _commonAddresses.beefyFeeRecipient;
|
|
beefyFeeConfig = IFeeConfig(_commonAddresses.beefyFeeConfig);
|
|
withdrawalFee = 10;
|
|
}
|
|
|
|
|
|
modifier onlyManager() {
|
|
require(msg.sender == owner() || msg.sender == keeper, "!manager");
|
|
_;
|
|
}
|
|
|
|
|
|
function getFees() internal view returns (IFeeConfig.FeeCategory memory) {
|
|
return beefyFeeConfig.getFees(address(this));
|
|
}
|
|
|
|
|
|
function getAllFees() external view returns (IFeeConfig.AllFees memory) {
|
|
return IFeeConfig.AllFees(getFees(), depositFee(), withdrawFee());
|
|
}
|
|
|
|
function getStratFeeId() external view returns (uint256) {
|
|
return beefyFeeConfig.stratFeeId(address(this));
|
|
}
|
|
|
|
function setStratFeeId(uint256 _feeId) external onlyManager {
|
|
beefyFeeConfig.setStratFeeId(_feeId);
|
|
emit SetStratFeeId(_feeId);
|
|
}
|
|
|
|
|
|
function setWithdrawalFee(uint256 _fee) public onlyManager {
|
|
require(_fee <= WITHDRAWAL_FEE_CAP, "!cap");
|
|
withdrawalFee = _fee;
|
|
emit SetWithdrawalFee(_fee);
|
|
}
|
|
|
|
|
|
function setVault(address _vault) external onlyOwner {
|
|
vault = _vault;
|
|
emit SetVault(_vault);
|
|
}
|
|
|
|
|
|
function setUnirouter(address _unirouter) external onlyOwner {
|
|
unirouter = _unirouter;
|
|
emit SetUnirouter(_unirouter);
|
|
}
|
|
|
|
|
|
function setKeeper(address _keeper) external onlyManager {
|
|
keeper = _keeper;
|
|
emit SetKeeper(_keeper);
|
|
}
|
|
|
|
|
|
function setStrategist(address _strategist) external {
|
|
require(msg.sender == strategist, "!strategist");
|
|
strategist = _strategist;
|
|
emit SetStrategist(_strategist);
|
|
}
|
|
|
|
|
|
function setBeefyFeeRecipient(address _beefyFeeRecipient) external onlyOwner {
|
|
beefyFeeRecipient = _beefyFeeRecipient;
|
|
emit SetBeefyFeeRecipient(_beefyFeeRecipient);
|
|
}
|
|
|
|
|
|
function setBeefyFeeConfig(address _beefyFeeConfig) external onlyOwner {
|
|
beefyFeeConfig = IFeeConfig(_beefyFeeConfig);
|
|
emit SetBeefyFeeConfig(_beefyFeeConfig);
|
|
}
|
|
|
|
function depositFee() public virtual view returns (uint256) {
|
|
return 0;
|
|
}
|
|
|
|
function withdrawFee() public virtual view returns (uint256) {
|
|
return paused() ? 0 : withdrawalFee;
|
|
}
|
|
|
|
function beforeDeposit() external virtual {}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity >=0.8.0 <0.9.0;
|
|
|
|
|
|
library BytesLib {
|
|
function concat(
|
|
bytes memory _preBytes,
|
|
bytes memory _postBytes
|
|
)
|
|
internal
|
|
pure
|
|
returns (bytes memory)
|
|
{
|
|
bytes memory tempBytes;
|
|
|
|
assembly {
|
|
|
|
|
|
tempBytes := mload(0x40)
|
|
|
|
|
|
|
|
let length := mload(_preBytes)
|
|
mstore(tempBytes, length)
|
|
|
|
|
|
|
|
|
|
let mc := add(tempBytes, 0x20)
|
|
|
|
|
|
let end := add(mc, length)
|
|
|
|
for {
|
|
|
|
|
|
let cc := add(_preBytes, 0x20)
|
|
} lt(mc, end) {
|
|
|
|
mc := add(mc, 0x20)
|
|
cc := add(cc, 0x20)
|
|
} {
|
|
|
|
|
|
mstore(mc, mload(cc))
|
|
}
|
|
|
|
|
|
|
|
|
|
length := mload(_postBytes)
|
|
mstore(tempBytes, add(length, mload(tempBytes)))
|
|
|
|
|
|
|
|
mc := end
|
|
|
|
|
|
end := add(mc, length)
|
|
|
|
for {
|
|
let cc := add(_postBytes, 0x20)
|
|
} lt(mc, end) {
|
|
mc := add(mc, 0x20)
|
|
cc := add(cc, 0x20)
|
|
} {
|
|
mstore(mc, mload(cc))
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mstore(0x40, and(
|
|
add(add(end, iszero(add(length, mload(_preBytes)))), 31),
|
|
not(31)
|
|
))
|
|
}
|
|
|
|
return tempBytes;
|
|
}
|
|
|
|
function concatStorage(bytes storage _preBytes, bytes memory _postBytes) internal {
|
|
assembly {
|
|
|
|
|
|
|
|
let fslot := sload(_preBytes.slot)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let slength := div(and(fslot, sub(mul(0x100, iszero(and(fslot, 1))), 1)), 2)
|
|
let mlength := mload(_postBytes)
|
|
let newlength := add(slength, mlength)
|
|
|
|
|
|
|
|
switch add(lt(slength, 32), lt(newlength, 32))
|
|
case 2 {
|
|
|
|
|
|
|
|
sstore(
|
|
_preBytes.slot,
|
|
|
|
|
|
add(
|
|
|
|
|
|
fslot,
|
|
add(
|
|
mul(
|
|
div(
|
|
|
|
mload(add(_postBytes, 0x20)),
|
|
|
|
exp(0x100, sub(32, mlength))
|
|
),
|
|
|
|
|
|
exp(0x100, sub(32, newlength))
|
|
),
|
|
|
|
|
|
mul(mlength, 2)
|
|
)
|
|
)
|
|
)
|
|
}
|
|
case 1 {
|
|
|
|
|
|
|
|
mstore(0x0, _preBytes.slot)
|
|
let sc := add(keccak256(0x0, 0x20), div(slength, 32))
|
|
|
|
|
|
sstore(_preBytes.slot, add(mul(newlength, 2), 1))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let submod := sub(32, slength)
|
|
let mc := add(_postBytes, submod)
|
|
let end := add(_postBytes, mlength)
|
|
let mask := sub(exp(0x100, submod), 1)
|
|
|
|
sstore(
|
|
sc,
|
|
add(
|
|
and(
|
|
fslot,
|
|
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00
|
|
),
|
|
and(mload(mc), mask)
|
|
)
|
|
)
|
|
|
|
for {
|
|
mc := add(mc, 0x20)
|
|
sc := add(sc, 1)
|
|
} lt(mc, end) {
|
|
sc := add(sc, 1)
|
|
mc := add(mc, 0x20)
|
|
} {
|
|
sstore(sc, mload(mc))
|
|
}
|
|
|
|
mask := exp(0x100, sub(mc, end))
|
|
|
|
sstore(sc, mul(div(mload(mc), mask), mask))
|
|
}
|
|
default {
|
|
|
|
mstore(0x0, _preBytes.slot)
|
|
|
|
let sc := add(keccak256(0x0, 0x20), div(slength, 32))
|
|
|
|
|
|
sstore(_preBytes.slot, add(mul(newlength, 2), 1))
|
|
|
|
|
|
|
|
let slengthmod := mod(slength, 32)
|
|
let mlengthmod := mod(mlength, 32)
|
|
let submod := sub(32, slengthmod)
|
|
let mc := add(_postBytes, submod)
|
|
let end := add(_postBytes, mlength)
|
|
let mask := sub(exp(0x100, submod), 1)
|
|
|
|
sstore(sc, add(sload(sc), and(mload(mc), mask)))
|
|
|
|
for {
|
|
sc := add(sc, 1)
|
|
mc := add(mc, 0x20)
|
|
} lt(mc, end) {
|
|
sc := add(sc, 1)
|
|
mc := add(mc, 0x20)
|
|
} {
|
|
sstore(sc, mload(mc))
|
|
}
|
|
|
|
mask := exp(0x100, sub(mc, end))
|
|
|
|
sstore(sc, mul(div(mload(mc), mask), mask))
|
|
}
|
|
}
|
|
}
|
|
|
|
function slice(
|
|
bytes memory _bytes,
|
|
uint256 _start,
|
|
uint256 _length
|
|
)
|
|
internal
|
|
pure
|
|
returns (bytes memory)
|
|
{
|
|
require(_length + 31 >= _length, "slice_overflow");
|
|
require(_bytes.length >= _start + _length, "slice_outOfBounds");
|
|
|
|
bytes memory tempBytes;
|
|
|
|
assembly {
|
|
switch iszero(_length)
|
|
case 0 {
|
|
|
|
|
|
tempBytes := mload(0x40)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let lengthmod := and(_length, 31)
|
|
|
|
|
|
|
|
|
|
|
|
let mc := add(add(tempBytes, lengthmod), mul(0x20, iszero(lengthmod)))
|
|
let end := add(mc, _length)
|
|
|
|
for {
|
|
|
|
|
|
let cc := add(add(add(_bytes, lengthmod), mul(0x20, iszero(lengthmod))), _start)
|
|
} lt(mc, end) {
|
|
mc := add(mc, 0x20)
|
|
cc := add(cc, 0x20)
|
|
} {
|
|
mstore(mc, mload(cc))
|
|
}
|
|
|
|
mstore(tempBytes, _length)
|
|
|
|
|
|
|
|
mstore(0x40, and(add(mc, 31), not(31)))
|
|
}
|
|
|
|
default {
|
|
tempBytes := mload(0x40)
|
|
|
|
|
|
mstore(tempBytes, 0)
|
|
|
|
mstore(0x40, add(tempBytes, 0x20))
|
|
}
|
|
}
|
|
|
|
return tempBytes;
|
|
}
|
|
|
|
function toAddress(bytes memory _bytes, uint256 _start) internal pure returns (address) {
|
|
require(_bytes.length >= _start + 20, "toAddress_outOfBounds");
|
|
address tempAddress;
|
|
|
|
assembly {
|
|
tempAddress := div(mload(add(add(_bytes, 0x20), _start)), 0x1000000000000000000000000)
|
|
}
|
|
|
|
return tempAddress;
|
|
}
|
|
|
|
function toUint8(bytes memory _bytes, uint256 _start) internal pure returns (uint8) {
|
|
require(_bytes.length >= _start + 1 , "toUint8_outOfBounds");
|
|
uint8 tempUint;
|
|
|
|
assembly {
|
|
tempUint := mload(add(add(_bytes, 0x1), _start))
|
|
}
|
|
|
|
return tempUint;
|
|
}
|
|
|
|
function toUint16(bytes memory _bytes, uint256 _start) internal pure returns (uint16) {
|
|
require(_bytes.length >= _start + 2, "toUint16_outOfBounds");
|
|
uint16 tempUint;
|
|
|
|
assembly {
|
|
tempUint := mload(add(add(_bytes, 0x2), _start))
|
|
}
|
|
|
|
return tempUint;
|
|
}
|
|
|
|
function toUint24(bytes memory _bytes, uint256 _start) internal pure returns (uint24) {
|
|
require(_start + 3 >= _start, 'toUint24_overflow');
|
|
require(_bytes.length >= _start + 3, 'toUint24_outOfBounds');
|
|
uint24 tempUint;
|
|
|
|
assembly {
|
|
tempUint := mload(add(add(_bytes, 0x3), _start))
|
|
}
|
|
|
|
return tempUint;
|
|
}
|
|
|
|
function toUint32(bytes memory _bytes, uint256 _start) internal pure returns (uint32) {
|
|
require(_bytes.length >= _start + 4, "toUint32_outOfBounds");
|
|
uint32 tempUint;
|
|
|
|
assembly {
|
|
tempUint := mload(add(add(_bytes, 0x4), _start))
|
|
}
|
|
|
|
return tempUint;
|
|
}
|
|
|
|
function toUint64(bytes memory _bytes, uint256 _start) internal pure returns (uint64) {
|
|
require(_bytes.length >= _start + 8, "toUint64_outOfBounds");
|
|
uint64 tempUint;
|
|
|
|
assembly {
|
|
tempUint := mload(add(add(_bytes, 0x8), _start))
|
|
}
|
|
|
|
return tempUint;
|
|
}
|
|
|
|
function toUint96(bytes memory _bytes, uint256 _start) internal pure returns (uint96) {
|
|
require(_bytes.length >= _start + 12, "toUint96_outOfBounds");
|
|
uint96 tempUint;
|
|
|
|
assembly {
|
|
tempUint := mload(add(add(_bytes, 0xc), _start))
|
|
}
|
|
|
|
return tempUint;
|
|
}
|
|
|
|
function toUint128(bytes memory _bytes, uint256 _start) internal pure returns (uint128) {
|
|
require(_bytes.length >= _start + 16, "toUint128_outOfBounds");
|
|
uint128 tempUint;
|
|
|
|
assembly {
|
|
tempUint := mload(add(add(_bytes, 0x10), _start))
|
|
}
|
|
|
|
return tempUint;
|
|
}
|
|
|
|
function toUint256(bytes memory _bytes, uint256 _start) internal pure returns (uint256) {
|
|
require(_bytes.length >= _start + 32, "toUint256_outOfBounds");
|
|
uint256 tempUint;
|
|
|
|
assembly {
|
|
tempUint := mload(add(add(_bytes, 0x20), _start))
|
|
}
|
|
|
|
return tempUint;
|
|
}
|
|
|
|
function toBytes32(bytes memory _bytes, uint256 _start) internal pure returns (bytes32) {
|
|
require(_bytes.length >= _start + 32, "toBytes32_outOfBounds");
|
|
bytes32 tempBytes32;
|
|
|
|
assembly {
|
|
tempBytes32 := mload(add(add(_bytes, 0x20), _start))
|
|
}
|
|
|
|
return tempBytes32;
|
|
}
|
|
|
|
function equal(bytes memory _preBytes, bytes memory _postBytes) internal pure returns (bool) {
|
|
bool success = true;
|
|
|
|
assembly {
|
|
let length := mload(_preBytes)
|
|
|
|
|
|
switch eq(length, mload(_postBytes))
|
|
case 1 {
|
|
|
|
|
|
|
|
|
|
let cb := 1
|
|
|
|
let mc := add(_preBytes, 0x20)
|
|
let end := add(mc, length)
|
|
|
|
for {
|
|
let cc := add(_postBytes, 0x20)
|
|
|
|
|
|
} eq(add(lt(mc, end), cb), 2) {
|
|
mc := add(mc, 0x20)
|
|
cc := add(cc, 0x20)
|
|
} {
|
|
|
|
if iszero(eq(mload(mc), mload(cc))) {
|
|
|
|
success := 0
|
|
cb := 0
|
|
}
|
|
}
|
|
}
|
|
default {
|
|
|
|
success := 0
|
|
}
|
|
}
|
|
|
|
return success;
|
|
}
|
|
|
|
function equalStorage(
|
|
bytes storage _preBytes,
|
|
bytes memory _postBytes
|
|
)
|
|
internal
|
|
view
|
|
returns (bool)
|
|
{
|
|
bool success = true;
|
|
|
|
assembly {
|
|
|
|
let fslot := sload(_preBytes.slot)
|
|
|
|
let slength := div(and(fslot, sub(mul(0x100, iszero(and(fslot, 1))), 1)), 2)
|
|
let mlength := mload(_postBytes)
|
|
|
|
|
|
switch eq(slength, mlength)
|
|
case 1 {
|
|
|
|
|
|
|
|
if iszero(iszero(slength)) {
|
|
switch lt(slength, 32)
|
|
case 1 {
|
|
|
|
fslot := mul(div(fslot, 0x100), 0x100)
|
|
|
|
if iszero(eq(fslot, mload(add(_postBytes, 0x20)))) {
|
|
|
|
success := 0
|
|
}
|
|
}
|
|
default {
|
|
|
|
|
|
|
|
|
|
let cb := 1
|
|
|
|
|
|
mstore(0x0, _preBytes.slot)
|
|
let sc := keccak256(0x0, 0x20)
|
|
|
|
let mc := add(_postBytes, 0x20)
|
|
let end := add(mc, mlength)
|
|
|
|
|
|
|
|
for {} eq(add(lt(mc, end), cb), 2) {
|
|
sc := add(sc, 1)
|
|
mc := add(mc, 0x20)
|
|
} {
|
|
if iszero(eq(sload(sc), mload(mc))) {
|
|
|
|
success := 0
|
|
cb := 0
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
default {
|
|
|
|
success := 0
|
|
}
|
|
}
|
|
|
|
return success;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
pragma solidity >=0.6.0;
|
|
|
|
|
|
library Path {
|
|
using BytesLib for bytes;
|
|
|
|
|
|
uint256 private constant ADDR_SIZE = 20;
|
|
|
|
uint256 private constant FEE_SIZE = 3;
|
|
|
|
|
|
uint256 private constant NEXT_OFFSET = ADDR_SIZE + FEE_SIZE;
|
|
|
|
uint256 private constant POP_OFFSET = NEXT_OFFSET + ADDR_SIZE;
|
|
|
|
uint256 private constant MULTIPLE_POOLS_MIN_LENGTH = POP_OFFSET + NEXT_OFFSET;
|
|
|
|
|
|
|
|
|
|
function hasMultiplePools(bytes memory path) internal pure returns (bool) {
|
|
return path.length >= MULTIPLE_POOLS_MIN_LENGTH;
|
|
}
|
|
|
|
|
|
|
|
|
|
function numPools(bytes memory path) internal pure returns (uint256) {
|
|
|
|
return ((path.length - ADDR_SIZE) / NEXT_OFFSET);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function decodeFirstPool(bytes memory path)
|
|
internal
|
|
pure
|
|
returns (
|
|
address tokenA,
|
|
address tokenB,
|
|
uint24 fee
|
|
)
|
|
{
|
|
tokenA = path.toAddress(0);
|
|
fee = path.toUint24(ADDR_SIZE);
|
|
tokenB = path.toAddress(NEXT_OFFSET);
|
|
}
|
|
|
|
|
|
|
|
|
|
function getFirstPool(bytes memory path) internal pure returns (bytes memory) {
|
|
return path.slice(0, POP_OFFSET);
|
|
}
|
|
|
|
|
|
|
|
|
|
function skipToken(bytes memory path) internal pure returns (bytes memory) {
|
|
return path.slice(NEXT_OFFSET, path.length - NEXT_OFFSET);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
interface IKyberElastic {
|
|
struct ExactInputSingleParams {
|
|
address tokenIn;
|
|
address tokenOut;
|
|
uint24 fee;
|
|
address recipient;
|
|
uint256 deadline;
|
|
uint256 amountIn;
|
|
uint256 minAmountOut;
|
|
uint160 limitSqrtP;
|
|
}
|
|
|
|
|
|
|
|
|
|
function swapExactInputSingle(ExactInputSingleParams calldata params) external payable returns (uint256 amountOut);
|
|
|
|
struct ExactInputParams {
|
|
bytes path;
|
|
address recipient;
|
|
uint256 deadline;
|
|
uint256 amountIn;
|
|
uint256 minAmountOut;
|
|
}
|
|
|
|
|
|
|
|
|
|
function swapExactInput(ExactInputParams calldata params) external payable returns (uint256 amountOut);
|
|
|
|
struct ExactOutputSingleParams {
|
|
address tokenIn;
|
|
address tokenOut;
|
|
uint24 fee;
|
|
address recipient;
|
|
uint256 deadline;
|
|
uint256 amountOut;
|
|
uint256 maxAmountIn;
|
|
uint160 limitSqrtP;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function swapExactOutputSingle(ExactOutputSingleParams calldata params) external payable returns (uint256 amountIn);
|
|
|
|
struct ExactOutputParams {
|
|
bytes path;
|
|
address recipient;
|
|
uint256 deadline;
|
|
uint256 amountOut;
|
|
uint256 maxAmountIn;
|
|
}
|
|
|
|
|
|
|
|
|
|
function swapExactOutput(ExactOutputParams calldata params) external payable returns (uint256 amountIn);
|
|
}
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
interface IUniswapRouterV3 {
|
|
struct ExactInputSingleParams {
|
|
address tokenIn;
|
|
address tokenOut;
|
|
uint24 fee;
|
|
address recipient;
|
|
uint256 amountIn;
|
|
uint256 amountOutMinimum;
|
|
uint160 sqrtPriceLimitX96;
|
|
}
|
|
|
|
|
|
|
|
|
|
function exactInputSingle(ExactInputSingleParams calldata params) external payable returns (uint256 amountOut);
|
|
|
|
struct ExactInputParams {
|
|
bytes path;
|
|
address recipient;
|
|
uint256 amountIn;
|
|
uint256 amountOutMinimum;
|
|
}
|
|
|
|
|
|
|
|
|
|
function exactInput(ExactInputParams calldata params) external payable returns (uint256 amountOut);
|
|
|
|
struct ExactOutputSingleParams {
|
|
address tokenIn;
|
|
address tokenOut;
|
|
uint24 fee;
|
|
address recipient;
|
|
uint256 amountOut;
|
|
uint256 amountInMaximum;
|
|
uint160 sqrtPriceLimitX96;
|
|
}
|
|
|
|
|
|
|
|
|
|
function exactOutputSingle(ExactOutputSingleParams calldata params) external payable returns (uint256 amountIn);
|
|
|
|
struct ExactOutputParams {
|
|
bytes path;
|
|
address recipient;
|
|
uint256 amountOut;
|
|
uint256 amountInMaximum;
|
|
}
|
|
|
|
|
|
|
|
|
|
function exactOutput(ExactOutputParams calldata params) external payable returns (uint256 amountIn);
|
|
}
|
|
|
|
|
|
|
|
|
|
pragma solidity >=0.6.0;
|
|
pragma experimental ABIEncoderV2;
|
|
|
|
interface IUniswapRouterV3WithDeadline {
|
|
struct ExactInputSingleParams {
|
|
address tokenIn;
|
|
address tokenOut;
|
|
uint24 fee;
|
|
address recipient;
|
|
uint256 deadline;
|
|
uint256 amountIn;
|
|
uint256 amountOutMinimum;
|
|
uint160 sqrtPriceLimitX96;
|
|
}
|
|
|
|
|
|
|
|
|
|
function exactInputSingle(ExactInputSingleParams calldata params) external payable returns (uint256 amountOut);
|
|
|
|
struct ExactInputParams {
|
|
bytes path;
|
|
address recipient;
|
|
uint256 deadline;
|
|
uint256 amountIn;
|
|
uint256 amountOutMinimum;
|
|
}
|
|
|
|
|
|
|
|
|
|
function exactInput(ExactInputParams calldata params) external payable returns (uint256 amountOut);
|
|
|
|
struct ExactOutputSingleParams {
|
|
address tokenIn;
|
|
address tokenOut;
|
|
uint24 fee;
|
|
address recipient;
|
|
uint256 deadline;
|
|
uint256 amountOut;
|
|
uint256 amountInMaximum;
|
|
uint160 sqrtPriceLimitX96;
|
|
}
|
|
|
|
|
|
|
|
|
|
function exactOutputSingle(ExactOutputSingleParams calldata params) external payable returns (uint256 amountIn);
|
|
|
|
struct ExactOutputParams {
|
|
bytes path;
|
|
address recipient;
|
|
uint256 deadline;
|
|
uint256 amountOut;
|
|
uint256 amountInMaximum;
|
|
}
|
|
|
|
|
|
|
|
|
|
function exactOutput(ExactOutputParams calldata params) external payable returns (uint256 amountIn);
|
|
}
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
library UniV3Actions {
|
|
|
|
function kyberSwap(address _router, bytes memory _path, uint256 _amount) internal returns (uint256 amountOut) {
|
|
IKyberElastic.ExactInputParams memory swapParams = IKyberElastic.ExactInputParams({
|
|
path: _path,
|
|
recipient: address(this),
|
|
deadline: block.timestamp,
|
|
amountIn: _amount,
|
|
minAmountOut: 0
|
|
});
|
|
return IKyberElastic(_router).swapExactInput(swapParams);
|
|
}
|
|
|
|
|
|
function swapV3(address _router, bytes memory _path, uint256 _amount) internal returns (uint256 amountOut) {
|
|
IUniswapRouterV3.ExactInputParams memory swapParams = IUniswapRouterV3.ExactInputParams({
|
|
path: _path,
|
|
recipient: address(this),
|
|
amountIn: _amount,
|
|
amountOutMinimum: 0
|
|
});
|
|
return IUniswapRouterV3(_router).exactInput(swapParams);
|
|
}
|
|
|
|
|
|
function swapV3WithDeadline(address _router, bytes memory _path, uint256 _amount) internal returns (uint256 amountOut) {
|
|
IUniswapRouterV3WithDeadline.ExactInputParams memory swapParams = IUniswapRouterV3WithDeadline.ExactInputParams({
|
|
path: _path,
|
|
recipient: address(this),
|
|
deadline: block.timestamp,
|
|
amountIn: _amount,
|
|
amountOutMinimum: 0
|
|
});
|
|
return IUniswapRouterV3WithDeadline(_router).exactInput(swapParams);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
contract StrategyConvex is StratFeeManagerInitializable {
|
|
using Path for bytes;
|
|
using SafeERC20 for IERC20;
|
|
|
|
|
|
address public constant crv = 0xD533a949740bb3306d119CC777fa900bA034cd52;
|
|
address public constant cvx = 0x4e3FBD56CD56c3e72c1403e103b45Db9da5B9D2B;
|
|
address public constant native = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
|
|
address public constant unirouterV3 = 0xE592427A0AEce92De3Edee1F18E0157C05861564;
|
|
address public constant crvPool = 0x8301AE4fc9c624d1D396cbDAa1ed877821D7C511;
|
|
address public constant cvxPool = 0xB576491F1E6e5E62f1d8F26062Ee822B40B0E0d4;
|
|
IConvexBooster public constant booster = IConvexBooster(0xF403C135812408BFbE8713b5A23a04b3D48AAE31);
|
|
|
|
address public want;
|
|
address public pool;
|
|
address public zap;
|
|
address public depositToken;
|
|
address public rewardPool;
|
|
uint public pid;
|
|
uint public poolSize;
|
|
uint public depositIndex;
|
|
bool public useUnderlying;
|
|
bool public depositNative;
|
|
|
|
|
|
bytes public nativeToDepositPath;
|
|
address[] public nativeToDepositRoute;
|
|
|
|
struct RewardV3 {
|
|
address token;
|
|
bytes toNativePath;
|
|
uint minAmount;
|
|
}
|
|
RewardV3[] public rewardsV3;
|
|
|
|
struct RewardV2 {
|
|
address token;
|
|
address router;
|
|
address[] toNativeRoute;
|
|
uint minAmount;
|
|
}
|
|
RewardV2[] public rewards;
|
|
|
|
uint public curveSwapMinAmount;
|
|
bool public skipEarmarkRewards;
|
|
bool public harvestOnDeposit;
|
|
uint256 public lastHarvest;
|
|
|
|
event StratHarvest(address indexed harvester, uint256 wantHarvested, uint256 tvl);
|
|
event Deposit(uint256 tvl);
|
|
event Withdraw(uint256 tvl);
|
|
event ChargedFees(uint256 callFees, uint256 beefyFees, uint256 strategistFees);
|
|
|
|
function initialize(
|
|
address _want,
|
|
address _pool,
|
|
address _zap,
|
|
uint _pid,
|
|
uint[] calldata _params,
|
|
bytes calldata _nativeToDepositPath,
|
|
address[] calldata _nativeToDepositRoute,
|
|
CommonAddresses calldata _commonAddresses
|
|
) public initializer {
|
|
__StratFeeManager_init(_commonAddresses);
|
|
want = _want;
|
|
pool = _pool;
|
|
zap = _zap;
|
|
pid = _pid;
|
|
poolSize = _params[0];
|
|
depositIndex = _params[1];
|
|
useUnderlying = _params[2] > 0;
|
|
depositNative = _params[3] > 0;
|
|
(,,,rewardPool,,) = booster.poolInfo(_pid);
|
|
|
|
if (_nativeToDepositPath.length > 0) {
|
|
address[] memory nativeRoute = pathToRoute(_nativeToDepositPath);
|
|
require(nativeRoute[0] == native, '_nativeToDeposit[0] != native');
|
|
depositToken = nativeRoute[nativeRoute.length - 1];
|
|
nativeToDepositPath = _nativeToDepositPath;
|
|
} else {
|
|
require(_nativeToDepositRoute[0] == native, '_nativeToDepositRoute[0] != native');
|
|
depositToken = _nativeToDepositRoute[_nativeToDepositRoute.length - 1];
|
|
nativeToDepositRoute = _nativeToDepositRoute;
|
|
}
|
|
|
|
curveSwapMinAmount = 1e19;
|
|
withdrawalFee = 1;
|
|
_giveAllowances();
|
|
}
|
|
|
|
|
|
function deposit() public whenNotPaused {
|
|
uint256 wantBal = IERC20(want).balanceOf(address(this));
|
|
|
|
if (wantBal > 0) {
|
|
booster.deposit(pid, wantBal, true);
|
|
emit Deposit(balanceOf());
|
|
}
|
|
}
|
|
|
|
function withdraw(uint256 _amount) external {
|
|
require(msg.sender == vault, "!vault");
|
|
|
|
uint256 wantBal = IERC20(want).balanceOf(address(this));
|
|
|
|
if (wantBal < _amount) {
|
|
IConvexRewardPool(rewardPool).withdrawAndUnwrap(_amount - wantBal, false);
|
|
wantBal = IERC20(want).balanceOf(address(this));
|
|
}
|
|
|
|
if (wantBal > _amount) {
|
|
wantBal = _amount;
|
|
}
|
|
|
|
if (tx.origin != owner() && !paused()) {
|
|
uint256 withdrawalFeeAmount = wantBal * withdrawalFee / WITHDRAWAL_MAX;
|
|
wantBal = wantBal - withdrawalFeeAmount;
|
|
}
|
|
|
|
IERC20(want).safeTransfer(vault, wantBal);
|
|
|
|
emit Withdraw(balanceOf());
|
|
}
|
|
|
|
function beforeDeposit() external override {
|
|
if (harvestOnDeposit) {
|
|
require(msg.sender == vault, "!vault");
|
|
_harvest(tx.origin, true);
|
|
}
|
|
}
|
|
|
|
function harvest() external virtual {
|
|
_harvest(tx.origin, false);
|
|
}
|
|
|
|
function harvest(address callFeeRecipient) external virtual {
|
|
_harvest(callFeeRecipient, false);
|
|
}
|
|
|
|
function managerHarvest() external onlyManager {
|
|
_harvest(tx.origin, false);
|
|
}
|
|
|
|
|
|
function _harvest(address callFeeRecipient, bool onDeposit) internal whenNotPaused {
|
|
earmarkRewards();
|
|
IConvexRewardPool(rewardPool).getReward();
|
|
swapRewardsToNative();
|
|
uint256 nativeBal = IERC20(native).balanceOf(address(this));
|
|
if (nativeBal > 0) {
|
|
chargeFees(callFeeRecipient);
|
|
addLiquidity();
|
|
uint256 wantHarvested = balanceOfWant();
|
|
if (!onDeposit) {
|
|
deposit();
|
|
}
|
|
lastHarvest = block.timestamp;
|
|
emit StratHarvest(msg.sender, wantHarvested, balanceOf());
|
|
}
|
|
}
|
|
|
|
function earmarkRewards() internal {
|
|
if (!skipEarmarkRewards && IConvexRewardPool(rewardPool).periodFinish() < block.timestamp) {
|
|
booster.earmarkRewards(pid);
|
|
}
|
|
}
|
|
|
|
function swapRewardsToNative() internal {
|
|
if (curveSwapMinAmount > 0) {
|
|
uint bal = IERC20(crv).balanceOf(address(this));
|
|
if (bal > curveSwapMinAmount) {
|
|
ICurveSwap256(crvPool).exchange(1, 0, bal, 0);
|
|
}
|
|
bal = IERC20(cvx).balanceOf(address(this));
|
|
if (bal > curveSwapMinAmount) {
|
|
ICurveSwap256(cvxPool).exchange(1, 0, bal, 0);
|
|
}
|
|
}
|
|
for (uint i; i < rewardsV3.length; ++i) {
|
|
uint bal = IERC20(rewardsV3[i].token).balanceOf(address(this));
|
|
if (bal >= rewardsV3[i].minAmount) {
|
|
UniV3Actions.swapV3WithDeadline(unirouterV3, rewardsV3[i].toNativePath, bal);
|
|
}
|
|
}
|
|
for (uint i; i < rewards.length; ++i) {
|
|
uint bal = IERC20(rewards[i].token).balanceOf(address(this));
|
|
if (bal >= rewards[i].minAmount) {
|
|
IUniswapRouterETH(rewards[i].router).swapExactTokensForTokens(bal, 0, rewards[i].toNativeRoute, address(this), block.timestamp);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
function chargeFees(address callFeeRecipient) internal {
|
|
IFeeConfig.FeeCategory memory fees = getFees();
|
|
uint256 nativeBal = IERC20(native).balanceOf(address(this)) * fees.total / DIVISOR;
|
|
|
|
uint256 callFeeAmount = nativeBal * fees.call / DIVISOR;
|
|
IERC20(native).safeTransfer(callFeeRecipient, callFeeAmount);
|
|
|
|
uint256 beefyFeeAmount = nativeBal * fees.beefy / DIVISOR;
|
|
IERC20(native).safeTransfer(beefyFeeRecipient, beefyFeeAmount);
|
|
|
|
uint256 strategistFeeAmount = nativeBal * fees.strategist / DIVISOR;
|
|
IERC20(native).safeTransfer(strategist, strategistFeeAmount);
|
|
|
|
emit ChargedFees(callFeeAmount, beefyFeeAmount, strategistFeeAmount);
|
|
}
|
|
|
|
|
|
function addLiquidity() internal {
|
|
uint256 depositBal;
|
|
uint256 depositNativeAmount;
|
|
uint256 nativeBal = IERC20(native).balanceOf(address(this));
|
|
if (depositToken != native) {
|
|
if (nativeToDepositPath.length > 0) {
|
|
UniV3Actions.swapV3WithDeadline(unirouter, nativeToDepositPath, nativeBal);
|
|
} else {
|
|
IUniswapRouterETH(unirouter).swapExactTokensForTokens(nativeBal, 0, nativeToDepositRoute, address(this), block.timestamp);
|
|
}
|
|
depositBal = IERC20(depositToken).balanceOf(address(this));
|
|
} else {
|
|
depositBal = nativeBal;
|
|
if (depositNative) {
|
|
depositNativeAmount = nativeBal;
|
|
IWrappedNative(native).withdraw(depositNativeAmount);
|
|
}
|
|
}
|
|
|
|
if (poolSize == 2) {
|
|
uint256[2] memory amounts;
|
|
amounts[depositIndex] = depositBal;
|
|
if (useUnderlying) ICurveSwap(pool).add_liquidity(amounts, 0, true);
|
|
else ICurveSwap(pool).add_liquidity{value: depositNativeAmount}(amounts, 0);
|
|
} else if (poolSize == 3) {
|
|
uint256[3] memory amounts;
|
|
amounts[depositIndex] = depositBal;
|
|
if (useUnderlying) ICurveSwap(pool).add_liquidity(amounts, 0, true);
|
|
else if (zap != address(0)) ICurveSwap(zap).add_liquidity{value: depositNativeAmount}(pool, amounts, 0);
|
|
else ICurveSwap(pool).add_liquidity{value: depositNativeAmount}(amounts, 0);
|
|
} else if (poolSize == 4) {
|
|
uint256[4] memory amounts;
|
|
amounts[depositIndex] = depositBal;
|
|
if (zap != address(0)) ICurveSwap(zap).add_liquidity(pool, amounts, 0);
|
|
else ICurveSwap(pool).add_liquidity(amounts, 0);
|
|
} else if (poolSize == 5) {
|
|
uint256[5] memory amounts;
|
|
amounts[depositIndex] = depositBal;
|
|
if (zap != address(0)) ICurveSwap(zap).add_liquidity(pool, amounts, 0);
|
|
ICurveSwap(pool).add_liquidity(amounts, 0);
|
|
}
|
|
}
|
|
|
|
function addRewardV2(address _router, address[] calldata _rewardToNativeRoute, uint _minAmount) external onlyOwner {
|
|
address token = _rewardToNativeRoute[0];
|
|
require(token != want, "!want");
|
|
require(token != native, "!native");
|
|
|
|
rewards.push(RewardV2(token, _router, _rewardToNativeRoute, _minAmount));
|
|
IERC20(token).approve(_router, 0);
|
|
IERC20(token).approve(_router, type(uint).max);
|
|
}
|
|
|
|
function addRewardV3(bytes memory _rewardToNativePath, uint _minAmount) external onlyOwner {
|
|
address[] memory _rewardToNativeRoute = pathToRoute(_rewardToNativePath);
|
|
address token = _rewardToNativeRoute[0];
|
|
require(token != want, "!want");
|
|
require(token != native, "!native");
|
|
|
|
rewardsV3.push(RewardV3(token, _rewardToNativePath, _minAmount));
|
|
IERC20(token).approve(unirouterV3, 0);
|
|
IERC20(token).approve(unirouterV3, type(uint).max);
|
|
}
|
|
|
|
function resetRewardsV2() external onlyManager {
|
|
delete rewards;
|
|
}
|
|
|
|
function resetRewardsV3() external onlyManager {
|
|
delete rewardsV3;
|
|
}
|
|
|
|
|
|
function balanceOf() public view returns (uint256) {
|
|
return balanceOfWant() + balanceOfPool();
|
|
}
|
|
|
|
|
|
function balanceOfWant() public view returns (uint256) {
|
|
return IERC20(want).balanceOf(address(this));
|
|
}
|
|
|
|
|
|
function balanceOfPool() public view returns (uint256) {
|
|
return IConvexRewardPool(rewardPool).balanceOf(address(this));
|
|
}
|
|
|
|
function pathToRoute(bytes memory _path) public pure returns (address[] memory) {
|
|
uint numPools = _path.numPools();
|
|
address[] memory route = new address[](numPools + 1);
|
|
for (uint i; i < numPools; i++) {
|
|
(address tokenA, address tokenB,) = _path.decodeFirstPool();
|
|
route[i] = tokenA;
|
|
route[i + 1] = tokenB;
|
|
_path = _path.skipToken();
|
|
}
|
|
return route;
|
|
}
|
|
|
|
function nativeToDeposit() external view returns (address[] memory) {
|
|
if (nativeToDepositPath.length > 0) {
|
|
return pathToRoute(nativeToDepositPath);
|
|
} else return nativeToDepositRoute;
|
|
}
|
|
|
|
function rewardV3ToNative() external view returns (address[] memory) {
|
|
return pathToRoute(rewardsV3[0].toNativePath);
|
|
}
|
|
|
|
function rewardV3ToNative(uint i) external view returns (address[] memory) {
|
|
return pathToRoute(rewardsV3[i].toNativePath);
|
|
}
|
|
|
|
function rewardsV3Length() external view returns (uint) {
|
|
return rewardsV3.length;
|
|
}
|
|
|
|
function rewardToNative() external view returns (address[] memory) {
|
|
return rewards[0].toNativeRoute;
|
|
}
|
|
|
|
function rewardToNative(uint i) external view returns (address[] memory) {
|
|
return rewards[i].toNativeRoute;
|
|
}
|
|
|
|
function rewardsLength() external view returns (uint) {
|
|
return rewards.length;
|
|
}
|
|
|
|
function setDepositNative(bool _depositNative) external onlyOwner {
|
|
depositNative = _depositNative;
|
|
}
|
|
|
|
function setSkipEarmarkRewards(bool _skipEarmarkRewards) external onlyManager {
|
|
skipEarmarkRewards = _skipEarmarkRewards;
|
|
}
|
|
|
|
function setCurveSwapMinAmount(uint _minAmount) external onlyManager {
|
|
curveSwapMinAmount = _minAmount;
|
|
}
|
|
|
|
function setHarvestOnDeposit(bool _harvestOnDeposit) external onlyManager {
|
|
harvestOnDeposit = _harvestOnDeposit;
|
|
if (harvestOnDeposit) {
|
|
setWithdrawalFee(0);
|
|
} else {
|
|
setWithdrawalFee(1);
|
|
}
|
|
}
|
|
|
|
|
|
function rewardsAvailable() public view returns (uint256) {
|
|
return IConvexRewardPool(rewardPool).earned(address(this));
|
|
}
|
|
|
|
|
|
function callReward() public pure returns (uint256) {
|
|
return 0;
|
|
}
|
|
|
|
|
|
function retireStrat() external {
|
|
require(msg.sender == vault, "!vault");
|
|
|
|
IConvexRewardPool(rewardPool).withdrawAllAndUnwrap(false);
|
|
|
|
uint256 wantBal = IERC20(want).balanceOf(address(this));
|
|
IERC20(want).transfer(vault, wantBal);
|
|
}
|
|
|
|
|
|
function panic() public onlyManager {
|
|
pause();
|
|
IConvexRewardPool(rewardPool).withdrawAllAndUnwrap(false);
|
|
}
|
|
|
|
function pause() public onlyManager {
|
|
_pause();
|
|
|
|
_removeAllowances();
|
|
}
|
|
|
|
function unpause() external onlyManager {
|
|
_unpause();
|
|
|
|
_giveAllowances();
|
|
|
|
deposit();
|
|
}
|
|
|
|
function _giveAllowances() internal {
|
|
IERC20(want).approve(address(booster), type(uint).max);
|
|
IERC20(native).approve(unirouter, type(uint).max);
|
|
IERC20(depositToken).approve(pool, type(uint).max);
|
|
if (zap != address(0)) IERC20(depositToken).approve(zap, type(uint).max);
|
|
IERC20(crv).approve(crvPool, type(uint).max);
|
|
IERC20(cvx).approve(cvxPool, type(uint).max);
|
|
}
|
|
|
|
function _removeAllowances() internal {
|
|
IERC20(want).approve(address(booster), 0);
|
|
IERC20(native).approve(unirouter, 0);
|
|
IERC20(depositToken).approve(pool, 0);
|
|
if (zap != address(0)) IERC20(depositToken).approve(zap, 0);
|
|
IERC20(crv).approve(crvPool, 0);
|
|
IERC20(cvx).approve(cvxPool, 0);
|
|
}
|
|
|
|
receive () external payable {}
|
|
} |