Skip to content

Commit

Permalink
Add processExpiredCover to Cover contract
Browse files Browse the repository at this point in the history
  • Loading branch information
MilGard91 committed Dec 20, 2023
1 parent c6c8a8e commit 12ee137
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions contracts/modules/cover/Cover.sol
Original file line number Diff line number Diff line change
Expand Up @@ -235,39 +235,11 @@ contract Cover is ICover, MasterAwareV2, IStakingPoolBeacon, ReentrancyGuard, Mu
)
);

// Update totalActiveCover
{
ActiveCover memory _activeCover = activeCover[params.coverAsset];

uint currentBucketId = block.timestamp / BUCKET_SIZE;
uint totalActiveCover = _activeCover.totalActiveCoverInAsset;

if (totalActiveCover != 0) {
totalActiveCover -= getExpiredCoverAmount(
params.coverAsset,
_activeCover.lastBucketUpdateId,
currentBucketId
);
}

totalActiveCover -= previousSegmentAmount;
totalActiveCover += coverAmountInCoverAsset;

_activeCover.lastBucketUpdateId = currentBucketId.toUint64();
_activeCover.totalActiveCoverInAsset = totalActiveCover.toUint192();

// update total active cover in storage
activeCover[params.coverAsset] = _activeCover;

// update amount to expire at the end of this cover segment
uint bucketAtExpiry = Math.divCeil(block.timestamp + params.period, BUCKET_SIZE);
activeCoverExpirationBuckets[params.coverAsset][bucketAtExpiry] += coverAmountInCoverAsset;
}

// can pay with cover asset or nxm only
if (params.paymentAsset != params.coverAsset && params.paymentAsset != NXM_ASSET_ID) {
revert InvalidPaymentAsset();
}
_processExpiredCover(params.coverAsset, coverAmountInCoverAsset, params.period, previousSegmentAmount);

retrievePayment(
amountDueInNXM,
Expand Down Expand Up @@ -487,6 +459,38 @@ contract Cover is ICover, MasterAwareV2, IStakingPoolBeacon, ReentrancyGuard, Mu
}
}

function processExpiredCover(uint coverAsset) public {
_processExpiredCover(coverAsset, 0, 0, 0);
}

function _processExpiredCover(uint coverAsset, uint coverAmountInCoverAsset, uint period, uint previousSegmentAmount) internal {
ActiveCover memory _activeCover = activeCover[coverAsset];

uint currentBucketId = block.timestamp / BUCKET_SIZE;
uint totalActiveCover = _activeCover.totalActiveCoverInAsset;

if (totalActiveCover != 0) {
totalActiveCover -= getExpiredCoverAmount(
coverAsset,
_activeCover.lastBucketUpdateId,
currentBucketId
);
}

totalActiveCover -= previousSegmentAmount;
totalActiveCover += coverAmountInCoverAsset;

_activeCover.lastBucketUpdateId = currentBucketId.toUint64();
_activeCover.totalActiveCoverInAsset = totalActiveCover.toUint192();

// update total active cover in storage
activeCover[coverAsset] = _activeCover;

// update amount to expire at the end of this cover segment
uint bucketAtExpiry = Math.divCeil(block.timestamp + period, BUCKET_SIZE);
activeCoverExpirationBuckets[coverAsset][bucketAtExpiry] += coverAmountInCoverAsset;
}

function addLegacyCover(
uint productId,
uint coverAsset,
Expand Down

0 comments on commit 12ee137

Please sign in to comment.