Skip to content

Latest commit

 

History

History
1078 lines (860 loc) · 29.3 KB

StrategyLibV1.md

File metadata and controls

1078 lines (860 loc) · 29.3 KB

StrategyLibV1.sol

View Source: contracts/libraries/StrategyLibV1.sol

StrategyLibV1

Contract Members

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

Functions

_getIsActiveStrategyKey

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

_getIsDisabledStrategyKey

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

disableStrategyInternal

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

deleteStrategyInternal

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

addStrategiesInternal

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

getRiskPoolingPeriodsInternal

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

setRiskPoolingPeriodsInternal

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

getLendingPeriodKey

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

getMaxLendingRatioInternal

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

setMaxLendingRatioInternal

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

getMaxLendingRatioKey

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

getWithdrawalWindowKey

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

_addStrategy

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

_disableStrategy

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

_deleteStrategy

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

getDisabledStrategiesInternal

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

getActiveStrategiesInternal

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

getStrategyOutKey

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

getSpecificStrategyOutKey

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

getAmountInStrategies

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

getAmountInStrategy

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

preTransferToStrategyInternal

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

postReceiveFromStrategyInternal

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

_addToStrategyOut

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

_reduceStrategyOut

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

_addToSpecificStrategyOut

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

_clearSpecificStrategyOut

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

_logIncomes

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

getStablecoinOwnedByVaultInternal

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

Contracts