Skip to content

Latest commit

 

History

History
813 lines (661 loc) · 22.3 KB

RoutineInvokerLibV1.md

File metadata and controls

813 lines (661 loc) · 22.3 KB

RoutineInvokerLibV1.sol

View Source: contracts/libraries/RoutineInvokerLibV1.sol

RoutineInvokerLibV1

Enums

Action

enum Action {
 Deposit,
 Withdraw
}

Functions

updateStateAndLiquidity

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);
  }

_invoke

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);
    }
  }

_getUpdateInterval

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);
  }

getWithdrawalInfoInternal

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;
    }
  }

_isWithdrawalPeriod

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;
  }

_updateWithdrawalPeriod

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);
    }
  }

isAccrualCompleteInternal

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));
  }

setAccrualCompleteInternal

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);
  }

getAccrualInvocationKey

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));
  }

getNextWithdrawalStartKey

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));
  }

getNextWithdrawalEndKey

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));
  }

mustBeDuringWithdrawalPeriod

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");
  }

_executeAndGetAction

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;
  }

_canDeposit

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;
  }

_invokeAssetManagement

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);
    }
  }

_executeStrategy

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);
  }

_depositToStrategy

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);
  }

_withdrawAllFromStrategy

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);
    }
  }

_withdrawFromDisabled

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);
      }
    }
  }

Contracts