Skip to content

Latest commit

 

History

History
1301 lines (1024 loc) · 35.5 KB

ValidationLibV1.md

File metadata and controls

1301 lines (1024 loc) · 35.5 KB

ValidationLibV1.sol

View Source: contracts/libraries/ValidationLibV1.sol

ValidationLibV1

Functions

mustNotBePaused

Reverts if the protocol is paused

function mustNotBePaused(IStore s) public view

Arguments

Name Type Description
s IStore
Source Code
function mustNotBePaused(IStore s) public view {
    address protocol = s.getProtocolAddress();
    require(IPausable(protocol).paused() == false, "Protocol is paused");
  }

mustHaveNormalCoverStatus

Reverts if the key does not resolve in a valid cover contract or if the cover is under governance.

function mustHaveNormalCoverStatus(IStore s, bytes32 coverKey) external view

Arguments

Name Type Description
s IStore
coverKey bytes32 Enter the cover key to check
Source Code
function mustHaveNormalCoverStatus(IStore s, bytes32 coverKey) external view {
    require(s.getBoolByKeys(ProtoUtilV1.NS_COVER, coverKey), "Cover does not exist");
    require(s.getCoverStatus(coverKey) == CoverUtilV1.CoverStatus.Normal, "Status not normal");
  }

mustHaveStoppedCoverStatus

Reverts if the key does not resolve in a valid cover contract or if the cover is under governance.

function mustHaveStoppedCoverStatus(IStore s, bytes32 coverKey) external view

Arguments

Name Type Description
s IStore
coverKey bytes32 Enter the cover key to check
Source Code
function mustHaveStoppedCoverStatus(IStore s, bytes32 coverKey) external view {
    require(s.getBoolByKeys(ProtoUtilV1.NS_COVER, coverKey), "Cover does not exist");
    require(s.getCoverStatus(coverKey) == CoverUtilV1.CoverStatus.Stopped, "Cover isn't stopped");
  }

mustBeValidCoverKey

Reverts if the key does not resolve in a valid cover contract.

function mustBeValidCoverKey(IStore s, bytes32 coverKey) external view

Arguments

Name Type Description
s IStore
coverKey bytes32 Enter the cover key to check
Source Code
function mustBeValidCoverKey(IStore s, bytes32 coverKey) external view {
    require(s.getBoolByKeys(ProtoUtilV1.NS_COVER, coverKey), "Cover does not exist");
  }

mustBeCoverOwner

Reverts if the sender is not the cover owner

function mustBeCoverOwner(IStore s, bytes32 coverKey, address sender) public view

Arguments

Name Type Description
s IStore ender The msg.sender value
coverKey bytes32 Enter the cover key to check
sender address The msg.sender value
Source Code
function mustBeCoverOwner(
    IStore s,
    bytes32 coverKey,
    address sender
  ) public view {
    bool isCoverOwner = s.getCoverOwner(coverKey) == sender;
    require(isCoverOwner, "Forbidden");
  }

mustBeCoverOwnerOrCoverContract

Reverts if the sender is not the cover owner or the cover contract

function mustBeCoverOwnerOrCoverContract(IStore s, bytes32 coverKey, address sender) external view

Arguments

Name Type Description
s IStore ender The msg.sender value
coverKey bytes32 Enter the cover key to check
sender address The msg.sender value
Source Code
function mustBeCoverOwnerOrCoverContract(
    IStore s,
    bytes32 coverKey,
    address sender
  ) external view {
    bool isCoverOwner = s.getCoverOwner(coverKey) == sender;
    bool isCoverContract = address(s.getCoverContract()) == sender;

    require(isCoverOwner || isCoverContract, "Forbidden");
  }

senderMustBeCoverOwnerOrAdmin

function senderMustBeCoverOwnerOrAdmin(IStore s, bytes32 coverKey) external view

Arguments

Name Type Description
s IStore
coverKey bytes32
Source Code
function senderMustBeCoverOwnerOrAdmin(IStore s, bytes32 coverKey) external view {
    if (AccessControlLibV1.hasAccess(s, AccessControlLibV1.NS_ROLES_ADMIN, msg.sender) == false) {
      mustBeCoverOwner(s, coverKey, msg.sender);
    }
  }

senderMustBePolicyContract

function senderMustBePolicyContract(IStore s) external view

Arguments

