-
Notifications
You must be signed in to change notification settings - Fork 505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix tests and use BalanceDeltas #130
Merged
snreynolds
merged 8 commits into
sra/addLiq-accounting-updates
from
sauce/addLiq-accounting-updates-cleanup
Jun 26, 2024
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e605af4
fix some assertions
saucepoint e12a3fb
use BalanceDeltas for arithmetic
saucepoint 0d85aca
cleanest code in the game???
saucepoint eb85ede
additional cleaning
saucepoint 3ba713d
typo lol
saucepoint a98bb14
autocompound gas benchmarks
saucepoint 4bc5851
autocompound excess credit gas benchmark
saucepoint b382a1e
save 600 gas, cleaner code when moving caller delta to tokensOwed
saucepoint File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
176386 | ||
172027 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
151968 | ||
147609 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
471675 | ||
467316 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,66 +126,45 @@ contract BaseLiquidityManagement is IBaseLiquidityManagement, SafeCallback { | |
position.liquidity | ||
); | ||
|
||
console2.log(callerFeesAccrued.amount0()); | ||
console2.log(callerFeesAccrued.amount1()); | ||
console2.log("totalFees"); | ||
console2.log(totalFeesAccrued.amount0()); | ||
console2.log(totalFeesAccrued.amount1()); | ||
|
||
// Calculate the accurate tokens owed to the caller. | ||
// If the totalFeesAccrued equals the callerFeesAccrued then the total owed to the caller is just the liquidityDelta. | ||
// If the totalFeesAccrued is greater than the callerFeesAccrued, we must account for the difference. | ||
// 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. | ||
(int128 callerDelta0, int128 callerDelta1) = totalFeesAccrued != callerFeesAccrued | ||
? _calculateCallerDeltas(liquidityDelta, totalFeesAccrued, callerFeesAccrued) | ||
: (liquidityDelta.amount0(), liquidityDelta.amount1()); | ||
// the delta for increase liquidity assuming that totalFeesAccrued was not applied | ||
BalanceDelta principalDelta = liquidityDelta - totalFeesAccrued; | ||
|
||
// 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. | ||
uint128 tokensOwed0 = 0; | ||
uint128 tokensOwed1 = 0; | ||
(tokensOwed0, callerDelta0) = callerDelta0 > 0 ? (uint128(callerDelta0), int128(0)) : (uint128(0), callerDelta0); | ||
(tokensOwed1, callerDelta1) = callerDelta1 > 0 ? (uint128(callerDelta1), int128(0)) : (uint128(0), callerDelta1); | ||
BalanceDelta tokensOwed; | ||
if (callerDelta.amount0() > 0) { | ||
// credit the excess tokens to the position's tokensOwed | ||
tokensOwed = toBalanceDelta(callerDelta.amount0(), 0); | ||
|
||
position.addTokensOwed(tokensOwed0, tokensOwed1); | ||
position.addLiquidity(liquidityToAdd); | ||
position.updateFeeGrowthInside(feeGrowthInside0X128, feeGrowthInside1X128); | ||
// this contract is responsible for custodying the excess tokens | ||
thisDelta = thisDelta + toBalanceDelta(callerDelta.amount0(), 0); | ||
|
||
// 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 | ||
int128 thisDelta0 = liquidityDelta.amount0() - callerDelta0; | ||
int128 thisDelta1 = liquidityDelta.amount1() - callerDelta1; | ||
// the caller is not expected to collect the excess tokens | ||
callerDelta = toBalanceDelta(0, callerDelta.amount1()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since all of this is shared once for token0 and once for token1 can we just put it in a helper? |
||
} | ||
|
||
return (toBalanceDelta(callerDelta0, callerDelta1), toBalanceDelta(thisDelta0, thisDelta1)); | ||
} | ||
if (callerDelta.amount1() > 0) { | ||
// credit the excess tokens to the position's tokensOwed | ||
tokensOwed = toBalanceDelta(tokensOwed.amount0(), callerDelta.amount1()); | ||
|
||
// Returns the delta paid/credited by/to the caller. | ||
function _calculateCallerDeltas( | ||
BalanceDelta liquidityDelta, | ||
BalanceDelta totalFeesAccrued, | ||
BalanceDelta callerFeesAccrued | ||
) private pure returns (int128 callerDelta0, int128 callerDelta1) { | ||
(int128 liquidityDelta0, int128 liquidityDelta1) = (liquidityDelta.amount0(), liquidityDelta.amount1()); | ||
(int128 totalFeesAccrued0, int128 totalFeesAccrued1) = (totalFeesAccrued.amount0(), totalFeesAccrued.amount1()); | ||
(int128 callerFeesAccrued0, int128 callerFeesAccrued1) = | ||
(callerFeesAccrued.amount0(), callerFeesAccrued.amount1()); | ||
|
||
callerDelta0 = _calculateCallerDelta(liquidityDelta0, totalFeesAccrued0, callerFeesAccrued0); | ||
callerDelta1 = _calculateCallerDelta(liquidityDelta1, totalFeesAccrued1, callerFeesAccrued1); | ||
} | ||
// this contract is responsible for custodying the excess tokens | ||
thisDelta = thisDelta + toBalanceDelta(0, callerDelta.amount1()); | ||
|
||
function _calculateCallerDelta(int128 liquidityDelta, int128 totalFeesAccrued, int128 callerFeesAccrued) | ||
private | ||
pure | ||
returns (int128 callerDelta) | ||
{ | ||
unchecked { | ||
// The principle delta owed/debited to the caller before any LP fees are deducted. | ||
int128 principleDelta = liquidityDelta - totalFeesAccrued; | ||
// The new caller delta is this principle delta plus the callerFeesAccrued which consists of | ||
// the custodied fees by posm and unclaimed fees from the modifyLiq call. | ||
callerDelta = principleDelta + callerFeesAccrued; | ||
// 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); | ||
|
||
return (callerDelta, thisDelta); | ||
} | ||
|
||
function _increaseLiquidityAndZeroOut( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think its a slight optimization to skip all the math stuff if totalFeesAccrued != callerFeesAccrued.. which I think might happen most of the time (that they are equal)