Skip to content

Latest commit

 

History

History
770 lines (631 loc) · 25.8 KB

CoverLibV1.md

File metadata and controls

770 lines (631 loc) · 25.8 KB

CoverLibV1.sol

View Source: contracts/libraries/CoverLibV1.sol

CoverLibV1

Events

event CoverUserWhitelistUpdated(bytes32 indexed coverKey, bytes32 indexed productKey, address indexed account, bool  status);

Functions

initializeCoverInternal

function initializeCoverInternal(IStore s, address stablecoin, bytes32 friendlyName) external nonpayable

Arguments

Name Type Description
s IStore tablecoin Provide the address of the token this cover will be quoted against.
stablecoin address Provide the address of the token this cover will be quoted against.
friendlyName bytes32 Enter a description or ENS name of your liquidity token.
Source Code
function initializeCoverInternal(
    IStore s,
    address stablecoin,
    bytes32 friendlyName
  ) external {
    s.setAddressByKey(ProtoUtilV1.CNS_COVER_STABLECOIN, stablecoin);
    s.setBytes32ByKey(ProtoUtilV1.NS_COVER_STABLECOIN_NAME, friendlyName);

    s.updateStateAndLiquidity(0);
  }

addCoverInternal

Adds a new coverage pool or cover contract. To add a new cover, you need to pay cover creation fee and stake minimum amount of NPM in the Vault.

Through the governance portal, projects will be able redeem the full cover fee at a later date.

Apply for Fee Redemption
https://docs.neptunemutual.com/covers/cover-fee-redemption

As the cover creator, you will earn a portion of all cover fees generated in this pool.

Read the documentation to learn more about the fees:
https://docs.neptunemutual.com/covers/contract-creators

function addCoverInternal(IStore s, bytes32 coverKey, bool supportsProducts, bytes32 info, bool requiresWhitelist, uint256[] values) external nonpayable

Arguments

Name Type Description
s IStore Provide store instance
coverKey bytes32 Enter a unique key for this cover
supportsProducts bool Indicates that this cover supports product(s)
info bytes32 IPFS info of the cover contract
requiresWhitelist bool Signifies if this cover only enables whitelisted addresses to purchase policies.
values uint256[] [0] stakeWithFee Enter the total NPM amount (stake + fee) to transfer to this contract.
Source Code
function addCoverInternal(
    IStore s,
    bytes32 coverKey,
    bool supportsProducts,
    bytes32 info,
    bool requiresWhitelist,
    uint256[] calldata values
  ) external {
    // First validate the information entered
    (uint256 fee, ) = _validateAndGetFee(s, coverKey, info, values[0]);

    // Set the basic cover info
    _addCover(s, coverKey, supportsProducts, info, requiresWhitelist, values, fee);

    // Stake the supplied NPM tokens and burn the fees
    s.getStakingContract().increaseStake(coverKey, msg.sender, values[0], fee);

    // Add cover reassurance
    if (values[1] > 0) {
      s.getReassuranceContract().addReassurance(coverKey, msg.sender, values[1]);
    }
  }

_addCover

Adds a new cover

function _addCover(IStore s, bytes32 coverKey, bool supportsProducts, bytes32 info, bool requiresWhitelist, uint256[] values, uint256 fee) private nonpayable

Arguments