Name Type Description
s IStore
Source Code
function senderMustBePolicyContract(IStore s) external view {
    s.senderMustBeExactContract(ProtoUtilV1.CNS_COVER_POLICY);
  }

senderMustBePolicyManagerContract

function senderMustBePolicyManagerContract(IStore s) external view

Arguments

Name Type Description
s IStore
Source Code
function senderMustBePolicyManagerContract(IStore s) external view {
    s.senderMustBeExactContract(ProtoUtilV1.CNS_COVER_POLICY_MANAGER);
  }

senderMustBeCoverContract

function senderMustBeCoverContract(IStore s) external view

Arguments

Name Type Description
s IStore
Source Code
function senderMustBeCoverContract(IStore s) external view {
    s.senderMustBeExactContract(ProtoUtilV1.CNS_COVER);
  }

senderMustBeVaultContract

function senderMustBeVaultContract(IStore s, bytes32 coverKey) external view

Arguments

Name Type Description
s IStore
coverKey bytes32
Source Code
function senderMustBeVaultContract(IStore s, bytes32 coverKey) external view {
    address vault = s.getVaultAddress(coverKey);
    require(msg.sender == vault, "Forbidden");
  }

senderMustBeGovernanceContract

function senderMustBeGovernanceContract(IStore s) external view

Arguments

Name Type Description
s IStore
Source Code
function senderMustBeGovernanceContract(IStore s) external view {
    s.senderMustBeExactContract(ProtoUtilV1.CNS_GOVERNANCE);
  }

senderMustBeClaimsProcessorContract

function senderMustBeClaimsProcessorContract(IStore s) external view

Arguments

Name Type Description
s IStore
Source Code
function senderMustBeClaimsProcessorContract(IStore s) external view {
    s.senderMustBeExactContract(ProtoUtilV1.CNS_CLAIM_PROCESSOR);
  }

callerMustBeClaimsProcessorContract

function callerMustBeClaimsProcessorContract(IStore s, address caller) external view

Arguments

Name Type Description
s IStore
caller address
Source Code
function callerMustBeClaimsProcessorContract(IStore s, address caller) external view {
    s.callerMustBeExactContract(ProtoUtilV1.CNS_CLAIM_PROCESSOR, caller);
  }

senderMustBeStrategyContract

function senderMustBeStrategyContract(IStore s) external view

Arguments

Name Type Description
s IStore
Source Code
function senderMustBeStrategyContract(IStore s) external view {
    bool senderIsStrategyContract = s.getBoolByKey(_getIsActiveStrategyKey(msg.sender));
    require(senderIsStrategyContract == true, "Not a strategy contract");
  }

callerMustBeStrategyContract

function callerMustBeStrategyContract(IStore s, address caller) external view

Arguments

Name Type Description
s IStore
caller address
Source Code
function callerMustBeStrategyContract(IStore s, address caller) external view {
    bool callerIsStrategyContract = s.getBoolByKey(_getIsActiveStrategyKey(caller));
    require(callerIsStrategyContract == true, "Not a strategy contract");
  }

callerMustBeSpecificStrategyContract

function callerMustBeSpecificStrategyContract(IStore s, address caller, bytes32 ) external view

Arguments

Name Type Description
s IStore
caller address
bytes32
Source Code
function callerMustBeSpecificStrategyContract(
    IStore s,
    address caller,
    bytes32 /*strategyName*/
  ) external view {
    bool callerIsStrategyContract = s.getBoolByKey(_getIsActiveStrategyKey(caller));
    require(callerIsStrategyContract == true, "Not a strategy contract");
  }

_getIsActiveStrategyKey

function _getIsActiveStrategyKey(address strategyAddress) private pure
returns(bytes32)

Arguments

Name Type Description
strategyAddress address
Source Code
function _getIsActiveStrategyKey(address strategyAddress) private pure returns (bytes32) {
    return keccak256(abi.encodePacked(ProtoUtilV1.NS_LENDING_STRATEGY_ACTIVE, strategyAddress));
  }

senderMustBeProtocolMember

function senderMustBeProtocolMember(IStore s) external view

Arguments

Name Type Description
s IStore
Source Code
function senderMustBeProtocolMember(IStore s) external view {
    require(s.isProtocolMember(msg.sender), "Forbidden");
  }

mustBeReporting

function mustBeReporting(IStore s, bytes32 coverKey) external view

Arguments

