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

Perform multiplication before division for fee calculations #132

Merged
merged 4 commits into from
Dec 8, 2022
Merged
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
27 changes: 17 additions & 10 deletions src/OptionSettlementEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,10 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine {
revert ExpiredOption(optionId, expiry);
}

uint256 rxAmount = amount * optionRecord.underlyingAmount;
uint256 fee = ((rxAmount / 10_000) * feeBps);
// Calculate, record, and emit event for fee accrual on underlying asset
uint256 rxAmount = optionRecord.underlyingAmount * amount;
address underlyingAsset = optionRecord.underlyingAsset;

feeBalance[underlyingAsset] += fee;

uint256 encodedClaimId = claimId;
if (claimId == 0) {
// create new claim
Expand All @@ -356,8 +354,7 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine {
uint16 bucketIndex = _addOrUpdateClaimBucket(optionKey, amount);
_addOrUpdateClaimIndex(encodedClaimId, bucketIndex, amount);

// Emit events
emit FeeAccrued(underlyingAsset, msg.sender, fee);
uint256 fee = _calculateRecordAndEmitFee(underlyingAsset, rxAmount);
emit OptionsWritten(optionId, msg.sender, encodedClaimId, amount);

if (claimId == 0) {
Expand Down Expand Up @@ -409,14 +406,16 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine {
revert CallerHoldsInsufficientOptions(optionId, amount);
}

// Calculate, record, and emit event for fee accrual on exercise asset
uint256 rxAmount = optionRecord.exerciseAmount * amount;
uint256 txAmount = optionRecord.underlyingAmount * amount;
uint256 fee = ((rxAmount / 10_000) * feeBps);
address exerciseAsset = optionRecord.exerciseAsset;
address underlyingAsset = optionRecord.underlyingAsset;
uint256 fee = _calculateRecordAndEmitFee(exerciseAsset, rxAmount);

_assignExercise(optionKey, optionRecord, amount);
emit OptionsExercised(optionId, msg.sender, amount);

feeBalance[exerciseAsset] += fee;
_assignExercise(optionKey, optionRecord, amount);

emit FeeAccrued(exerciseAsset, msg.sender, fee);
emit OptionsExercised(optionId, msg.sender, amount);
Expand All @@ -427,7 +426,7 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine {
SafeTransferLib.safeTransferFrom(ERC20(exerciseAsset), msg.sender, address(this), (rxAmount + fee));

// Transfer out the underlying
SafeTransferLib.safeTransfer(ERC20(optionRecord.underlyingAsset), msg.sender, txAmount);
SafeTransferLib.safeTransfer(ERC20(underlyingAsset), msg.sender, txAmount);
}

/*//////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -540,6 +539,14 @@ contract OptionSettlementEngine is ERC1155, IOptionSettlementEngine {
// Internal Helper Functions
//////////////////////////////////////////////////////////////*/

/// @dev Internal helper function to calculate, record, and emit event for fee accrual
/// when writing (on underlying asset) and when exercising (on exercise asset)
function _calculateRecordAndEmitFee(address assetAddress, uint256 assetAmount) internal returns (uint256 fee) {
fee = ((assetAmount * feeBps) / 10_000);
feeBalance[assetAddress] += fee;
emit FeeAccrued(assetAddress, msg.sender, fee);
}

/// @dev Performs fair exercise assignment by pseudorandomly selecting a claim
/// bucket between the intial creation of the option type and "today". The buckets
/// are then iterated from oldest to newest (looping if we reach "today") if the
Expand Down