Name Type Description
s IStore Specify store instance
coverKey bytes32 Enter cover key
supportsProducts bool Indicates that this cover supports product(s)
info bytes32 IPFS info of the cover contract
requiresWhitelist bool Signifies if this cover only enables whitelisted addresses to purchase policies.
values uint256[] [0] stakeWithFee Enter the total NPM amount (stake + fee) to transfer to this contract.
fee uint256
Source Code
function _addCover(
    IStore s,
    bytes32 coverKey,
    bool supportsProducts,
    bytes32 info,
    bool requiresWhitelist,
    uint256[] calldata values,
    uint256 fee
  ) private {
    require(coverKey > 0, "Invalid cover key");
    require(info > 0, "Invalid info");
    require(values[2] > 0, "Invalid min reporting stake");
    require(values[3] > 0, "Invalid reporting period");
    require(values[4] > 0, "Invalid cooldown period");
    require(values[5] > 0, "Invalid claim period");
    require(values[6] > 0, "Invalid floor rate");
    require(values[7] > 0, "Invalid ceiling rate");
    require(values[8] > 0, "Invalid reassurance rate");
    require(values[9] > 0 && values[9] < 25, "Invalid leverage");

    if (supportsProducts == false) {
      // Standalone pools do not support any leverage
      require(values[9] == 1, "Invalid leverage");
    }

    s.setBoolByKeys(ProtoUtilV1.NS_COVER, coverKey, true);

    s.setBoolByKeys(ProtoUtilV1.NS_COVER_SUPPORTS_PRODUCTS, coverKey, supportsProducts);
    s.setAddressByKeys(ProtoUtilV1.NS_COVER_OWNER, coverKey, msg.sender);
    s.setBytes32ByKeys(ProtoUtilV1.NS_COVER_INFO, coverKey, info);
    s.setUintByKeys(ProtoUtilV1.NS_COVER_REASSURANCE_WEIGHT, coverKey, ProtoUtilV1.MULTIPLIER); // 100% weight because it's a stablecoin

    // Set the fee charged during cover creation
    s.setUintByKeys(ProtoUtilV1.NS_COVER_CREATION_FEE_EARNING, coverKey, fee);

    s.setUintByKeys(ProtoUtilV1.NS_COVER_CREATION_DATE, coverKey, block.timestamp); // solhint-disable-line

    s.setBoolByKeys(ProtoUtilV1.NS_COVER_REQUIRES_WHITELIST, coverKey, requiresWhitelist);

    s.setUintByKeys(ProtoUtilV1.NS_GOVERNANCE_REPORTING_MIN_FIRST_STAKE, coverKey, values[2]);
    s.setUintByKeys(ProtoUtilV1.NS_GOVERNANCE_REPORTING_PERIOD, coverKey, values[3]);
    s.setUintByKeys(ProtoUtilV1.NS_RESOLUTION_COOL_DOWN_PERIOD, coverKey, values[4]);
    s.setUintByKeys(ProtoUtilV1.NS_CLAIM_PERIOD, coverKey, values[5]);
    s.setUintByKeys(ProtoUtilV1.NS_COVER_POLICY_RATE_FLOOR, coverKey, values[6]);
    s.setUintByKeys(ProtoUtilV1.NS_COVER_POLICY_RATE_CEILING, coverKey, values[7]);
    s.setUintByKeys(ProtoUtilV1.NS_COVER_REASSURANCE_RATE, coverKey, values[8]);
    s.setUintByKeys(ProtoUtilV1.NS_COVER_LEVERAGE_FACTOR, coverKey, values[9]);
  }

addProductInternal

Adds a product under a diversified cover pool

function addProductInternal(IStore s, bytes32 coverKey, bytes32 productKey, bytes32 info, bool requiresWhitelist, uint256[] values) external nonpayable

Arguments

Name Type Description
s IStore Specify store instance
coverKey bytes32 Enter a cover key
productKey bytes32 Enter the product key
info bytes32 IPFS hash. Check out the documentation for more info.
requiresWhitelist bool Enter true if you want to maintain a whitelist and restrict non-whitelisted users to purchase policies.
values uint256[] [0] Product status
Source Code
function addProductInternal(
    IStore s,
    bytes32 coverKey,
    bytes32 productKey,
    bytes32 info,
    bool requiresWhitelist,
    uint256[] calldata values
  ) external {
    s.mustBeValidCoverKey(coverKey);
    s.mustSupportProducts(coverKey);

    require(productKey > 0, "Invalid product key");
    require(info > 0, "Invalid info");

    // Product Status
    // 0 --> Deleted
    // 1 --> Active
    // 2 --> Retired
    require(values[0] == 1, "Status must be active");
    require(values[1] > 0 && values[1] <= 10_000, "Invalid efficiency");

    require(s.getBoolByKeys(ProtoUtilV1.NS_COVER_PRODUCT, coverKey, productKey) == false, "Already exists");

    s.setBoolByKeys(ProtoUtilV1.NS_COVER_PRODUCT, coverKey, productKey, true);
    s.setBytes32ByKeys(ProtoUtilV1.NS_COVER_PRODUCT, coverKey, productKey, info);
    s.setBytes32ArrayByKeys(ProtoUtilV1.NS_COVER_PRODUCT, coverKey, productKey);
    s.setBoolByKeys(ProtoUtilV1.NS_COVER_REQUIRES_WHITELIST, coverKey, productKey, requiresWhitelist);

    s.setUintByKeys(ProtoUtilV1.NS_COVER_PRODUCT, coverKey, productKey, values[0]);
    s.setUintByKeys(ProtoUtilV1.NS_COVER_PRODUCT_EFFICIENCY, coverKey, productKey, values[1]);
  }

updateProductInternal

