Skip to content

Latest commit

 

History

History
599 lines (475 loc) · 17.2 KB

LiquidityEngine.md

File metadata and controls

599 lines (475 loc) · 17.2 KB

Liquidity Engine contract (LiquidityEngine.sol)

View Source: contracts/core/liquidity/LiquidityEngine.sol

↗ Extends: ILiquidityEngine, Recoverable

LiquidityEngine

The liquidity engine contract enables liquidity manager(s) to add, disable, remove, or manage lending or other income strategies.

Functions

function (IStore s) public nonpayable Recoverable 

Arguments

Name Type Description
s IStore
Source Code
constructor(IStore s) Recoverable(s) {}

addStrategies

Adds an array of strategies to the liquidity engine.

function addStrategies(address[] strategies) external nonpayable nonReentrant 

Arguments

Name Type Description
strategies address[] Enter one or more strategies.
Source Code
function addStrategies(address[] calldata strategies) external override nonReentrant {
    require(strategies.length > 0, "No strategy specified");

    s.mustNotBePaused();
    AccessControlLibV1.mustBeLiquidityManager(s);

    s.addStrategiesInternal(strategies);
  }

setLiquidityStateUpdateInterval

The liquidity state update interval allows the protocol to perform various activies such as NPM token price update, deposits or withdrawals to lending strategies, and more.

function setLiquidityStateUpdateInterval(uint256 value) external nonpayable nonReentrant 

Arguments

Name Type Description
value uint256 Specify the update interval value
Source Code
function setLiquidityStateUpdateInterval(uint256 value) external override nonReentrant {
    require(value > 0, "Invalid value");

    s.mustNotBePaused();
    AccessControlLibV1.mustBeLiquidityManager(s);

    s.setUintByKey(ProtoUtilV1.NS_LIQUIDITY_STATE_UPDATE_INTERVAL, value);
    emit LiquidityStateUpdateIntervalSet(value);
  }

disableStrategy

Disables a strategy by address. When a strategy is disabled, it immediately withdraws and cannot lend any further.

function disableStrategy(address strategy) external nonpayable nonReentrant 

Arguments

Name Type Description
strategy address Enter the strategy contract address to disable
Source Code
function disableStrategy(address strategy) external override nonReentrant {
    // because this function can only be invoked by a liquidity manager.
    s.mustNotBePaused();
    AccessControlLibV1.mustBeLiquidityManager(s);

    s.disableStrategyInternal(strategy);
    emit StrategyDisabled(strategy);
  }

deleteStrategy

Permanently deletes a disabled strategy by address.

function deleteStrategy(address strategy) external nonpayable nonReentrant 

Arguments

Name Type Description
strategy address Enter the strategy contract address to delete
Source Code
function deleteStrategy(address strategy) external override nonReentrant {
    s.mustNotBePaused();
    AccessControlLibV1.mustBeLiquidityManager(s);

    s.deleteStrategyInternal(strategy);
    emit StrategyDeleted(strategy);
  }

setRiskPoolingPeriods

In order to pool risks collectively, liquidity providers may lend their stablecoins to a cover pool of their choosing during "lending periods" and withdraw them during "withdrawal windows." These periods are known as risk pooling periods.

The default lending period is six months, and the withdrawal window is seven days. Specify a cover key if you want to configure or override these periods for a cover. If no cover key is specified, the values entered will be set as global parameters.

function setRiskPoolingPeriods(bytes32 coverKey, uint256 lendingPeriod, uint256 withdrawalWindow) external nonpayable nonReentrant 

Arguments

Name Type Description
coverKey bytes32 Enter a cover key to set the periods. Enter 0x if you want to set the values globally.
lendingPeriod uint256 Enter the lending duration. Example: 180 days.
withdrawalWindow uint256 Enter the withdrawal duration. Example: 7 days.
Source Code
function setRiskPoolingPeriods(
    bytes32 coverKey,
    uint256 lendingPeriod,
    uint256 withdrawalWindow
  ) external override nonReentrant {
    require(lendingPeriod > 0, "Please specify lending period");
    require(withdrawalWindow > 0, "Please specify withdrawal window");

    s.mustNotBePaused();
    AccessControlLibV1.mustBeLiquidityManager(s);

    s.setRiskPoolingPeriodsInternal(coverKey, lendingPeriod, withdrawalWindow);
    // event emitted in the above function
  }

setMaxLendingRatio

