Skip to content

Commit

Permalink
elide unneeded enum and related logic (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
Flip-Liquid authored Dec 20, 2022
1 parent b6db273 commit bc99806
Showing 1 changed file with 6 additions and 25 deletions.
31 changes: 6 additions & 25 deletions src/OptionSettlementEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine {
// Internal Data Structures
//////////////////////////////////////////////////////////////*/

/// @notice Stores the exercise state of a given collateral bucket.
enum BucketExerciseState {
Exercised,
PartiallyExercised,
Unexercised
}

/**
* @notice Stores the state of options written and exercised for a bucket.
* Used in fair exercise assignment assignment to calculate the ratio of
Expand All @@ -68,8 +61,6 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine {
Bucket[] buckets;
/// @custom:member An array of bucket indices with collateral available for exercise.
uint96[] unexercisedBucketIndices;
/// @custom:member A mapping of bucket indices to a boolean indicating if the bucket has any collateral available for exercise.
mapping(uint96 => BucketExerciseState) bucketExerciseStates;
}

/**
Expand Down Expand Up @@ -789,19 +780,18 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine {
uint112 amountAvailable = bucketInfo.amountWritten - bucketInfo.amountExercised;
uint112 amountPresentlyExercised = 0;
if (amountAvailable <= amount) {
// Bucket is fully exercised/assigned
amount -= amountAvailable;
amountPresentlyExercised = amountAvailable;
// Perform "swap and pop" index management.
numUnexercisedBuckets--;
uint96 overwrite = unexercisedBucketIndices[numUnexercisedBuckets];
unexercisedBucketIndices[exerciseIndex] = overwrite;
unexercisedBucketIndices.pop();

optionTypeState.bucketInfo.bucketExerciseStates[bucketIndex] = BucketExerciseState.Exercised;
} else {
// Bucket is partially exercised/assigned
amountPresentlyExercised = amount;
amount = 0;
optionTypeState.bucketInfo.bucketExerciseStates[bucketIndex] = BucketExerciseState.PartiallyExercised;
}
bucketInfo.amountExercised += amountPresentlyExercised;

Expand All @@ -816,10 +806,7 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine {
}

/// @notice Adds or updates a bucket as needed for a given option type and amount written.
function _addOrUpdateBucket(OptionTypeState storage optionTypeState, uint112 amount)
private
returns (uint96 bucketIndex)
{
function _addOrUpdateBucket(OptionTypeState storage optionTypeState, uint112 amount) private returns (uint96) {
// Setup pointers to buckets.
BucketInfo storage bucketInfo = optionTypeState.bucketInfo;
Bucket[] storage buckets = bucketInfo.buckets;
Expand All @@ -828,7 +815,7 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine {
if (buckets.length == 0) {
// Add a new bucket for this option type, because none exist.
buckets.push(Bucket(amount, 0));
_updateUnexercisedBucketIndices(bucketInfo, writtenBucketIndex);
bucketInfo.unexercisedBucketIndices.push(writtenBucketIndex);

return writtenBucketIndex;
}
Expand All @@ -837,10 +824,10 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine {
uint96 currentBucketIndex = writtenBucketIndex - 1;
Bucket storage currentBucket = buckets[currentBucketIndex];

if (bucketInfo.bucketExerciseStates[currentBucketIndex] != BucketExerciseState.Unexercised) {
if (currentBucket.amountExercised != 0) {
// Add a new bucket to this option type, because the last was partially or fully exercised.
buckets.push(Bucket(amount, 0));
_updateUnexercisedBucketIndices(bucketInfo, writtenBucketIndex);
bucketInfo.unexercisedBucketIndices.push(writtenBucketIndex);
} else {
// Write to the existing unexercised bucket.
currentBucket.amountWritten += amount;
Expand All @@ -850,12 +837,6 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine {
return writtenBucketIndex;
}

/// @notice Adds the bucket index to the list of unexercised buckets and sets state to Unexercised.
function _updateUnexercisedBucketIndices(BucketInfo storage bucketInfo, uint96 bucketIndex) private {
bucketInfo.unexercisedBucketIndices.push(bucketIndex);
bucketInfo.bucketExerciseStates[bucketIndex] = BucketExerciseState.Unexercised;
}

/// @notice Updates claimIndices for a given claim key.
function _addOrUpdateClaimIndex(
OptionTypeState storage optionTypeState,
Expand Down

0 comments on commit bc99806

Please sign in to comment.