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

[CL Incentives]: Sync global uptime accumulators upon tick crossing #4399

Merged
merged 123 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
123 commits
Select commit Hold shift + click to select a range
235653b
initial push
czarcas7ic Jan 26, 2023
e4941f6
push
czarcas7ic Jan 26, 2023
a3ac7b7
untracked files
czarcas7ic Jan 26, 2023
e888677
Merge branch 'adam/add-freeze-api' of https://github.com/osmosis-labs…
czarcas7ic Jan 26, 2023
61f5c76
add untracked files
czarcas7ic Jan 26, 2023
afc350f
Merge branch 'adam/add-freeze-api' of https://github.com/osmosis-labs…
czarcas7ic Jan 26, 2023
085bdc1
merge branches
czarcas7ic Jan 26, 2023
b7a2a70
Merge branch 'main' into adam/add-freeze-api
czarcas7ic Jan 26, 2023
2e4fd0f
add changes required for fees
czarcas7ic Jan 26, 2023
79017a3
lint
czarcas7ic Jan 26, 2023
8e68719
fix test errors
czarcas7ic Jan 26, 2023
b221dcf
clean up
czarcas7ic Jan 26, 2023
d2ede58
remove print lines
czarcas7ic Jan 26, 2023
a515d5d
feat: single fee accum (#4116)
czarcas7ic Jan 27, 2023
838c2ab
no longer need to get all positions for fee
czarcas7ic Jan 27, 2023
c75f163
incentive accum initialization
AlpinYukseloglu Jan 27, 2023
447967e
lint
AlpinYukseloglu Jan 27, 2023
0adb801
Update x/concentrated-liquidity/store_test.go
czarcas7ic Jan 28, 2023
17f47fe
add lower level tests and make uptime accum access private
AlpinYukseloglu Jan 29, 2023
75e0c14
lint
AlpinYukseloglu Jan 29, 2023
5f2e7a5
add gotests for helper fn
AlpinYukseloglu Jan 29, 2023
433e215
Merge branch 'main' into alpo/uptime-accums
AlpinYukseloglu Jan 29, 2023
008379a
Merge adam/add-freeze-api into alpo/uptime-accums
AlpinYukseloglu Jan 29, 2023
400ac3e
Update test to use existing variable
AlpinYukseloglu Jan 29, 2023
a4fbf0b
create or update records upon position creation/update
AlpinYukseloglu Jan 29, 2023
c604659
add tests and clean up
AlpinYukseloglu Jan 30, 2023
1f75cea
sync position-related changes
AlpinYukseloglu Jan 30, 2023
982830e
add support for negative liquidity delta
AlpinYukseloglu Jan 30, 2023
768ec68
add and test helper & set up tick initialization logic
AlpinYukseloglu Jan 31, 2023
c318fa0
implement accumulator updates on tick crossing and clean up proto
AlpinYukseloglu Jan 31, 2023
d0184b7
clean up comments
AlpinYukseloglu Jan 31, 2023
6020836
minor test cleanup
AlpinYukseloglu Feb 1, 2023
0d5559a
Merge branch 'alpo/position-uptime-accums' into alpo/CL-accum-update-…
AlpinYukseloglu Feb 1, 2023
01bd068
ensure accum initalization follows conventions and clean up tests
AlpinYukseloglu Feb 1, 2023
1bc68b5
update pool proto and CL pool extension for tracking liq changes
AlpinYukseloglu Feb 1, 2023
188778d
update new field at pool initialization and add tests
AlpinYukseloglu Feb 1, 2023
093024d
comment todos
AlpinYukseloglu Feb 2, 2023
a778dbb
clean up naming and move empty options to global var
AlpinYukseloglu Feb 2, 2023
6fbf412
Merge branch 'main' into alpo/position-uptime-accums
AlpinYukseloglu Feb 2, 2023
0c9c235
add clarifying comments and lint
AlpinYukseloglu Feb 2, 2023
a5fe6f6
Merge branch 'main' into alpo/position-uptime-accums
AlpinYukseloglu Feb 2, 2023
2ba1d26
Merge branch 'alpo/position-uptime-accums' into alpo/CL-accum-update-…
AlpinYukseloglu Feb 2, 2023
7c6eb8f
implement IncentiveRecord proto
AlpinYukseloglu Feb 2, 2023
a4b8620
add comments
AlpinYukseloglu Feb 2, 2023
d79c226
lint
AlpinYukseloglu Feb 2, 2023
677bc8a
Merge branch 'alpo/position-uptime-accums' into alpo/CL-accum-update-…
AlpinYukseloglu Feb 2, 2023
e0ae23e
set up wiring for proto
AlpinYukseloglu Feb 2, 2023
f9f11b3
implement updateUptimeAccumulatorsToNow
AlpinYukseloglu Feb 2, 2023
2c29631
implement and test calcAccruedIncentivesForAccum
AlpinYukseloglu Feb 3, 2023
798b66e
add tests for new helpers
AlpinYukseloglu Feb 3, 2023
fcc5c8c
minor comment updates
AlpinYukseloglu Feb 3, 2023
debab7f
Merge branch 'alpo/position-uptime-accums' into alpo/CL-accum-update-…
AlpinYukseloglu Feb 3, 2023
9287d3c
accum test updates
AlpinYukseloglu Feb 3, 2023
37aa74f
lint
AlpinYukseloglu Feb 3, 2023
a7986a3
Merge branch 'main' into alpo/CL-accum-update-logic
AlpinYukseloglu Feb 3, 2023
81a7b99
Merge branch 'main' into alpo/CL-accum-update-logic
AlpinYukseloglu Feb 5, 2023
7a7c52d
clean up diff
AlpinYukseloglu Feb 5, 2023
2961572
lint
AlpinYukseloglu Feb 5, 2023
19ef297
update go.mod for e2e
AlpinYukseloglu Feb 5, 2023
3d8a86e
clean up proto and add error message
AlpinYukseloglu Feb 7, 2023
2f93b76
lint
AlpinYukseloglu Feb 7, 2023
bec7d79
sketch out testing approach
AlpinYukseloglu Feb 9, 2023
a208eb2
set up keys
AlpinYukseloglu Feb 9, 2023
6ba9927
clean up diff
AlpinYukseloglu Feb 9, 2023
940f0b7
clean up comments
AlpinYukseloglu Feb 9, 2023
e35280e
lint
AlpinYukseloglu Feb 9, 2023
b777ee1
implement set and get for incentive records
AlpinYukseloglu Feb 10, 2023
8355980
comment cleanup
AlpinYukseloglu Feb 10, 2023
85da656
minor comment and test cleanup
AlpinYukseloglu Feb 10, 2023
d167d77
clean up tests and comments
AlpinYukseloglu Feb 11, 2023
de7c146
[CL Incentives] Implement `setIncentiveRecords` and `getIncentiveReco…
AlpinYukseloglu Feb 11, 2023
ad5db48
Revert "[CL Incentives] Implement `setIncentiveRecords` and `getIncen…
AlpinYukseloglu Feb 11, 2023
c65ddc1
Merge branch 'alpo-set-incentives' into alpo-cl-uptime-global
AlpinYukseloglu Feb 13, 2023
ad3938f
set incentive records in new position tests
AlpinYukseloglu Feb 13, 2023
9a5649e
Merge branch 'main' into alpo-cl-uptime-global
AlpinYukseloglu Feb 13, 2023
3859c0b
fix merge conflicts
AlpinYukseloglu Feb 13, 2023
3c5c9de
Merge branch 'main' into alpo-cl-uptime-proto-helpers
AlpinYukseloglu Feb 13, 2023
deaf70c
add check for init position record values
AlpinYukseloglu Feb 13, 2023
2661c58
fix and finalize uptime accum position tests
AlpinYukseloglu Feb 13, 2023
4c7f178
fix go mod for e2e
AlpinYukseloglu Feb 13, 2023
26b4505
Merge branch 'alpo-cl-uptime-proto-helpers' in
AlpinYukseloglu Feb 13, 2023
74cadb6
Merge branch 'alpo-cl-uptime-proto-helpers' into alpo-set-incentives
AlpinYukseloglu Feb 13, 2023
166656a
Merge branch 'alpo-set-incentives' into alpo-cl-uptime-global
AlpinYukseloglu Feb 13, 2023
a636539
clean up diff
AlpinYukseloglu Feb 13, 2023
ee5c2c6
add err check to accum update
AlpinYukseloglu Feb 13, 2023
5407223
fix rounding error
AlpinYukseloglu Feb 16, 2023
b5b6643
Merge branch 'alpo-cl-uptime-proto-helpers' into alpo-set-incentives
AlpinYukseloglu Feb 16, 2023
a1e4deb
Merge branch 'alpo-cl-uptime-proto-helpers' into alpo-cl-uptime-global
AlpinYukseloglu Feb 16, 2023
83bd08f
implement tick init uptime tracker logic
AlpinYukseloglu Feb 16, 2023
acd94eb
fix test compatibility
AlpinYukseloglu Feb 16, 2023
dde309a
implement fix for global accums falling out of sync before tick init
AlpinYukseloglu Feb 16, 2023
cc21105
add tests to ensure uptime trackers are initialized properly
AlpinYukseloglu Feb 16, 2023
5fe763c
add comment for new helper
AlpinYukseloglu Feb 17, 2023
10919f4
add core logic
AlpinYukseloglu Feb 17, 2023
9419d69
Merge branch 'main' into alpo-cl-uptime-proto-helpers
AlpinYukseloglu Feb 20, 2023
a16a58b
go mod tidy
AlpinYukseloglu Feb 20, 2023
586e7bd
fix conflicts
AlpinYukseloglu Feb 20, 2023
efe299c
fix e2e
AlpinYukseloglu Feb 20, 2023
4df2525
update go mod
AlpinYukseloglu Feb 20, 2023
05acab6
Merge branch 'alpo-cl-uptime-proto-helpers' into alpo-set-incentives
AlpinYukseloglu Feb 20, 2023
c68cca2
Merge branch 'alpo-cl-uptime-proto-helpers' into alpo-cl-uptime-global
AlpinYukseloglu Feb 20, 2023
dfa9215
Merge branch 'alpo-cl-uptime-global' into alpo-cl-uptime-tick-init
AlpinYukseloglu Feb 20, 2023
07f94f5
Merge branch 'main' into alpo-set-incentives
AlpinYukseloglu Feb 20, 2023
b7e5cd6
Merge branch 'alpo-set-incentives' into alpo-cl-uptime-global
AlpinYukseloglu Feb 20, 2023
736820b
fix and further test tick init logic
AlpinYukseloglu Feb 20, 2023
ea20428
remove redundant values from incentive record state
AlpinYukseloglu Feb 22, 2023
3738f83
cleanup from review
AlpinYukseloglu Feb 22, 2023
2cf4d75
Merge branch 'main' into alpo-cl-uptime-global
AlpinYukseloglu Feb 22, 2023
54b9bd3
Merge branch 'alpo-cl-uptime-global' into alpo-cl-uptime-tick-init
AlpinYukseloglu Feb 22, 2023
a607e48
comment cleanup
AlpinYukseloglu Feb 22, 2023
bbd6e49
set up cross tick tests
AlpinYukseloglu Feb 23, 2023
63bffa4
Merge branch 'alpo-cl-uptime-tick-init' into alpo-cl-uptime-cross
AlpinYukseloglu Feb 23, 2023
bba3209
add further tests
AlpinYukseloglu Feb 23, 2023
1d208be
test and comment cleanup
AlpinYukseloglu Feb 23, 2023
b080e81
Merge branch 'alpo-cl-uptime-tick-init' into alpo-cl-uptime-cross
AlpinYukseloglu Feb 23, 2023
bcf4abd
clean up test comments
AlpinYukseloglu Feb 23, 2023
30aa0c5
Merge branch 'alpo-cl-uptime-tick-init' into alpo-cl-uptime-cross
AlpinYukseloglu Feb 23, 2023
b0e9edb
fix test names
AlpinYukseloglu Feb 23, 2023
a7d07f0
Merge branch 'main' into alpo-cl-uptime-tick-init
AlpinYukseloglu Feb 23, 2023
e610a05
Merge branch 'alpo-cl-uptime-tick-init' into alpo-cl-uptime-cross
AlpinYukseloglu Feb 23, 2023
a55414b
update uptime accums before gettings them for tick crossing
AlpinYukseloglu Feb 25, 2023
0a51d05
lint
AlpinYukseloglu Feb 28, 2023
86badc2
clean up tests
AlpinYukseloglu Mar 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions x/concentrated-liquidity/incentives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,34 @@ var (
)

type ExpectedUptimes struct {
emptyExpectedAccumValues []sdk.DecCoins
hundredTokensSingleDenom []sdk.DecCoins
hundredTokensMultiDenom []sdk.DecCoins
twoHundredTokensMultiDenom []sdk.DecCoins
varyingTokensSingleDenom []sdk.DecCoins
varyingTokensMultiDenom []sdk.DecCoins
emptyExpectedAccumValues []sdk.DecCoins
hundredTokensSingleDenom []sdk.DecCoins
hundredTokensMultiDenom []sdk.DecCoins
twoHundredTokensMultiDenom []sdk.DecCoins
threeHundredTokensMultiDenom []sdk.DecCoins
varyingTokensSingleDenom []sdk.DecCoins
varyingTokensMultiDenom []sdk.DecCoins
}

// getExpectedUptimes returns a base set of expected values for testing based on the number
// of supported uptimes at runtime. This abstraction exists only to ensure backwards-compatibility
// of incentives-related tests if the supported uptimes are ever changed.
func getExpectedUptimes() ExpectedUptimes {
expUptimes := ExpectedUptimes{
emptyExpectedAccumValues: []sdk.DecCoins{},
hundredTokensSingleDenom: []sdk.DecCoins{},
hundredTokensMultiDenom: []sdk.DecCoins{},
twoHundredTokensMultiDenom: []sdk.DecCoins{},
varyingTokensSingleDenom: []sdk.DecCoins{},
varyingTokensMultiDenom: []sdk.DecCoins{},
emptyExpectedAccumValues: []sdk.DecCoins{},
hundredTokensSingleDenom: []sdk.DecCoins{},
hundredTokensMultiDenom: []sdk.DecCoins{},
twoHundredTokensMultiDenom: []sdk.DecCoins{},
threeHundredTokensMultiDenom: []sdk.DecCoins{},
varyingTokensSingleDenom: []sdk.DecCoins{},
varyingTokensMultiDenom: []sdk.DecCoins{},
}
for i := range types.SupportedUptimes {
expUptimes.emptyExpectedAccumValues = append(expUptimes.emptyExpectedAccumValues, cl.EmptyCoins)
expUptimes.hundredTokensSingleDenom = append(expUptimes.hundredTokensSingleDenom, sdk.NewDecCoins(cl.HundredFooCoins))
expUptimes.hundredTokensMultiDenom = append(expUptimes.hundredTokensMultiDenom, sdk.NewDecCoins(cl.HundredFooCoins, cl.HundredBarCoins))
expUptimes.twoHundredTokensMultiDenom = append(expUptimes.twoHundredTokensMultiDenom, sdk.NewDecCoins(cl.HundredFooCoins.Add(cl.HundredFooCoins), cl.HundredBarCoins.Add(cl.HundredBarCoins)))
expUptimes.threeHundredTokensMultiDenom = append(expUptimes.threeHundredTokensMultiDenom, sdk.NewDecCoins(cl.HundredFooCoins.Add(cl.HundredFooCoins).Add(cl.HundredFooCoins), cl.HundredBarCoins.Add(cl.HundredBarCoins).Add(cl.HundredBarCoins)))
expUptimes.varyingTokensSingleDenom = append(expUptimes.varyingTokensSingleDenom, sdk.NewDecCoins(cl.HundredFooCoins.Add(sdk.NewDecCoin("foo", sdk.NewInt(int64(i))))))
expUptimes.varyingTokensMultiDenom = append(expUptimes.varyingTokensMultiDenom, sdk.NewDecCoins(cl.HundredFooCoins.Add(sdk.NewDecCoin("foo", sdk.NewInt(int64(i)))), cl.HundredBarCoins.Add(sdk.NewDecCoin("bar", sdk.NewInt(int64(i*3))))))
}
Expand Down Expand Up @@ -139,6 +142,19 @@ func chargeIncentive(incentiveRecord types.IncentiveRecord, timeElapsed time.Dur
return incentiveRecord
}

// Helper for adding a predetermined amount to each global uptime accum in clPool
func addToUptimeAccums(ctx sdk.Context, poolId uint64, clKeeper *cl.Keeper, addValues []sdk.DecCoins) error {
poolUptimeAccumulators, err := clKeeper.GetUptimeAccumulators(ctx, poolId)
if err != nil {
return err
}

for uptimeIndex, uptimeAccum := range poolUptimeAccumulators {
uptimeAccum.AddToAccumulator(addValues[uptimeIndex])
}

return nil
}
func createIncentiveRecord(incentiveDenom string, remainingAmt, emissionRate sdk.Dec, startTime time.Time, minUpTime time.Duration) types.IncentiveRecord {
return types.IncentiveRecord{
IncentiveDenom: incentiveDenom,
Expand Down
40 changes: 37 additions & 3 deletions x/concentrated-liquidity/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,31 @@ func (k Keeper) crossTick(ctx sdk.Context, poolId uint64, tickIndex int64, swapS
return sdk.Dec{}, err
}

accum, err := k.getFeeAccumulator(ctx, poolId)
feeAccum, err := k.getFeeAccumulator(ctx, poolId)
if err != nil {
return sdk.Dec{}, err
}

// subtract tick's fee growth outside from current fee growth global, including the fee growth of the current swap.
tickInfo.FeeGrowthOutside = accum.GetValue().Add(swapStateFeeGrowth).Sub(tickInfo.FeeGrowthOutside)
tickInfo.FeeGrowthOutside = feeAccum.GetValue().Add(swapStateFeeGrowth).Sub(tickInfo.FeeGrowthOutside)

// Update global accums to now before uptime outside changes
if err := k.updateUptimeAccumulatorsToNow(ctx, poolId); err != nil {
return sdk.Dec{}, err
}

uptimeAccums, err := k.getUptimeAccumulators(ctx, poolId)
if err != nil {
return sdk.Dec{}, err
}

// For each supported uptime, subtract tick's uptime growth outside from the respective uptime accumulator
// This is functionally equivalent to "flipping" the trackers once the tick is crossed
updatedUptimeTrackers := tickInfo.UptimeTrackers
for uptimeId, uptimeAccum := range uptimeAccums {
updatedUptimeTrackers[uptimeId].UptimeGrowthOutside = uptimeAccum.GetValue().Sub(updatedUptimeTrackers[uptimeId].UptimeGrowthOutside)
}

k.SetTickInfo(ctx, poolId, tickIndex, tickInfo)

return tickInfo.LiquidityNet, nil
Expand All @@ -94,7 +112,23 @@ func (k Keeper) getTickInfo(ctx sdk.Context, poolId uint64, tickIndex int64) (ti
return tickStruct, err
}

return model.TickInfo{LiquidityGross: sdk.ZeroDec(), LiquidityNet: sdk.ZeroDec(), FeeGrowthOutside: initialFeeGrowthOutside}, nil
// Sync global uptime accumulators to ensure the uptime tracker init values are up to date.
if err := k.updateUptimeAccumulatorsToNow(ctx, poolId); err != nil {
return tickStruct, err
}

// Initialize uptime trackers for the new tick to the appropriate starting values.
valuesToAdd, err := k.getInitialUptimeGrowthOutsidesForTick(ctx, poolId, tickIndex)
if err != nil {
return tickStruct, err
}

initialUptimeTrackers := []model.UptimeTracker{}
for _, uptimeTrackerValue := range valuesToAdd {
initialUptimeTrackers = append(initialUptimeTrackers, model.UptimeTracker{UptimeGrowthOutside: uptimeTrackerValue})
}

return model.TickInfo{LiquidityGross: sdk.ZeroDec(), LiquidityNet: sdk.ZeroDec(), FeeGrowthOutside: initialFeeGrowthOutside, UptimeTrackers: initialUptimeTrackers}, nil
}
if err != nil {
return tickStruct, err
Expand Down
Loading