Skip to content

Commit

Permalink
feat: query pool accumulator rewards (APR #2 and 3)
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn committed May 31, 2023
1 parent 20650cf commit 8c91e0b
Show file tree
Hide file tree
Showing 8 changed files with 692 additions and 101 deletions.
25 changes: 24 additions & 1 deletion proto/osmosis/concentrated-liquidity/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package osmosis.concentratedliquidity.v1beta1;

import "gogoproto/gogo.proto";
import "osmosis/concentrated-liquidity/params.proto";
import "osmosis/concentrated-liquidity/tickInfo.proto";

import "cosmos/base/v1beta1/coin.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
Expand Down Expand Up @@ -73,6 +74,14 @@ service Query {
option (google.api.http).get =
"/osmosis/concentratedliquidity/v1beta1/position_by_id";
};

// PoolAccumulatorRewards returns the pool-global accumulator rewards.
// Contains spread factor rewards and uptime rewards.
rpc PoolAccumulatorRewards(PoolAccumulatorRewardsRequest)
returns (PoolAccumulatorRewardsResponse) {
option (google.api.http).get =
"/osmosis/concentratedliquidity/v1beta1/pool_accum_rewards";
};
}

//=============================== UserPositions
Expand Down Expand Up @@ -185,4 +194,18 @@ message ClaimableIncentivesResponse {
(gogoproto.moretags) = "yaml:\"forfeited_incentives\"",
(gogoproto.nullable) = false
];
}
}

// ===================== QueryPoolAccumulatorRewards
message PoolAccumulatorRewardsRequest {
uint64 pool_id = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
}

message PoolAccumulatorRewardsResponse {
repeated cosmos.base.v1beta1.DecCoin spread_reward_growth_global = 1 [
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins",
(gogoproto.nullable) = false
];
repeated UptimeTracker uptime_growth_global = 2
[ (gogoproto.moretags) = "yaml:\"list\"", (gogoproto.nullable) = false ];
}
3 changes: 3 additions & 0 deletions proto/osmosis/concentrated-liquidity/query.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ queries:
query_func: "k.PositionById"
cli:
cmd: "PositionById"
PoolAccumulatorRewards:
proto_wrapper:
query_func: "k.PoolAccumulatorRewards"
10 changes: 10 additions & 0 deletions x/concentrated-liquidity/client/grpc/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ func (q Querier) Pools(grpcCtx context.Context,
return q.Q.Pools(ctx, *req)
}

func (q Querier) PoolAccumulatorRewards(grpcCtx context.Context,
req *queryproto.PoolAccumulatorRewardsRequest,
) (*queryproto.PoolAccumulatorRewardsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(grpcCtx)
return q.Q.PoolAccumulatorRewards(ctx, *req)
}

func (q Querier) Params(grpcCtx context.Context,
req *queryproto.ParamsRequest,
) (*queryproto.ParamsResponse, error) {
Expand Down
28 changes: 28 additions & 0 deletions x/concentrated-liquidity/client/query_proto_wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,31 @@ func (q Querier) ClaimableIncentives(ctx sdk.Context, req clquery.ClaimableIncen
ForfeitedIncentives: forfeitedIncentives,
}, nil
}

