/
grpc_query.go
177 lines (147 loc) · 5.36 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
package keeper
import (
"context"
"time"
"github.com/NibiruChain/collections"
sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/NibiruChain/nibiru/x/common/asset"
"github.com/NibiruChain/nibiru/x/perp/types"
)
type queryServer struct {
k Keeper
}
func NewQuerier(k Keeper) types.QueryServer {
return queryServer{k: k}
}
var _ types.QueryServer = queryServer{}
func (q queryServer) QueryPositions(
goCtx context.Context, req *types.QueryPositionsRequest,
) (*types.QueryPositionsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
traderAddr, err := sdk.AccAddressFromBech32(req.Trader) // just for validation purposes
if err != nil {
return nil, err
}
ctx := sdk.UnwrapSDKContext(goCtx)
pools := q.k.VpoolKeeper.GetAllPools(ctx)
var positions []*types.QueryPositionResponse
for _, pool := range pools {
position, err := q.position(ctx, pool.Pair, traderAddr)
if err == nil {
positions = append(positions, position)
}
}
return &types.QueryPositionsResponse{
Positions: positions,
}, nil
}
func (q queryServer) QueryPosition(
goCtx context.Context, req *types.QueryPositionRequest,
) (*types.QueryPositionResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
traderAddr, err := sdk.AccAddressFromBech32(req.Trader) // just for validation purposes
if err != nil {
return nil, err
}
ctx := sdk.UnwrapSDKContext(goCtx)
return q.position(ctx, req.Pair, traderAddr)
}
func (q queryServer) position(ctx sdk.Context, pair asset.Pair, trader sdk.AccAddress) (*types.QueryPositionResponse, error) {
position, err := q.k.Positions.Get(ctx, collections.Join(pair, trader))
if err != nil {
return nil, err
}
positionNotional, unrealizedPnl, err := q.k.getPositionNotionalAndUnrealizedPnL(ctx, position, types.PnLCalcOption_SPOT_PRICE)
if err != nil {
return nil, err
}
marginRatioMark, err := q.k.GetMarginRatio(ctx, position, types.MarginCalculationPriceOption_MAX_PNL)
if err != nil {
return nil, err
}
marginRatioIndex, err := q.k.GetMarginRatio(ctx, position, types.MarginCalculationPriceOption_INDEX)
if err != nil {
// The index portion of the query fails silently as not to distrupt all
// position queries when oracles aren't posting prices.
q.k.Logger(ctx).Error(err.Error())
marginRatioIndex = sdk.Dec{}
}
return &types.QueryPositionResponse{
Position: &position,
PositionNotional: positionNotional,
UnrealizedPnl: unrealizedPnl,
MarginRatioMark: marginRatioMark,
MarginRatioIndex: marginRatioIndex,
BlockNumber: ctx.BlockHeight(),
}, nil
}
func (q queryServer) Params(
goCtx context.Context, req *types.QueryParamsRequest,
) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
return &types.QueryParamsResponse{Params: q.k.GetParams(ctx)}, nil
}
func (q queryServer) CumulativePremiumFraction(
goCtx context.Context,
req *types.QueryCumulativePremiumFractionRequest,
) (*types.QueryCumulativePremiumFractionResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
pairMetadata, err := q.k.PairsMetadata.Get(ctx, req.Pair)
if err != nil {
return nil, status.Errorf(codes.NotFound, "could not find pair: %s", req.Pair)
}
if !q.k.VpoolKeeper.ExistsPool(ctx, pairMetadata.Pair) {
return nil, status.Errorf(codes.NotFound, "could not find pair: %s", req.Pair)
}
indexTWAP, err := q.k.OracleKeeper.GetExchangeRateTwap(ctx, pairMetadata.Pair)
if err != nil {
return nil, status.Errorf(codes.NotFound, "failed to fetch twap index price for pair: %s", req.Pair)
}
if indexTWAP.IsZero() {
return nil, status.Errorf(codes.FailedPrecondition, "twap index price for pair: %s is zero", req.Pair)
}
markTwap, err := q.k.VpoolKeeper.GetMarkPriceTWAP(ctx, pairMetadata.Pair, q.k.GetParams(ctx).TwapLookbackWindow)
if err != nil {
return nil, status.Errorf(codes.NotFound, "failed to fetch twap mark price for pair: %s", req.Pair)
}
if markTwap.IsZero() {
return nil, status.Errorf(codes.FailedPrecondition, "twap mark price for pair: %s is zero", req.Pair)
}
epochInfo := q.k.EpochKeeper.GetEpochInfo(ctx, q.k.GetParams(ctx).FundingRateInterval)
intervalsPerDay := (24 * time.Hour) / epochInfo.Duration
premiumFraction := markTwap.Sub(indexTWAP).QuoInt64(int64(intervalsPerDay))
return &types.QueryCumulativePremiumFractionResponse{
CumulativePremiumFraction: pairMetadata.LatestCumulativePremiumFraction,
EstimatedNextCumulativePremiumFraction: pairMetadata.LatestCumulativePremiumFraction.Add(premiumFraction),
}, nil
}
func (q queryServer) Metrics(
goCtx context.Context, req *types.QueryMetricsRequest,
) (*types.QueryMetricsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
if !q.k.VpoolKeeper.ExistsPool(ctx, req.Pair) {
return nil, status.Errorf(codes.InvalidArgument, "pool not found: %s", req.Pair)
}
metrics := q.k.Metrics.GetOr(ctx, req.Pair, types.Metrics{
Pair: req.Pair,
NetSize: sdk.NewDec(0),
VolumeQuote: sdk.NewDec(0),
VolumeBase: sdk.NewDec(0),
})
return &types.QueryMetricsResponse{Metrics: metrics}, nil
}