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

Underlying test #63

Merged
merged 4 commits into from
Oct 20, 2022
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
10 changes: 5 additions & 5 deletions src/OptionSettlement.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you didn't use the private accessor here?

revert TokenNotFound(tokenId);
}

Expand All @@ -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)
});
}
}
Expand Down Expand Up @@ -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);
}
}
47 changes: 33 additions & 14 deletions src/test/OptionSettlement.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}

// **********************************************************************
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
}
}
}