-
Notifications
You must be signed in to change notification settings - Fork 608
/
Copy pathgrpc_query.go
450 lines (358 loc) · 12.8 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package keeper
import (
"context"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/osmosis-labs/osmosis/v13/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/v13/x/gamm/types"
"github.com/osmosis-labs/osmosis/v13/x/gamm/v2types"
)
var _ types.QueryServer = Querier{}
// Querier defines a wrapper around the x/gamm keeper providing gRPC method
// handlers.
type Querier struct {
Keeper
}
func NewQuerier(k Keeper) Querier {
return Querier{Keeper: k}
}
// QuerierV2 defines a wrapper around the x/gamm keeper providing gRPC method
// handlers for v2 queries.
type QuerierV2 struct {
Keeper
}
func NewV2Querier(k Keeper) QuerierV2 {
return QuerierV2{Keeper: k}
}
// Pool checks if a pool exists and their respective poolWeights.
func (q Querier) Pool(
ctx context.Context,
req *types.QueryPoolRequest,
) (*types.QueryPoolResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
pool, err := q.Keeper.GetPoolAndPoke(sdkCtx, req.PoolId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
any, err := codectypes.NewAnyWithValue(pool)
if err != nil {
return nil, err
}
return &types.QueryPoolResponse{Pool: any}, nil
}
// Pools checks existence of multiple pools and their poolWeights
func (q Querier) Pools(
ctx context.Context,
req *types.QueryPoolsRequest,
) (*types.QueryPoolsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
store := sdkCtx.KVStore(q.Keeper.storeKey)
poolStore := prefix.NewStore(store, types.KeyPrefixPools)
var anys []*codectypes.Any
pageRes, err := query.Paginate(poolStore, req.Pagination, func(_, value []byte) error {
poolI, err := q.Keeper.UnmarshalPool(value)
if err != nil {
return err
}
// Use GetPoolAndPoke function because it runs PokeWeights
poolI, err = q.Keeper.GetPoolAndPoke(sdkCtx, poolI.GetId())
if err != nil {
return err
}
any, err := codectypes.NewAnyWithValue(poolI)
if err != nil {
return err
}
anys = append(anys, any)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryPoolsResponse{
Pools: anys,
Pagination: pageRes,
}, nil
}
// NumPools returns total number of pools.
func (q Querier) NumPools(ctx context.Context, _ *types.QueryNumPoolsRequest) (*types.QueryNumPoolsResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return &types.QueryNumPoolsResponse{
NumPools: q.Keeper.GetNextPoolId(sdkCtx) - 1,
}, nil
}
func (q Querier) PoolType(ctx context.Context, req *types.QueryPoolTypeRequest) (*types.QueryPoolTypeResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
poolType, err := q.Keeper.GetPoolType(sdkCtx, req.PoolId)
return &types.QueryPoolTypeResponse{
PoolType: poolType,
}, err
}
// CalcJoinPoolShares queries the amount of shares you get by providing specific amount of tokens
func (q Querier) CalcJoinPoolShares(ctx context.Context, req *types.QueryCalcJoinPoolSharesRequest) (*types.QueryCalcJoinPoolSharesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.TokensIn == nil {
return nil, status.Error(codes.InvalidArgument, "no tokens in")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
pool, err := q.Keeper.getPoolForSwap(sdkCtx, req.PoolId)
if err != nil {
return nil, err
}
numShares, newLiquidity, err := pool.CalcJoinPoolShares(sdkCtx, req.TokensIn, pool.GetSwapFee(sdkCtx))
if err != nil {
return nil, err
}
return &types.QueryCalcJoinPoolSharesResponse{
ShareOutAmount: numShares,
TokensOut: newLiquidity,
}, nil
}
// PoolsWithFilter query allows to query pools with specific parameters
func (q Querier) PoolsWithFilter(ctx context.Context, req *types.QueryPoolsWithFilterRequest) (*types.QueryPoolsWithFilterResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
store := sdkCtx.KVStore(q.Keeper.storeKey)
poolStore := prefix.NewStore(store, types.KeyPrefixPools)
var response = []*codectypes.Any{}
pageRes, err := query.FilteredPaginate(poolStore, req.Pagination, func(_, value []byte, accumulate bool) (bool, error) {
pool, err := q.Keeper.UnmarshalPool(value)
if err != nil {
return false, err
}
poolId := pool.GetId()
// if liquidity specified in request
if len(req.MinLiquidity) > 0 {
poolLiquidity := pool.GetTotalPoolLiquidity(sdkCtx)
if !poolLiquidity.IsAllGTE(req.MinLiquidity) {
return false, nil
}
}
// if pool type specified in request
if req.PoolType != "" {
poolType, err := q.GetPoolType(sdkCtx, poolId)
if err != nil {
return false, types.ErrPoolNotFound
}
if poolType != req.PoolType {
return false, nil
}
}
any, err := codectypes.NewAnyWithValue(pool)
if err != nil {
return false, err
}
if accumulate {
response = append(response, any)
}
return true, nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryPoolsWithFilterResponse{
Pools: response,
Pagination: pageRes,
}, nil
}
// CalcExitPoolCoinsFromShares queries the amount of tokens you get by exiting a specific amount of shares
func (q Querier) CalcExitPoolCoinsFromShares(ctx context.Context, req *types.QueryCalcExitPoolCoinsFromSharesRequest) (*types.QueryCalcExitPoolCoinsFromSharesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
pool, err := q.Keeper.GetPoolAndPoke(sdkCtx, req.PoolId)
if err != nil {
return nil, types.ErrPoolNotFound
}
exitFee := pool.GetExitFee(sdkCtx)
totalSharesAmount := pool.GetTotalShares()
if req.ShareInAmount.GTE(totalSharesAmount) || req.ShareInAmount.LTE(sdk.ZeroInt()) {
return nil, sdkerrors.Wrapf(types.ErrInvalidMathApprox, "share ratio is zero or negative")
}
exitCoins, err := pool.CalcExitPoolCoinsFromShares(sdkCtx, req.ShareInAmount, exitFee)
if err != nil {
return nil, err
}
return &types.QueryCalcExitPoolCoinsFromSharesResponse{TokensOut: exitCoins}, nil
}
// CalcJoinPoolNoSwapShares returns the amount of shares you'd get if joined a pool without a swap and tokens which need to be provided
func (q Querier) CalcJoinPoolNoSwapShares(ctx context.Context, req *types.QueryCalcJoinPoolNoSwapSharesRequest) (*types.QueryCalcJoinPoolNoSwapSharesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
pool, err := q.GetPoolAndPoke(sdkCtx, req.PoolId)
if err != nil {
return nil, err
}
sharesOut, tokensJoined, err := pool.CalcJoinPoolNoSwapShares(sdkCtx, req.TokensIn, pool.GetSwapFee(sdkCtx))
if err != nil {
return nil, err
}
return &types.QueryCalcJoinPoolNoSwapSharesResponse{
TokensOut: tokensJoined,
SharesOut: sharesOut,
}, nil
}
// PoolParams queries a specified pool for its params.
func (q Querier) PoolParams(ctx context.Context, req *types.QueryPoolParamsRequest) (*types.QueryPoolParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
pool, err := q.Keeper.GetPoolAndPoke(sdkCtx, req.PoolId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
switch pool := pool.(type) {
case *balancer.Pool:
any, err := codectypes.NewAnyWithValue(&pool.PoolParams)
if err != nil {
return nil, err
}
return &types.QueryPoolParamsResponse{
Params: any,
}, nil
default:
errMsg := fmt.Sprintf("unrecognized %s pool type: %T", types.ModuleName, pool)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnpackAny, errMsg)
}
}
// TotalPoolLiquidity returns total liquidity in pool.
func (q Querier) TotalPoolLiquidity(ctx context.Context, req *types.QueryTotalPoolLiquidityRequest) (*types.QueryTotalPoolLiquidityResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
pool, err := q.Keeper.GetPoolAndPoke(sdkCtx, req.PoolId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryTotalPoolLiquidityResponse{
Liquidity: pool.GetTotalPoolLiquidity(sdkCtx),
}, nil
}
// TotalShares returns total pool shares.
func (q Querier) TotalShares(ctx context.Context, req *types.QueryTotalSharesRequest) (*types.QueryTotalSharesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
pool, err := q.Keeper.GetPoolAndPoke(sdkCtx, req.PoolId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryTotalSharesResponse{
TotalShares: sdk.NewCoin(
types.GetPoolShareDenom(req.PoolId),
pool.GetTotalShares()),
}, nil
}
// SpotPrice returns target pool asset prices on base and quote assets.
// nolint: staticcheck
func (q Querier) SpotPrice(ctx context.Context, req *types.QuerySpotPriceRequest) (*types.QuerySpotPriceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.BaseAssetDenom == "" {
return nil, status.Error(codes.InvalidArgument, "invalid base asset denom")
}
if req.QuoteAssetDenom == "" {
return nil, status.Error(codes.InvalidArgument, "invalid quote asset denom")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
// Note: the base and quote asset are provided as argument incorrectly intentionally.
// due to the historic bug in the original implementation.
sp, err := q.Keeper.CalculateSpotPrice(sdkCtx, req.PoolId, req.BaseAssetDenom, req.QuoteAssetDenom)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QuerySpotPriceResponse{
SpotPrice: sp.String(),
}, nil
}
func (q QuerierV2) SpotPrice(ctx context.Context, req *v2types.QuerySpotPriceRequest) (*v2types.QuerySpotPriceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.BaseAssetDenom == "" {
return nil, status.Error(codes.InvalidArgument, "invalid base asset denom")
}
if req.QuoteAssetDenom == "" {
return nil, status.Error(codes.InvalidArgument, "invalid quote asset denom")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
sp, err := q.Keeper.CalculateSpotPrice(sdkCtx, req.PoolId, req.QuoteAssetDenom, req.BaseAssetDenom)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &v2types.QuerySpotPriceResponse{
SpotPrice: sp.String(),
}, nil
}
// TotalLiquidity returns total liquidity across all pools.
func (q Querier) TotalLiquidity(ctx context.Context, _ *types.QueryTotalLiquidityRequest) (*types.QueryTotalLiquidityResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return &types.QueryTotalLiquidityResponse{
Liquidity: q.Keeper.GetTotalLiquidity(sdkCtx),
}, nil
}
// EstimateSwapExactAmountIn estimates input token amount for a swap.
func (q Querier) EstimateSwapExactAmountIn(ctx context.Context, req *types.QuerySwapExactAmountInRequest) (*types.QuerySwapExactAmountInResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.TokenIn == "" {
return nil, status.Error(codes.InvalidArgument, "invalid token")
}
tokenIn, err := sdk.ParseCoinNormalized(req.TokenIn)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid token: %s", err.Error())
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
tokenOutAmount, err := q.Keeper.MultihopEstimateOutGivenExactAmountIn(sdkCtx, req.Routes, tokenIn)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QuerySwapExactAmountInResponse{
TokenOutAmount: tokenOutAmount,
}, nil
}
// EstimateSwapExactAmountOut estimates token output amount for a swap.
func (q Querier) EstimateSwapExactAmountOut(ctx context.Context, req *types.QuerySwapExactAmountOutRequest) (*types.QuerySwapExactAmountOutResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.TokenOut == "" {
return nil, status.Error(codes.InvalidArgument, "invalid token")
}
tokenOut, err := sdk.ParseCoinNormalized(req.TokenOut)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid token: %s", err.Error())
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
tokenInAmount, err := q.Keeper.MultihopEstimateInGivenExactAmountOut(sdkCtx, req.Routes, tokenOut)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QuerySwapExactAmountOutResponse{
TokenInAmount: tokenInAmount,
}, nil
}