-
Notifications
You must be signed in to change notification settings - Fork 560
/
grpc_query.go
231 lines (192 loc) · 8.62 KB
/
grpc_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package keeper
import (
"context"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/osmosis/osmomath"
incentivetypes "github.com/osmosis-labs/osmosis/v19/x/incentives/types"
"github.com/osmosis-labs/osmosis/v19/x/pool-incentives/types"
poolmanagertypes "github.com/osmosis-labs/osmosis/v19/x/poolmanager/types"
)
var _ types.QueryServer = Querier{}
// Querier defines a wrapper around the x/pool-incentives keeper providing gRPC
// method handlers.
type Querier struct {
Keeper
}
func NewQuerier(k Keeper) Querier {
return Querier{Keeper: k}
}
// GaugeIds takes provided gauge request and returns the respective internally incentivized gaugeIDs.
// If internally incentivized for a given pool id is not found, returns an error.
func (q Querier) GaugeIds(ctx context.Context, req *types.QueryGaugeIdsRequest) (*types.QueryGaugeIdsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
distrInfo := q.Keeper.GetDistrInfo(sdkCtx)
totalWeightDec := distrInfo.TotalWeight.ToLegacyDec()
incentivePercentage := osmomath.NewDec(0)
percentMultiplier := osmomath.NewInt(100)
pool, err := q.Keeper.poolmanagerKeeper.GetPool(sdkCtx, req.PoolId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
isConcentratedPool := pool.GetType() == poolmanagertypes.Concentrated
if isConcentratedPool {
incentiveEpochDuration := q.Keeper.incentivesKeeper.GetEpochInfo(sdkCtx).Duration
gaugeId, err := q.Keeper.GetPoolGaugeId(sdkCtx, req.PoolId, incentiveEpochDuration)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
for _, record := range distrInfo.Records {
if record.GaugeId == gaugeId {
// Pool incentive % = (gauge_id_weight / sum_of_all_pool_gauge_weight) * 100
incentivePercentage = record.Weight.ToLegacyDec().Quo(totalWeightDec).MulInt(percentMultiplier)
}
}
return &types.QueryGaugeIdsResponse{
GaugeIdsWithDuration: []*types.QueryGaugeIdsResponse_GaugeIdWithDuration{
{
GaugeId: gaugeId,
Duration: incentiveEpochDuration,
GaugeIncentivePercentage: incentivePercentage.String(),
},
},
}, nil
}
lockableDurations := q.Keeper.GetLockableDurations(sdkCtx)
gaugeIdsWithDuration := make([]*types.QueryGaugeIdsResponse_GaugeIdWithDuration, len(lockableDurations))
for i, duration := range lockableDurations {
gaugeId, err := q.Keeper.GetPoolGaugeId(sdkCtx, req.PoolId, duration)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
for _, record := range distrInfo.Records {
if record.GaugeId == gaugeId {
// Pool incentive % = (gauge_id_weight / sum_of_all_pool_gauge_weight) * 100
incentivePercentage = record.Weight.ToLegacyDec().Quo(totalWeightDec).MulInt(percentMultiplier)
}
}
gaugeIdsWithDuration[i] = &types.QueryGaugeIdsResponse_GaugeIdWithDuration{
GaugeId: gaugeId,
Duration: duration,
GaugeIncentivePercentage: incentivePercentage.String(),
}
}
return &types.QueryGaugeIdsResponse{GaugeIdsWithDuration: gaugeIdsWithDuration}, nil
}
// DistrInfo returns gauges receiving pool rewards and their respective weights.
func (q Querier) DistrInfo(ctx context.Context, _ *types.QueryDistrInfoRequest) (*types.QueryDistrInfoResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return &types.QueryDistrInfoResponse{DistrInfo: q.Keeper.GetDistrInfo(sdkCtx)}, nil
}
// Params return pool-incentives module params.
func (q Querier) Params(ctx context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return &types.QueryParamsResponse{Params: q.Keeper.GetParams(sdkCtx)}, nil
}
// LockableDurations returns the lock durations that are incentivized through pool-incentives.
func (q Querier) LockableDurations(ctx context.Context, _ *types.QueryLockableDurationsRequest) (*types.QueryLockableDurationsResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return &types.QueryLockableDurationsResponse{LockableDurations: q.Keeper.GetLockableDurations(sdkCtx)}, nil
}
// IncentivizedPools iterates over all internally incentivized gauges and returns their corresponding pools.
func (q Querier) IncentivizedPools(ctx context.Context, _ *types.QueryIncentivizedPoolsRequest) (*types.QueryIncentivizedPoolsResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
lockableDurations := q.Keeper.GetLockableDurations(sdkCtx)
distrInfo := q.Keeper.GetDistrInfo(sdkCtx)
// Add epoch duration to lockable durations if not already present.
// This is to ensure that concentrated gauges (which run on epoch time) are
// always included in the query, even if the epoch duration changes in the future.
epochDuration := q.incentivesKeeper.GetEpochInfo(sdkCtx).Duration
epochAlreadyLockable := false
for _, lockableDuration := range lockableDurations {
if lockableDuration == epochDuration {
epochAlreadyLockable = true
break
}
}
// Ensure that we only add epoch duration if it does not already exist as a lockable duration.
if !epochAlreadyLockable {
lockableDurations = append(lockableDurations, epochDuration)
}
// While there are exceptions, typically the number of incentivizedPools
// equals to the number of incentivized gauges / number of lockable durations.
incentivizedPools := make([]types.IncentivizedPool, 0, len(distrInfo.Records)/len(lockableDurations))
incentivizedPoolIDs := make(map[uint64]time.Duration)
// Loop over the distribution records and fill in the incentivized pools struct.
for _, record := range distrInfo.Records {
for _, lockableDuration := range lockableDurations {
poolId, err := q.Keeper.GetPoolIdFromGaugeId(sdkCtx, record.GaugeId, lockableDuration)
if err == nil {
incentivizedPool := types.IncentivizedPool{
PoolId: poolId,
LockableDuration: lockableDuration,
GaugeId: record.GaugeId,
}
incentivizedPools = append(incentivizedPools, incentivizedPool)
incentivizedPoolIDs[poolId] = lockableDuration
}
}
}
// Only run the following if the above loop determined there were incentivized pools.
if len(incentivizedPoolIDs) > 0 {
// Retrieve the migration records between balancer pools and concentrated liquidity pools.
// This comes from the superfluid keeper, since superfluid is the only pool incentives connected
// module that has access to the gamm modules store.
migrationRecords, err := q.gammKeeper.GetAllMigrationInfo(sdkCtx)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
// Iterate over all migration records.
for _, record := range migrationRecords.BalancerToConcentratedPoolLinks {
// If the cl pool is not in the list of incentivized pools, skip it.
lockableDuration, incentivized := incentivizedPoolIDs[record.ClPoolId]
if !incentivized {
continue
}
// Add the indirectly incentivized balancer pools to the list of incentivized pools.
gaugeId, err := q.Keeper.GetPoolGaugeId(sdkCtx, record.BalancerPoolId, lockableDuration)
if err == nil {
incentivizedPool := types.IncentivizedPool{
PoolId: record.BalancerPoolId,
LockableDuration: lockableDuration,
GaugeId: gaugeId,
}
incentivizedPools = append(incentivizedPools, incentivizedPool)
}
}
}
return &types.QueryIncentivizedPoolsResponse{
IncentivizedPools: incentivizedPools,
}, nil
}
// ExternalIncentiveGauges iterates over all gauges and returns gauges externally incentivized by excluding default (internal) gauges.
func (q Querier) ExternalIncentiveGauges(ctx context.Context, req *types.QueryExternalIncentiveGaugesRequest) (*types.QueryExternalIncentiveGaugesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
store := sdkCtx.KVStore(q.Keeper.storeKey)
prefixStore := prefix.NewStore(store, []byte("pool-incentives/"))
iterator := prefixStore.Iterator(nil, nil)
defer iterator.Close()
// map true to default gauges created with pool
poolGaugeIds := make(map[uint64]bool)
for ; iterator.Valid(); iterator.Next() {
poolGaugeIds[sdk.BigEndianToUint64(iterator.Value())] = true
}
// iterate over all gauges, exclude default created gauges, leaving externally incentivized gauges
allGauges := q.Keeper.GetAllGauges(sdkCtx)
gauges := []incentivetypes.Gauge{}
for _, gauge := range allGauges {
if _, ok := poolGaugeIds[gauge.Id]; !ok {
gauges = append(gauges, gauge)
}
}
return &types.QueryExternalIncentiveGaugesResponse{Data: gauges}, nil
}