/
querier.go
122 lines (95 loc) · 3.22 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
package keeper
import (
"github.com/ayher/anatha/codec"
sdk "github.com/ayher/anatha/types"
sdkerrors "github.com/ayher/anatha/types/errors"
"github.com/ayher/project-anatha/config"
"github.com/ayher/project-anatha/x/distribution/internal/types"
abci "github.com/tendermint/tendermint/abci/types"
)
const (
QueryParameters = "parameters"
QueryNameReward = "name-reward"
QueryValidatorReward = "validator-reward"
QuerySavingsReward = "savings-reward"
QuerySavings = "savings"
)
func NewQuerier(k Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
switch path[0] {
case QueryParameters:
return queryParams(ctx, k)
case QueryNameReward:
return queryNameReward(ctx, path[1:], req, k)
case QueryValidatorReward:
return queryValidatorReward(ctx, path[1:], req, k)
case QuerySavingsReward:
return querySavingsReward(ctx,path[1:], req, k)
case QuerySavings:
return querySavings(ctx, path[1:], req, k)
default:
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown distribution query endpoint")
}
}
}
func queryParams(ctx sdk.Context, k Keeper) ([]byte, error) {
params := k.GetParams(ctx)
res, err := codec.MarshalJSONIndent(types.ModuleCdc, params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
func queryNameReward(ctx sdk.Context, path []string, req abci.RequestQuery, k Keeper) ([]byte, error) {
address, err := sdk.AccAddressFromBech32(path[0])
if err != nil {
return nil, err
}
reward, _ := k.calculateNameReward(ctx, address)
escrow := k.GetNameRewardEscrow(ctx, address)
reward = reward.Add(escrow...)
res, err := codec.MarshalJSONIndent(types.ModuleCdc, reward)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
func querySavingsReward(ctx sdk.Context, path []string, req abci.RequestQuery, k Keeper) ([]byte, error) {
address, err := sdk.AccAddressFromBech32(path[0])
if err != nil {
return nil, err
}
reward, _ := k.calculateSavingsReward(ctx, address)
escrow := k.GetSavingsRewardEscrow(ctx, address)
reward = reward.Add(escrow...)
res, err := codec.MarshalJSONIndent(types.ModuleCdc, reward)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
func querySavings(ctx sdk.Context, path []string, req abci.RequestQuery, k Keeper) ([]byte, error) {
address, err := sdk.AccAddressFromBech32(path[0])
if err != nil {
return nil, err
}
savings, _ := k.GetSavingsStakeByAddress(ctx, address)
coins := sdk.NewCoins(sdk.NewCoin(config.DefaultDenom, savings))
res, err := codec.MarshalJSONIndent(types.ModuleCdc, coins)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
func queryValidatorReward(ctx sdk.Context, path []string, req abci.RequestQuery, k Keeper) ([]byte, error) {
address, err := sdk.ValAddressFromBech32(path[0])
if err != nil {
return nil, err
}
reward := k.GetValidatorAccumulatedRewards(ctx, address)
res, err := codec.MarshalJSONIndent(types.ModuleCdc, reward)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}