View Source: contracts/libraries/StrategyLibV1.sol
StrategyLibV1
Constants & Variables
uint256 public constant DEFAULT_LENDING_PERIOD;
uint256 public constant DEFAULT_WITHDRAWAL_WINDOW;
Events
event StrategyAdded(address indexed strategy);
event RiskPoolingPeriodSet(bytes32 indexed key, uint256 lendingPeriod, uint256 withdrawalWindow);
event MaxLendingRatioSet(uint256 ratio);
- _getIsActiveStrategyKey(address strategyAddress)
- _getIsDisabledStrategyKey(address strategyAddress)
- disableStrategyInternal(IStore s, address toFind)
- deleteStrategyInternal(IStore s, address toFind)
- addStrategiesInternal(IStore s, address[] strategies)
- getRiskPoolingPeriodsInternal(IStore s, bytes32 coverKey)
- setRiskPoolingPeriodsInternal(IStore s, bytes32 coverKey, uint256 lendingPeriod, uint256 withdrawalWindow)
- getLendingPeriodKey(bytes32 coverKey)
- getMaxLendingRatioInternal(IStore s)
- setMaxLendingRatioInternal(IStore s, uint256 ratio)
- getMaxLendingRatioKey()
- getWithdrawalWindowKey(bytes32 coverKey)
- _addStrategy(IStore s, address deployedOn)
- _disableStrategy(IStore s, address toFind)
- _deleteStrategy(IStore s, address toFind)
- getDisabledStrategiesInternal(IStore s)
- getActiveStrategiesInternal(IStore s)
- getStrategyOutKey(bytes32 coverKey, address token)
- getSpecificStrategyOutKey(bytes32 coverKey, bytes32 strategyName, address token)
- getAmountInStrategies(IStore s, bytes32 coverKey, address token)
- getAmountInStrategy(IStore s, bytes32 coverKey, bytes32 strategyName, address token)
- preTransferToStrategyInternal(IStore s, IERC20 token, bytes32 coverKey, bytes32 strategyName, uint256 amount)
- postReceiveFromStrategyInternal(IStore s, IERC20 token, bytes32 coverKey, bytes32 strategyName, uint256 received)
- _addToStrategyOut(IStore s, bytes32 coverKey, address token, uint256 amountToAdd)
- _reduceStrategyOut(IStore s, bytes32 coverKey, address token, uint256 amount)
- _addToSpecificStrategyOut(IStore s, bytes32 coverKey, bytes32 strategyName, address token, uint256 amountToAdd)
- _clearSpecificStrategyOut(IStore s, bytes32 coverKey, bytes32 strategyName, address token)
- _logIncomes(IStore s, bytes32 coverKey, bytes32 strategyName, uint256 income, uint256 loss)
- getStablecoinOwnedByVaultInternal(IStore s, bytes32 coverKey)
Hash key of the "active strategy flag". Warning: this function does not validate the input arguments.
function _getIsActiveStrategyKey(address strategyAddress) private pure
returns(bytes32)
Arguments
Name | Type | Description |
---|---|---|
strategyAddress | address | Enter a strategy address |
Source Code
function _getIsActiveStrategyKey(address strategyAddress) private pure returns (bytes32) {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_LENDING_STRATEGY_ACTIVE, strategyAddress));
}
Hash key of the "disabled strategy flag". Warning: this function does not validate the input arguments.
function _getIsDisabledStrategyKey(address strategyAddress) private pure
returns(bytes32)
Arguments
Name | Type | Description |
---|---|---|
strategyAddress | address | Enter a strategy address |
Source Code
function _getIsDisabledStrategyKey(address strategyAddress) private pure returns (bytes32) {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_LENDING_STRATEGY_DISABLED, strategyAddress));
}
Disables a strategy
function disableStrategyInternal(IStore s, address toFind) external nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
toFind | address |
Source Code
function disableStrategyInternal(IStore s, address toFind) external {
_disableStrategy(s, toFind);
s.setAddressArrayByKey(ProtoUtilV1.NS_LENDING_STRATEGY_DISABLED, toFind);
}
Deletes a strategy
function deleteStrategyInternal(IStore s, address toFind) external nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
toFind | address |
Source Code
function deleteStrategyInternal(IStore s, address toFind) external {
_deleteStrategy(s, toFind);
}
function addStrategiesInternal(IStore s, address[] strategies) external nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
strategies | address[] |
Source Code
function addStrategiesInternal(IStore s, address[] calldata strategies) external {
for (uint256 i = 0; i < strategies.length; i++) {
address strategy = strategies[i];
_addStrategy(s, strategy);
}
}
function getRiskPoolingPeriodsInternal(IStore s, bytes32 coverKey) external view
returns(lendingPeriod uint256, withdrawalWindow uint256)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 |
Source Code
function getRiskPoolingPeriodsInternal(IStore s, bytes32 coverKey) external view returns (uint256 lendingPeriod, uint256 withdrawalWindow) {
lendingPeriod = s.getUintByKey(getLendingPeriodKey(coverKey));
withdrawalWindow = s.getUintByKey(getWithdrawalWindowKey(coverKey));
if (lendingPeriod == 0) {
lendingPeriod = s.getUintByKey(getLendingPeriodKey(0));
withdrawalWindow = s.getUintByKey(getWithdrawalWindowKey(0));
}
lendingPeriod = lendingPeriod == 0 ? DEFAULT_LENDING_PERIOD : lendingPeriod;
withdrawalWindow = withdrawalWindow == 0 ? DEFAULT_WITHDRAWAL_WINDOW : withdrawalWindow;
}
function setRiskPoolingPeriodsInternal(IStore s, bytes32 coverKey, uint256 lendingPeriod, uint256 withdrawalWindow) external nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 | |
lendingPeriod | uint256 | |
withdrawalWindow | uint256 |
Source Code
function setRiskPoolingPeriodsInternal(
IStore s,
bytes32 coverKey,
uint256 lendingPeriod,
uint256 withdrawalWindow
) external {
s.setUintByKey(getLendingPeriodKey(coverKey), lendingPeriod);
s.setUintByKey(getWithdrawalWindowKey(coverKey), withdrawalWindow);
emit RiskPoolingPeriodSet(coverKey, lendingPeriod, withdrawalWindow);
}
Hash key of the "lending period" for the given cover. Warning: this function does not validate the cover key supplied.
function getLendingPeriodKey(bytes32 coverKey) public pure
returns(bytes32)
Arguments
Name | Type | Description |
---|---|---|
coverKey | bytes32 | Enter cover key |
Source Code
function getLendingPeriodKey(bytes32 coverKey) public pure returns (bytes32) {
if (coverKey > 0) {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_COVER_LIQUIDITY_LENDING_PERIOD, coverKey));
}
return ProtoUtilV1.NS_COVER_LIQUIDITY_LENDING_PERIOD;
}
function getMaxLendingRatioInternal(IStore s) external view
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
s | IStore |
Source Code
function getMaxLendingRatioInternal(IStore s) external view returns (uint256) {
return s.getUintByKey(getMaxLendingRatioKey());
}
function setMaxLendingRatioInternal(IStore s, uint256 ratio) external nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
ratio | uint256 |
Source Code
function setMaxLendingRatioInternal(IStore s, uint256 ratio) external {
s.setUintByKey(getMaxLendingRatioKey(), ratio);
emit MaxLendingRatioSet(ratio);
}
Hash key of the "maximum lending ratio" for the given cover.
function getMaxLendingRatioKey() public pure
returns(bytes32)
Arguments
Name | Type | Description |
---|
Source Code
function getMaxLendingRatioKey() public pure returns (bytes32) {
return ProtoUtilV1.NS_COVER_LIQUIDITY_MAX_LENDING_RATIO;
}
Hash key of the "withdrawal window duration" for the given cover. Warning: this function does not validate the cover key supplied.
function getWithdrawalWindowKey(bytes32 coverKey) public pure
returns(bytes32)
Arguments
Name | Type | Description |
---|---|---|
coverKey | bytes32 | Enter cover key |
Source Code
function getWithdrawalWindowKey(bytes32 coverKey) public pure returns (bytes32) {
if (coverKey > 0) {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_COVER_LIQUIDITY_WITHDRAWAL_WINDOW, coverKey));
}
return ProtoUtilV1.NS_COVER_LIQUIDITY_WITHDRAWAL_WINDOW;
}
function _addStrategy(IStore s, address deployedOn) private nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
deployedOn | address |
Source Code
function _addStrategy(IStore s, address deployedOn) private {
ILendingStrategy strategy = ILendingStrategy(deployedOn);
require(strategy.getWeight() <= ProtoUtilV1.MULTIPLIER, "Weight too much");
s.setBoolByKey(_getIsActiveStrategyKey(deployedOn), true);
s.setAddressArrayByKey(ProtoUtilV1.NS_LENDING_STRATEGY_ACTIVE, deployedOn);
emit StrategyAdded(deployedOn);
}
function _disableStrategy(IStore s, address toFind) private nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
toFind | address |
Source Code
function _disableStrategy(IStore s, address toFind) private {
bytes32 key = ProtoUtilV1.NS_LENDING_STRATEGY_ACTIVE;
uint256 pos = s.getAddressArrayItemPosition(key, toFind);
require(pos > 0, "Invalid strategy");
s.deleteAddressArrayItem(key, toFind);
s.setBoolByKey(_getIsActiveStrategyKey(toFind), false);
s.setBoolByKey(_getIsDisabledStrategyKey(toFind), true);
}
function _deleteStrategy(IStore s, address toFind) private nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
toFind | address |
Source Code
function _deleteStrategy(IStore s, address toFind) private {
bytes32 key = ProtoUtilV1.NS_LENDING_STRATEGY_DISABLED;
uint256 pos = s.getAddressArrayItemPosition(key, toFind);
require(pos > 0, "Invalid strategy");
s.deleteAddressArrayItem(key, toFind);
s.setBoolByKey(_getIsDisabledStrategyKey(toFind), false);
}
function getDisabledStrategiesInternal(IStore s) external view
returns(strategies address[])
Arguments
Name | Type | Description |
---|---|---|
s | IStore |
Source Code
function getDisabledStrategiesInternal(IStore s) external view returns (address[] memory strategies) {
return s.getAddressArrayByKey(ProtoUtilV1.NS_LENDING_STRATEGY_DISABLED);
}
function getActiveStrategiesInternal(IStore s) external view
returns(strategies address[])
Arguments
Name | Type | Description |
---|---|---|
s | IStore |
Source Code
function getActiveStrategiesInternal(IStore s) external view returns (address[] memory strategies) {
return s.getAddressArrayByKey(ProtoUtilV1.NS_LENDING_STRATEGY_ACTIVE);
}
Hash key of the "strategy outs" for the given cover and token. Warning: this function does not validate the cover key and token supplied.
function getStrategyOutKey(bytes32 coverKey, address token) public pure
returns(bytes32)
Arguments
Name | Type | Description |
---|---|---|
coverKey | bytes32 | Enter cover key |
token | address | Enter the token address |
Source Code
function getStrategyOutKey(bytes32 coverKey, address token) public pure returns (bytes32) {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_VAULT_STRATEGY_OUT, coverKey, token));
}
Hash key of the "outs" to a specific strategy for the given cover and token. Warning: this function does not validate the cover key and token supplied.
function getSpecificStrategyOutKey(bytes32 coverKey, bytes32 strategyName, address token) public pure
returns(bytes32)
Arguments
Name | Type | Description |
---|---|---|
coverKey | bytes32 | Enter cover key |
strategyName | bytes32 | |
token | address | Enter the token address |
Source Code
function getSpecificStrategyOutKey(
bytes32 coverKey,
bytes32 strategyName,
address token
) public pure returns (bytes32) {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_VAULT_STRATEGY_OUT, coverKey, strategyName, token));
}
function getAmountInStrategies(IStore s, bytes32 coverKey, address token) public view
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 | |
token | address |
Source Code
function getAmountInStrategies(
IStore s,
bytes32 coverKey,
address token
) public view returns (uint256) {
bytes32 k = getStrategyOutKey(coverKey, token);
return s.getUintByKey(k);
}
function getAmountInStrategy(IStore s, bytes32 coverKey, bytes32 strategyName, address token) public view
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 | |
strategyName | bytes32 | |
token | address |
Source Code
function getAmountInStrategy(
IStore s,
bytes32 coverKey,
bytes32 strategyName,
address token
) public view returns (uint256) {
bytes32 k = getSpecificStrategyOutKey(coverKey, strategyName, token);
return s.getUintByKey(k);
}
function preTransferToStrategyInternal(IStore s, IERC20 token, bytes32 coverKey, bytes32 strategyName, uint256 amount) external nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
token | IERC20 | |
coverKey | bytes32 | |
strategyName | bytes32 | |
amount | uint256 |
Source Code
function preTransferToStrategyInternal(
IStore s,
IERC20 token,
bytes32 coverKey,
bytes32 strategyName,
uint256 amount
) external {
if (s.getStablecoin() != address(token)) {
return;
}
_addToStrategyOut(s, coverKey, address(token), amount);
_addToSpecificStrategyOut(s, coverKey, strategyName, address(token), amount);
}
function postReceiveFromStrategyInternal(IStore s, IERC20 token, bytes32 coverKey, bytes32 strategyName, uint256 received) external nonpayable
returns(income uint256, loss uint256)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
token | IERC20 | |
coverKey | bytes32 | |
strategyName | bytes32 | |
received | uint256 |
Source Code
function postReceiveFromStrategyInternal(
IStore s,
IERC20 token,
bytes32 coverKey,
bytes32 strategyName,
uint256 received
) external returns (uint256 income, uint256 loss) {
if (s.getStablecoin() != address(token)) {
return (income, loss);
}
uint256 amountInThisStrategy = getAmountInStrategy(s, coverKey, strategyName, address(token));
income = received > amountInThisStrategy ? received - amountInThisStrategy : 0;
loss = received < amountInThisStrategy ? amountInThisStrategy - received : 0;
_reduceStrategyOut(s, coverKey, address(token), amountInThisStrategy);
_clearSpecificStrategyOut(s, coverKey, strategyName, address(token));
_logIncomes(s, coverKey, strategyName, income, loss);
}
function _addToStrategyOut(IStore s, bytes32 coverKey, address token, uint256 amountToAdd) private nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 | |
token | address | |
amountToAdd | uint256 |
Source Code
function _addToStrategyOut(
IStore s,
bytes32 coverKey,
address token,
uint256 amountToAdd
) private {
bytes32 k = getStrategyOutKey(coverKey, token);
s.addUintByKey(k, amountToAdd);
}
function _reduceStrategyOut(IStore s, bytes32 coverKey, address token, uint256 amount) private nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 | |
token | address | |
amount | uint256 |
Source Code
function _reduceStrategyOut(
IStore s,
bytes32 coverKey,
address token,
uint256 amount
) private {
bytes32 k = getStrategyOutKey(coverKey, token);
s.subtractUintByKey(k, amount);
}
function _addToSpecificStrategyOut(IStore s, bytes32 coverKey, bytes32 strategyName, address token, uint256 amountToAdd) private nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 | |
strategyName | bytes32 | |
token | address | |
amountToAdd | uint256 |
Source Code
function _addToSpecificStrategyOut(
IStore s,
bytes32 coverKey,
bytes32 strategyName,
address token,
uint256 amountToAdd
) private {
bytes32 k = getSpecificStrategyOutKey(coverKey, strategyName, token);
s.addUintByKey(k, amountToAdd);
}
function _clearSpecificStrategyOut(IStore s, bytes32 coverKey, bytes32 strategyName, address token) private nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 | |
strategyName | bytes32 | |
token | address |
Source Code
function _clearSpecificStrategyOut(
IStore s,
bytes32 coverKey,
bytes32 strategyName,
address token
) private {
bytes32 k = getSpecificStrategyOutKey(coverKey, strategyName, token);
s.deleteUintByKey(k);
}
function _logIncomes(IStore s, bytes32 coverKey, bytes32 strategyName, uint256 income, uint256 loss) private nonpayable
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 | |
strategyName | bytes32 | |
income | uint256 | |
loss | uint256 |
Source Code
function _logIncomes(
IStore s,
bytes32 coverKey,
bytes32 strategyName,
uint256 income,
uint256 loss
) private {
// Overall Income
s.addUintByKey(ProtoUtilV1.NS_VAULT_LENDING_INCOMES, income);
// By Cover
s.addUintByKey(keccak256(abi.encodePacked(ProtoUtilV1.NS_VAULT_LENDING_INCOMES, coverKey)), income);
// By Cover on This Strategy
s.addUintByKey(keccak256(abi.encodePacked(ProtoUtilV1.NS_VAULT_LENDING_INCOMES, coverKey, strategyName)), income);
// Overall Loss
s.addUintByKey(ProtoUtilV1.NS_VAULT_LENDING_LOSSES, loss);
// By Cover
s.addUintByKey(keccak256(abi.encodePacked(ProtoUtilV1.NS_VAULT_LENDING_LOSSES, coverKey)), loss);
// By Cover on This Strategy
s.addUintByKey(keccak256(abi.encodePacked(ProtoUtilV1.NS_VAULT_LENDING_LOSSES, coverKey, strategyName)), loss);
}
function getStablecoinOwnedByVaultInternal(IStore s, bytes32 coverKey) external view
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 |
Source Code
function getStablecoinOwnedByVaultInternal(IStore s, bytes32 coverKey) external view returns (uint256) {
address stablecoin = s.getStablecoin();
uint256 balance = IERC20(stablecoin).balanceOf(s.getVaultAddress(coverKey));
uint256 inStrategies = getAmountInStrategies(s, coverKey, stablecoin);
return balance + inStrategies;
}
- 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