forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
querier.go
57 lines (46 loc) · 1.46 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
package auth
import (
"fmt"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// query endpoints supported by the auth Querier
const (
QueryAccount = "account"
)
// creates a querier for auth REST endpoints
func NewQuerier(keeper AccountKeeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, sdk.Error) {
switch path[0] {
case QueryAccount:
return queryAccount(ctx, req, keeper)
default:
return nil, sdk.ErrUnknownRequest("unknown auth query endpoint")
}
}
}
// defines the params for query: "custom/acc/account"
type QueryAccountParams struct {
Address sdk.AccAddress
}
func NewQueryAccountParams(addr sdk.AccAddress) QueryAccountParams {
return QueryAccountParams{
Address: addr,
}
}
func queryAccount(ctx sdk.Context, req abci.RequestQuery, keeper AccountKeeper) ([]byte, sdk.Error) {
var params QueryAccountParams
if err := keeper.cdc.UnmarshalJSON(req.Data, ¶ms); err != nil {
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
}
account := keeper.GetAccount(ctx, params.Address)
if account == nil {
return nil, sdk.ErrUnknownAddress(fmt.Sprintf("account %s does not exist", params.Address))
}
bz, err := codec.MarshalJSONIndent(keeper.cdc, account)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
}
return bz, nil
}