-
Notifications
You must be signed in to change notification settings - Fork 5
/
querier.go
executable file
·109 lines (86 loc) · 2.9 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
package multisig
import (
"strings"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
)
// query endpoints supported by the multisig Querier
const (
ListWallets = "listWallets"
GetWallet = "getWallet"
ListTransactions = "listTransactions"
GetTransaction = "getTransaction"
)
// NewQuerier is the module level router for state queries
func NewQuerier(keeper Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
switch path[0] {
case ListWallets:
return queryWallets(ctx, path[1:], req, keeper)
case GetWallet:
return getWallet(ctx, path[1:], req, keeper)
case ListTransactions:
return queryTransactions(ctx, path[1:], req, keeper)
case GetTransaction:
return getTransaction(ctx, path[1:], req, keeper)
default:
return nil, sdk.ErrUnknownRequest("unknown multisig query endpoint")
}
}
}
func queryWallets(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
var walletList QueryWallets
iterator := keeper.GetIterator(ctx)
for ; iterator.Valid(); iterator.Next() {
if strings.HasPrefix(string(iterator.Key()), "wallet-") {
address := strings.TrimPrefix(string(iterator.Key()), "wallet-")
wallet := keeper.GetWallet(ctx, address)
for _, pubkey := range wallet.PubKeys {
if pubkey == path[0] {
walletList = append(walletList, wallet)
break
}
}
}
}
res, err := codec.MarshalJSONIndent(keeper.cdc, walletList)
if err != nil {
panic("could not marshal result to JSON")
}
return res, nil
}
func getWallet(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
wallet := keeper.GetWallet(ctx, path[0])
res, err := codec.MarshalJSONIndent(keeper.cdc, wallet)
if err != nil {
panic("could not marshal result to JSON")
}
return res, nil
}
func queryTransactions(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
var transactionList QueryTransactions
iterator := keeper.GetIterator(ctx)
for ; iterator.Valid(); iterator.Next() {
if strings.HasPrefix(string(iterator.Key()), "transaction-") {
uidStr := strings.TrimPrefix(string(iterator.Key()), "transaction-")
transaction := keeper.GetTransaction(ctx, uidStr)
if transaction.From.String() == path[0] {
transactionList = append(transactionList, transaction)
}
}
}
res, err := codec.MarshalJSONIndent(keeper.cdc, transactionList)
if err != nil {
panic("could not marshal result to JSON")
}
return res, nil
}
func getTransaction(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
transaction := keeper.GetTransaction(ctx, path[0])
res, err := codec.MarshalJSONIndent(keeper.cdc, transaction)
if err != nil {
panic("could not marshal result to JSON")
}
return res, nil
}