Skip to content

Commit

Permalink
fix: pay claim don't update utilization post maturation, non-locked f…
Browse files Browse the repository at this point in the history
…unds only
  • Loading branch information
amarinkovic committed Oct 26, 2023
1 parent 46af6fa commit 06af514
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/diamonds/nayms/libs/LibSimplePolicy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,14 @@ library LibSimplePolicy {

bytes32 entityId = LibObject._getParent(_policyId);
Entity memory entity = s.entities[entityId];
s.lockedBalances[entityId][entity.assetId] -= (_amount * entity.collateralRatio) / LC.BP_FACTOR;

s.entities[entityId].utilizedCapacity -= (_amount * entity.collateralRatio) / LC.BP_FACTOR;
if (simplePolicy.fundsLocked) {
s.lockedBalances[entityId][entity.assetId] -= (_amount * entity.collateralRatio) / LC.BP_FACTOR;
s.entities[entityId].utilizedCapacity -= (_amount * entity.collateralRatio) / LC.BP_FACTOR;
} else {
uint256 availableBalance = LibTokenizedVault._internalBalanceOf(entityId, simplePolicy.asset) - LibTokenizedVault._getLockedBalance(entityId, simplePolicy.asset);
require(availableBalance > _amount, "not enough balance");
}

LibObject._createObject(_claimId, LC.OBJECT_TYPE_CLAIM);

Expand Down
18 changes: 16 additions & 2 deletions test/T04Entity.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import { SimplePolicyFixture } from "test/fixtures/SimplePolicyFixture.sol";
// solhint-disable no-global-import
import "src/diamonds/nayms/interfaces/CustomErrors.sol";

import { StdStyle } from "forge-std/StdStyle.sol";

// solhint-disable no-console
contract T04EntityTest is D03ProtocolDefaults {
using LibHelpers for *;
using StdStyle for *;

bytes32 internal entityId1 = makeId(LC.OBJECT_TYPE_ENTITY, address(bytes20(bytes32(0xe10d947335abff84f4d0ebc75f32f3a549614348ab29e220c4b20b0acbd1fa38))));
bytes32 internal policyId1 = makeId(LC.OBJECT_TYPE_POLICY, address(bytes20(bytes32(0x1ea6c707069e49cdc3a4ad357dbe9f52e3a3679636e37698a9ca254b9cb33869))));
Expand Down Expand Up @@ -1082,7 +1085,7 @@ contract T04EntityTest is D03ProtocolDefaults {
nayms.assignRole(entityId1, entityId1, LC.ROLE_ENTITY_COMPTROLLER_COMBINED); // </3
vm.stopPrank();

uint256 limitAmount = 2000;
uint256 limitAmount = 20000;

vm.startPrank(su.addr);
(stakeholders, simplePolicy) = initPolicyWithLimit(testPolicyDataHash, limitAmount);
Expand All @@ -1098,6 +1101,17 @@ contract T04EntityTest is D03ProtocolDefaults {
bytes32 claimId = makeId(LC.OBJECT_TYPE_CLAIM, address(bytes20("claimId")));

vm.startPrank(account0);
nayms.paySimpleClaim(claimId, policyId1, DEFAULT_INSURED_PARTY_ENTITY_ID, 2);
nayms.externalWithdrawFromEntity(entityId1, account0, wethAddress, 20000);
c.log("balance of entity: ", nayms.internalBalanceOf(entityId1, wethId).green());

uint256 lockedBalance = nayms.getLockedBalance(entityId1, wethId);
uint256 utilizedCapacity = nayms.getEntityInfo(entityId1).utilizedCapacity;

vm.expectRevert("not enough balance");
nayms.paySimpleClaim(claimId, policyId1, DEFAULT_INSURED_PARTY_ENTITY_ID, 2000);

nayms.paySimpleClaim(claimId, policyId1, DEFAULT_INSURED_PARTY_ENTITY_ID, 500);
assertEq(lockedBalance, nayms.getLockedBalance(entityId1, wethId), "locked balance should not change");
assertEq(utilizedCapacity, nayms.getEntityInfo(entityId1).utilizedCapacity, "utilized capacity should not change");
}
}

0 comments on commit 06af514

Please sign in to comment.