Skip to content

Commit

Permalink
[CL][Internal Review][CollectIncentives] fix uptime Accumulator init …
Browse files Browse the repository at this point in the history
…genesis [part2] (#5411)

* updated

* removed

* cleanup

* fixing

* fixed test
  • Loading branch information
stackman27 authored Jun 3, 2023
1 parent a89cc93 commit 613fd46
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 52 deletions.
2 changes: 2 additions & 0 deletions proto/osmosis/concentrated-liquidity/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ message PositionData {
uint64 lock_id = 2 [ (gogoproto.moretags) = "yaml:\"lock_id\"" ];
osmosis.accum.v1beta1.Record spread_reward_accum_record = 3
[ (gogoproto.nullable) = false ];
repeated osmosis.accum.v1beta1.Record uptime_accum_records = 4
[ (gogoproto.nullable) = false ];
}

// GenesisState defines the concentrated liquidity module's genesis state.
Expand Down
28 changes: 28 additions & 0 deletions x/concentrated-liquidity/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState genesis.GenesisState) {
spreadRewardPositionKey := types.KeySpreadRewardPositionAccumulator(positionWrapper.Position.PositionId)

k.initOrUpdateAccumPosition(ctx, spreadRewardAccumObject, positionWrapper.SpreadRewardAccumRecord.AccumValuePerShare, spreadRewardPositionKey, positionWrapper.SpreadRewardAccumRecord.NumShares, positionWrapper.SpreadRewardAccumRecord.UnclaimedRewardsTotal, positionWrapper.SpreadRewardAccumRecord.Options)

positionName := string(types.KeyPositionId(positionWrapper.Position.PositionId))
uptimeAccumulators, err := k.GetUptimeAccumulators(ctx, positionWrapper.Position.PoolId)
if err != nil {
panic(err)
}

for uptimeIndex, uptimeRecord := range positionWrapper.UptimeAccumRecords {
k.initOrUpdateAccumPosition(ctx, uptimeAccumulators[uptimeIndex], uptimeRecord.AccumValuePerShare, positionName, uptimeRecord.NumShares, uptimeRecord.UnclaimedRewardsTotal, uptimeRecord.Options)
}
}
}

Expand Down Expand Up @@ -189,10 +199,28 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *genesis.GenesisState {
panic(err)
}

// Retrieve uptime incentive accumulator state for position
positionName := string(types.KeyPositionId(position.PositionId))
uptimeAccumulators, err := k.GetUptimeAccumulators(ctx, position.PoolId)
if err != nil {
panic(err)
}

uptimeAccumObject := make([]accum.Record, len(uptimeAccumulators))
for uptimeIndex := range types.SupportedUptimes {
accumRecord, err := uptimeAccumulators[uptimeIndex].GetPosition(positionName)
if err != nil {
panic(err)
}

uptimeAccumObject[uptimeIndex] = accumRecord
}

positionData = append(positionData, genesis.PositionData{
LockId: lockId,
Position: &position,
SpreadRewardAccumRecord: spreadRewardAccumPositionRecord,
UptimeAccumRecords: uptimeAccumObject,
})
}

Expand Down
81 changes: 81 additions & 0 deletions x/concentrated-liquidity/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,32 @@ var (
UnclaimedRewardsTotal: sdk.NewDecCoins(sdk.NewDecCoin("foo", sdk.NewInt(5))),
Options: nil,
}

accumRecord = accum.Record{
NumShares: sdk.OneDec(),
AccumValuePerShare: sdk.NewDecCoins(sdk.NewDecCoin("foo", sdk.NewInt(50))),
UnclaimedRewardsTotal: sdk.NewDecCoins(sdk.NewDecCoin("foo", sdk.NewInt(25))),
Options: nil,
}

// five records because we have 5 supported uptimes
testUptimeAccumRecord = []accum.Record{
accumRecord,
accumRecord,
accumRecord,
accumRecord,
accumRecord,
accumRecord,
}
)

func accumRecordWithDefinedValues(accumRecord accum.Record, numShares sdk.Dec, initAccumValue, unclaimedRewards sdk.Int) accum.Record {
accumRecord.NumShares = numShares
accumRecord.AccumValuePerShare = sdk.NewDecCoins(sdk.NewDecCoin("uion", initAccumValue))
accumRecord.UnclaimedRewardsTotal = sdk.NewDecCoins(sdk.NewDecCoin("uosmo", unclaimedRewards))
return accumRecord
}