Name Type Description
s IStore
coverKey bytes32
Source Code
function mustBeReporting(IStore s, bytes32 coverKey) external view {
    require(s.getCoverStatus(coverKey) == CoverUtilV1.CoverStatus.IncidentHappened, "Not reporting");
  }

mustBeDisputed

function mustBeDisputed(IStore s, bytes32 coverKey) external view

Arguments

Name Type Description
s IStore
coverKey bytes32
Source Code
function mustBeDisputed(IStore s, bytes32 coverKey) external view {
    require(s.getCoverStatus(coverKey) == CoverUtilV1.CoverStatus.FalseReporting, "Not disputed");
  }

mustBeClaimable

function mustBeClaimable(IStore s, bytes32 coverKey) public view

Arguments

Name Type Description
s IStore
coverKey bytes32
Source Code
function mustBeClaimable(IStore s, bytes32 coverKey) public view {
    require(s.getCoverStatus(coverKey) == CoverUtilV1.CoverStatus.Claimable, "Not claimable");
  }

mustBeClaimingOrDisputed

function mustBeClaimingOrDisputed(IStore s, bytes32 coverKey) external view

Arguments

Name Type Description
s IStore
coverKey bytes32
Source Code
function mustBeClaimingOrDisputed(IStore s, bytes32 coverKey) external view {
    CoverUtilV1.CoverStatus status = s.getCoverStatus(coverKey);

    bool claiming = status == CoverUtilV1.CoverStatus.Claimable;
    bool falseReporting = status == CoverUtilV1.CoverStatus.FalseReporting;

    require(claiming || falseReporting, "Not claimable nor disputed");
  }

mustBeReportingOrDisputed

function mustBeReportingOrDisputed(IStore s, bytes32 coverKey) external view

Arguments

Name Type Description
s IStore
coverKey bytes32
Source Code
function mustBeReportingOrDisputed(IStore s, bytes32 coverKey) external view {
    CoverUtilV1.CoverStatus status = s.getCoverStatus(coverKey);
    bool incidentHappened = status == CoverUtilV1.CoverStatus.IncidentHappened;
    bool falseReporting = status == CoverUtilV1.CoverStatus.FalseReporting;

    require(incidentHappened || falseReporting, "Not reported nor disputed");
  }

mustBeBeforeResolutionDeadline

function mustBeBeforeResolutionDeadline(IStore s, bytes32 coverKey) external view

Arguments

Name Type Description
s IStore
coverKey bytes32
Source Code
function mustBeBeforeResolutionDeadline(IStore s, bytes32 coverKey) external view {
    uint256 deadline = s.getResolutionDeadlineInternal(coverKey);

    if (deadline > 0) {
      require(block.timestamp < deadline, "Emergency resolution deadline over"); // solhint-disable-line
    }
  }

mustNotHaveResolutionDeadline

function mustNotHaveResolutionDeadline(IStore s, bytes32 coverKey) external view

Arguments

Name Type Description
s IStore
coverKey bytes32
Source Code
function mustNotHaveResolutionDeadline(IStore s, bytes32 coverKey) external view {
    uint256 deadline = s.getResolutionDeadlineInternal(coverKey);
    require(deadline == 0, "Resolution already has deadline");
  }

mustBeAfterResolutionDeadline

function mustBeAfterResolutionDeadline(IStore s, bytes32 coverKey) public view

Arguments

Name Type Description
s IStore
coverKey bytes32
Source Code
function mustBeAfterResolutionDeadline(IStore s, bytes32 coverKey) public view {
    uint256 deadline = s.getResolutionDeadlineInternal(coverKey);
    require(deadline > 0 && block.timestamp > deadline, "Still unresolved"); // solhint-disable-line
  }

mustBeValidIncidentDate

function mustBeValidIncidentDate(IStore s, bytes32 coverKey, uint256 incidentDate) public view

Arguments

Name Type Description
s IStore
coverKey bytes32
incidentDate uint256
Source Code
function mustBeValidIncidentDate(
    IStore s,
    bytes32 coverKey,
    uint256 incidentDate
  ) public view {
    require(s.getLatestIncidentDateInternal(coverKey) == incidentDate, "Invalid incident date");
  }

mustHaveDispute

function mustHaveDispute(IStore s, bytes32 coverKey) external view

Arguments

