Skip to content

Commit

Permalink
Move balance delta assignment into unchecked block in Pool.donate (#…
Browse files Browse the repository at this point in the history
…643)

* Move balance delta assignment into unchecked block in `Pool.donate`

The balance delta calculation has been moved inside an unchecked block in the 'donate' function since overflow isn't possible.

* Add comment
  • Loading branch information
shuhuiluo authored May 13, 2024
1 parent 30671fd commit 0f5d739
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/donate gas with 1 token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
108887
108741
2 changes: 1 addition & 1 deletion .forge-snapshots/donate gas with 2 tokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149382
149236
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23609
23579
3 changes: 2 additions & 1 deletion src/libraries/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,9 @@ library Pool {
function donate(State storage state, uint256 amount0, uint256 amount1) internal returns (BalanceDelta delta) {
uint128 liquidity = state.liquidity;
if (liquidity == 0) revert NoLiquidityToReceiveFees();
delta = toBalanceDelta(-(amount0.toInt128()), -(amount1.toInt128()));
unchecked {
// negation safe as amount0 and amount1 are always positive
delta = toBalanceDelta(-(amount0.toInt128()), -(amount1.toInt128()));
if (amount0 > 0) {
state.feeGrowthGlobal0X128 += FullMath.mulDiv(amount0, FixedPoint128.Q128, liquidity);
}
Expand Down

0 comments on commit 0f5d739

Please sign in to comment.