Skip to content

Latest commit

 

History

History
448 lines (380 loc) · 14 KB

cxToken.md

File metadata and controls

448 lines (380 loc) · 14 KB

cxToken (cxToken.sol)

View Source: contracts/core/cxToken/cxToken.sol

↗ Extends: ICxToken, Recoverable, ERC20

cxToken

cxTokens are minted when someone purchases a cover.

The cxTokens can be exchanged for a USD stablecoin at a 1:1 exchange rate after a cover incident is successfully resolved (minus platform fees).

Restrictions:

  • cxTokens cannot be transferred from one person to another.
  • Only claims can be submitted with cxTokens
  • There is a lag period before your cxTokens starts its coverage. cxTokens start coverage the next day (or longer) at the UTC EOD timestamp and remain valid until the expiration date.
  • The lag configuration can be found in ProtoUtilV1.NS_COVERAGE_LAG and PolicyAdmin.getCoverageLag function.

Contract Members

Constants & Variables

bytes32 public COVER_KEY;
bytes32 public PRODUCT_KEY;
uint256 public createdOn;
uint256 public expiresOn;
mapping(address => mapping(uint256 => uint256)) public coverageStartsFrom;

Functions

Constructs this contract.

function (IStore store, bytes32 coverKey, bytes32 productKey, string tokenName, uint256 expiry) public nonpayable ERC20 Recoverable 

Arguments

Name Type Description
store IStore Provide the store contract instance
coverKey bytes32 Enter the cover key
productKey bytes32 Enter the product key
tokenName string Enter token name for this ERC-20 contract. The token symbol will be cxUSD.
expiry uint256 Provide the cover expiry timestamp
Source Code
constructor(
    IStore store,
    bytes32 coverKey,
    bytes32 productKey,
    string memory tokenName,
    uint256 expiry
  ) ERC20(tokenName, "cxUSD") Recoverable(store) {
    COVER_KEY = coverKey;
    PRODUCT_KEY = productKey;
    expiresOn = expiry;
  }

getCoverageStartsFrom

Returns the value of the coverageStartsFrom mapping. Warning: this function does not validate the input arguments.

function getCoverageStartsFrom(address account, uint256 date) external view
returns(uint256)

Arguments

Name Type Description
account address Enter an account to get the coverageStartsFrom value.
date uint256 Enter a date. Ensure that you supply a UTC EOD value.
Source Code
function getCoverageStartsFrom(address account, uint256 date) external view override returns (uint256) {
    return coverageStartsFrom[account][date];
  }

_getExcludedCoverageOf

Gets sum of the lagged and, therefore, excluded policy of a given account.

Only policies purchased within 24-48 hours (or longer depending on this cover's configuration) are valid. Given the present codebase, the loop that follows may appear pointless and invalid.

Since the protocol is upgradable but not cxTokens, erroneous code could be introduced in the future, which is why we go all the way until the resolution deadline.

function _getExcludedCoverageOf(address account) private view
returns(exclusion uint256)

Arguments

Name Type Description
account address Enter an account.
Source Code
function _getExcludedCoverageOf(address account) private view returns (uint256 exclusion) {
    uint256 incidentDate = s.getActiveIncidentDateInternal(COVER_KEY, PRODUCT_KEY);
    uint256 resolutionEOD = PolicyHelperV1.getEODInternal(s.getResolutionTimestampInternal(COVER_KEY, PRODUCT_KEY));
    uint256 totalDays = (resolutionEOD - incidentDate) / 1 days;

    for (uint256 i = 0; i < totalDays; i++) {
      uint256 date = PolicyHelperV1.getEODInternal(incidentDate + (i * 1 days));
      exclusion += coverageStartsFrom[account][date];
    }
  }

getClaimablePolicyOf

Gets the claimable policy of an account. Warning: this function does not validate the input arguments.

function getClaimablePolicyOf(address account) external view
returns(uint256)

Arguments

Name Type Description
account address Enter an account.
Source Code
function getClaimablePolicyOf(address account) external view override returns (uint256) {
    uint256 exclusion = _getExcludedCoverageOf(account);
    uint256 balance = super.balanceOf(account);

    if (exclusion > balance) {
      return 0;
    }

    return balance - exclusion;
  }

mint

Mints cxTokens when a policy is purchased. This feature can only be accessed by the latest policy smart contract.

function mint(bytes32 coverKey, bytes32 productKey, address to, uint256 amount) external nonpayable nonReentrant 

Arguments

Name Type Description
coverKey bytes32 Enter the cover key for which the cxTokens are being minted
productKey bytes32
to address Enter the address where the minted token will be sent
amount uint256 Specify the amount of cxTokens to mint
Source Code
function mint(
    bytes32 coverKey,
    bytes32 productKey,
    address to,
    uint256 amount
  ) external override nonReentrant {
    require(amount > 0, "Please specify amount");
    require(coverKey == COVER_KEY, "Invalid cover");
    require(productKey == PRODUCT_KEY, "Invalid product");

    s.mustNotBePaused();
    s.senderMustBePolicyContract();
    s.mustBeSupportedProductOrEmpty(coverKey, productKey);

    uint256 effectiveFrom = PolicyHelperV1.getEODInternal(block.timestamp) + s.getCoverageLagInternal(coverKey); // solhint-disable-line
    coverageStartsFrom[to][effectiveFrom] += amount;

    super._mint(to, amount);
  }

burn

Burns the tokens held by the sender.

function burn(uint256 amount) external nonpayable nonReentrant 

Arguments

Name Type Description
amount uint256 Specify the amount of tokens to burn
Source Code
function burn(uint256 amount) external override nonReentrant {
    require(amount > 0, "Please specify amount");

    s.mustNotBePaused();
    super._burn(msg.sender, amount);
  }

_beforeTokenTransfer

Overrides Openzeppelin ERC-20 contract's _beforeTokenTransfer hook. This is called during transfer, transferFrom, mint, and burn function invocation.

cxToken Restrictions:

  • An expired cxToken can't be transferred.
  • cxTokens can only be transferred to the claims processor contract.
function _beforeTokenTransfer(address from, address to, uint256 ) internal view

Arguments

Name Type Description
from address The account sending the cxTokens
to address The account receiving the cxTokens
uint256 from The account sending the cxTokens
Source Code
function _beforeTokenTransfer(
    address from,
    address to,
    uint256
  ) internal view override {
    // solhint-disable-next-line
    if (block.timestamp > expiresOn) {
      require(to == address(0), "Expired cxToken");
    }

    // cxTokens can only be transferred to the claims processor contract
    if (from != address(0) && to != address(0)) {
      s.mustBeExactContract(ProtoUtilV1.CNS_CLAIM_PROCESSOR, ProtoUtilV1.KEY_INTENTIONALLY_EMPTY, to);
    }
  }

Contracts