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: query tick accumulator trackers (APR #4) #5368

Merged
merged 4 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
49 changes: 48 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,22 @@ 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";
};

// TickAccumulatorTrackers returns the tick accumulator trackers.
// Contains spread factor and uptime accumulator trackers.
rpc TickAccumulatorTrackers(TickAccumulatorTrackersRequest)
returns (TickAccumulatorTrackersResponse) {
option (google.api.http).get =
"/osmosis/concentratedliquidity/v1beta1/tick_accum_trackers";
};
}

//=============================== UserPositions
Expand Down Expand Up @@ -185,4 +202,34 @@ 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 ];
}

// ===================== QueryTickAccumulatorTrackers
message TickAccumulatorTrackersRequest {
uint64 pool_id = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
int64 tick_index = 2 [ (gogoproto.moretags) = "yaml:\"tick_index\"" ];
}

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

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

func (q Querier) PositionById(grpcCtx context.Context,
req *queryproto.PositionByIdRequest,
) (*queryproto.PositionByIdResponse, error) {
Expand All @@ -50,6 +60,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
47 changes: 47 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,50 @@ 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
}

// TickAccumulatorTrackers returns tick accumulator trackers.
// It includes spread reward growth in the opposite direction of last traversal and uptime tracker values.
func (q Querier) TickAccumulatorTrackers(ctx sdk.Context, req clquery.TickAccumulatorTrackersRequest) (*clquery.TickAccumulatorTrackersResponse, error) {
if req.PoolId == 0 {
return nil, status.Error(codes.InvalidArgument, "pool id is zero")
}

cacheCtx, _ := ctx.CacheContext()
tickInfo, err := q.Keeper.GetTickInfo(cacheCtx, req.PoolId, req.TickIndex)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &clquery.TickAccumulatorTrackersResponse{
SpreadRewardGrowthOppositeDirectionOfLastTraversal: tickInfo.SpreadRewardGrowthOppositeDirectionOfLastTraversal,
UptimeTrackers: tickInfo.UptimeTrackers.List,
}, nil
}
Loading