Name Type Description
s IStore
coverKey bytes32
Source Code
function mustHaveDispute(IStore s, bytes32 coverKey) external view {
    bool hasDispute = s.getBoolByKey(GovernanceUtilV1.getHasDisputeKeyInternal(coverKey));
    require(hasDispute == true, "Not disputed");
  }

mustNotHaveDispute

function mustNotHaveDispute(IStore s, bytes32 coverKey) external view

Arguments

Name Type Description
s IStore
coverKey bytes32
Source Code
function mustNotHaveDispute(IStore s, bytes32 coverKey) external view {
    bool hasDispute = s.getBoolByKey(GovernanceUtilV1.getHasDisputeKeyInternal(coverKey));
    require(hasDispute == false, "Already disputed");
  }

mustBeDuringReportingPeriod

function mustBeDuringReportingPeriod(IStore s, bytes32 coverKey) external view

Arguments

Name Type Description
s IStore
coverKey bytes32
Source Code
function mustBeDuringReportingPeriod(IStore s, bytes32 coverKey) external view {
    require(s.getResolutionTimestampInternal(coverKey) >= block.timestamp, "Reporting window closed"); // solhint-disable-line
  }

mustBeAfterReportingPeriod

function mustBeAfterReportingPeriod(IStore s, bytes32 coverKey) public view

Arguments

Name Type Description
s IStore
coverKey bytes32
Source Code
function mustBeAfterReportingPeriod(IStore s, bytes32 coverKey) public view {
    require(block.timestamp > s.getResolutionTimestampInternal(coverKey), "Reporting still active"); // solhint-disable-line
  }

mustBeValidCxToken

function mustBeValidCxToken(IStore s, bytes32 coverKey, address cxToken, uint256 incidentDate) public view

Arguments

Name Type Description
s IStore
coverKey bytes32
cxToken address
incidentDate uint256
Source Code
function mustBeValidCxToken(
    IStore s,
    bytes32 coverKey,
    address cxToken,
    uint256 incidentDate
  ) public view {
    require(s.getBoolByKeys(ProtoUtilV1.NS_COVER_CXTOKEN, cxToken) == true, "Unknown cxToken");

    bytes32 COVER_KEY = ICxToken(cxToken).COVER_KEY(); // solhint-disable-line
    require(coverKey == COVER_KEY, "Invalid cxToken");

    uint256 expires = ICxToken(cxToken).expiresOn();
    require(expires > incidentDate, "Invalid or expired cxToken");
  }

mustBeValidClaim

function mustBeValidClaim(IStore s, address account, bytes32 coverKey, address cxToken, uint256 incidentDate, uint256 amount) external view

Arguments

Name Type Description
s IStore
account address
coverKey bytes32
cxToken address
incidentDate uint256
amount uint256
Source Code
function mustBeValidClaim(
    IStore s,
    address account,
    bytes32 coverKey,
    address cxToken,
    uint256 incidentDate,
    uint256 amount
  ) external view {
    // @note: cxTokens are no longer protocol members
    // as we will end up with way too many contracts
    // s.mustBeProtocolMember(cxToken);
    mustBeValidCxToken(s, coverKey, cxToken, incidentDate);
    mustBeClaimable(s, coverKey);
    mustBeValidIncidentDate(s, coverKey, incidentDate);
    mustBeDuringClaimPeriod(s, coverKey);

    require(ICxToken(cxToken).getClaimablePolicyOf(account) >= amount, "Claim exceeds your coverage");
  }

mustNotHaveUnstaken

function mustNotHaveUnstaken(IStore s, address account, bytes32 coverKey, uint256 incidentDate) public view

Arguments

Name Type Description
s IStore
account address
coverKey bytes32
incidentDate uint256
Source Code
function mustNotHaveUnstaken(
    IStore s,
    address account,
    bytes32 coverKey,
    uint256 incidentDate
  ) public view {
    uint256 withdrawal = s.getReportingUnstakenAmountInternal(account, coverKey, incidentDate);
    require(withdrawal == 0, "Already unstaken");
  }

validateUnstakeWithoutClaim

function validateUnstakeWithoutClaim(IStore s, bytes32 coverKey, uint256 incidentDate) external view

Arguments

