View Source: contracts/libraries/PolicyHelperV1.sol
PolicyHelperV1
Constants & Variables
uint256 public constant COVER_LAG_FALLBACK_VALUE;
- calculatePolicyFeeInternal(IStore s, bytes32 coverKey, bytes32 productKey, uint256 coverDuration, uint256 amountToCover)
- getPolicyFeeInternal(IStore s, bytes32 coverKey, bytes32 productKey, uint256 coverDuration, uint256 amountToCover)
- _getCoverPoolAmounts(IStore s, bytes32 coverKey, bytes32 productKey)
- getPolicyRatesInternal(IStore s, bytes32 coverKey)
- getCxTokenInternal(IStore s, bytes32 coverKey, bytes32 productKey, uint256 coverDuration)
- getCxTokenOrDeployInternal(IStore s, bytes32 coverKey, bytes32 productKey, uint256 coverDuration)
- _getMonthName(uint256 date)
- _getCxTokenName(bytes32 coverKey, bytes32 productKey, uint256 expiry)
- purchaseCoverInternal(IStore s, address onBehalfOf, bytes32 coverKey, bytes32 productKey, uint256 coverDuration, uint256 amountToCover)
- getCoverageLagInternal(IStore s, bytes32 coverKey)
function calculatePolicyFeeInternal(IStore s, bytes32 coverKey, bytes32 productKey, uint256 coverDuration, uint256 amountToCover) public view
returns(fee uint256, utilizationRatio uint256, totalAvailableLiquidity uint256, floor uint256, ceiling uint256, rate uint256)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 | |
productKey | bytes32 | |
coverDuration | uint256 | |
amountToCover | uint256 |
Source Code
function calculatePolicyFeeInternal(
IStore s,
bytes32 coverKey,
bytes32 productKey,
uint256 coverDuration,
uint256 amountToCover
)
public
view
returns (
uint256 fee,
uint256 utilizationRatio,
uint256 totalAvailableLiquidity,
uint256 floor,
uint256 ceiling,
uint256 rate
)
{
(floor, ceiling) = getPolicyRatesInternal(s, coverKey);
(uint256 availableLiquidity, uint256 commitment, uint256 reassuranceFund) = _getCoverPoolAmounts(s, coverKey, productKey);
require(amountToCover > 0, "Please enter an amount");
require(coverDuration > 0 && coverDuration <= ProtoUtilV1.MAX_POLICY_DURATION, "Invalid duration");
require(floor > 0 && ceiling > floor, "Policy rate config error");
require(availableLiquidity - commitment > amountToCover, "Insufficient fund");
totalAvailableLiquidity = availableLiquidity + reassuranceFund;
utilizationRatio = (ProtoUtilV1.MULTIPLIER * (commitment + amountToCover)) / totalAvailableLiquidity;
rate = utilizationRatio > floor ? utilizationRatio : floor;
rate = rate + (coverDuration * 100);
if (rate > ceiling) {
rate = ceiling;
}
uint256 expiryDate = CoverUtilV1.getExpiryDateInternal(block.timestamp, coverDuration); // solhint-disable-line
uint256 daysCovered = BokkyPooBahsDateTimeLibrary.diffDays(block.timestamp, expiryDate); // solhint-disable-line
fee = (amountToCover * rate * daysCovered) / (365 * ProtoUtilV1.MULTIPLIER);
}
function getPolicyFeeInternal(IStore s, bytes32 coverKey, bytes32 productKey, uint256 coverDuration, uint256 amountToCover) public view
returns(fee uint256, platformFee uint256)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 | |
productKey | bytes32 | |
coverDuration | uint256 | |
amountToCover | uint256 |
Source Code
function getPolicyFeeInternal(
IStore s,
bytes32 coverKey,
bytes32 productKey,
uint256 coverDuration,
uint256 amountToCover
) public view returns (uint256 fee, uint256 platformFee) {
(fee, , , , , ) = calculatePolicyFeeInternal(s, coverKey, productKey, coverDuration, amountToCover);
uint256 rate = s.getUintByKey(ProtoUtilV1.NS_COVER_PLATFORM_FEE);
platformFee = (fee * rate) / ProtoUtilV1.MULTIPLIER;
}
function _getCoverPoolAmounts(IStore s, bytes32 coverKey, bytes32 productKey) private view
returns(availableLiquidity uint256, commitment uint256, reassuranceFund uint256)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 | |
productKey | bytes32 |
Source Code
function _getCoverPoolAmounts(
IStore s,
bytes32 coverKey,
bytes32 productKey
)
private
view
returns (
uint256 availableLiquidity,
uint256 commitment,
uint256 reassuranceFund
)
{
uint256[] memory values = s.getCoverPoolSummaryInternal(coverKey, productKey);
/*
* values[0] stablecoinOwnedByVault --> The total amount in the cover pool
* values[1] commitment --> The total commitment amount
* values[2] reassurance
* values[3] reassurancePoolWeight
* values[4] count --> Count of products under this cover
* values[5] leverage
* values[6] efficiency --> Cover product efficiency weight
*/
availableLiquidity = values[0];
commitment = values[1];
// (reassurance * reassurancePoolWeight) / multiplier
reassuranceFund = (values[2] * values[3]) / ProtoUtilV1.MULTIPLIER;
if (s.supportsProductsInternal(coverKey)) {
require(values[4] > 0, "Misconfigured or retired product");
// (stablecoinOwnedByVault * leverage * efficiency) / (count * multiplier)
availableLiquidity = (values[0] * values[5] * values[6]) / (values[4] * ProtoUtilV1.MULTIPLIER);
}
}
function getPolicyRatesInternal(IStore s, bytes32 coverKey) public view
returns(floor uint256, ceiling uint256)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 |
Source Code
function getPolicyRatesInternal(IStore s, bytes32 coverKey) public view returns (uint256 floor, uint256 ceiling) {
if (coverKey > 0) {
floor = s.getUintByKeys(ProtoUtilV1.NS_COVER_POLICY_RATE_FLOOR, coverKey);
ceiling = s.getUintByKeys(ProtoUtilV1.NS_COVER_POLICY_RATE_CEILING, coverKey);
}
if (floor == 0) {
// Fallback to default values
floor = s.getUintByKey(ProtoUtilV1.NS_COVER_POLICY_RATE_FLOOR);
ceiling = s.getUintByKey(ProtoUtilV1.NS_COVER_POLICY_RATE_CEILING);
}
}
function getCxTokenInternal(IStore s, bytes32 coverKey, bytes32 productKey, uint256 coverDuration) public view
returns(cxToken address, expiryDate uint256)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 | |
productKey | bytes32 | |
coverDuration | uint256 |
Source Code
function getCxTokenInternal(
IStore s,
bytes32 coverKey,
bytes32 productKey,
uint256 coverDuration
) public view returns (address cxToken, uint256 expiryDate) {
expiryDate = CoverUtilV1.getExpiryDateInternal(block.timestamp, coverDuration); // solhint-disable-line
bytes32 k = keccak256(abi.encodePacked(ProtoUtilV1.NS_COVER_CXTOKEN, coverKey, productKey, expiryDate));
cxToken = s.getAddress(k);
}
Gets the instance of cxToken or deploys a new one based on the cover expiry timestamp
function getCxTokenOrDeployInternal(IStore s, bytes32 coverKey, bytes32 productKey, uint256 coverDuration) public nonpayable
returns(contract ICxToken)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 | Enter the cover key |
productKey | bytes32 | |
coverDuration | uint256 | Enter the number of months to cover. Accepted values: 1-3. |
Source Code
function getCxTokenOrDeployInternal(
IStore s,
bytes32 coverKey,
bytes32 productKey,
uint256 coverDuration
) public returns (ICxToken) {
(address cxToken, uint256 expiryDate) = getCxTokenInternal(s, coverKey, productKey, coverDuration);
if (cxToken != address(0)) {
return ICxToken(cxToken);
}
ICxTokenFactory factory = s.getCxTokenFactory();
cxToken = factory.deploy(coverKey, productKey, _getCxTokenName(coverKey, productKey, expiryDate), expiryDate);
// @warning: Do not uncomment the following line
// Reason: cxTokens are no longer protocol members
// as we will end up with way too many contracts
// s.getProtocol().addMember(cxToken);
return ICxToken(cxToken);
}
Returns month name of a given date
function _getMonthName(uint256 date) private pure
returns(bytes3)
Arguments
Name | Type | Description |
---|---|---|
date | uint256 |
Source Code
function _getMonthName(uint256 date) private pure returns (bytes3) {
bytes3[13] memory m = [bytes3(0), "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
uint256 month = BokkyPooBahsDateTimeLibrary.getMonth(date);
return m[month];
}
Returns cxToken name from the supplied inputs. Format: For basket cover pool product --> cxusd:dex:uni:nov (cxUSD) For standalone cover pool --> cxusd:bal:nov (cxUSD)
function _getCxTokenName(bytes32 coverKey, bytes32 productKey, uint256 expiry) private pure
returns(string)
Arguments
Name | Type | Description |
---|---|---|
coverKey | bytes32 | |
productKey | bytes32 | |
expiry | uint256 |
Source Code
function _getCxTokenName(
bytes32 coverKey,
bytes32 productKey,
uint256 expiry
) private pure returns (string memory) {
bytes3 month = _getMonthName(expiry);
if (productKey > 0) {
return string(abi.encodePacked("cxusd:", string(abi.encodePacked(coverKey)), ":", string(abi.encodePacked(productKey)), ":", string(abi.encodePacked(month))));
}
return string(abi.encodePacked("cxusd:", string(abi.encodePacked(coverKey)), ":", string(abi.encodePacked(month))));
}
Purchase cover for the specified amount.
When you purchase covers, you receive equal amount of cxTokens back.
You need the cxTokens to claim the cover when resolution occurs.
Each unit of cxTokens are fully redeemable at 1:1 ratio to the given
stablecoins (like wxDai, DAI, USDC, or BUSD) based on the chain.
function purchaseCoverInternal(IStore s, address onBehalfOf, bytes32 coverKey, bytes32 productKey, uint256 coverDuration, uint256 amountToCover) external nonpayable
returns(cxToken contract ICxToken, fee uint256, platformFee uint256)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
onBehalfOf | address | Enter the address where the claim tokens (cxTokens) should be sent. |
coverKey | bytes32 | Enter the cover key you wish to purchase the policy for |
productKey | bytes32 | |
coverDuration | uint256 | Enter the number of months to cover. Accepted values: 1-3. |
amountToCover | uint256 | Enter the amount of the stablecoin to cover. |
Source Code
function purchaseCoverInternal(
IStore s,
address onBehalfOf,
bytes32 coverKey,
bytes32 productKey,
uint256 coverDuration,
uint256 amountToCover
)
external
returns (
ICxToken cxToken,
uint256 fee,
uint256 platformFee
)
{
(fee, platformFee) = getPolicyFeeInternal(s, coverKey, productKey, coverDuration, amountToCover);
require(fee > 0, "Insufficient fee");
require(platformFee > 0, "Insufficient platform fee");
address stablecoin = s.getStablecoin();
require(stablecoin != address(0), "Cover liquidity uninitialized");
IERC20(stablecoin).ensureTransferFrom(msg.sender, address(this), fee);
IERC20(stablecoin).ensureTransfer(s.getVaultAddress(coverKey), fee - platformFee);
IERC20(stablecoin).ensureTransfer(s.getTreasury(), platformFee);
uint256 stablecoinPrecision = s.getStablecoinPrecision();
uint256 toMint = (amountToCover * ProtoUtilV1.CXTOKEN_PRECISION) / stablecoinPrecision;
cxToken = getCxTokenOrDeployInternal(s, coverKey, productKey, coverDuration);
cxToken.mint(coverKey, productKey, onBehalfOf, toMint);
s.updateStateAndLiquidity(coverKey);
}
function getCoverageLagInternal(IStore s, bytes32 coverKey) external view
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
coverKey | bytes32 |
Source Code
function getCoverageLagInternal(IStore s, bytes32 coverKey) external view returns (uint256) {
uint256 custom = s.getUintByKeys(ProtoUtilV1.NS_COVERAGE_LAG, coverKey);
// Custom means set for this exact cover
if (custom > 0) {
return custom;
}
// Global means set for all covers (without specifying a cover key)
uint256 global = s.getUintByKey(ProtoUtilV1.NS_COVERAGE_LAG);
if (global > 0) {
return global;
}
// Fallback means the default option
return COVER_LAG_FALLBACK_VALUE;
}
- AaveStrategy
- AccessControl
- AccessControlLibV1
- Address
- BaseLibV1
- BokkyPooBahsDateTimeLibrary
- BondPool
- BondPoolBase
- BondPoolLibV1
- CompoundStrategy
- Context
- Cover
- CoverBase
- CoverLibV1
- CoverReassurance
- CoverStake
- CoverUtilV1
- cxToken
- cxTokenFactory
- cxTokenFactoryLibV1
- Delayable
- Destroyable
- ERC165
- ERC20
- FakeAaveLendingPool
- FakeCompoundDaiDelegator
- FakePriceOracle
- FakeRecoverable
- FakeStore
- FakeToken
- FakeUniswapPair
- FakeUniswapV2FactoryLike
- FakeUniswapV2PairLike
- FakeUniswapV2RouterLike
- FaultyAaveLendingPool
- FaultyCompoundDaiDelegator
- Finalization
- ForceEther
- Governance
- GovernanceUtilV1
- IAaveV2LendingPoolLike
- IAccessControl
- IBondPool
- IClaimsProcessor
- ICompoundERC20DelegatorLike
- ICover
- ICoverReassurance
- ICoverStake
- ICxToken
- ICxTokenFactory
- IERC165
- IERC20
- IERC20Detailed
- IERC20Metadata
- IERC3156FlashBorrower
- IERC3156FlashLender
- IFinalization
- IGovernance
- ILendingStrategy
- ILiquidityEngine
- IMember
- InvalidStrategy
- IPausable
- IPolicy
- IPolicyAdmin
- IPriceOracle
- IProtocol
- IRecoverable
- IReporter
- IResolution
- IResolvable
- IStakingPools
- IStore
- IStoreLike
- IUniswapV2FactoryLike
- IUniswapV2PairLike
- IUniswapV2RouterLike
- IUnstakable
- IVault
- IVaultDelegate
- IVaultFactory
- IWitness
- LiquidityEngine
- MaliciousToken
- MockAccessControlUser
- MockCoverUtilUser
- MockCxToken
- MockCxTokenPolicy
- MockCxTokenStore
- MockFlashBorrower
- MockLiquidityEngineUser
- MockProcessorStore
- MockProcessorStoreLib
- MockProtocol
- MockRegistryClient
- MockStore
- MockStoreKeyUtilUser
- MockValidationLibUser
- MockVault
- MockVaultLibUser
- NPM
- NpmDistributor
- NTransferUtilV2
- NTransferUtilV2Intermediate
- Ownable
- Pausable
- Policy
- PolicyAdmin
- PolicyHelperV1
- PoorMansERC20
- POT
- PriceLibV1
- Processor
- ProtoBase
- Protocol
- ProtoUtilV1
- Recoverable
- ReentrancyGuard
- RegistryLibV1
- Reporter
- Resolution
- Resolvable
- RoutineInvokerLibV1
- SafeERC20
- StakingPoolBase
- StakingPoolCoreLibV1
- StakingPoolInfo
- StakingPoolLibV1
- StakingPoolReward
- StakingPools
- Store
- StoreBase
- StoreKeyUtil
- StrategyLibV1
- Strings
- TimelockController
- Unstakable
- ValidationLibV1
- Vault
- VaultBase
- VaultDelegate
- VaultDelegateBase
- VaultDelegateWithFlashLoan
- VaultFactory
- VaultFactoryLibV1
- VaultLibV1
- VaultLiquidity
- VaultStrategy
- WithFlashLoan
- WithPausability
- WithRecovery
- Witness