Skip to content
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

feat(CL): concentrated liquidity fees POC CU-86776j1g7 #3893

Closed
wants to merge 62 commits into from

Conversation

p0mvn
Copy link
Member

@p0mvn p0mvn commented Dec 30, 2022

Closes: #XXX

What is the purpose of the change

This PR implements concentrated liquidity fees by utilizing our newly built accum module for claiming rewards.

There are a few breaking changes to the accum package made. For example, we need the ability to create, add to, and remove from a position with a custom accumulator update. In the context of concentrated liquidity, this custom accumulator update signifies the computation of fee growth outside the position. Without this change, we would be distributing rewards linearly across all positions.

By providing a custom position accumulator update, we are able to propagate the fee growth within a range to it.

This PR will be split into multiple smaller PRs. Tests are to be added.

Additional Notes

I moved away from the idea of defining additional "Trackers" for ticks inside the package. Instead, tracked the per-tick accumulators directly in tick structs.

I think I was able to mitigate the problem that we were discussing earlier. The one about accum module distributing rewards linearly with the number of shares without accounting for whether a position range was ever activated.

It is mitigated by introducing an ability to provide custom updates to the position's accumulators. This way, we should be able to compute fee growth outside the range and, then, inject it into the accum module's position.

This custom "fee growth outside" position accumulator can then be used directly by the accum module's ClaimRewards function since it would be subtracted from pool's "fee growth global" to get position's "fee growth inside" and multiply it by the number of shares inside that position to get final result.

@github-actions github-actions bot added C:docs Improvements or additions to documentation C:x/concentrated-liquidity labels Dec 30, 2022
Comment on lines 51 to 59
feeAccumulator, err := k.getFeeAccumulator(ctx, poolId)
if err != nil {
return err
}

if err := feeAccumulator.NewPositionCustom(owner.String(), zero, sdk.NewDecCoins(), nil); err != nil {
return err
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to use `liquidityDelta instead of initializing them with zero values. Changed them in #

@@ -38,8 +38,9 @@ func (s oneForZeroStrategy) ComputeSwapStep(sqrtPriceCurrent, nextSqrtPrice, liq
amountIn := math.CalcAmount1Delta(liquidity, nextSqrtPrice, sqrtPriceCurrent, false)
if amountRemaining.LT(amountIn) {
nextSqrtPrice = math.GetNextSqrtPriceFromAmount1RoundingDown(sqrtPriceCurrent, liquidity, amountRemaining)
amountIn = math.CalcAmount1Delta(liquidity, nextSqrtPrice, sqrtPriceCurrent, false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why / where is this change coming from? Do you think we need extra tests for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a drive-by change

It's logically equivalent to the old logic but avoids redundant recomputation in some cases.

It's unrelated to fees, I will submit this separately.

return nil
}

func (k Keeper) getFeeGrowthOutside(ctx sdk.Context, poolId uint64, owner sdk.AccAddress, lowerTick, upperTick int64) (sdk.DecCoins, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

owner not needed as argument. Removed in #

Comment on lines 137 to 141
poolFeeGrowth := poolFeeAccumulator.GetValue()

// calculate fee growth for upper tick and lower tick
feeGrowthAboveUpperTick := calculateFeeGrowth(upperTick, upperTickInfo.FeeGrowthOutside, currentTick, poolFeeGrowth, true)
feeGrowthBelowLowerTick := calculateFeeGrowth(lowerTick, lowerTickInfo.FeeGrowthOutside, currentTick, poolFeeGrowth, false)
feeGrowthGlobal := feeGlobalAccumulator.GetValue()

feeGrowthAboveUpperTick := calculateFeeGrowthAbove(upperTick, upperTickInfo.FeeGrowthOutside, currentTick, feeGrowthGlobal)
feeGrowthBelowLowerTick := calculateFeeGrowthBelow(lowerTick, lowerTickInfo.FeeGrowthOutside, currentTick, feeGrowthGlobal)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sunnya97
Copy link
Collaborator

Task linked: CU-86776j1g7 CL - Swap Fees

@p0mvn
Copy link
Member Author

p0mvn commented Jan 20, 2023

Task linked: CU-86776j1g7 CL - Swap Fees

Looks like click-up integration is connected via Sunny's account. We should consider switching to osmobot instead

@github-actions github-actions bot removed C:app-wiring Changes to the app folder C:x/poolmanager C:x/gamm Changes, features and bugs related to the gamm module. labels Jan 23, 2023
@@ -0,0 +1,3 @@
# Concentrated Liquidity Sage Math Scripts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: add docs

@p0mvn
Copy link
Member Author

p0mvn commented Jan 25, 2023

Everything that has been prototyped in this PR has been merged separately.

The last item is here: #4097

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:docs Improvements or additions to documentation C:x/concentrated-liquidity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants