Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cairoeth committed Jan 2, 2025
1 parent 2f15d8b commit ecd1107
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
15 changes: 13 additions & 2 deletions src/base/BaseCustomAccounting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,47 @@ abstract contract BaseCustomAccounting is BaseHook {
override
returns (bytes4, BeforeSwapDelta, uint24)
{
// Determine if the swap is exact input or exact output
bool exactInput = params.amountSpecified < 0;

// Determine which currency is specified and which is unspecified
(Currency specified, Currency unspecified) =
(params.zeroForOne == exactInput) ? (key.currency0, key.currency1) : (key.currency1, key.currency0);

// Get the positive specified amount
uint256 specifiedAmount = exactInput ? uint256(-params.amountSpecified) : uint256(params.amountSpecified);

// Get the amount of the unspecified currency to be taken or settled
uint256 unspecifiedAmount = _getAmount(
specifiedAmount,
exactInput ? specified : unspecified,
exactInput ? unspecified : specified,
params.zeroForOne,
exactInput
);

// New delta must be returned, so store in memory
BeforeSwapDelta returnDelta;

if (exactInput) {
specified.take(poolManager, address(this), specifiedAmount, true);
unspecified.settle(poolManager, address(this), unspecifiedAmount, true);

// On exact input, amount0 is specified and amount1 is unspecified.
returnDelta = toBeforeSwapDelta(specifiedAmount.toInt128(), -unspecifiedAmount.toInt128());
} else {
unspecified.take(poolManager, address(this), unspecifiedAmount, true);
specified.settle(poolManager, address(this), specifiedAmount, true);

// On exact output, amount1 is specified and amount0 is unspecified.
returnDelta = toBeforeSwapDelta(-specifiedAmount.toInt128(), unspecifiedAmount.toInt128());
}

return (BaseHook.beforeSwap.selector, returnDelta, 0);
return (this.beforeSwap.selector, returnDelta, 0);
}

/**
* @dev Calculate the amount of tokens to be take or settle.
* @dev Calculate the amount of tokens to take or send (settle).
*/
function _getAmount(uint256 amountIn, Currency input, Currency output, bool zeroForOne, bool exactInput)
internal
Expand Down
4 changes: 2 additions & 2 deletions src/base/BaseNoOp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ abstract contract BaseNoOp is BaseHook {
uint256 amountTaken = uint256(-params.amountSpecified);
Currency input = params.zeroForOne ? key.currency0 : key.currency1;
poolManager.mint(address(this), input.toId(), amountTaken);
return (BaseHook.beforeSwap.selector, toBeforeSwapDelta(amountTaken.toInt128(), 0), 0);
return (this.beforeSwap.selector, toBeforeSwapDelta(amountTaken.toInt128(), 0), 0);
} else {
return (BaseHook.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0);
return (this.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/fee/BaseDynamicFee.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ abstract contract BaseDynamicFee is BaseHook {
returns (bytes4)
{
poolManager.updateDynamicLPFee(key, _getFee(key));
return BaseHook.afterInitialize.selector;
return this.afterInitialize.selector;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/fee/BaseOverrideFee.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ abstract contract BaseOverrideFee is BaseHook {
bytes calldata hookData
) internal virtual override returns (bytes4, BeforeSwapDelta, uint24) {
uint24 fee = _getFee(sender, key, params, hookData);
return (BaseHook.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, fee | LPFeeLibrary.OVERRIDE_FEE_FLAG);
return (this.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, fee | LPFeeLibrary.OVERRIDE_FEE_FLAG);
}

/**
Expand Down
10 changes: 8 additions & 2 deletions src/fee/DynamicAfterFee.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,30 @@ abstract contract DynamicAfterFee is BaseHook {
BalanceDelta delta,
bytes calldata
) internal virtual override returns (bytes4, int128) {
// Only apply target delta for exact-input swaps
if (params.amountSpecified < 0) {
PoolId poolId = key.toId();
BalanceDelta targetDelta = _targetDeltas[poolId];
int128 feeAmount = 0;

// Skip empty/undefined target delta
if (BalanceDelta.unwrap(targetDelta) != 0) {
// Reset storage target delta to 0 and use one stored in memory
_targetDeltas[poolId] = BalanceDelta.wrap(0);

if (delta.amount0() == targetDelta.amount0() && delta.amount1() > targetDelta.amount1()) {
feeAmount = delta.amount1() - targetDelta.amount1();

// feeAmount is positive and int128, so we can safely cast to uint128 given that uint128
// has a larger maximum value.
poolManager.donate(key, 0, uint256(uint128(feeAmount)), "");
}

// Apply target delta to only exact-input swaps
if (delta.amount1() == targetDelta.amount1() && delta.amount0() > targetDelta.amount0()) {
feeAmount = delta.amount0() - targetDelta.amount0();
// feeAmount is positive and int128, so we can safely cast to uint128 and then to uint256

// feeAmount is positive and int128, so we can safely cast to uint128 given that uint128
// has a larger maximum value.
poolManager.donate(key, uint256(uint128(feeAmount)), 0, "");
}
}
Expand Down

0 comments on commit ecd1107

Please sign in to comment.