Skip to content

Commit

Permalink
normalize error interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAlcibiades authored Oct 13, 2022
1 parent 43635a4 commit 42d8c11
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
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
45 changes: 33 additions & 12 deletions src/interfaces/IOptionSettlementEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ 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.
* @param expiry Timestamp of expiry
*/
error ExpiryTooSoon(uint256 optionId, uint40 expiry);

/// @notice The option exercise window is less than 24 hours long.
error ExerciseWindowTooShort();
Expand Down Expand Up @@ -58,20 +62,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

0 comments on commit 42d8c11

Please sign in to comment.