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

normalize error interfaces #64

Merged
merged 5 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 6 additions & 6 deletions src/OptionSettlement.sol
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine {

// Make sure that expiry is at least 24 hours from now
if (optionInfo.expiryTimestamp < (block.timestamp + 86400)) {
revert ExpiryTooSoon();
revert ExpiryTooSoon(optionId, optionInfo.expiryTimestamp);
}

// Ensure the exercise window is at least 24 hours
Expand Down Expand Up @@ -247,7 +247,7 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine {
function assignExercise(uint160 optionId, uint96 claimsLen, uint112 amount, uint160 settlementSeed) internal {
// Number of claims enqueued for this option
if (claimsLen == 0) {
revert NoClaims();
revert NoClaims(optionId);
}

// Initial storage pointer
Expand Down Expand Up @@ -329,7 +329,7 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine {
}
// Require that we have reached the exercise timestamp
if (optionRecord.exerciseTimestamp >= block.timestamp) {
revert ExerciseTooEarly();
revert ExerciseTooEarly(optionId, optionRecord.exerciseTimestamp);
}

uint256 rxAmount = optionRecord.exerciseAmount * amount;
Expand Down Expand Up @@ -364,19 +364,19 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine {
uint256 balance = this.balanceOf(msg.sender, claimId);

if (balance != 1) {
revert BalanceTooLow();
revert RedeemerDoesNotOwnClaimId(claimId);
}

Claim storage claimRecord = _claim[claimId];

if (claimRecord.claimed) {
revert AlreadyClaimed();
revert AlreadyClaimed(claimId);
}

Option storage optionRecord = _option[optionId];

if (optionRecord.expiryTimestamp > block.timestamp) {
revert ClaimTooSoon();
revert ClaimTooSoon(claimId, optionRecord.expiryTimestamp);
}

uint256 exerciseAmount = optionRecord.exerciseAmount * claimRecord.amountExercised;
Expand Down
44 changes: 32 additions & 12 deletions src/interfaces/IOptionSettlementEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ interface IOptionSettlementEngine {
*/
error OptionsTypeExists(uint256 optionId);

/// @notice The expiry timestamp is less than 24 hours from now.
error ExpiryTooSoon();
/**
* @notice The expiry timestamp is less than 24 hours from now.
* @param optionId Supplied option ID.
0xAlcibiades marked this conversation as resolved.
Show resolved Hide resolved
*/
error ExpiryTooSoon(uint256 optionId, uint40 expiry);

/// @notice The option exercise window is less than 24 hours long.
error ExerciseWindowTooShort();
0xAlcibiades marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -58,20 +61,37 @@ interface IOptionSettlementEngine {
*/
error ExpiredOption(uint256 optionId, uint40 expiry);

/// @notice This option cannot yet be exercised.
error ExerciseTooEarly();
/**
* @notice This option cannot yet be exercised.
* @param optionId Supplied option ID.
* @param exercise The time when the optionId can be exercised.
*/
error ExerciseTooEarly(uint256 optionId, uint40 exercise);

/// @notice This option has no claims written against it.
error NoClaims();
/**
* @notice This option has no claims written against it.
* @param optionId Supplied option ID.
*/
error NoClaims(uint256 optionId);

/// @notice This account has no claims.
error BalanceTooLow();
/**
* @notice This account has no claims.
* @param claimId Supplied claim ID.
*/
error RedeemerDoesNotOwnClaimId(uint256 claimId);

/// @notice This claimId has already been claimed.
error AlreadyClaimed();
/**
* @notice This claimId has already been claimed.
* @param claimId Supplied claim ID.
*/
error AlreadyClaimed(uint256 claimId);

/// @notice You can't claim before expiry.
error ClaimTooSoon();
/**
* @notice You can't claim before expiry.
* @param claimId Supplied claim ID.
* @param expiry timestamp at which the options chain expires
*/
error ClaimTooSoon(uint256 claimId, uint40 expiry);

/// @notice The amount provided to write() must be > 0.
error AmountWrittenCannotBeZero();
Expand Down
10 changes: 6 additions & 4 deletions src/test/OptionSettlement.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -420,18 +420,18 @@ contract OptionSettlementTest is Test, NFTreceiver {
vm.startPrank(ALICE);
uint256 claimId = engine.write(testOptionId, 1);
engine.safeTransferFrom(ALICE, BOB, testOptionId, 1, "");
vm.expectRevert(IOptionSettlementEngine.BalanceTooLow.selector);
vm.expectRevert(IOptionSettlementEngine.RedeemerDoesNotOwnClaimId.selector);
engine.redeem(claimId);
vm.stopPrank();
// Carol feels left out and tries to redeem what she can't
vm.startPrank(CAROL);
vm.expectRevert(IOptionSettlementEngine.BalanceTooLow.selector);
vm.expectRevert(IOptionSettlementEngine.RedeemerDoesNotOwnClaimId.selector);
engine.redeem(claimId);
vm.stopPrank();
// Bob redeems, which should burn, and then be unable to redeem a second time
vm.startPrank(BOB);
engine.redeem(claimId);
vm.expectRevert(IOptionSettlementEngine.BalanceTooLow.selector);
vm.expectRevert(IOptionSettlementEngine.RedeemerDoesNotOwnClaimId.selector);
engine.redeem(claimId);
}

Expand All @@ -450,7 +450,9 @@ contract OptionSettlementTest is Test, NFTreceiver {
vm.startPrank(ALICE);
uint256 claimId = engine.write(testOptionId, 1);
vm.warp(testExerciseTimestamp - 1);
vm.expectRevert(IOptionSettlementEngine.ClaimTooSoon.selector);
vm.expectRevert(
abi.encodeWithSelector(IOptionSettlementEngine.ClaimTooSoon.selector, claimId, testExpiryTimestamp)
);
engine.redeem(claimId);
}

Expand Down