Skip to content

Commit

Permalink
Perform multiplication before division for fee calculations
Browse files Browse the repository at this point in the history
Co-authored-by: Alcibiades Athens <[email protected]>
  • Loading branch information
neodaoist and 0xAlcibiades authored Dec 8, 2022
1 parent cf78a08 commit c8b1d62
Showing 1 changed file with 17 additions and 10 deletions.
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

0 comments on commit c8b1d62

Please sign in to comment.