You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -135,52 +135,31 @@ contract BaseLiquidityManagement is IBaseLiquidityManagement, SafeCallback {
135
135
// Calculate the accurate tokens owed to the caller.
136
136
// If the totalFeesAccrued equals the callerFeesAccrued then the total owed to the caller is just the liquidityDelta.
137
137
// If the totalFeesAccrued is greater than the callerFeesAccrued, we must account for the difference.
138
+
// TODO: If totalFeesAccrued == callerFeesAccrued, I think we can just apply the entire delta onto the caller, even if this implicitly collects on behalf of another user in the same range.
// and 20 token fee revenue is posted (i.e. swap revenue or donate)
145
-
146
-
// bob calls collects()
147
-
// liquidityDelta: 20
148
-
// totalFeesAccrued: 20
149
-
// callerFeesAccrued: 5
150
-
// (5 tokens sent to bob, and posm caches the 15 tokens for alice)
151
-
152
-
// assume another 20 token fee revenue is posted (a net new 5 tokens for bob and 15 new tokens for alice)
153
-
// alice now has 30 tokens of fee revenue, half of custodied by posm and the other half unclaimed in the PM
154
-
155
-
// when alice increases her liquidity, using exactly 30 tokens (autocompound):
156
-
// liquidityDelta: -10
157
-
// totalFeesAccrued: 20 (new fees: 5 for bob, 15 for alice)
158
-
// callerFeesAccrued: 30 (alice's fees, per feeGrowthInside)
159
-
160
-
// naively resolving deltas:
161
-
// posm: take 5 tokens (bob's fees), liquidityDelta is now: -15
162
-
// posm: pay PM the 15 tokens (alice's cached fees)
163
-
// alice: pay nothing, as its a pure and exact autocompound
164
-
165
-
// to solve:
166
-
// we need a way to discern cached fees and use them against liquidityDelta
167
-
168
-
// Update position storage, sanitizing the tokensOwed and callerDelta values first.
169
-
// if callerDelta > 0, then even after re-investing old fees, the caller still has some amount to collect that were not added into the position so they are accounted.
170
-
// if callerDelta <= 0, then tokensOwed0 and tokensOwed1 should be zero'd out as all fees were re-invested into a new position.
143
+
// Update position storage, flushing the callerDelta value to tokensOwed first if necessary.
144
+
// 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.
// When chaining many actions, this should be called at the very end to close out any open deltas owed to or by this contract for other users on the same range.
227
206
// This is safe because any amounts the caller should not pay or take have already been accounted for in closeCallerDeltas.
207
+
function _closeThisDeltas(BalanceDelta delta, Currency currency0, Currency currency1) internal {
208
+
int128 delta0 = delta.amount0();
209
+
int128 delta1 = delta.amount1();
210
+
211
+
// Mint a receipt for the tokens owed to this address.
212
+
if (delta0 >0) currency0.take(manager, address(this), uint128(delta0), true);
213
+
if (delta1 >0) currency1.take(manager, address(this), uint128(delta1), true);
214
+
// Burn the receipt for tokens owed to this address.
215
+
if (delta0 <0) currency0.settle(manager, address(this), uint256(int256(-delta0)), true);
216
+
if (delta1 <0) currency1.settle(manager, address(this), uint256(int256(-delta1)), true);
217
+
}
218
+
219
+
//TODO @sara deprecate when moving to _closeThisDeltas for decreaes and collect
228
220
function _closeAllDeltas(Currency currency0, Currency currency1) internal {
0 commit comments