func positionWithPoolId(position model.Position, poolId uint64) *model.Position {
position.PoolId = poolId
return &position
Expand Down Expand Up @@ -200,11 +224,20 @@ func (s *KeeperTestSuite) TestInitGenesis() {
LockId: 1,
Position: &testPositionModel,
SpreadRewardAccumRecord: testSpreadRewardAccumRecord,
UptimeAccumRecords: testUptimeAccumRecord,
},
{
LockId: 0,
Position: withPositionId(testPositionModel, 2),
SpreadRewardAccumRecord: testSpreadRewardAccumRecord,
UptimeAccumRecords: []accum.Record{
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(10000), sdk.NewInt(100), sdk.NewInt(50)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(1000), sdk.NewInt(100), sdk.NewInt(50)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(100), sdk.NewInt(100), sdk.NewInt(50)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(10), sdk.NewInt(100), sdk.NewInt(50)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(1), sdk.NewInt(100), sdk.NewInt(50)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(1), sdk.NewInt(100), sdk.NewInt(50)),
},
},
},
spreadFactorAccumValues: genesis.AccumObject{
Expand Down Expand Up @@ -255,11 +288,20 @@ func (s *KeeperTestSuite) TestInitGenesis() {
LockId: 1,
Position: &testPositionModel,
SpreadRewardAccumRecord: testSpreadRewardAccumRecord,
UptimeAccumRecords: testUptimeAccumRecord,
},
{
LockId: 0,
Position: withPositionId(testPositionModel, 2),
SpreadRewardAccumRecord: testSpreadRewardAccumRecord,
UptimeAccumRecords: []accum.Record{
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(10000), sdk.NewInt(100), sdk.NewInt(50)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(1000), sdk.NewInt(100), sdk.NewInt(50)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(100), sdk.NewInt(100), sdk.NewInt(50)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(10), sdk.NewInt(100), sdk.NewInt(50)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(1), sdk.NewInt(100), sdk.NewInt(50)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(1), sdk.NewInt(100), sdk.NewInt(50)),
},
},
},
expectedspreadFactorAccumValues: []genesis.AccumObject{
Expand Down Expand Up @@ -309,6 +351,7 @@ func (s *KeeperTestSuite) TestInitGenesis() {
LockId: 1,
Position: &testPositionModel,
SpreadRewardAccumRecord: testSpreadRewardAccumRecord,
UptimeAccumRecords: testUptimeAccumRecord,
},
},
spreadFactorAccumValues: genesis.AccumObject{
Expand Down Expand Up @@ -343,6 +386,14 @@ func (s *KeeperTestSuite) TestInitGenesis() {
{
LockId: 2,
Position: withPositionId(*positionWithPoolId(testPositionModel, 2), DefaultPositionId+1),
UptimeAccumRecords: []accum.Record{
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(99999), sdk.NewInt(10), sdk.NewInt(5)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(9999), sdk.NewInt(10), sdk.NewInt(5)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(999), sdk.NewInt(100), sdk.NewInt(50)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(99), sdk.NewInt(50), sdk.NewInt(25)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(9), sdk.NewInt(50), sdk.NewInt(25)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(9), sdk.NewInt(50), sdk.NewInt(25)),
},
},
},

Expand Down Expand Up @@ -427,11 +478,20 @@ func (s *KeeperTestSuite) TestInitGenesis() {
LockId: 1,
Position: &testPositionModel,
SpreadRewardAccumRecord: testSpreadRewardAccumRecord,
UptimeAccumRecords: testUptimeAccumRecord,
},
{
LockId: 2,
Position: withPositionId(*positionWithPoolId(testPositionModel, 2), DefaultPositionId+1),
SpreadRewardAccumRecord: testSpreadRewardAccumRecord,
UptimeAccumRecords: []accum.Record{
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(99999), sdk.NewInt(10), sdk.NewInt(5)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(9999), sdk.NewInt(10), sdk.NewInt(5)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(999), sdk.NewInt(100), sdk.NewInt(50)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(99), sdk.NewInt(50), sdk.NewInt(25)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(9), sdk.NewInt(50), sdk.NewInt(25)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(9), sdk.NewInt(50), sdk.NewInt(25)),
},
},
},
},
Expand Down Expand Up @@ -519,6 +579,7 @@ func (s *KeeperTestSuite) TestInitGenesis() {
LockId: actualLockId,
Position: &getPosition,
SpreadRewardAccumRecord: positionDataEntry.SpreadRewardAccumRecord,
UptimeAccumRecords: positionDataEntry.UptimeAccumRecords,
})
}

Expand Down Expand Up @@ -586,6 +647,7 @@ func (s *KeeperTestSuite) TestExportGenesis() {
LockId: 1,
Position: &testPositionModel,
SpreadRewardAccumRecord: testSpreadRewardAccumRecord,
UptimeAccumRecords: testUptimeAccumRecord,
},
},
spreadFactorAccumValues: genesis.AccumObject{
Expand Down Expand Up @@ -636,11 +698,13 @@ func (s *KeeperTestSuite) TestExportGenesis() {
LockId: 1,
Position: &testPositionModel,
SpreadRewardAccumRecord: testSpreadRewardAccumRecord,
UptimeAccumRecords: testUptimeAccumRecord,
},
{
LockId: 0,
Position: withPositionId(testPositionModel, DefaultPositionId+1),
SpreadRewardAccumRecord: testSpreadRewardAccumRecord,
UptimeAccumRecords: testUptimeAccumRecord,
},
},
spreadFactorAccumValues: genesis.AccumObject{
Expand Down Expand Up @@ -697,6 +761,14 @@ func (s *KeeperTestSuite) TestExportGenesis() {
LockId: 2,
Position: withPositionId(*positionWithPoolId(testPositionModel, 2), DefaultPositionId+2),
SpreadRewardAccumRecord: testSpreadRewardAccumRecord,
UptimeAccumRecords: []accum.Record{
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(99999), sdk.NewInt(10), sdk.NewInt(5)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(9999), sdk.NewInt(10), sdk.NewInt(5)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(999), sdk.NewInt(100), sdk.NewInt(50)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(99), sdk.NewInt(50), sdk.NewInt(25)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(9), sdk.NewInt(50), sdk.NewInt(25)),
accumRecordWithDefinedValues(accumRecord, sdk.NewDec(9), sdk.NewInt(50), sdk.NewInt(25)),
},
},
},
},
Expand Down Expand Up @@ -752,6 +824,15 @@ func (s *KeeperTestSuite) TestExportGenesis() {
}
}

// Validate uptime accumulators
for i, actualPositionData := range actualExported.PositionData {
expectedPositionData := expectedGenesis.PositionData[i]
// validate incentive accumulator
for i, uptimeAccum := range actualPositionData.UptimeAccumRecords {
s.Require().Equal(expectedPositionData.UptimeAccumRecords[i], uptimeAccum)
}
}

// Validate positions.
s.Require().Equal(tc.genesis.PositionData, actualExported.PositionData)

Expand Down
Loading

0 comments on commit 613fd46

Please sign in to comment.