// PoolAccumulatorRewards returns pool accumulator rewards.
// It includes global spread reward growth and global uptime growth accumulator values.
func (q Querier) PoolAccumulatorRewards(ctx sdk.Context, req clquery.PoolAccumulatorRewardsRequest) (*clquery.PoolAccumulatorRewardsResponse, error) {
if req.PoolId == 0 {
return nil, status.Error(codes.InvalidArgument, "pool id is zero")
}

spreadRewardsAcc, err := q.Keeper.GetSpreadRewardAccumulator(ctx, req.PoolId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

uptimeAccValues, err := q.Keeper.GetUptimeAccumulatorValues(ctx, req.PoolId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

uptimeGrowthTrackers := make([]model.UptimeTracker, 0, len(uptimeAccValues))
for _, uptimeTrackerValue := range uptimeAccValues {
uptimeGrowthTrackers = append(uptimeGrowthTrackers, model.UptimeTracker{UptimeGrowthOutside: uptimeTrackerValue})
}

return &clquery.PoolAccumulatorRewardsResponse{
SpreadRewardGrowthGlobal: spreadRewardsAcc.GetValue(),
UptimeGrowthGlobal: uptimeGrowthTrackers,
}, nil
}
630 changes: 539 additions & 91 deletions x/concentrated-liquidity/client/queryproto/query.pb.go

Large diffs are not rendered by default.

83 changes: 83 additions & 0 deletions x/concentrated-liquidity/client/queryproto/query.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions x/concentrated-liquidity/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,6 @@ func (k Keeper) CreateUptimeAccumulators(ctx sdk.Context, poolId uint64) error {
return k.createUptimeAccumulators(ctx, poolId)
}

func (k Keeper) GetUptimeAccumulatorValues(ctx sdk.Context, poolId uint64) ([]sdk.DecCoins, error) {
return k.getUptimeAccumulatorValues(ctx, poolId)
}

func CalcAccruedIncentivesForAccum(ctx sdk.Context, accumUptime time.Duration, qualifyingLiquidity sdk.Dec, timeElapsed sdk.Dec, poolIncentiveRecords []types.IncentiveRecord) (sdk.DecCoins, []types.IncentiveRecord, error) {
return calcAccruedIncentivesForAccum(ctx, accumUptime, qualifyingLiquidity, timeElapsed, poolIncentiveRecords)
}
Expand Down
10 changes: 5 additions & 5 deletions x/concentrated-liquidity/incentives.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func (k Keeper) GetUptimeAccumulators(ctx sdk.Context, poolId uint64) ([]accum.A
return accums, nil
}

// getUptimeAccumulatorValues gets the accumulator values for the supported uptimes for the given poolId
// GetUptimeAccumulatorValues gets the accumulator values for the supported uptimes for the given poolId
// Returns error if accumulator for the given poolId does not exist.
func (k Keeper) getUptimeAccumulatorValues(ctx sdk.Context, poolId uint64) ([]sdk.DecCoins, error) {
func (k Keeper) GetUptimeAccumulatorValues(ctx sdk.Context, poolId uint64) ([]sdk.DecCoins, error) {
uptimeAccums, err := k.GetUptimeAccumulators(ctx, poolId)
if err != nil {
return []sdk.DecCoins{}, err
Expand Down Expand Up @@ -88,7 +88,7 @@ func (k Keeper) getInitialUptimeGrowthOppositeDirectionOfLastTraversalForTick(ct

currentTick := pool.GetCurrentTick()
if currentTick >= tick {
uptimeAccumulatorValues, err := k.getUptimeAccumulatorValues(ctx, poolId)
uptimeAccumulatorValues, err := k.GetUptimeAccumulatorValues(ctx, poolId)
if err != nil {
return []sdk.DecCoins{}, err
}
Expand Down Expand Up @@ -624,7 +624,7 @@ func (k Keeper) GetUptimeGrowthInsideRange(ctx sdk.Context, poolId uint64, lower
}

// Get global uptime accumulator values
globalUptimeValues, err := k.getUptimeAccumulatorValues(ctx, poolId)
globalUptimeValues, err := k.GetUptimeAccumulatorValues(ctx, poolId)
if err != nil {
return []sdk.DecCoins{}, err
}
Expand Down Expand Up @@ -669,7 +669,7 @@ func (k Keeper) GetUptimeGrowthInsideRange(ctx sdk.Context, poolId uint64, lower
// attempt to claim their incentives.
// When LPs are ready to claim their incentives we calculate it using: (shares of # of LP) * (uptimeGrowthOutside - uptimeGrowthInside)
func (k Keeper) GetUptimeGrowthOutsideRange(ctx sdk.Context, poolId uint64, lowerTick int64, upperTick int64) ([]sdk.DecCoins, error) {
globalUptimeValues, err := k.getUptimeAccumulatorValues(ctx, poolId)
globalUptimeValues, err := k.GetUptimeAccumulatorValues(ctx, poolId)
if err != nil {
return []sdk.DecCoins{}, err
}
Expand Down

0 comments on commit 8c91e0b

Please sign in to comment.