View Source: contracts/libraries/RoutineInvokerLibV1.sol
RoutineInvokerLibV1
Enums
enum Action {
Deposit,
Withdraw
}
- updateStateAndLiquidity(IStore s, bytes32 coverKey)
- _invoke(IStore s, bytes32 coverKey)
- _getUpdateInterval(IStore s)
- getWithdrawalInfoInternal(IStore s, bytes32 coverKey)
- _isWithdrawalPeriod(IStore s, bytes32 coverKey)
- _updateWithdrawalPeriod(IStore s, bytes32 coverKey)
- isAccrualCompleteInternal(IStore s, bytes32 coverKey)
- setAccrualCompleteInternal(IStore s, bytes32 coverKey, bool flag)
- getAccrualInvocationKey(bytes32 coverKey)
- getNextWithdrawalStartKey(bytes32 coverKey)
- getNextWithdrawalEndKey(bytes32 coverKey)
- mustBeDuringWithdrawalPeriod(IStore s, bytes32 coverKey)
- _executeAndGetAction(IStore s, ILendingStrategy , bytes32 coverKey)
- _canDeposit(IStore s, ILendingStrategy strategy, uint256 totalStrategies, bytes32 coverKey)
- _invokeAssetManagement(IStore s, bytes32 coverKey)
- _executeStrategy(IStore s, ILendingStrategy strategy, uint256 totalStrategies, address vault, bytes32 coverKey)
- _depositToStrategy(ILendingStrategy strategy, bytes32 coverKey, uint256 amount)
- _withdrawAllFromStrategy(ILendingStrategy strategy, address vault, bytes32 coverKey)
- _withdrawFromDisabled(IStore s, bytes32 coverKey, address onBehalfOf)
function updateStateAndLiquidity(IStore s, bytes32 coverKey) external nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 |
Source Code
function updateStateAndLiquidity(IStore s, bytes32 coverKey) external {
_invoke(s, coverKey);
}
function _invoke(IStore s, bytes32 coverKey) private nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 |
Source Code
function _invoke(IStore s, bytes32 coverKey) private {
// solhint-disable-next-line
if (s.getLastUpdatedOnInternal(coverKey) + _getUpdateInterval(s) > block.timestamp) {
return;
}
PriceLibV1.setNpmPrice(s);
if (coverKey > 0) {
_updateWithdrawalPeriod(s, coverKey);
_invokeAssetManagement(s, coverKey);
s.setLastUpdatedOn(coverKey);
}
}
function _getUpdateInterval(IStore s) private view
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
s | IStore |
Source Code
function _getUpdateInterval(IStore s) private view returns (uint256) {
return s.getUintByKey(ProtoUtilV1.NS_LIQUIDITY_STATE_UPDATE_INTERVAL);
}
function getWithdrawalInfoInternal(IStore s, bytes32 coverKey) public view
returns(isWithdrawalPeriod bool, lendingPeriod uint256, withdrawalWindow uint256, start uint256, end uint256)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 |
Source Code
function getWithdrawalInfoInternal(IStore s, bytes32 coverKey)
public
view
returns (
bool isWithdrawalPeriod,
uint256 lendingPeriod,
uint256 withdrawalWindow,
uint256 start,
uint256 end
)
{
(lendingPeriod, withdrawalWindow) = s.getRiskPoolingPeriodsInternal(coverKey);
// Get the withdrawal period of this cover liquidity
start = s.getUintByKey(getNextWithdrawalStartKey(coverKey));
end = s.getUintByKey(getNextWithdrawalEndKey(coverKey));
// solhint-disable-next-line
if (block.timestamp >= start && block.timestamp <= end) {
isWithdrawalPeriod = true;
}
}
function _isWithdrawalPeriod(IStore s, bytes32 coverKey) private view
returns(bool)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 |
Source Code
function _isWithdrawalPeriod(IStore s, bytes32 coverKey) private view returns (bool) {
(bool isWithdrawalPeriod, , , , ) = getWithdrawalInfoInternal(s, coverKey);
return isWithdrawalPeriod;
}
function _updateWithdrawalPeriod(IStore s, bytes32 coverKey) private nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 |
Source Code
function _updateWithdrawalPeriod(IStore s, bytes32 coverKey) private {
(, uint256 lendingPeriod, uint256 withdrawalWindow, uint256 start, uint256 end) = getWithdrawalInfoInternal(s, coverKey);
// Without a lending period and withdrawal window, nothing can be updated
if (lendingPeriod == 0 || withdrawalWindow == 0) {
return;
}
// The withdrawal period is now over.
// Deposits can be performed again.
// Set the next withdrawal cycle
if (block.timestamp > end) {
// solhint-disable-previous-line
// Next Withdrawal Cycle
// Withdrawals can start after the lending period
start = block.timestamp + lendingPeriod; // solhint-disable
// Withdrawals can be performed until the end of the next withdrawal cycle
end = start + withdrawalWindow;
s.setUintByKey(getNextWithdrawalStartKey(coverKey), start);
s.setUintByKey(getNextWithdrawalEndKey(coverKey), end);
setAccrualCompleteInternal(s, coverKey, false);
}
}
function isAccrualCompleteInternal(IStore s, bytes32 coverKey) external view
returns(bool)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 |
Source Code
function isAccrualCompleteInternal(IStore s, bytes32 coverKey) external view returns (bool) {
return s.getBoolByKey(getAccrualInvocationKey(coverKey));
}
function setAccrualCompleteInternal(IStore s, bytes32 coverKey, bool flag) public nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 | |
flag | bool |
Source Code
function setAccrualCompleteInternal(
IStore s,
bytes32 coverKey,
bool flag
) public {
s.setBoolByKey(getAccrualInvocationKey(coverKey), flag);
}
Hash key of the "accrual invocation status" for the given cover. Warning: this function does not validate the cover key supplied.
function getAccrualInvocationKey(bytes32 coverKey) public pure
returns(bytes32)
Arguments
Name | Type | Description |
---|---|---|
coverKey | bytes32 | Enter cover key |
Source Code
function getAccrualInvocationKey(bytes32 coverKey) public pure returns (bytes32) {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_ACCRUAL_INVOCATION, coverKey));
}
Hash key of the "next withdrawal start date" for the given cover. Warning: this function does not validate the cover key supplied.
function getNextWithdrawalStartKey(bytes32 coverKey) public pure
returns(bytes32)
Arguments
Name | Type | Description |
---|---|---|
coverKey | bytes32 | Enter cover key |
Source Code
function getNextWithdrawalStartKey(bytes32 coverKey) public pure returns (bytes32) {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_LENDING_STRATEGY_WITHDRAWAL_START, coverKey));
}
Hash key of the "next withdrawal end date" for the given cover. Warning: this function does not validate the cover key supplied.
function getNextWithdrawalEndKey(bytes32 coverKey) public pure
returns(bytes32)
Arguments
Name | Type | Description |
---|---|---|
coverKey | bytes32 | Enter cover key |
Source Code
function getNextWithdrawalEndKey(bytes32 coverKey) public pure returns (bytes32) {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_LENDING_STRATEGY_WITHDRAWAL_END, coverKey));
}
function mustBeDuringWithdrawalPeriod(IStore s, bytes32 coverKey) external view
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 |
Source Code
function mustBeDuringWithdrawalPeriod(IStore s, bytes32 coverKey) external view {
// Get the withdrawal period of this cover liquidity
uint256 start = s.getUintByKey(getNextWithdrawalStartKey(coverKey));
uint256 end = s.getUintByKey(getNextWithdrawalEndKey(coverKey));
require(start > 0 && block.timestamp >= start, "Withdrawal period has not started");
require(end > 0 && block.timestamp <= end, "Withdrawal period has already ended");
}
function _executeAndGetAction(IStore s, ILendingStrategy , bytes32 coverKey) private nonpayable
returns(enum RoutineInvokerLibV1.Action)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
ILendingStrategy | ||
coverKey | bytes32 |
Source Code
function _executeAndGetAction(
IStore s,
ILendingStrategy,
bytes32 coverKey
) private returns (Action) {
// If the cover is undergoing reporting, withdraw everything
bool isNormal = s.isCoverNormalInternal(coverKey);
if (isNormal != true) {
// Reset the withdrawal window
s.setUintByKey(getNextWithdrawalStartKey(coverKey), 0);
s.setUintByKey(getNextWithdrawalEndKey(coverKey), 0);
return Action.Withdraw;
}
if (_isWithdrawalPeriod(s, coverKey) == true) {
return Action.Withdraw;
}
return Action.Deposit;
}
function _canDeposit(IStore s, ILendingStrategy strategy, uint256 totalStrategies, bytes32 coverKey) private view
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
strategy | ILendingStrategy | |
totalStrategies | uint256 | |
coverKey | bytes32 |
Source Code
function _canDeposit(
IStore s,
ILendingStrategy strategy,
uint256 totalStrategies,
bytes32 coverKey
) private view returns (uint256) {
IERC20 stablecoin = IERC20(s.getStablecoin());
uint256 totalBalance = s.getStablecoinOwnedByVaultInternal(coverKey);
uint256 maximumAllowed = (totalBalance * s.getMaxLendingRatioInternal()) / ProtoUtilV1.MULTIPLIER;
uint256 allocation = maximumAllowed / totalStrategies;
uint256 weight = strategy.getWeight();
uint256 canDeposit = (allocation * weight) / ProtoUtilV1.MULTIPLIER;
uint256 alreadyDeposited = s.getAmountInStrategy(coverKey, strategy.getName(), address(stablecoin));
if (alreadyDeposited >= canDeposit) {
return 0;
}
return canDeposit - alreadyDeposited;
}
function _invokeAssetManagement(IStore s, bytes32 coverKey) private nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 |
Source Code
function _invokeAssetManagement(IStore s, bytes32 coverKey) private {
address vault = s.getVaultAddress(coverKey);
_withdrawFromDisabled(s, coverKey, vault);
address[] memory strategies = s.getActiveStrategiesInternal();
for (uint256 i = 0; i < strategies.length; i++) {
ILendingStrategy strategy = ILendingStrategy(strategies[i]);
_executeStrategy(s, strategy, strategies.length, vault, coverKey);
}
}
function _executeStrategy(IStore s, ILendingStrategy strategy, uint256 totalStrategies, address vault, bytes32 coverKey) private nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
strategy | ILendingStrategy | |
totalStrategies | uint256 | |
vault | address | |
coverKey | bytes32 |
Source Code
function _executeStrategy(
IStore s,
ILendingStrategy strategy,
uint256 totalStrategies,
address vault,
bytes32 coverKey
) private {
uint256 canDeposit = _canDeposit(s, strategy, totalStrategies, coverKey);
uint256 balance = IERC20(s.getStablecoin()).balanceOf(vault);
if (canDeposit > balance) {
canDeposit = balance;
}
Action action = _executeAndGetAction(s, strategy, coverKey);
if (action == Action.Deposit && canDeposit == 0) {
return;
}
if (action == Action.Withdraw) {
_withdrawAllFromStrategy(strategy, vault, coverKey);
return;
}
_depositToStrategy(strategy, coverKey, canDeposit);
}
function _depositToStrategy(ILendingStrategy strategy, bytes32 coverKey, uint256 amount) private nonpayable
Arguments
Name | Type | Description |
---|---|---|
strategy | ILendingStrategy | |
coverKey | bytes32 | |
amount | uint256 |
Source Code
function _depositToStrategy(
ILendingStrategy strategy,
bytes32 coverKey,
uint256 amount
) private {
strategy.deposit(coverKey, amount);
}
function _withdrawAllFromStrategy(ILendingStrategy strategy, address vault, bytes32 coverKey) private nonpayable
returns(stablecoinWithdrawn uint256)
Arguments
Name | Type | Description |
---|---|---|
strategy | ILendingStrategy | |
vault | address | |
coverKey | bytes32 |
Source Code
function _withdrawAllFromStrategy(
ILendingStrategy strategy,
address vault,
bytes32 coverKey
) private returns (uint256 stablecoinWithdrawn) {
uint256 balance = IERC20(strategy.getDepositCertificate()).balanceOf(vault);
if (balance > 0) {
stablecoinWithdrawn = strategy.withdraw(coverKey);
}
}
function _withdrawFromDisabled(IStore s, bytes32 coverKey, address onBehalfOf) private nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 | |
onBehalfOf | address |
Source Code
function _withdrawFromDisabled(
IStore s,
bytes32 coverKey,
address onBehalfOf
) private {
address[] memory strategies = s.getDisabledStrategiesInternal();
for (uint256 i = 0; i < strategies.length; i++) {
ILendingStrategy strategy = ILendingStrategy(strategies[i]);
uint256 balance = IERC20(strategy.getDepositCertificate()).balanceOf(onBehalfOf);
if (balance > 0) {
strategy.withdraw(coverKey);
}
}
}
- AaveStrategy
- AccessControl
- AccessControlLibV1
- Address
- BaseLibV1
- BokkyPooBahsDateTimeLibrary
- BondPool
- BondPoolBase
- BondPoolLibV1
- CompoundStrategy
- Context
- Cover
- CoverBase
- CoverLibV1
- CoverReassurance
- CoverStake
- CoverUtilV1
- cxToken
- cxTokenFactory
- cxTokenFactoryLibV1
- Delayable
- Destroyable
- ERC165
- ERC20
- FakeAaveLendingPool
- FakeCompoundDaiDelegator
- FakePriceOracle
- FakeRecoverable
- FakeStore
- FakeToken
- FakeUniswapPair
- FakeUniswapV2FactoryLike
- FakeUniswapV2PairLike
- FakeUniswapV2RouterLike
- FaultyAaveLendingPool
- FaultyCompoundDaiDelegator
- Finalization
- ForceEther
- Governance
- GovernanceUtilV1
- IAaveV2LendingPoolLike
- IAccessControl
- IBondPool
- IClaimsProcessor
- ICompoundERC20DelegatorLike
- ICover
- ICoverReassurance
- ICoverStake
- ICxToken
- ICxTokenFactory
- IERC165
- IERC20
- IERC20Detailed
- IERC20Metadata
- IERC3156FlashBorrower
- IERC3156FlashLender
- IFinalization
- IGovernance
- ILendingStrategy
- ILiquidityEngine
- IMember
- INeptuneRouterV1
- InvalidStrategy
- IPausable
- IPolicy
- IPolicyAdmin
- IPriceOracle
- IProtocol
- IRecoverable
- IReporter
- IResolution
- IResolvable
- IStakingPools
- IStore
- IStoreLike
- IUniswapV2FactoryLike
- IUniswapV2PairLike
- IUniswapV2RouterLike
- IUnstakable
- IVault
- IVaultDelegate
- IVaultFactory
- IWitness
- LiquidityEngine
- MaliciousToken
- MockAccessControlUser
- MockCoverUtilUser
- MockCxToken
- MockCxTokenPolicy
- MockCxTokenStore
- MockFlashBorrower
- MockLiquidityEngineUser
- MockProcessorStore
- MockProcessorStoreLib
- MockProtocol
- MockRegistryClient
- MockStore
- MockStoreKeyUtilUser
- MockValidationLibUser
- MockVault
- MockVaultLibUser
- NeptuneRouterV1
- NPM
- NpmDistributor
- NTransferUtilV2
- NTransferUtilV2Intermediate
- Ownable
- Pausable
- Policy
- PolicyAdmin
- PolicyHelperV1
- PoorMansERC20
- POT
- PriceLibV1
- Processor
- ProtoBase
- Protocol
- ProtoUtilV1
- Recoverable
- ReentrancyGuard
- RegistryLibV1
- Reporter
- Resolution
- Resolvable
- RoutineInvokerLibV1
- SafeERC20
- StakingPoolBase
- StakingPoolCoreLibV1
- StakingPoolInfo
- StakingPoolLibV1
- StakingPoolReward
- StakingPools
- Store
- StoreBase
- StoreKeyUtil
- StrategyLibV1
- Strings
- TimelockController
- Unstakable
- ValidationLibV1
- Vault
- VaultBase
- VaultDelegate
- VaultDelegateBase
- VaultDelegateWithFlashLoan
- VaultFactory
- VaultFactoryLibV1
- VaultLibV1
- VaultLiquidity
- VaultStrategy
- WithFlashLoan
- WithPausability
- WithRecovery
- Witness