-
Notifications
You must be signed in to change notification settings - Fork 602
/
query_proto_wrap.go
191 lines (157 loc) · 6.12 KB
/
query_proto_wrap.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
package client
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/osmosis-labs/osmosis/v16/x/poolmanager"
"github.com/osmosis-labs/osmosis/v16/x/poolmanager/client/queryproto"
"github.com/osmosis-labs/osmosis/v16/x/poolmanager/types"
)
// This file should evolve to being code gen'd, off of `proto/poolmanager/v1beta/query.yml`
type Querier struct {
K poolmanager.Keeper
}
func NewQuerier(k poolmanager.Keeper) Querier {
return Querier{k}
}
func (q Querier) Params(ctx sdk.Context,
req queryproto.ParamsRequest,
) (*queryproto.ParamsResponse, error) {
params := q.K.GetParams(ctx)
return &queryproto.ParamsResponse{Params: params}, nil
}
// EstimateSwapExactAmountIn estimates input token amount for a swap.
func (q Querier) EstimateSwapExactAmountIn(ctx sdk.Context, req queryproto.EstimateSwapExactAmountInRequest) (*queryproto.EstimateSwapExactAmountInResponse, error) {
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())
}
tokenOutAmount, err := q.K.MultihopEstimateOutGivenExactAmountIn(ctx, req.Routes, tokenIn)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &queryproto.EstimateSwapExactAmountInResponse{
TokenOutAmount: tokenOutAmount,
}, nil
}
// EstimateSwapExactAmountOut estimates token output amount for a swap.
func (q Querier) EstimateSwapExactAmountOut(ctx sdk.Context, req queryproto.EstimateSwapExactAmountOutRequest) (*queryproto.EstimateSwapExactAmountOutResponse, error) {
if req.TokenOut == "" {
return nil, status.Error(codes.InvalidArgument, "invalid token")
}
if err := types.SwapAmountOutRoutes(req.Routes).Validate(); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
tokenOut, err := sdk.ParseCoinNormalized(req.TokenOut)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid token: %s", err.Error())
}
tokenInAmount, err := q.K.MultihopEstimateInGivenExactAmountOut(ctx, req.Routes, tokenOut)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &queryproto.EstimateSwapExactAmountOutResponse{
TokenInAmount: tokenInAmount,
}, nil
}
func (q Querier) EstimateSinglePoolSwapExactAmountOut(ctx sdk.Context, req queryproto.EstimateSinglePoolSwapExactAmountOutRequest) (*queryproto.EstimateSwapExactAmountOutResponse, error) {
routeReq := &queryproto.EstimateSwapExactAmountOutRequest{
PoolId: req.PoolId,
TokenOut: req.TokenOut,
Routes: types.SwapAmountOutRoutes{{PoolId: req.PoolId, TokenInDenom: req.TokenInDenom}},
}
return q.EstimateSwapExactAmountOut(ctx, *routeReq)
}
func (q Querier) EstimateSinglePoolSwapExactAmountIn(ctx sdk.Context, req queryproto.EstimateSinglePoolSwapExactAmountInRequest) (*queryproto.EstimateSwapExactAmountInResponse, error) {
routeReq := &queryproto.EstimateSwapExactAmountInRequest{
PoolId: req.PoolId,
TokenIn: req.TokenIn,
Routes: types.SwapAmountInRoutes{{PoolId: req.PoolId, TokenOutDenom: req.TokenOutDenom}},
}
return q.EstimateSwapExactAmountIn(ctx, *routeReq)
}
// NumPools returns total number of pools.
func (q Querier) NumPools(ctx sdk.Context, _ queryproto.NumPoolsRequest) (*queryproto.NumPoolsResponse, error) {
return &queryproto.NumPoolsResponse{
NumPools: q.K.GetNextPoolId(ctx) - 1,
}, nil
}
// Pool returns the pool specified by id.
func (q Querier) Pool(ctx sdk.Context, req queryproto.PoolRequest) (*queryproto.PoolResponse, error) {
pool, err := q.K.GetPool(ctx, req.PoolId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
pool = pool.AsSerializablePool()
any, err := codectypes.NewAnyWithValue(pool)
if err != nil {
return nil, err
}
return &queryproto.PoolResponse{
Pool: any,
}, nil
}
func (q Querier) AllPools(ctx sdk.Context, req queryproto.AllPoolsRequest) (*queryproto.AllPoolsResponse, error) {
pools, err := q.K.AllPools(ctx)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
var anyPools []*codectypes.Any
for _, pool := range pools {
any, err := codectypes.NewAnyWithValue(pool.AsSerializablePool())
if err != nil {
return nil, err
}
anyPools = append(anyPools, any)
}
return &queryproto.AllPoolsResponse{
Pools: anyPools,
}, nil
}
// SpotPrice returns the spot price of the pool with the given quote and base asset denoms.
func (q Querier) SpotPrice(ctx sdk.Context, req queryproto.SpotPriceRequest) (*queryproto.SpotPriceResponse, error) {
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")
}
sp, err := q.K.RouteCalculateSpotPrice(ctx, req.PoolId, req.QuoteAssetDenom, req.BaseAssetDenom)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &queryproto.SpotPriceResponse{
SpotPrice: sp.String(),
}, err
}
// TotalPoolLiquidity returns the total liquidity of the pool.
func (q Querier) TotalPoolLiquidity(ctx sdk.Context, req queryproto.TotalPoolLiquidityRequest) (*queryproto.TotalPoolLiquidityResponse, error) {
if req.PoolId == 0 {
return nil, status.Error(codes.InvalidArgument, "Invalid Pool Id")
}
poolI, err := q.K.GetPool(ctx, req.PoolId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
coins, err := q.K.GetTotalPoolLiquidity(ctx, poolI.GetId())
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &queryproto.TotalPoolLiquidityResponse{
Liquidity: coins,
}, nil
}
// TotalLiquidity returns the total liquidity across all pools.
func (q Querier) TotalLiquidity(ctx sdk.Context, req queryproto.TotalLiquidityRequest) (*queryproto.TotalLiquidityResponse, error) {
totalLiquidity, err := q.K.TotalLiquidity(ctx)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &queryproto.TotalLiquidityResponse{
Liquidity: totalLiquidity,
}, nil
}