Name Type Description
s IStore
coverKey bytes32
incidentDate uint256
Source Code
function validateUnstakeWithoutClaim(
    IStore s,
    bytes32 coverKey,
    uint256 incidentDate
  ) external view {
    mustNotBePaused(s);
    mustNotHaveUnstaken(s, msg.sender, coverKey, incidentDate);
    mustBeAfterReportingPeriod(s, coverKey);

    // Before the deadline, emergency resolution can still happen
    // that may have an impact on the final decision. We, therefore, have to wait.
    mustBeAfterResolutionDeadline(s, coverKey);

    // @note: when this reporting gets finalized, the emergency resolution deadline resets to 0
    // The above code is not useful after finalization but it helps avoid
    // people calling unstake before a decision is obtained
  }

validateUnstakeWithClaim

function validateUnstakeWithClaim(IStore s, bytes32 coverKey, uint256 incidentDate) external view

Arguments

Name Type Description
s IStore
coverKey bytes32
incidentDate uint256
Source Code
function validateUnstakeWithClaim(
    IStore s,
    bytes32 coverKey,
    uint256 incidentDate
  ) external view {
    mustNotBePaused(s);
    mustNotHaveUnstaken(s, msg.sender, coverKey, incidentDate);
    mustBeAfterReportingPeriod(s, coverKey);

    // If this reporting gets finalized, incident date will become invalid
    // meaning this execution will revert thereby restricting late comers
    // to access this feature. But they can still access `unstake` feature
    // to withdraw their stake.
    mustBeValidIncidentDate(s, coverKey, incidentDate);

    // Before the deadline, emergency resolution can still happen
    // that may have an impact on the final decision. We, therefore, have to wait.
    mustBeAfterResolutionDeadline(s, coverKey);

    bool incidentHappened = s.getCoverStatus(coverKey) == CoverUtilV1.CoverStatus.Claimable;

    if (incidentHappened) {
      // Incident occurred. Must unstake with claim during the claim period.
      mustBeDuringClaimPeriod(s, coverKey);
      return;
    }
  }

mustBeDuringClaimPeriod

function mustBeDuringClaimPeriod(IStore s, bytes32 coverKey) public view

Arguments

Name Type Description
s IStore
coverKey bytes32
Source Code
function mustBeDuringClaimPeriod(IStore s, bytes32 coverKey) public view {
    uint256 beginsFrom = s.getUintByKeys(ProtoUtilV1.NS_CLAIM_BEGIN_TS, coverKey);
    uint256 expiresAt = s.getUintByKeys(ProtoUtilV1.NS_CLAIM_EXPIRY_TS, coverKey);

    require(beginsFrom > 0, "Invalid claim begin date");
    require(expiresAt > beginsFrom, "Invalid claim period");

    require(block.timestamp >= beginsFrom, "Claim period hasn't begun"); // solhint-disable-line
    require(block.timestamp <= expiresAt, "Claim period has expired"); // solhint-disable-line
  }

mustBeAfterClaimExpiry

function mustBeAfterClaimExpiry(IStore s, bytes32 coverKey) external view

Arguments

Name Type Description
s IStore
coverKey bytes32
Source Code
function mustBeAfterClaimExpiry(IStore s, bytes32 coverKey) external view {
    require(block.timestamp > s.getUintByKeys(ProtoUtilV1.NS_CLAIM_EXPIRY_TS, coverKey), "Claim still active"); // solhint-disable-line
  }

senderMustBeWhitelistedCoverCreator

Reverts if the sender is not whitelisted cover creator.

function senderMustBeWhitelistedCoverCreator(IStore s) external view

Arguments

Name Type Description
s IStore
Source Code
function senderMustBeWhitelistedCoverCreator(IStore s) external view {
    require(s.getAddressBooleanByKey(ProtoUtilV1.NS_COVER_CREATOR_WHITELIST, msg.sender), "Not whitelisted");
  }

senderMustBeWhitelistedIfRequired

function senderMustBeWhitelistedIfRequired(IStore s, bytes32 coverKey, address sender) external view

Arguments

Name Type Description
s IStore
coverKey bytes32
sender address
Source Code
function senderMustBeWhitelistedIfRequired(
    IStore s,
    bytes32 coverKey,
    address sender
  ) external view {
    bool required = s.checkIfRequiresWhitelist(coverKey);

    if (required == false) {
      return;
    }

    require(s.getAddressBooleanByKeys(ProtoUtilV1.NS_COVER_USER_WHITELIST, coverKey, sender), "You are not whitelisted");
  }

Contracts