// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v4.8.0) (utils/Address.sol)pragmasolidity ^0.8.1;/**
* @dev Collection of functions related to the address type
*/libraryAddress{
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
* ====
*
* [IMPORTANT]
* ====
* You shouldn't rely on `isContract` to protect against flash loan attacks!
*
* Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
* like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
* constructor.
* ====
*/functionisContract(address account) internalviewreturns (bool) {
// This method relies on extcodesize/address.code.length, which returns 0// for contracts in construction, since the code is only stored at the end// of the constructor execution.return account.code.length>0;
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/functionsendValue(addresspayable recipient, uint256 amount) internal{
require(
address(this).balance>= amount,
"Address: insufficient balance"
);
(bool success, ) = recipient.call{value: amount}("");
require(
success,
"Address: unable to send value, recipient may have reverted"
);
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain `call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason, it is bubbled up by this
* function (like regular Solidity function calls).
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*
* _Available since v3.1._
*/functionfunctionCall(address target,
bytesmemory data
) internalreturns (bytesmemory) {
return
functionCallWithValue(
target,
data,
0,
"Address: low-level call failed"
);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
* `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/functionfunctionCall(address target,
bytesmemory data,
stringmemory errorMessage
) internalreturns (bytesmemory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*
* _Available since v3.1._
*/functionfunctionCallWithValue(address target,
bytesmemory data,
uint256 value
) internalreturns (bytesmemory) {
return
functionCallWithValue(
target,
data,
value,
"Address: low-level call with value failed"
);
}
/**
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
* with `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/functionfunctionCallWithValue(address target,
bytesmemory data,
uint256 value,
stringmemory errorMessage
) internalreturns (bytesmemory) {
require(
address(this).balance>= value,
"Address: insufficient balance for call"
);
(bool success, bytesmemory returndata) = target.call{value: value}(
data
);
return
verifyCallResultFromTarget(
target,
success,
returndata,
errorMessage
);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/functionfunctionStaticCall(address target,
bytesmemory data
) internalviewreturns (bytesmemory) {
return
functionStaticCall(
target,
data,
"Address: low-level static call failed"
);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/functionfunctionStaticCall(address target,
bytesmemory data,
stringmemory errorMessage
) internalviewreturns (bytesmemory) {
(bool success, bytesmemory returndata) = target.staticcall(data);
return
verifyCallResultFromTarget(
target,
success,
returndata,
errorMessage
);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/functionfunctionDelegateCall(address target,
bytesmemory data
) internalreturns (bytesmemory) {
return
functionDelegateCall(
target,
data,
"Address: low-level delegate call failed"
);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/functionfunctionDelegateCall(address target,
bytesmemory data,
stringmemory errorMessage
) internalreturns (bytesmemory) {
(bool success, bytesmemory returndata) = target.delegatecall(data);
return
verifyCallResultFromTarget(
target,
success,
returndata,
errorMessage
);
}
/**
* @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
* the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
*
* _Available since v4.8._
*/functionverifyCallResultFromTarget(address target,
bool success,
bytesmemory returndata,
stringmemory errorMessage
) internalviewreturns (bytesmemory) {
if (success) {
if (returndata.length==0) {
// only check isContract if the call was successful and the return data is empty// otherwise we already know that it was a contractrequire(isContract(target), "Address: call to non-contract");
}
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
/**
* @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
* revert reason or using the provided one.
*
* _Available since v4.3._
*/functionverifyCallResult(bool success,
bytesmemory returndata,
stringmemory errorMessage
) internalpurereturns (bytesmemory) {
if (success) {
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function_revert(bytesmemory returndata,
stringmemory errorMessage
) privatepure{
// Look for revert reason and bubble it up if presentif (returndata.length>0) {
// The easiest way to bubble the revert reason is using memory via assembly/// @solidity memory-safe-assemblyassembly {
let returndata_size :=mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)pragmasolidity ^0.8.0;/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/abstractcontractContext{
function_msgSender() internalviewvirtualreturns (address) {
returnmsg.sender;
}
function_msgData() internalviewvirtualreturns (bytescalldata) {
returnmsg.data;
}
}
Contract Source Code
File 4 of 20: Errors.sol
// SPDX-License-Identifier: gpl-3.0pragmasolidity ^0.8.0;/**
* @title Errors library
* @notice Defines the error messages emitted by the different contracts
* @dev Error messages prefix glossary:
* - VL = ValidationLogic
* - VT = Vault
* - LP = LendingPool
* - P = Pausable
*/libraryErrors{
//contract specific errorsstringinternalconstant VL_TRANSACTION_TOO_OLD ="0"; // 'Transaction too old'stringinternalconstant VL_NO_ACTIVE_RESERVE ="1"; // 'Action requires an active reserve'stringinternalconstant VL_RESERVE_FROZEN ="2"; // 'Action cannot be performed because the reserve is frozen'stringinternalconstant VL_CURRENT_AVAILABLE_LIQUIDITY_NOT_ENOUGH ="3"; // 'The current liquidity is not enough'stringinternalconstant VL_NOT_ENOUGH_AVAILABLE_USER_BALANCE ="4"; // 'User cannot withdraw more than the available balance'stringinternalconstant VL_TRANSFER_NOT_ALLOWED ="5"; // 'Transfer cannot be allowed.'stringinternalconstant VL_BORROWING_NOT_ENABLED ="6"; // 'Borrowing is not enabled'stringinternalconstant VL_INVALID_DEBT_OWNER ="7"; // 'Invalid interest rate mode selected'stringinternalconstant VL_BORROWING_CALLER_NOT_IN_WHITELIST ="8"; // 'The collateral balance is 0'stringinternalconstant VL_DEPOSIT_TOO_MUCH ="9"; // 'Deposit too much'stringinternalconstant VL_OUT_OF_CAPACITY ="10"; // 'There is not enough collateral to cover a new borrow'stringinternalconstant VL_OUT_OF_CREDITS ="11"; // 'Out of credits, there is not enough credits to borrow'stringinternalconstant VL_PERCENT_TOO_LARGE ="12"; // 'Percentage too large'stringinternalconstant VL_ADDRESS_CANNOT_ZERO ="13"; // vault address cannot be zerostringinternalconstant VL_VAULT_UN_ACTIVE ="14";
stringinternalconstant VL_VAULT_FROZEN ="15";
stringinternalconstant VL_VAULT_BORROWING_DISABLED ="16";
stringinternalconstant VL_NOT_WETH9 ="17";
stringinternalconstant VL_INSUFFICIENT_WETH9 ="18";
stringinternalconstant VL_INSUFFICIENT_TOKEN ="19";
stringinternalconstant VL_LIQUIDATOR_NOT_IN_WHITELIST ="20";
stringinternalconstant VL_COMPOUNDER_NOT_IN_WHITELIST ="21";
stringinternalconstant VL_VAULT_ALREADY_INITIALIZED ="22";
stringinternalconstant VL_TREASURY_ADDRESS_NOT_SET ="23";
stringinternalconstant VT_INVALID_RESERVE_ID ="40"; // invalid reserve idstringinternalconstant VT_INVALID_POOL ="41"; // invalid uniswap v3 poolstringinternalconstant VT_INVALID_VAULT_POSITION_MANAGER ="42"; // invalid vault position managerstringinternalconstant VT_VAULT_POSITION_NOT_ACTIVE ="43"; // vault position is not activestringinternalconstant VT_VAULT_POSITION_AUTO_COMPOUND_NOT_ENABLED ="44"; // 'auto compound not enabled'stringinternalconstant VT_VAULT_POSITION_ID_INVALID ="45"; // 'VaultPositionId invalid'stringinternalconstant VT_VAULT_PAUSED ="46"; // 'vault is paused'stringinternalconstant VT_VAULT_FROZEN ="47"; // 'vault is frozen'stringinternalconstant VT_VAULT_CALLBACK_INVALID_SENDER ="48"; // 'callback must be initiate by the vault selfstringinternalconstant VT_VAULT_DEBT_RATIO_TOO_LOW_TO_LIQUIDATE ="49"; // 'debt ratio haven't reach liquidate ratio'stringinternalconstant VT_VAULT_POSITION_MANAGER_INVALID ="50"; // 'invalid vault manager'stringinternalconstant VT_VAULT_POSITION_RANGE_STOP_DISABLED ="60"; // 'vault positions' range stop is disabled'stringinternalconstant VT_VAULT_POSITION_RANGE_STOP_PRICE_INVALID ="61"; // 'invalid range stop price'stringinternalconstant VT_VAULT_POSITION_OUT_OF_MAX_LEVERAGE ="62";
stringinternalconstant VT_VAULT_POSITION_SHARES_INVALID ="63";
stringinternalconstant LP_NOT_ENOUGH_LIQUIDITY_TO_BORROW ="80"; // 'There is not enough liquidity available to borrow'stringinternalconstant LP_CALLER_MUST_BE_LENDING_POOL ="81"; // 'Caller must be lending pool contract'stringinternalconstant LP_BORROW_INDEX_OVERFLOW ="82"; // 'The borrow index overflow'stringinternalconstant LP_IS_PAUSED ="83"; // lending pool is paused
}
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)pragmasolidity ^0.8.0;/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/interfaceIERC20{
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/eventTransfer(addressindexedfrom, addressindexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/eventApproval(addressindexed owner,
addressindexed spender,
uint256 value
);
/**
* @dev Returns the amount of tokens in existence.
*/functiontotalSupply() externalviewreturns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/functionbalanceOf(address account) externalviewreturns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/functiontransfer(address to, uint256 amount) externalreturns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/functionallowance(address owner,
address spender
) externalviewreturns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/functionapprove(address spender, uint256 amount) externalreturns (bool);
/**
* @dev Moves `amount` tokens from `from` to `to` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/functiontransferFrom(addressfrom,
address to,
uint256 amount
) externalreturns (bool);
}
// SPDX-License-Identifier: gpl-3.0pragmasolidity ^0.8.0;import"../external/velodrome/contracts/interfaces/IRouter.sol";
import"../libraries/types/VaultTypes.sol";
interfaceIVeloVaultPositionManager{
/// @notice New a vaultPosition/// @param vaultId The id of the vault/// @param manager The manager of the position, usually the caller/// @param vaultPositionId The id of the newed positioneventNewVaultPosition(uint256indexed vaultId,
uint256indexed vaultPositionId,
addressindexed manager
);
/// @notice Invest to a position/// @param vaultId The id of the vault/// @param vaultPositionId The id of the new vaultPosition/// @param manager The manager of the vaultPosition, usually the caller/// @param amount0Invest The amount of token0 user wants to transfer/// @param amount1Invest The amount of token1 user wants to transfer/// @param amount0Borrow The amount of token0 user wants to borrow/// @param amount1Borrow The amount of token1 user wants to borrow/// @param liquidity The amount of lp tokens added to the pooleventInvestToVaultPosition(uint256indexed vaultId,
uint256indexed vaultPositionId,
addressindexed manager,
uint256 amount0Invest,
uint256 amount1Invest,
uint256 amount0Borrow,
uint256 amount1Borrow,
uint256 liquidity
);
/// @notice Close a position partially/// @param vaultId The id of the vault/// @param vaultPositionId The id of the vaultPosition/// @param manager The manager of the vaultPosition/// @param percent The percentage of the vault user want to close/// @param amount0Received The amount of token0 user received after close/// @param amount1Received The amount of token1 user received after close/// @param amount0Repaid The amount of token0 user repaid when close/// @param amount1Repaid The amount of token1 user repaid when closeeventCloseVaultPositionPartially(uint256indexed vaultId,
uint256indexed vaultPositionId,
addressindexed manager,
uint16 percent,
uint256 amount0Received,
uint256 amount1Received,
uint256 amount0Repaid,
uint256 amount1Repaid
);
/// @notice Close a position which is outof price range/// @param vaultId The id of the vault/// @param vaultPositionId The id of the vaultPosition/// @param manager The manager of the vaultPosition/// @param caller The caller initiate range stop/// @param percent The percentage of the vault user want to close/// @param amount0Received The amount of token0 user received after close/// @param amount1Received The amount of token1 user received after close/// @param fee0 The caller received/// @param fee1 The caller receivedeventCloseOutOfRangePosition(uint256indexed vaultId,
uint256indexed vaultPositionId,
addressindexed manager,
address caller,
uint16 percent,
uint64 timestamp,
uint256 price,
uint256 amount0Received,
uint256 amount1Received,
uint256 fee0,
uint256 fee1
);
/// @notice Liquidate a position partially/// @param vaultId The id of the vault/// @param vaultPositionId The id of the vaultPosition/// @param manager The manager of the vaultPosition/// @param liquidator The caller of the function/// @param percent The percentage of the vault user want to liquidate/// @param amount0Left The amount of token0 transferred to the position's manager after close/// @param amount1Left The amount of token1 transferred to the position's manager after close/// @param liquidateFee0 The amount of token0 for liquidation bonus/// @param liquidateFee1 The amount of token1 for liquidation bonuseventLiquidateVaultPositionPartially(uint256indexed vaultId,
uint256indexed vaultPositionId,
addressindexed manager,
address liquidator,
uint16 percent,
uint64 timestamp,
uint256 price,
uint256 debtValueOfPosition,
uint256 liquidityValueOfPosition,
uint256 amount0Left,
uint256 amount1Left,
uint256 liquidateFee0,
uint256 liquidateFee1
);
/// @notice Liquidate a position partially/// @param vaultId The id of the vault/// @param vaultPositionId The id of the vaultPosition/// @param manager The manager of the vaultPosition/// @param caller The initiator of the repay action/// @param amount0Repaid The amount of token0 repaid/// @param amount1Repaid The amount of token1 repaideventExactRepay(uint256indexed vaultId,
uint256indexed vaultPositionId,
addressindexed manager,
address caller,
uint256 amount0Repaid,
uint256 amount1Repaid
);
/// @notice InvestEarnedFeeToLiquidity/// @param vaultId The id of the vault/// @param caller The initiator of the repay action/// @param liquidityAdded The liquidity amount added by adding rewards to the pool/// @param fee0 The fee in token0/// @param fee1 The fee in token1/// @param rewards The rewards claimedeventInvestEarnedFeeToLiquidity(uint256indexed vaultId,
addressindexed caller,
uint256 liquidityAdded,
uint256 fee0,
uint256 fee1,
uint256[] rewards
);
eventFeePaid(uint256indexed vaultId,
addressindexed asset,
uint256indexed feeType,
uint256 amount
);
functiongetVaultPosition(uint256 vaultId,
uint256 vaultPositionId
) externalviewreturns (VaultTypes.VeloPositionValue memory state);
functiongetVault(uint256 vaultId
) externalviewreturns (VaultTypes.VeloVaultState memory);
structPayToVaultCallbackParams {
uint256 vaultId;
uint256 amount0;
uint256 amount1;
address payer;
}
/// @notice Callback functions called by the vault to pay tokens to the vault contract./// The caller to this function must be the vault contractfunctionpayToVaultCallback(
PayToVaultCallbackParams calldata params
) external;
/// @notice Callback functions called by the vault to pay fees to treasury./// The caller to this function must be the vault contractfunctionpayFeeToTreasuryCallback(uint256 vaultId,
address asset,
uint256 amount,
uint256 feeType
) external;
/// @notice The param struct used in function newOrInvestToVaultPosition(param)structNewOrInvestToVaultPositionParams {
// vaultIduint256 vaultId;
// The vaultPositionId of the vaultPosition to invest// 0 if open a new vaultPositionuint256 vaultPositionId;
// The amount of token0 user want to investuint256 amount0Invest;
// The amount of token0 user want to borrowuint256 amount0Borrow;
// The amount of token1 user want to investuint256 amount1Invest;
// The amount of token1 user want to borrowuint256 amount1Borrow;
// The minimal amount of token0 should be added to the liquidity// This value will be used when call mint() or addLiquidity() of uniswap V3 pooluint256 amount0Min;
// The minimal amount of token1 should be added to the liquidity// This value will be used when call mint() or addLiquidity() of uniswap V3 pooluint256 amount1Min;
// The deadline of the txuint256 deadline;
// The swapExecutor to swap tokensuint256 swapExecutorId;
// The swap path to swap tokensbytes swapPath;
}
/// @notice Open a new vaultPosition or invest to a existed vault position/// @param params The parameters necessary, encoded as `NewOrInvestToVaultPositionParams` in calldatafunctionnewOrInvestToVaultPosition(
NewOrInvestToVaultPositionParams calldata params
) externalpayable;
/// @notice The param struct used in function closeVaultPositionPartially(param)structCloseVaultPositionPartiallyParams {
// vaultIduint256 vaultId;
// The Id of vaultPosition to be closeduint256 vaultPositionId;
// The percentage of the entire position to closeuint16 percent;
// The receiver of the left tokens when close then position// Or the fee receiver when used in `closeOutOfRangePosition`address receiver;
bool receiveNativeETH;
// The receiveType of the left tokens// 0: only receive token0, swap all left token1 to token0// 1: only receive token1, swap all left token0 to token1// 2: receive tokens according to minimal swap ruleuint8 receiveType;
// The minimal token0 receive after remove the liquidity, will be used when call removeLiquidity() of uniswapuint256 minAmount0WhenRemoveLiquidity;
// The minimal token1 receive after remove the liquidity, will be used when call removeLiquidity() of uniswapuint256 minAmount1WhenRemoveLiquidity;
// The deadline of the tx, the tx will be reverted if the _blockTimestamp() > deadlineuint256 deadline;
// The swapExecutor to swap tokensuint256 swapExecutorId;
// The swap path to swap tokensbytes swapPath;
}
/// @notice Close a vaultPosition partially/// @param params The parameters necessary, encoded as `CloseVaultPositionPartiallyParams` in calldatafunctioncloseVaultPositionPartially(
CloseVaultPositionPartiallyParams calldata params
) external;
/// @notice The param struct used in function closeVaultPositionPartially(param)structLiquidateVaultPositionPartiallyParams {
// vaultIduint256 vaultId;
// The Id of vaultPosition to be closeduint256 vaultPositionId;
// The percentage of the entire position to closeuint16 percent;
// The liquidator's fee receiveraddress receiver;
bool receiveNativeETH;
// The receiveType of the left tokens// 0: only receive token0, swap all left token1 to token0// 1: only receive token1, swap all left token0 to token1// 2: receive tokens according to minimal swap ruleuint8 receiveType;
// The minimal token0 receive after remove the liquidity, will be used when call removeLiquidity() of uniswapuint256 minAmount0WhenRemoveLiquidity;
// The minimal token1 receive after remove the liquidity, will be used when call removeLiquidity() of uniswapuint256 minAmount1WhenRemoveLiquidity;
// The deadline of the tx, the tx will be reverted if the _blockTimestamp() > deadlineuint256 deadline;
// The maximum amount of token0 to repay debtsuint256 maxRepay0;
// The maximum amount of token1 to repay debtsuint256 maxRepay1;
// The swapExecutor to swap tokensuint256 swapExecutorId;
// The swap path to swap tokensbytes swapPath;
}
/// @notice Liquidate a vaultPosition partially/// @param params The parameters necessary, encoded as `CloseVaultPositionPartiallyParam` in calldatafunctionliquidateVaultPositionPartially(
LiquidateVaultPositionPartiallyParams calldata params
) externalpayable;
structInvestEarnedFeeToLiquidityParam {
// vaultIduint256 vaultId;
// The compound fee receiveaddress compoundFeeReceiver;
IRouter.route[][] routes;
bool receiveNativeETH;
// The deadline of the tx, the tx will be reverted if the _blockTimestamp() > deadlineuint256 deadline;
}
/// @notice Invest the earned fee by the position to liquidity/// The manager of the position can call this function with no compound fee charged/// If the manager allow others to compound the fee, there will be a small fee charged as bonus for the callerfunctioninvestEarnedFeeToLiquidity(
InvestEarnedFeeToLiquidityParam calldata params
) external;
structExactRepayParam {
// vaultIduint256 vaultId;
// The Id of vaultPosition to be closeduint256 vaultPositionId;
// The max amount of token0 to repayuint256 amount0ToRepay;
// The max amount of token1 to repayuint256 amount1ToRepay;
// whether receive nativeETH or WETH when there are un-repaid ETHbool receiveNativeETH;
// The deadline of the tx, the tx will be reverted if the _blockTimestamp() > deadlineuint256 deadline;
}
/// @notice Repay exact value of debtsfunctionexactRepay(
ExactRepayParam calldata params
) externalpayablereturns (uint256, uint256);
/// @notice Transfer the position's manager to another wallet/// Must be called by the current manager of the posistion/// @param vaultId The Id of the vault/// @param vaultPositionId The Id of the position/// @param newManager The new address of the managerfunctiontransferManagerOfVaultPosition(uint256 vaultId,
uint256 vaultPositionId,
address newManager
) external;
/// @notice Set stop-loss price range of the position/// Users can set a stop-loss price range for a position only if the position is enabled `RangeStop` feature./// If current price goes out of the stop-loss price range, extraFi's bots will close the position/// @param vaultId The Id of the vault/// @param vaultPositionId The Id of the position/// @param enable The enable status to set/// @param minPrice The lower price of the stop-loss price range/// @param maxPrice The upper price of the stop-loss price rangefunctionsetRangeStop(uint256 vaultId,
uint256 vaultPositionId,
bool enable,
uint256 minPrice,
uint256 maxPrice
) external;
}
Contract Source Code
File 11 of 20: IWETH9.sol
// SPDX-License-Identifier: GPL-2.0-or-laterpragmasolidity ^0.8.0;import"../external/openzeppelin/contracts/token/ERC20/IERC20.sol";
/// @title Interface for WETH9interfaceIWETH9isIERC20{
/// @notice Deposit ether to get wrapped etherfunctiondeposit() externalpayable;
/// @notice Withdraw wrapped ether to get etherfunctionwithdraw(uint256) external;
}
Contract Source Code
File 12 of 20: Ownable.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)pragmasolidity ^0.8.0;import"../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/abstractcontractOwnableisContext{
addressprivate _owner;
eventOwnershipTransferred(addressindexed previousOwner,
addressindexed newOwner
);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/constructor() {
_transferOwnership(_msgSender());
}
/**
* @dev Throws if called by any account other than the owner.
*/modifieronlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/functionowner() publicviewvirtualreturns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/function_checkOwner() internalviewvirtual{
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/functionrenounceOwnership() publicvirtualonlyOwner{
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/functiontransferOwnership(address newOwner) publicvirtualonlyOwner{
require(
newOwner !=address(0),
"Ownable: new owner is the zero address"
);
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/function_transferOwnership(address newOwner) internalvirtual{
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
Contract Source Code
File 13 of 20: Payments.sol
// SPDX-License-Identifier: UNLICENSEDpragmasolidity ^0.8.0;pragmaabicoderv2;import"./interfaces/IWETH9.sol";
import"./libraries/TransferHelper.sol";
import"./libraries/helpers/Errors.sol";
abstractcontractPayments{
addresspublicimmutable WETH9;
modifieravoidUsingNativeEther() {
require(msg.value==0, "avoid using native ether");
_;
}
constructor(address _WETH9) {
WETH9 = _WETH9;
}
receive() externalpayable{
require(msg.sender== WETH9, Errors.VL_NOT_WETH9);
}
functionunwrapWETH9(uint256 amountMinimum, address recipient) internal{
uint256 balanceWETH9 = IWETH9(WETH9).balanceOf(address(this));
require(balanceWETH9 >= amountMinimum, Errors.VL_INSUFFICIENT_WETH9);
if (balanceWETH9 >0) {
IWETH9(WETH9).withdraw(balanceWETH9);
TransferHelper.safeTransferETH(recipient, balanceWETH9);
}
}
functionrefundETH() internal{
if (address(this).balance>0)
TransferHelper.safeTransferETH(msg.sender, address(this).balance);
}
/// @param token The token to pay/// @param payer The entity that must pay/// @param recipient The entity that will receive payment/// @param value The amount to payfunctionpay(address token,
address payer,
address recipient,
uint256 value
) internal{
if (token == WETH9 &&address(this).balance>= value) {
// pay with WETH9
IWETH9(WETH9).deposit{value: value}(); // wrap only what is needed to payrequire(
IWETH9(WETH9).transfer(recipient, value),
"transfer failed"
);
} elseif (payer ==address(this)) {
// pay with tokens already in the contract (for the exact input multihop case)
TransferHelper.safeTransfer(token, recipient, value);
} else {
// pull payment
TransferHelper.safeTransferFrom(token, payer, recipient, value);
}
}
}
Contract Source Code
File 14 of 20: ReentrancyGuard.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v4.8.0) (security/ReentrancyGuard.sol)pragmasolidity ^0.8.0;/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/abstractcontractReentrancyGuard{
// Booleans are more expensive than uint256 or any type that takes up a full// word because each write operation emits an extra SLOAD to first read the// slot's contents, replace the bits taken up by the boolean, and then write// back. This is the compiler's defense against contract upgrades and// pointer aliasing, and it cannot be disabled.// The values being non-zero value makes deployment a bit more expensive,// but in exchange the refund on every call to nonReentrant will be lower in// amount. Since refunds are capped to a percentage of the total// transaction's gas, it is best to keep them low in cases like this one, to// increase the likelihood of the full refund coming into effect.uint256privateconstant _NOT_ENTERED =1;
uint256privateconstant _ENTERED =2;
uint256private _status;
constructor() {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/modifiernonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function_nonReentrantBefore() private{
// On the first call to nonReentrant, _status will be _NOT_ENTEREDrequire(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
}
function_nonReentrantAfter() private{
// By storing the original value once again, a refund is triggered (see// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
}
Contract Source Code
File 15 of 20: SafeERC20.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v4.8.0) (token/ERC20/utils/SafeERC20.sol)pragmasolidity ^0.8.0;import"../IERC20.sol";
import"../extensions/draft-IERC20Permit.sol";
import"../../../utils/Address.sol";
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/librarySafeERC20{
usingAddressforaddress;
functionsafeTransfer(IERC20 token, address to, uint256 value) internal{
_callOptionalReturn(
token,
abi.encodeWithSelector(token.transfer.selector, to, value)
);
}
functionsafeTransferFrom(
IERC20 token,
addressfrom,
address to,
uint256 value
) internal{
_callOptionalReturn(
token,
abi.encodeWithSelector(token.transferFrom.selector, from, to, value)
);
}
/**
* @dev Deprecated. This function has issues similar to the ones found in
* {IERC20-approve}, and its usage is discouraged.
*
* Whenever possible, use {safeIncreaseAllowance} and
* {safeDecreaseAllowance} instead.
*/functionsafeApprove(
IERC20 token,
address spender,
uint256 value
) internal{
// safeApprove should only be called when setting an initial allowance,// or when resetting it to zero. To increase and decrease it, use// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'require(
(value ==0) || (token.allowance(address(this), spender) ==0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(
token,
abi.encodeWithSelector(token.approve.selector, spender, value)
);
}
functionsafeIncreaseAllowance(
IERC20 token,
address spender,
uint256 value
) internal{
uint256 newAllowance = token.allowance(address(this), spender) + value;
_callOptionalReturn(
token,
abi.encodeWithSelector(
token.approve.selector,
spender,
newAllowance
)
);
}
functionsafeDecreaseAllowance(
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
)
);
}
}
functionsafePermit(
IERC20Permit token,
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal{
uint256 nonceBefore = token.nonces(owner);
token.permit(owner, spender, value, deadline, v, r, s);
uint256 nonceAfter = token.nonces(owner);
require(
nonceAfter == nonceBefore +1,
"SafeERC20: permit did not succeed"
);
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*/function_callOptionalReturn(IERC20 token, bytesmemory data) private{
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since// we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that// the target address contains contract code and also asserts for success in the low-level call.bytesmemory returndata =address(token).functionCall(
data,
"SafeERC20: low-level call failed"
);
if (returndata.length>0) {
// Return data is optionalrequire(
abi.decode(returndata, (bool)),
"SafeERC20: ERC20 operation did not succeed"
);
}
}
}
Contract Source Code
File 16 of 20: SafeMath.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v4.6.0) (utils/math/SafeMath.sol)pragmasolidity ^0.8.0;// CAUTION// This version of SafeMath should only be used with Solidity 0.8 or later,// because it relies on the compiler's built in overflow checks./**
* @dev Wrappers over Solidity's arithmetic operations.
*
* NOTE: `SafeMath` is generally not needed starting with Solidity 0.8, since the compiler
* now has built in overflow checking.
*/librarySafeMath{
/**
* @dev Returns the addition of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/functiontryAdd(uint256 a,
uint256 b
) internalpurereturns (bool, uint256) {
unchecked {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
}
/**
* @dev Returns the subtraction of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/functiontrySub(uint256 a,
uint256 b
) internalpurereturns (bool, uint256) {
unchecked {
if (b > a) return (false, 0);
return (true, a - b);
}
}
/**
* @dev Returns the multiplication of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/functiontryMul(uint256 a,
uint256 b
) internalpurereturns (bool, uint256) {
unchecked {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the// benefit is lost if 'b' is also tested.// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522if (a ==0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
}
/**
* @dev Returns the division of two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/functiontryDiv(uint256 a,
uint256 b
) internalpurereturns (bool, uint256) {
unchecked {
if (b ==0) return (false, 0);
return (true, a / b);
}
}
/**
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/functiontryMod(uint256 a,
uint256 b
) internalpurereturns (bool, uint256) {
unchecked {
if (b ==0) return (false, 0);
return (true, a % b);
}
}
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/functionadd(uint256 a, uint256 b) internalpurereturns (uint256) {
return a + b;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/functionsub(uint256 a, uint256 b) internalpurereturns (uint256) {
return a - b;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/functionmul(uint256 a, uint256 b) internalpurereturns (uint256) {
return a * b;
}
/**
* @dev Returns the integer division of two unsigned integers, reverting on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator.
*
* Requirements:
*
* - The divisor cannot be zero.
*/functiondiv(uint256 a, uint256 b) internalpurereturns (uint256) {
return a / b;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/functionmod(uint256 a, uint256 b) internalpurereturns (uint256) {
return a % b;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {trySub}.
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/functionsub(uint256 a,
uint256 b,
stringmemory errorMessage
) internalpurereturns (uint256) {
unchecked {
require(b <= a, errorMessage);
return a - b;
}
}
/**
* @dev Returns the integer division of two unsigned integers, reverting with custom message on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/functiondiv(uint256 a,
uint256 b,
stringmemory errorMessage
) internalpurereturns (uint256) {
unchecked {
require(b >0, errorMessage);
return a / b;
}
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting with custom message when dividing by zero.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryMod}.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/functionmod(uint256 a,
uint256 b,
stringmemory errorMessage
) internalpurereturns (uint256) {
unchecked {
require(b >0, errorMessage);
return a % b;
}
}
}
Contract Source Code
File 17 of 20: TransferHelper.sol
// SPDX-License-Identifier: GPL-2.0-or-laterpragmasolidity >=0.6.0;import"../external/openzeppelin/contracts/token/ERC20/IERC20.sol";
libraryTransferHelper{
/// @notice Transfers tokens from the targeted address to the given destination/// @notice Errors with 'STF' if transfer fails/// @param token The contract address of the token to be transferred/// @param from The originating address from which the tokens will be transferred/// @param to The destination address of the transfer/// @param value The amount to be transferredfunctionsafeTransferFrom(address token,
addressfrom,
address to,
uint256 value
) internal{
(bool success, bytesmemory data) = token.call(
abi.encodeWithSelector(
IERC20.transferFrom.selector,
from,
to,
value
)
);
require(
success && (data.length==0||abi.decode(data, (bool))),
"STF"
);
}
/// @notice Transfers tokens from msg.sender to a recipient/// @dev Errors with ST if transfer fails/// @param token The contract address of the token which will be transferred/// @param to The recipient of the transfer/// @param value The value of the transferfunctionsafeTransfer(address token, address to, uint256 value) internal{
(bool success, bytesmemory data) = token.call(
abi.encodeWithSelector(IERC20.transfer.selector, to, value)
);
require(
success && (data.length==0||abi.decode(data, (bool))),
"ST"
);
}
/// @notice Approves the stipulated contract to spend the given allowance in the given token/// @dev Errors with 'SA' if transfer fails/// @param token The contract address of the token to be approved/// @param to The target of the approval/// @param value The amount of the given token the target will be allowed to spendfunctionsafeApprove(address token, address to, uint256 value) internal{
(bool success, bytesmemory data) = token.call(
abi.encodeWithSelector(IERC20.approve.selector, to, value)
);
require(
success && (data.length==0||abi.decode(data, (bool))),
"SA"
);
}
/// @notice Transfers ETH to the recipient address/// @dev Fails with `STE`/// @param to The destination of the transfer/// @param value The value to be transferredfunctionsafeTransferETH(address to, uint256 value) internal{
(bool success, ) = to.call{value: value}(newbytes(0));
require(success, "STE");
}
}
Contract Source Code
File 18 of 20: VaultTypes.sol
// SPDX-License-Identifier: gpl-3.0pragmasolidity ^0.8.0;pragmaabicoderv2;libraryVaultTypes{
structVeloVaultStorage {
VeloVaultState state;
mapping(uint256=> VeloPosition) positions;
uint256 nextPositionId;
address[] rewardTokens;
address addressProvider;
address veloFactory;
address veloRouter;
address swapPathManager;
address lendingPool;
address vaultPositionManager;
address WETH9;
address veToken;
}
structVeloPositionValue {
// manager of the position, who can adjust the positionaddress manager;
bool isActive;
bool enableRangeStop;
// timestamp when openuint64 openedAt;
// timestamp nowuint64 current;
// token0Principal is the original invested token0uint256 token0Principal;
// token1Principal is the original invested token1uint256 token1Principal;
// liquidityPrincipal is the original liquidity user added to the pooluint256 liquidityPrincipal;
// left token0 not added to the liquidityuint256 token0Left;
// left Token1 not added to the liquidityuint256 token1Left;
// left token0 in liquidityuint256 token0InLiquidity;
// left Token1 not added to the liquidityuint256 token1InLiquidity;
// The lp amountuint256 liquidity;
// The debt share of debtPosition0 in the vaultuint256 debt0;
// The debt share for debtPosition1 in the vaultuint256 debt1;
// The borrowingIndex of debt0 in lendingPooluint256 borrowingIndex0;
// The borrowingIndex of debt1 in lendingPooluint256 borrowingIndex1;
// range stop configuint256 minPriceOfRangeStop;
uint256 maxPriceOfRangeStop;
}
structVeloPosition {
// manager of the position, who can adjust the positionaddress manager;
bool isActive;
bool enableRangeStop;
// timestamp when openuint64 openedAt;
// token0Principal is the original invested token0uint256 token0Principal;
// token1Principal is the original invested token1uint256 token1Principal;
// liquidityPrincipal is the original liquidity user added to the pooluint256 liquidityPrincipal;
// left token0 not added to the liquidityuint256 token0Left;
// left Token1 not added to the liquidityuint256 token1Left;
// The lp shares in the vaultuint256 lpShares;
// The debt share of debtPosition0 in the vaultuint256 debtShare0;
// The debt share for debtPosition1 in the vaultuint256 debtShare1;
// range stop configuint256 minPriceOfRangeStop;
uint256 maxPriceOfRangeStop;
}
structVeloVaultState {
address gauge;
address pair;
address token0;
address token1;
bool stable;
// If the vault is paused, new or close positions would be rejected by the contract.bool paused;
// If the vault is frozen, only new position action is rejected, the close is normal.bool frozen;
// Only if this feature is true, users of the vault can borrow tokens from lending pool.bool borrowingEnabled;
// liquidate with TWAPbool liquidateWithTWAP;
// max leverage when open a position// the value has with a multiplier of 100// 1x -> 1 * 100// 2x -> 2 * 100uint16 maxLeverage;
// premium leverage for a position of users who have specific veToken's voting poweruint16 premiumMaxLeverage;
uint16 maxPriceDiff;
// The debt ratio trigger liquidation// When a position's debt ratio goes out of liquidateDebtRatio// the position can be liquidateduint16 liquidateDebtRatio;
uint16 withdrawFeeRate;
uint16 compoundFeeRate;
uint16 liquidateFeeRate;
uint16 rangeStopFeeRate;
uint16 protocolFeeRate;
// the minimal voting power reqruired to use premium functionsuint256 premiumRequirement;
// Protocol Feeuint256 protocolFee0Accumulated;
uint256 protocolFee1Accumulated;
// minimal invest valueuint256 minInvestValue;
uint256 minSwapAmount0;
uint256 minSwapAmount1;
// total lpuint256 totalLp;
uint256 totalLpShares;
// the utilization of the lending reserve pool trigger premium checkuint256 premiumUtilizationOfReserve0;
// debt limit of token0uint256 debtLimit0;
// debt positionId of token0uint256 debtPositionId0;
// debt total_sharesuint256 debtTotalShares0;
// the utilization of the lending reserve pool trigger premium checkuint256 premiumUtilizationOfReserve1;
// debt limit of token1uint256 debtLimit1;
// debt positionId of token1uint256 debtPositionId1;
// debt total_sharesuint256 debtTotalShares1;
}
}
Contract Source Code
File 19 of 20: VeloPositionManager.sol
// SPDX-License-Identifier: gpl-3.0pragmasolidity ^0.8.0;pragmaabicoderv2;import"./external/openzeppelin/contracts/security/ReentrancyGuard.sol";
import"./external/openzeppelin/contracts/access/Ownable.sol";
import"./external/openzeppelin/contracts/utils/math/SafeMath.sol";
import"./external/openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import"./interfaces/IVeloVaultPositionManager.sol";
import"./interfaces/IVaultFactory.sol";
import"./interfaces/IVeloVault.sol";
import"./libraries/helpers/Errors.sol";
import"./Payments.sol";
import"./interfaces/IAddressRegistry.sol";
import"./libraries/helpers/AddressId.sol";
import"./libraries/types/VaultTypes.sol";
contractVeloPositionManagerisReentrancyGuard,
IVeloVaultPositionManager,
Ownable,
Payments{
usingSafeMathforuint256;
/// @notice Contract address of the AddressProvideraddresspublicimmutable addressProvider;
/// @notice Contract address of the VaultFactoryaddresspublicimmutable vaultFactory;
/// @notice permissionLessLiquidation feature/// Only if this feature is true, users can liquidate positions without permissions./// Otherwise only liquidators in the whitelist can liquidate positions.boolpublic permissionLessLiquidationEnabled;
/// @notice liquidatorWhitelistmapping(address=>bool) public liquidatorWhitelist;
/// @notice permissionLessCompoundEnabled feature/// Only if this feature is true, users can claim the vaults' rewards and reinvest to liquidity without permissions./// Otherwise only users in the whitelist can call the reinvest functionboolpublic permissionLessCompoundEnabled;
/// @notice CompounderWhitelistmapping(address=>bool) public compounderWhitelist;
/// @notice permissionLessRangeStopEnabled feature/// Only if this feature is true, users can close outof-range positionsboolpublic permissionLessRangeStopEnabled;
/// @notice rangeStopCallerWhitelistmapping(address=>bool) public rangeStopCallerWhitelist;
modifiercheckDeadline(uint256 deadline) {
require(block.timestamp<= deadline, Errors.VL_TRANSACTION_TOO_OLD);
_;
}
modifierliquidatorInWhitelist() {
require(
permissionLessLiquidationEnabled || liquidatorWhitelist[msg.sender],
Errors.VL_LIQUIDATOR_NOT_IN_WHITELIST
);
_;
}
modifiercompounderInWhitelist() {
require(
permissionLessCompoundEnabled || compounderWhitelist[msg.sender],
Errors.VL_LIQUIDATOR_NOT_IN_WHITELIST
);
_;
}
modifierrangeStopCallerInWhitelist() {
require(
permissionLessRangeStopEnabled ||
rangeStopCallerWhitelist[msg.sender],
Errors.VL_LIQUIDATOR_NOT_IN_WHITELIST
);
_;
}
constructor(address _addressProvider
)
Payments(
IAddressRegistry(_addressProvider).getAddress(
AddressId.ADDRESS_ID_WETH9
)
)
{
require(_addressProvider !=address(0), Errors.VL_ADDRESS_CANNOT_ZERO);
addressProvider = _addressProvider;
vaultFactory = IAddressRegistry(addressProvider).getAddress(
AddressId.ADDRESS_ID_VAULT_FACTORY
);
disablePermissionLessLiquidation();
addPermissionedLiquidator(msg.sender);
disablePermissionLessCompound();
addPermissionedCompounder(msg.sender);
disablePermissonLessRangeStop();
addPermissionedRangeStopCaller(msg.sender);
}
/// @notice Callback functions called by the vault to pay tokens to the vault contract./// The caller to this function must be the vault contractfunctionpayToVaultCallback(
PayToVaultCallbackParams calldata params
) external{
address vaultAddress = IVaultFactory(vaultFactory).vaults(
params.vaultId
);
require(vaultAddress !=address(0), Errors.VL_ADDRESS_CANNOT_ZERO);
require(
vaultAddress == _msgSender(),
Errors.VT_VAULT_CALLBACK_INVALID_SENDER
);
// transfer token0 and token1 from user's wallet to vaultif (params.amount0 >0) {
pay(
IVeloVault(vaultAddress).token0(),
params.payer,
vaultAddress,
params.amount0
);
}
if (params.amount1 >0) {
pay(
IVeloVault(vaultAddress).token1(),
params.payer,
vaultAddress,
params.amount1
);
}
}
/// @notice Callback functions called by the vault to pay protocol fee./// The caller to this function must be the vault contractfunctionpayFeeToTreasuryCallback(uint256 vaultId,
address asset,
uint256 amount,
uint256 feeType
) external{
address vaultAddress = IVaultFactory(vaultFactory).vaults(vaultId);
require(vaultAddress !=address(0), Errors.VL_ADDRESS_CANNOT_ZERO);
require(
vaultAddress == _msgSender(),
Errors.VT_VAULT_CALLBACK_INVALID_SENDER
);
address treasury = IAddressRegistry(addressProvider).getAddress(
AddressId.ADDRESS_ID_TREASURY
);
require(treasury !=address(0), "zero-address treasury");
SafeERC20.safeTransferFrom(
IERC20(asset),
vaultAddress,
treasury,
amount
);
emit FeePaid(vaultId, asset, feeType, amount);
}
/// @notice Open a new vaultPosition or invest to a existing vault position/// @param params The parameters necessary, encoded as `NewOrInvestToVaultPositionParams` in calldatafunctionnewOrInvestToVaultPosition(
NewOrInvestToVaultPositionParams calldata params
) externalpayablenonReentrantcheckDeadline(params.deadline) {
address vaultAddress = IVaultFactory(vaultFactory).vaults(
params.vaultId
);
require(vaultAddress !=address(0), Errors.VL_ADDRESS_CANNOT_ZERO);
(uint256 positionId, uint256 liquidity) = IVeloVault(vaultAddress)
.newOrInvestToVaultPosition(params, _msgSender());
if (params.vaultPositionId ==0) {
emit NewVaultPosition(params.vaultId, positionId, _msgSender());
}
// if user use ether, refund unused ether to msg.senderif (msg.value>0) {
refundETH();
}
emit InvestToVaultPosition(
params.vaultId,
positionId,
_msgSender(),
params.amount0Invest,
params.amount1Invest,
params.amount0Borrow,
params.amount1Borrow,
liquidity
);
}
/// @notice Close a vaultPosition partially/// @param params The parameters necessary, encoded as `CloseVaultPositionPartiallyParams` in calldatafunctioncloseVaultPositionPartially(
CloseVaultPositionPartiallyParams calldata params
) externaloverridenonReentrantcheckDeadline(params.deadline) {
address vaultAddress = IVaultFactory(vaultFactory).vaults(
params.vaultId
);
require(vaultAddress !=address(0), Errors.VL_ADDRESS_CANNOT_ZERO);
(
uint256 amount0,
uint256 amount1,
uint256 repay0,
uint256 repay1
) = IVeloVault(vaultAddress).closeAndRepayPartially(
params,
_msgSender()
);
if (
amount0 >0&&
IVeloVault(vaultAddress).token0() == WETH9 &&
params.receiveNativeETH
) {
unwrapWETH9(amount0, params.receiver);
}
if (
amount1 >0&&
IVeloVault(vaultAddress).token1() == WETH9 &&
params.receiveNativeETH
) {
unwrapWETH9(amount1, params.receiver);
}
emit CloseVaultPositionPartially(
params.vaultId,
params.vaultPositionId,
_msgSender(),
params.percent,
amount0,
amount1,
repay0,
repay1
);
}
/// @notice Close the position which is outof price range./// This function can be called only if the `rangeStop` feature is enabled./// Any permissioned user can call this function, regardless of whether they are the position owner./// @param params The parameters necessary, encoded as `CloseVaultPositionPartiallyParams` in calldatafunctioncloseOutOfRangePosition(
CloseVaultPositionPartiallyParams calldata params
)
externalpayablenonReentrantrangeStopCallerInWhitelistavoidUsingNativeEther{
address vaultAddress = IVaultFactory(vaultFactory).vaults(
params.vaultId
);
require(vaultAddress !=address(0), Errors.VL_ADDRESS_CANNOT_ZERO);
(
address positionManager,
uint256 price,
uint256 amount0,
uint256 amount1,
uint256 fee0,
uint256 fee1
) = IVeloVault(vaultAddress).closeAndRepayOutOfRangePosition(params);
if (
fee0 >0&&
IVeloVault(vaultAddress).token0() == WETH9 &&
params.receiveNativeETH
) {
unwrapWETH9(fee0, params.receiver);
}
if (
fee1 >0&&
IVeloVault(vaultAddress).token1() == WETH9 &&
params.receiveNativeETH
) {
unwrapWETH9(fee1, params.receiver);
}
emit CloseOutOfRangePosition(
params.vaultId,
params.vaultPositionId,
positionManager,
_msgSender(),
params.percent,
uint64(block.timestamp),
price,
amount0,
amount1,
fee0,
fee1
);
}
/// @notice Liquidate a vaultPosition partially/// @param params The parameters necessary, encoded as `LiquidateVaultPositionPartiallyParams` in calldatafunctionliquidateVaultPositionPartially(
LiquidateVaultPositionPartiallyParams calldata params
) externalpayablenonReentrantliquidatorInWhitelist{
address vaultAddress = IVaultFactory(vaultFactory).vaults(
params.vaultId
);
require(vaultAddress !=address(0), Errors.VL_ADDRESS_CANNOT_ZERO);
IVeloVault.LiquidateState memory result = IVeloVault(vaultAddress)
.repayAndLiquidatePositionPartially(params, _msgSender());
if (
result.liquidatorReceive0 >0&&
IVeloVault(vaultAddress).token0() == WETH9 &&
params.receiveNativeETH
) {
unwrapWETH9(result.liquidatorReceive0, params.receiver);
}
if (
result.liquidatorReceive1 >0&&
IVeloVault(vaultAddress).token1() == WETH9 &&
params.receiveNativeETH
) {
unwrapWETH9(result.liquidatorReceive1, params.receiver);
}
// if user use ether, refund unused ether to msg.senderif (msg.value>0) {
refundETH();
}
emit LiquidateVaultPositionPartially(
params.vaultId,
params.vaultPositionId,
result.manager,
_msgSender(),
params.percent,
uint64(block.timestamp),
result.price,
result.repaidValue,
result.removedLiquidityValue,
result.amount0Left,
result.amount1Left,
result.liquidateFee0,
result.liquidateFee1
);
}
/// @notice Invest the earned fee by the position to liquidityfunctioninvestEarnedFeeToLiquidity(
InvestEarnedFeeToLiquidityParam calldata params
) externalnonReentrantcompounderInWhitelist{
address vaultAddress = IVaultFactory(vaultFactory).vaults(
params.vaultId
);
require(vaultAddress !=address(0), Errors.VL_ADDRESS_CANNOT_ZERO);
(
uint256 liquidity,
uint256 fee0,
uint256 fee1,
uint256[] memory rewards
) = IVeloVault(vaultAddress).claimRewardsAndReInvestToLiquidity(params);
if (
fee0 >0&&
IVeloVault(vaultAddress).token0() == WETH9 &&
params.receiveNativeETH
) {
unwrapWETH9(fee0, params.compoundFeeReceiver);
}
if (
fee1 >0&&
IVeloVault(vaultAddress).token1() == WETH9 &&
params.receiveNativeETH
) {
unwrapWETH9(fee1, params.compoundFeeReceiver);
}
emit InvestEarnedFeeToLiquidity(
params.vaultId,
_msgSender(),
liquidity,
fee0,
fee1,
rewards
);
}
/// @notice Repay exact value of debtsfunctionexactRepay(
ExactRepayParam calldata params
)
externalpayablenonReentrantreturns (uint256 amount0Repaid, uint256 amount1Repaid)
{
address vaultAddress = IVaultFactory(vaultFactory).vaults(
params.vaultId
);
require(vaultAddress !=address(0), Errors.VL_ADDRESS_CANNOT_ZERO);
address positionMananegr;
(positionMananegr, amount0Repaid, amount1Repaid) = IVeloVault(
vaultAddress
).exactRepay(params, _msgSender());
if (
params.amount0ToRepay > amount0Repaid &&
IVeloVault(vaultAddress).token0() == WETH9 &&
params.receiveNativeETH
) {
unwrapWETH9(params.amount0ToRepay.sub(amount0Repaid), _msgSender());
}
if (
params.amount1ToRepay > amount1Repaid &&
IVeloVault(vaultAddress).token1() == WETH9 &&
params.receiveNativeETH
) {
unwrapWETH9(params.amount1ToRepay.sub(amount1Repaid), _msgSender());
}
// if there is unused ETH, refund it to msg.senderif (msg.value>0) {
refundETH();
}
emit ExactRepay(
params.vaultId,
params.vaultPositionId,
positionMananegr,
_msgSender(),
amount0Repaid,
amount1Repaid
);
}
functionadminSetVault(uint256 vaultId,
bytescalldata params
) externalnonReentrantonlyOwner{
address vaultAddress = IVaultFactory(vaultFactory).vaults(vaultId);
require(vaultAddress !=address(0), Errors.VL_ADDRESS_CANNOT_ZERO);
IVeloVault(vaultAddress).adminSetVault(params);
}
/// @notice Transfer the position's manager to another wallet/// Must be called by the current manager of the posistion/// @param vaultId The Id of the vault/// @param vaultPositionId The Id of the position/// @param newManager The new address of the managerfunctiontransferManagerOfVaultPosition(uint256 vaultId,
uint256 vaultPositionId,
address newManager
) externaloverridenonReentrant{
address vaultAddress = IVaultFactory(vaultFactory).vaults(vaultId);
require(vaultAddress !=address(0), Errors.VL_ADDRESS_CANNOT_ZERO);
IVeloVault(vaultAddress).transferManagerTo(
_msgSender(),
vaultPositionId,
newManager
);
}
/// @notice Set stop-loss price range of the position/// Users can set a stop-loss price range for a position only if the position is enabled `RangeStop` feature./// If current price goes out of the stop-loss price range, extraFi's bots will close the position/// @param vaultId The Id of the vault/// @param vaultPositionId The Id of the position/// @param enable Enable or Disable the rangeStop feature/// @param minPrice The lower price of the stop-loss price range/// @param maxPrice The upper price of the stop-loss price rangefunctionsetRangeStop(uint256 vaultId,
uint256 vaultPositionId,
bool enable,
uint256 minPrice,
uint256 maxPrice
) externaloverridenonReentrant{
address vaultAddress = IVaultFactory(vaultFactory).vaults(vaultId);
require(vaultAddress !=address(0), Errors.VL_ADDRESS_CANNOT_ZERO);
IVeloVault(vaultAddress).setRangeStop(
_msgSender(),
vaultPositionId,
enable,
minPrice,
maxPrice
);
}
functionenablePermissionLessLiquidation() publicnonReentrantonlyOwner{
permissionLessLiquidationEnabled =true;
}
functiondisablePermissionLessLiquidation() publicnonReentrantonlyOwner{
permissionLessLiquidationEnabled =false;
}
functionaddPermissionedLiquidator(address addr
) publicnonReentrantonlyOwner{
liquidatorWhitelist[addr] =true;
}
functionremovePermissionedLiquidator(address addr
) publicnonReentrantonlyOwner{
liquidatorWhitelist[addr] =false;
}
functionenablePermissionLessCompound() publicnonReentrantonlyOwner{
permissionLessCompoundEnabled =true;
}
functiondisablePermissionLessCompound() publicnonReentrantonlyOwner{
permissionLessCompoundEnabled =false;
}
functionaddPermissionedCompounder(address addr
) publicnonReentrantonlyOwner{
compounderWhitelist[addr] =true;
}
functionremovePermissionedCompounder(address addr
) publicnonReentrantonlyOwner{
compounderWhitelist[addr] =false;
}
functionenablePermissonLessRangeStop() publicnonReentrantonlyOwner{
permissionLessRangeStopEnabled =true;
}
functiondisablePermissonLessRangeStop() publicnonReentrantonlyOwner{
permissionLessRangeStopEnabled =false;
}
functionaddPermissionedRangeStopCaller(address addr
) publicnonReentrantonlyOwner{
rangeStopCallerWhitelist[addr] =true;
}
functionremovePermissionedRangeStopCaller(address addr
) publicnonReentrantonlyOwner{
rangeStopCallerWhitelist[addr] =false;
}
//----------------->>>>> getters <<<<<-----------------functiongetVault(uint256 vaultId
) externalviewoverridereturns (VaultTypes.VeloVaultState memory) {
address vaultAddress = IVaultFactory(vaultFactory).vaults(vaultId);
return IVeloVault(vaultAddress).getVaultState();
}
functiongetVaultPosition(uint256 vaultId,
uint256 vaultPositionId
)
externalviewoverridereturns (VaultTypes.VeloPositionValue memory state)
{
address vaultAddress = IVaultFactory(vaultFactory).vaults(vaultId);
return IVeloVault(vaultAddress).getPositionValue(vaultPositionId);
}
}
Contract Source Code
File 20 of 20: draft-IERC20Permit.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/draft-IERC20Permit.sol)pragmasolidity ^0.8.0;/**
* @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
* https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
*
* Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
* presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't
* need to send a transaction, and thus is not required to hold Ether at all.
*/interfaceIERC20Permit{
/**
* @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens,
* given ``owner``'s signed approval.
*
* IMPORTANT: The same issues {IERC20-approve} has related to transaction
* ordering also apply here.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `deadline` must be a timestamp in the future.
* - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner`
* over the EIP712-formatted function arguments.
* - the signature must use ``owner``'s current nonce (see {nonces}).
*
* For more information on the signature format, see the
* https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP
* section].
*/functionpermit(address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
/**
* @dev Returns the current nonce for `owner`. This value must be
* included whenever a signature is generated for {permit}.
*
* Every successful call to {permit} increases ``owner``'s nonce by one. This
* prevents a signature from being used multiple times.
*/functionnonces(address owner) externalviewreturns (uint256);
/**
* @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}.
*/// solhint-disable-next-line func-name-mixedcasefunctionDOMAIN_SEPARATOR() externalviewreturns (bytes32);
}