Updates a cover product.

function updateProductInternal(IStore s, bytes32 coverKey, bytes32 productKey, bytes32 info, uint256[] values) external nonpayable

Arguments

Name Type Description
s IStore Specify store instance
coverKey bytes32 Enter the cover key
productKey bytes32 Enter the product key
info bytes32 Enter a new IPFS URL to update
values uint256[] [0] Product status
Source Code
function updateProductInternal(
    IStore s,
    bytes32 coverKey,
    bytes32 productKey,
    bytes32 info,
    uint256[] calldata values
  ) external {
    require(values[0] <= 2, "Invalid product status");
    require(values[1] > 0 && values[1] <= 10_000, "Invalid efficiency");

    s.mustBeValidCoverKey(coverKey);
    s.mustBeSupportedProductOrEmpty(coverKey, productKey);

    s.setUintByKeys(ProtoUtilV1.NS_COVER_PRODUCT, coverKey, productKey, values[0]);
    s.setUintByKeys(ProtoUtilV1.NS_COVER_PRODUCT_EFFICIENCY, coverKey, productKey, values[1]);
    s.setBytes32ByKeys(ProtoUtilV1.NS_COVER_PRODUCT, coverKey, productKey, info);
  }

deployVaultInternal

Deploys vault contract for the given cover key. The vault contract is also an ERC-20-compatible contract. Reverts if the vault was previously deployed.

function deployVaultInternal(IStore s, bytes32 coverKey, string tokenName, string tokenSymbol) external nonpayable
returns(address)

Arguments

Name Type Description
s IStore Specify store instance
coverKey bytes32 Enter cover key
tokenName string Enter a name for the ERC-20 token
tokenSymbol string Enter a symbol for the ERC-20 token
Source Code
function deployVaultInternal(
    IStore s,
    bytes32 coverKey,
    string calldata tokenName,
    string calldata tokenSymbol
  ) external returns (address) {
    address vault = s.getProtocolContract(ProtoUtilV1.CNS_COVER_VAULT, coverKey);
    require(vault == address(0), "Vault already deployed");

    // Deploy cover liquidity contract
    address deployed = s.getVaultFactoryContract().deploy(coverKey, tokenName, tokenSymbol);

    s.getProtocol().addContractWithKey(ProtoUtilV1.CNS_COVER_VAULT, coverKey, address(deployed));
    return deployed;
  }

_validateAndGetFee

Validation checks before adding a new cover

function _validateAndGetFee(IStore s, bytes32 coverKey, bytes32 info, uint256 stakeWithFee) private view
returns(fee uint256, minCoverCreationStake uint256)

Arguments

Name Type Description
s IStore
coverKey bytes32
info bytes32
stakeWithFee uint256
Source Code
function _validateAndGetFee(
    IStore s,
    bytes32 coverKey,
    bytes32 info,
    uint256 stakeWithFee
  ) private view returns (uint256 fee, uint256 minCoverCreationStake) {
    require(info > 0, "Invalid info");
    (fee, minCoverCreationStake, ) = s.getCoverCreationFeeInfo();

    uint256 minStake = fee + minCoverCreationStake;

    require(stakeWithFee > minStake, "NPM Insufficient");
    require(s.getBoolByKeys(ProtoUtilV1.NS_COVER, coverKey) == false, "Already exists");
  }

updateCoverInternal

Updates the cover info.

function updateCoverInternal(IStore s, bytes32 coverKey, bytes32 info) external nonpayable

Arguments

Name Type Description
s IStore Specify store instance
coverKey bytes32 Enter the cover key
info bytes32 IPFS hash. Check out the documentation for more info.
Source Code
function updateCoverInternal(
    IStore s,
    bytes32 coverKey,
    bytes32 info
  ) external {
    s.setBytes32ByKeys(ProtoUtilV1.NS_COVER_INFO, coverKey, info);
  }

updateCoverCreatorWhitelistInternal

Adds or removes an account to the cover creator whitelist. For the first version of the protocol, a cover creator has to be whitelisted before they can call the addCover function.

function updateCoverCreatorWhitelistInternal(IStore s, address account, bool status) external nonpayable

Arguments

Name Type Description
s IStore Specify store instance
account address Enter the address of the cover creator
status bool Set this to true if you want to add to or false to remove from the whitelist
Source Code
function updateCoverCreatorWhitelistInternal(
    IStore s,
    address account,
    bool status
  ) external {
    s.setAddressBooleanByKey(ProtoUtilV1.NS_COVER_CREATOR_WHITELIST, account, status);
  }

