-
Notifications
You must be signed in to change notification settings - Fork 208
/
grpc_query.go
157 lines (126 loc) · 4.16 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
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/NibiruChain/nibiru/x/common"
"github.com/NibiruChain/nibiru/x/pricefeed/types"
)
var _ types.QueryServer = Keeper{}
func (k Keeper) QueryPrice(goCtx context.Context, req *types.QueryPriceRequest) (*types.QueryPriceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
pair, err := common.NewAssetPair(req.PairId)
if err != nil {
return nil, err
}
if !k.GetPairs(ctx).Contains(pair) {
return nil, status.Error(codes.NotFound, "pair not in module params")
}
if !k.ActivePairsStore().getKV(ctx).Has([]byte(pair.String())) {
return nil, status.Error(codes.NotFound, "invalid market ID")
}
tokens := common.DenomsFromPoolName(req.PairId)
token0, token1 := tokens[0], tokens[1]
currentPrice, sdkErr := k.GetCurrentPrice(ctx, token0, token1)
if sdkErr != nil {
return nil, sdkErr
}
return &types.QueryPriceResponse{
Price: types.CurrentPriceResponse{PairID: req.PairId, Price: currentPrice.Price},
}, nil
}
func (k Keeper) QueryRawPrices(
goCtx context.Context, req *types.QueryRawPricesRequest,
) (*types.QueryRawPricesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
if !k.IsActivePair(ctx, req.PairId) {
return nil, status.Error(codes.NotFound, "invalid market ID")
}
var prices types.PostedPriceResponses
for _, rp := range k.GetRawPrices(ctx, req.PairId) {
prices = append(prices, types.PostedPriceResponse{
PairID: rp.PairID,
OracleAddress: rp.Oracle,
Price: rp.Price,
Expiry: rp.Expiry,
})
}
return &types.QueryRawPricesResponse{
RawPrices: prices,
}, nil
}
func (k Keeper) QueryPrices(goCtx context.Context, req *types.QueryPricesRequest) (*types.QueryPricesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
var currentPrices types.CurrentPriceResponses
for _, currentPrice := range k.GetCurrentPrices(ctx) {
if currentPrice.PairID != "" {
currentPrices = append(currentPrices, types.CurrentPriceResponse(currentPrice))
}
}
return &types.QueryPricesResponse{
Prices: currentPrices,
}, nil
}
func (k Keeper) QueryOracles(goCtx context.Context, req *types.QueryOraclesRequest) (*types.QueryOraclesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
_, err := common.NewAssetPair(req.PairId)
if err != nil {
return nil, status.Error(codes.NotFound, "invalid market ID")
}
oracles := k.GetOraclesForPair(ctx, req.PairId)
if len(oracles) == 0 {
return &types.QueryOraclesResponse{}, nil
}
var strOracles []string
for _, oracle := range oracles {
strOracles = append(strOracles, oracle.String())
}
return &types.QueryOraclesResponse{
Oracles: strOracles,
}, nil
}
func (k Keeper) QueryParams(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)
return &types.QueryParamsResponse{Params: k.GetParams(ctx)}, nil
}
func (k Keeper) QueryPairs(goCtx context.Context, req *types.QueryPairsRequest,
) (*types.QueryPairsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
var responses types.PairResponses
for _, pair := range k.GetParams(ctx).Pairs {
var oracleStrings []string
for _, oracle := range k.OraclesStore().Get(ctx, pair) {
oracleStrings = append(oracleStrings, oracle.String())
}
responses = append(responses, types.PairResponse{
PairID: pair.String(),
Token0: pair.Token0,
Token1: pair.Token1,
Oracles: oracleStrings,
Active: k.IsActivePair(ctx, pair.String()),
})
}
// TODO improve these variable names. PairResponse is confusing on field "Pairs"
return &types.QueryPairsResponse{
Pairs: responses,
}, nil
}