Skip to content

Commit

Permalink
Fix risk of insufficient liquidity
Browse files Browse the repository at this point in the history
  • Loading branch information
flashburst committed Aug 29, 2022
1 parent a98fcce commit 63fce22
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
4 changes: 1 addition & 3 deletions contracts/libraries/CoverUtilV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,7 @@ library CoverUtilV1 {
bytes32 productKey,
uint256 excludedExpiryDate
) private view returns (uint256 sum) {
uint256 maxMonthsToProtect = 3;

for (uint256 i = 0; i < maxMonthsToProtect; i++) {
for (uint256 i = 0; i <= ProtoUtilV1.MAX_POLICY_DURATION; i++) {
uint256 expiryDate = _getNextMonthEndDate(block.timestamp, i); // solhint-disable-line

if (expiryDate == excludedExpiryDate || expiryDate <= block.timestamp) {
Expand Down
26 changes: 25 additions & 1 deletion test/specs/libraries/cover-util.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-unused-expressions */
const { ethers } = require('hardhat')
const { ethers, network } = require('hardhat')
const BigNumber = require('bignumber.js')
const { deployer, key, helper } = require('../../../util')
const composer = require('../../../util/composer')
Expand Down Expand Up @@ -116,4 +116,28 @@ describe('CoverUtilV1: getActiveLiquidityUnderProtection', () => {
const result = await mockContract.getActiveLiquidityUnderProtection(coverKey, helper.emptyBytes32)
result.should.equal(coverageAmount)
})

it.only('must return correct active protection when active incident is greater than zero and policies purchased', async () => {
await network.provider.send('evm_increaseTime', [100 * DAYS]) // pass time so that previous policies expire
const [owner] = await ethers.getSigners()
let totalCoverageAmount = helper.ether(0, PRECISION)

// Purchase policy so that cxToken is created
await deployed.dai.approve(deployed.policy.address, ethers.constants.MaxUint256)

while (true) {
const coverageAmount = helper.ether(10_000, PRECISION)
await deployed.policy.purchaseCover(owner.address, coverKey, helper.emptyBytes32, '3', helper.ether(10_000, PRECISION), key.toBytes32(''))
totalCoverageAmount = helper.add(totalCoverageAmount, coverageAmount)
await network.provider.send('evm_increaseTime', [1 * DAYS])

const block = await ethers.provider.getBlock(await ethers.provider.getBlockNumber())
if (new Date(block.timestamp * 1000).getUTCDate() === 27) {
break
}
}

const result = await mockContract.getActiveLiquidityUnderProtection(coverKey, helper.emptyBytes32)
result.should.equal(totalCoverageAmount)
})
})

0 comments on commit 63fce22

Please sign in to comment.