_updateCoverUserWhitelistInternal

Adds or removes an account from the cover users whitelist

function _updateCoverUserWhitelistInternal(IStore s, bytes32 coverKey, bytes32 productKey, address account, bool status) private nonpayable

Arguments

Name Type Description
s IStore Specify store instance
coverKey bytes32 Enter cover key
productKey bytes32 Enter product key
account address Enter the account you would like to add or remove fom the whitelist
status bool Enter true to add or false to remove the specified account from the whitelist
Source Code
function _updateCoverUserWhitelistInternal(
    IStore s,
    bytes32 coverKey,
    bytes32 productKey,
    address account,
    bool status
  ) private {
    s.setAddressBooleanByKeys(ProtoUtilV1.NS_COVER_USER_WHITELIST, coverKey, productKey, account, status);
    emit CoverUserWhitelistUpdated(coverKey, productKey, account, status);
  }

updateCoverUsersWhitelistInternal

Adds or removes an account from the cover user whitelist. Whitelisting is an optional feature cover creators can enable. When a cover requires whitelist, you must add accounts to the cover user whitelist before they are able to purchase policies.

function updateCoverUsersWhitelistInternal(IStore s, bytes32 coverKey, bytes32 productKey, address[] accounts, bool[] statuses) external nonpayable

Arguments

Name Type Description
s IStore Specify store instance
coverKey bytes32 Enter cover key
productKey bytes32 Enter product key
accounts address[] Enter a list of accounts you would like to update the whitelist statuses of.
statuses bool[] Enter respective statuses of the specified whitelisted accounts.
Source Code
function updateCoverUsersWhitelistInternal(
    IStore s,
    bytes32 coverKey,
    bytes32 productKey,
    address[] calldata accounts,
    bool[] calldata statuses
  ) external {
    require(accounts.length == statuses.length, "Inconsistent array sizes");

    for (uint256 i = 0; i < accounts.length; i++) {
      _updateCoverUserWhitelistInternal(s, coverKey, productKey, accounts[i], statuses[i]);
    }
  }

setCoverCreationFeeInternal

Sets cover creation fee in NPM token units

function setCoverCreationFeeInternal(IStore s, uint256 value) external nonpayable
returns(previous uint256)

Arguments

Name Type Description
s IStore Specify store instance
value uint256 Enter the amount of NPM tokens to be charged as the cover creation fee.

Returns

previous Returns the previous cover creation fee.

Source Code
function setCoverCreationFeeInternal(IStore s, uint256 value) external returns (uint256 previous) {
    previous = s.getUintByKey(ProtoUtilV1.NS_COVER_CREATION_FEE);
    s.setUintByKey(ProtoUtilV1.NS_COVER_CREATION_FEE, value);

    s.updateStateAndLiquidity(0);
  }

setMinCoverCreationStakeInternal

Sets the minimum amount of NPM stake required to create a new cover

function setMinCoverCreationStakeInternal(IStore s, uint256 value) external nonpayable
returns(previous uint256)

Arguments

Name Type Description
s IStore Specify store instance
value uint256 Enter the amount of NPM tokens to be staked when creating a new cover.

Returns

previous Returns the previous minimum cover creation stake.

Source Code
function setMinCoverCreationStakeInternal(IStore s, uint256 value) external returns (uint256 previous) {
    s.mustNotBePaused();
    AccessControlLibV1.mustBeCoverManager(s);

    previous = s.getMinCoverCreationStake();
    s.setUintByKey(ProtoUtilV1.NS_COVER_CREATION_MIN_STAKE, value);

    s.updateStateAndLiquidity(0);
  }

setMinStakeToAddLiquidityInternal

Sets the minimum amount of NPM stake required to add liquidity

function setMinStakeToAddLiquidityInternal(IStore s, uint256 value) external nonpayable
returns(previous uint256)

Arguments

Name Type Description
s IStore Specify store instance
value uint256 Enter the amount of NPM tokens to be staked when adding liquidity.

Returns

previous Returns the previous minimum stake to add liquidity.

Source Code
function setMinStakeToAddLiquidityInternal(IStore s, uint256 value) external returns (uint256 previous) {
    s.mustNotBePaused();
    AccessControlLibV1.mustBeCoverManager(s);

    previous = s.getMinStakeToAddLiquidity();
    s.setUintByKey(ProtoUtilV1.NS_COVER_LIQUIDITY_MIN_STAKE, value);

    s.updateStateAndLiquidity(0);
  }

Contracts