diff --git a/src/OptionSettlement.sol b/src/OptionSettlement.sol index ef26d60..4ad1a8a 100644 --- a/src/OptionSettlement.sol +++ b/src/OptionSettlement.sol @@ -136,7 +136,7 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine { optionId = uint256(optionKey) << 96; // If it does, revert - if (_isOptionInitialized(optionKey)) { + if (isOptionInitialized(optionKey)) { revert OptionsTypeExists(optionId); } @@ -411,7 +411,7 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine { function underlying(uint256 tokenId) external view returns (Underlying memory underlyingPositions) { (uint160 optionId, uint96 claimIdx) = getDecodedIdComponents(tokenId); - if (!_isOptionInitialized(optionId)) { + if (!isOptionInitialized(optionId)) { revert TokenNotFound(tokenId); } @@ -434,9 +434,9 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine { (optionRecord.underlyingAmount * (claimRecord.amountWritten - claimRecord.amountExercised)); underlyingPositions = Underlying({ underlyingAsset: optionRecord.underlyingAsset, - underlyingPosition: int256(exerciseAmount), + underlyingPosition: int256(underlyingAmount), exerciseAsset: optionRecord.exerciseAsset, - exercisePosition: int256(underlyingAmount) + exercisePosition: int256(exerciseAmount) }); } } @@ -473,7 +473,7 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine { claimId |= uint256(claimIndex); } - function _isOptionInitialized(uint160 optionId) public view returns (bool) { + function isOptionInitialized(uint160 optionId) public view returns (bool) { return _option[optionId].underlyingAsset != address(0x0); } } diff --git a/src/test/OptionSettlement.t.sol b/src/test/OptionSettlement.t.sol index 4549f22..b6e091f 100644 --- a/src/test/OptionSettlement.t.sol +++ b/src/test/OptionSettlement.t.sol @@ -262,9 +262,9 @@ contract OptionSettlementTest is Test, NFTreceiver { IOptionSettlementEngine.Underlying memory underlyingPositions = engine.underlying(claimId); assertEq(underlyingPositions.underlyingAsset, WETH_A); - assertEq(underlyingPositions.underlyingPosition, 0); + _assertPosition(underlyingPositions.underlyingPosition, 7 * testUnderlyingAmount); assertEq(underlyingPositions.exerciseAsset, DAI_A); - assertEq(underlyingPositions.exercisePosition, 49000000000000000000); + assertEq(underlyingPositions.exercisePosition, 0); } function testGetEncodedIdComponents() public { @@ -276,11 +276,20 @@ contract OptionSettlementTest is Test, NFTreceiver { } function testUnderlyingAfterExercise() public { - uint256 claimId = _writeAndExerciseOption(testOptionId, ALICE, BOB, 2, 1); - + uint256 claimId = _writeAndExerciseOption(testOptionId, ALICE, BOB, 2, 0); IOptionSettlementEngine.Underlying memory underlyingPositions = engine.underlying(claimId); - emit log_named_int("underlyingPosition", underlyingPositions.underlyingPosition); - emit log_named_int("exercisePosition", underlyingPositions.exercisePosition); + _assertPosition(underlyingPositions.underlyingPosition, 2 * testUnderlyingAmount); + assertEq(underlyingPositions.exercisePosition, 0); + + _writeAndExerciseOption(testOptionId, ALICE, BOB, 0, 1); + underlyingPositions = engine.underlying(claimId); + _assertPosition(underlyingPositions.underlyingPosition, testUnderlyingAmount); + _assertPosition(underlyingPositions.exercisePosition, testExerciseAmount); + + _writeAndExerciseOption(testOptionId, ALICE, BOB, 0, 1); + underlyingPositions = engine.underlying(claimId); + _assertPosition(underlyingPositions.underlyingPosition, 0); + _assertPosition(underlyingPositions.exercisePosition, 2 * testExerciseAmount); } // ********************************************************************** @@ -639,6 +648,11 @@ contract OptionSettlementTest is Test, NFTreceiver { } } + function _assertPosition(int256 actual, uint96 expected) internal { + uint96 _actual = uint96(int96(actual)); + assertEq(_actual, expected); + } + function _writeAndExerciseNewOption( address underlyingAsset, uint40 exerciseTimestamp, @@ -698,13 +712,18 @@ contract OptionSettlementTest is Test, NFTreceiver { uint112 toWrite, uint112 toExercise ) internal returns (uint256 claimId) { - vm.startPrank(writer); - claimId = engine.write(optionId, toWrite); - engine.safeTransferFrom(writer, exerciser, optionId, toWrite, ""); - vm.stopPrank(); - vm.warp(testExerciseTimestamp + 1); - vm.startPrank(exerciser); - engine.exercise(optionId, toExercise); - vm.stopPrank(); + if (toWrite > 0) { + vm.startPrank(writer); + claimId = engine.write(optionId, toWrite); + engine.safeTransferFrom(writer, exerciser, optionId, toWrite, ""); + vm.stopPrank(); + } + + if (toExercise > 0) { + vm.warp(testExerciseTimestamp + 1); + vm.startPrank(exerciser); + engine.exercise(optionId, toExercise); + vm.stopPrank(); + } } }