-
Notifications
You must be signed in to change notification settings - Fork 1
/
querier.go
executable file
·157 lines (128 loc) · 5.51 KB
/
querier.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"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/fibonacci-chain/core/x/oracle/types"
)
// querier is used as Keeper will have duplicate methods if used directly, and gRPC names take precedence over q
type querier struct {
Keeper
}
// NewQuerier returns an implementation of the oracle QueryServer interface
// for the provided Keeper.
func NewQuerier(keeper Keeper) types.QueryServer {
return &querier{Keeper: keeper}
}
var _ types.QueryServer = querier{}
// Params queries params of distribution module
func (q querier) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
var params types.Params
q.paramSpace.GetParamSet(ctx, ¶ms)
return &types.QueryParamsResponse{Params: params}, nil
}
// ExchangeRate queries exchange rate of a denom
func (q querier) ExchangeRate(c context.Context, req *types.QueryExchangeRateRequest) (*types.QueryExchangeRateResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
if len(req.Denom) == 0 {
return nil, status.Error(codes.InvalidArgument, "empty denom")
}
ctx := sdk.UnwrapSDKContext(c)
exchangeRate, lastUpdate, err := q.GetBaseExchangeRate(ctx, req.Denom)
if err != nil {
return nil, err
}
return &types.QueryExchangeRateResponse{OracleExchangeRate: types.OracleExchangeRate{ExchangeRate: exchangeRate, LastUpdate: lastUpdate}}, nil
}
// ExchangeRates queries exchange rates of all denoms
func (q querier) ExchangeRates(c context.Context, req *types.QueryExchangeRatesRequest) (*types.QueryExchangeRatesResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
exchangeRates := []types.DenomOracleExchangeRatePair{}
q.IterateBaseExchangeRates(ctx, func(denom string, rate types.OracleExchangeRate) (stop bool) {
exchangeRates = append(exchangeRates, types.DenomOracleExchangeRatePair{Denom: denom, OracleExchangeRate: rate})
return false
})
return &types.QueryExchangeRatesResponse{DenomOracleExchangeRatePairs: exchangeRates}, nil
}
// Actives queries all denoms for which exchange rates exist
func (q querier) Actives(c context.Context, req *types.QueryActivesRequest) (*types.QueryActivesResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
denoms := []string{}
q.IterateBaseExchangeRates(ctx, func(denom string, rate types.OracleExchangeRate) (stop bool) {
denoms = append(denoms, denom)
return false
})
return &types.QueryActivesResponse{Actives: denoms}, nil
}
// VoteTargets queries the voting target list on current vote period
func (q querier) VoteTargets(c context.Context, req *types.QueryVoteTargetsRequest) (*types.QueryVoteTargetsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
return &types.QueryVoteTargetsResponse{VoteTargets: q.GetVoteTargets(ctx)}, nil
}
func (q querier) PriceSnapshotHistory(c context.Context, req *types.QueryPriceSnapshotHistoryRequest) (*types.QueryPriceSnapshotHistoryResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
priceSnapshots := types.PriceSnapshots{}
q.IteratePriceSnapshots(ctx, func(snapshot types.PriceSnapshot) (stop bool) {
priceSnapshots = append(priceSnapshots, snapshot)
return false
})
response := types.QueryPriceSnapshotHistoryResponse{PriceSnapshots: priceSnapshots}
return &response, nil
}
func (q querier) Twaps(c context.Context, req *types.QueryTwapsRequest) (*types.QueryTwapsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
twaps, err := q.CalculateTwaps(ctx, req.LookbackSeconds)
if err != nil {
return nil, err
}
response := types.QueryTwapsResponse{OracleTwaps: twaps}
return &response, nil
}
// FeederDelegation queries the account address that the validator operator delegated oracle vote rights to
func (q querier) FeederDelegation(c context.Context, req *types.QueryFeederDelegationRequest) (*types.QueryFeederDelegationResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
valAddr, err := sdk.ValAddressFromBech32(req.ValidatorAddr)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
ctx := sdk.UnwrapSDKContext(c)
return &types.QueryFeederDelegationResponse{
FeederAddr: q.GetFeederDelegation(ctx, valAddr).String(),
}, nil
}
// MissCounter queries oracle miss counter of a validator
func (q querier) VotePenaltyCounter(c context.Context, req *types.QueryVotePenaltyCounterRequest) (*types.QueryVotePenaltyCounterResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
valAddr, err := sdk.ValAddressFromBech32(req.ValidatorAddr)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
ctx := sdk.UnwrapSDKContext(c)
return &types.QueryVotePenaltyCounterResponse{
VotePenaltyCounter: &types.VotePenaltyCounter{
MissCount: q.GetMissCount(ctx, valAddr),
AbstainCount: q.GetAbstainCount(ctx, valAddr),
SuccessCount: q.GetSuccessCount(ctx, valAddr),
},
}, nil
}
func (q querier) SlashWindow(
goCtx context.Context,
req *types.QuerySlashWindowRequest,
) (*types.QuerySlashWindowResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
params := q.GetParams(ctx)
// The window progress is the number of vote periods that have been completed in the current slashing window. With a vote period of 1, this will be equivalent to the number of blocks that have progressed in the slash window.
return &types.QuerySlashWindowResponse{
WindowProgress: (uint64(ctx.BlockHeight()) % params.SlashWindow) /
params.VotePeriod,
}, nil
}