Skip to content

Commit

Permalink
cleanest code in the game???
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepoint committed Jun 26, 2024
1 parent e12a3fb commit 0d85aca
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/increaseLiquidity_erc20.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
172030
172027
2 changes: 1 addition & 1 deletion .forge-snapshots/increaseLiquidity_erc6909.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
147612
147609
2 changes: 1 addition & 1 deletion .forge-snapshots/mintWithLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
467319
467316
21 changes: 15 additions & 6 deletions contracts/base/BaseLiquidityManagement.sol
Original file line number Diff line number Diff line change
Expand Up @@ -132,29 +132,38 @@ contract BaseLiquidityManagement is IBaseLiquidityManagement, SafeCallback {
// outstanding deltas the caller is responsible for, after their fees are credited to the principal delta
BalanceDelta callerDelta = principalDelta + callerFeesAccrued;

// outstanding deltas this contract is responsible for, intuitively the contract is responsible for taking fees external to the caller's accrued fees
BalanceDelta thisDelta = totalFeesAccrued - callerFeesAccrued;

// Update position storage, flushing the callerDelta value to tokensOwed first if necessary.
// If callerDelta > 0, then even after investing callerFeesAccrued, the caller still has some amount to collect that were not added into the position so they are accounted to tokensOwed and removed from the final callerDelta returned.
BalanceDelta tokensOwed;
if (callerDelta.amount0() > 0) {
// credit the excess tokens to the position's tokensOwed
tokensOwed = toBalanceDelta(callerDelta.amount0(), 0);

// this contract is responsible for custodying the excess tokens
thisDelta = thisDelta + toBalanceDelta(callerDelta.amount0(), 0);

// the caller is not expected to collect the excess tokens
callerDelta = toBalanceDelta(0, callerDelta.amount1());
}

if (callerDelta.amount1() > 0) {
// credit the excess tokens to the position's tokensOwed
tokensOwed = toBalanceDelta(tokensOwed.amount0(), callerDelta.amount1());

// this contract is responsible for custodying the excess tokens
thisDelta = thisDelta + toBalanceDelta(0, callerDelta.amount1());

// the caller is not expected to collect the excess tokens
callerDelta = toBalanceDelta(callerDelta.amount0(), 0);
}

position.addTokensOwed(tokensOwed);
position.addLiquidity(liquidityToAdd);
position.updateFeeGrowthInside(feeGrowthInside0X128, feeGrowthInside1X128);

// The delta owed or credited by this contract.
// TODO @sauce check that if callerDelta == 0 (zerod out from above), then this line just credits the posm to takes on behalf of the caller
// outstanding deltas this contract is responsible for
// TODO: why can't we do `thisDelta = totalFeesAccrued - callerFeesAccrued`?
BalanceDelta thisDelta = liquidityDelta - callerDelta;

return (callerDelta, thisDelta);
}

Expand Down

0 comments on commit 0d85aca

Please sign in to comment.