Specify the maximum lending ratio a strategy can utilize, not to exceed 100 percent.

function setMaxLendingRatio(uint256 ratio) external nonpayable nonReentrant 

Arguments

Name Type Description
ratio uint256 . Enter the ratio as a percentage value. Use ProtoUtilV1.MULTIPLIER as your divisor.
Source Code
function setMaxLendingRatio(uint256 ratio) external override nonReentrant {
    require(ratio > 0, "Please specify lending ratio");
    require(ratio <= ProtoUtilV1.MULTIPLIER, "Invalid lending ratio");

    s.mustNotBePaused();
    AccessControlLibV1.mustBeLiquidityManager(s);

    s.setMaxLendingRatioInternal(ratio);
  }

getMaxLendingRatio

Gets the maximum lending ratio a strategy can utilize.

function getMaxLendingRatio() external view
returns(ratio uint256)

Arguments

Name Type Description
Source Code
function getMaxLendingRatio() external view override returns (uint256 ratio) {
    return s.getMaxLendingRatioInternal();
  }

getRiskPoolingPeriods

Returns the risk pooling periods of a given cover key. Global values are returned if the risk pooling period for the given cover key was not defined. If global values are also undefined, fallback value of 180-day lending period and 7-day withdrawal window are returned. Warning: this function does not validate the cover key supplied.

function getRiskPoolingPeriods(bytes32 coverKey) external view
returns(lendingPeriod uint256, withdrawalWindow uint256)

Arguments

Name Type Description
coverKey bytes32 Enter the coverkey to retrieve the lending period of. Warning: this function doesn't check if the supplied cover key is a valid.
Source Code
function getRiskPoolingPeriods(bytes32 coverKey) external view override returns (uint256 lendingPeriod, uint256 withdrawalWindow) {
    return s.getRiskPoolingPeriodsInternal(coverKey);
  }

getDisabledStrategies

Returns a list of disabled strategies.

function getDisabledStrategies() external view
returns(strategies address[])

Arguments

Name Type Description
Source Code
function getDisabledStrategies() external view override returns (address[] memory strategies) {
    return s.getDisabledStrategiesInternal();
  }

getActiveStrategies

Returns a list of actively lending strategies.

function getActiveStrategies() external view
returns(strategies address[])

Arguments

Name Type Description
Source Code
function getActiveStrategies() external view override returns (address[] memory strategies) {
    return s.getActiveStrategiesInternal();
  }

addBulkLiquidity

function addBulkLiquidity(struct IVault.AddLiquidityArgs[] args) external nonpayable

Arguments

Name Type Description
args struct IVault.AddLiquidityArgs[]
Source Code
function addBulkLiquidity(IVault.AddLiquidityArgs[] calldata args) external override {
    IERC20 stablecoin = IERC20(s.getStablecoinAddressInternal());
    IERC20 npm = s.getNpmTokenInstanceInternal();

    uint256 totalAmount;
    uint256 totalNpm;

    for (uint256 i = 0; i < args.length; i++) {
      totalAmount += args[i].amount;
      totalNpm += args[i].npmStakeToAdd;
    }

    stablecoin.ensureTransferFrom(msg.sender, address(this), totalAmount);
    npm.ensureTransferFrom(msg.sender, address(this), totalNpm);

    for (uint256 i = 0; i < args.length; i++) {
      IVault vault = s.getVault(args[i].coverKey);
      uint256 balance = vault.balanceOf(address(this));

      if (balance > 0) {
        IERC20(vault).ensureTransfer(s.getTreasuryAddressInternal(), balance);
      }

      stablecoin.approve(address(vault), args[i].amount);
      npm.approve(address(vault), args[i].npmStakeToAdd);

      vault.addLiquidity(args[i]);

      balance = vault.balanceOf(address(this));

      require(balance > 0, "Fatal, no PODs minted");

      IERC20(vault).ensureTransfer(msg.sender, vault.balanceOf(address(this)));
    }
  }

version

Version number of this contract

function version() external pure
returns(bytes32)

Arguments

Name Type Description
Source Code
function version() external pure override returns (bytes32) {
    return "v0.1";
  }

getName

Name of this contract

function getName() external pure
returns(bytes32)

Arguments

Name Type Description
Source Code
function getName() external pure override returns (bytes32) {
    return ProtoUtilV1.CNAME_LIQUIDITY_ENGINE;
  }

Contracts