-
Notifications
You must be signed in to change notification settings - Fork 111
/
querier.go
78 lines (64 loc) · 1.87 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
package keeper
import (
"encoding/json"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/axelarnetwork/axelar-core/x/snapshot/types"
)
// Query labels
const (
QProxy = "proxy"
QOperator = "operator"
)
// NewQuerier returns a new querier for the evm module
func NewQuerier(k Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
switch path[0] {
case QProxy:
return queryProxy(ctx, k, path[1])
case QOperator:
return queryOperator(ctx, k, path[1])
default:
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, fmt.Sprintf("unknown snapshot query endpoint: %s", path[0]))
}
}
}
func queryProxy(ctx sdk.Context, k Keeper, address string) ([]byte, error) {
addr, err := sdk.ValAddressFromBech32(address)
if err != nil {
return nil, sdkerrors.Wrap(types.ErrSnapshot, "address invalid")
}
proxy, active := k.GetProxy(ctx, addr)
if proxy == nil {
return nil, sdkerrors.Wrap(types.ErrSnapshot, "no proxy set for operator address")
}
statusStr := "inactive"
if active {
statusStr = "active"
}
reply := struct {
Address string `json:"address"`
Status string `json:"status"`
}{
Address: proxy.String(),
Status: statusStr,
}
bz, err := json.Marshal(reply)
if err != nil {
return nil, sdkerrors.Wrap(types.ErrSnapshot, err.Error())
}
return bz, nil
}
func queryOperator(ctx sdk.Context, k Keeper, proxy string) ([]byte, error) {
proxyAddr, err := sdk.AccAddressFromBech32(proxy)
if err != nil {
return nil, sdkerrors.Wrap(types.ErrSnapshot, "invalid proxy address")
}
operator := k.GetOperator(ctx, proxyAddr)
if operator == nil {
return nil, sdkerrors.Wrap(types.ErrSnapshot, "no operator associated to the proxy address")
}
return []byte(operator.String()), nil
}