Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double release funds #89

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/diamonds/nayms/interfaces/CustomErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,8 @@ error CancelCannotBeTrueWhenCreatingSimplePolicy();
/// @dev (non specific) The policyId must exist.
error PolicyDoesNotExist(bytes32 policyId);

/// @dev It is not possible to cancel policyId after maturation date has passed
error PolicyCannotCancelAfterMaturation(bytes32 policyId);

/// @dev There is a duplicate address in the list of signers (the previous signer in the list is not < the next signer in the list).
error DuplicateSignerCreatingSimplePolicy(address previousSigner, address nextSigner);
6 changes: 5 additions & 1 deletion src/diamonds/nayms/libs/LibSimplePolicy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { LibFeeRouter } from "./LibFeeRouter.sol";
import { LibHelpers } from "./LibHelpers.sol";
import { LibEIP712 } from "src/diamonds/nayms/libs/LibEIP712.sol";

import { EntityDoesNotExist, PolicyDoesNotExist } from "src/diamonds/nayms/interfaces/CustomErrors.sol";
import { EntityDoesNotExist, PolicyDoesNotExist, PolicyCannotCancelAfterMaturation } from "src/diamonds/nayms/interfaces/CustomErrors.sol";

library LibSimplePolicy {
event SimplePolicyMatured(bytes32 indexed id);
Expand Down Expand Up @@ -99,6 +99,10 @@ library LibSimplePolicy {
SimplePolicy storage simplePolicy = s.simplePolicies[_policyId];
require(!simplePolicy.cancelled, "Policy already cancelled");

if (block.timestamp >= simplePolicy.maturationDate) {
revert PolicyCannotCancelAfterMaturation(_policyId);
}

releaseFunds(_policyId);
simplePolicy.cancelled = true;

Expand Down
37 changes: 37 additions & 0 deletions test/T04Entity.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Vm } from "forge-std/Vm.sol";
import { c, D03ProtocolDefaults, LibHelpers, LC } from "./defaults/D03ProtocolDefaults.sol";
import { Entity, MarketInfo, SimplePolicy, SimplePolicyInfo, Stakeholders } from "src/diamonds/nayms/interfaces/FreeStructs.sol";
import { IDiamondCut } from "src/diamonds/nayms/INayms.sol";
import { StdStyle } from "forge-std/StdStyle.sol";

import { SimplePolicyFixture } from "test/fixtures/SimplePolicyFixture.sol";

Expand All @@ -15,6 +16,7 @@ import "src/diamonds/nayms/interfaces/CustomErrors.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 @@ -1049,4 +1051,39 @@ contract T04EntityTest is D03ProtocolDefaults {
vm.expectRevert("utilized capacity starts at 0");
nayms.createEntity(entityId1, account0Id, e, "entity test hash");
}

function testSimplePolicyDoubleUnlockFunds() public {
getReadyToCreatePolicies();

vm.stopPrank();
vm.startPrank(sa.addr);
nayms.assignRole(em.id, systemContext, LC.ROLE_ENTITY_MANAGER);
vm.stopPrank();

vm.startPrank(em.addr);
nayms.assignRole(entityId1, entityId1, LC.ROLE_ENTITY_COMPTROLLER_COMBINED); // </3
vm.stopPrank();

uint256 limitAmount = 2000;

vm.startPrank(su.addr);
(stakeholders, simplePolicy) = initPolicyWithLimit(testPolicyDataHash, limitAmount);
nayms.createSimplePolicy(policyId1, entityId1, stakeholders, simplePolicy, testPolicyDataHash);

uint256 lockedAmount = nayms.getLockedBalance(entityId1, wethId);

bytes32 policyId2 = makeId(LC.OBJECT_TYPE_POLICY, address(bytes20("0xC0FFEF")));
(stakeholders, simplePolicy) = initPolicyWithLimit(testPolicyDataHash, limitAmount);
nayms.createSimplePolicy(policyId2, entityId1, stakeholders, simplePolicy, testPolicyDataHash);

lockedAmount = nayms.getLockedBalance(entityId1, wethId);

vm.warp(block.timestamp + 3 days);
nayms.checkAndUpdateSimplePolicyState(policyId1);

lockedAmount = nayms.getLockedBalance(entityId1, wethId);

vm.expectRevert(abi.encodeWithSelector(PolicyCannotCancelAfterMaturation.selector, policyId1));
nayms.cancelSimplePolicy(policyId1);
}
}