-
Notifications
You must be signed in to change notification settings - Fork 182
/
querier.go
169 lines (150 loc) · 5.06 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
158
159
160
161
162
163
164
165
166
167
168
169
package token
import (
"fmt"
"github.com/okex/okexchain/x/token/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
)
// 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 types.QueryInfo:
return queryInfo(ctx, path[1:], req, keeper)
case types.QueryTokens:
return queryTokens(ctx, path[1:], req, keeper)
case types.QueryParameters:
return queryParameters(ctx, keeper)
case types.QueryCurrency:
return queryCurrency(ctx, path[1:], req, keeper)
case types.QueryAccount:
return queryAccount(ctx, path[1:], req, keeper)
case types.QueryKeysNum:
return queryKeysNum(ctx, keeper)
case types.QueryAccountV2:
return queryAccountV2(ctx, path[1:], req, keeper)
case types.QueryTokensV2:
return queryTokensV2(ctx, path[1:], req, keeper)
case types.QueryTokenV2:
return queryTokenV2(ctx, path[1:], req, keeper)
default:
return nil, sdk.ErrUnknownRequest("unknown token query endpoint")
}
}
}
// nolint: unparam
func queryInfo(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
name := path[0]
token := keeper.GetTokenInfo(ctx, name)
if token.Symbol == "" {
return nil, sdk.ErrInvalidCoins("unknown token")
}
tokenResp := types.GenTokenResp(token)
tokenResp.TotalSupply = keeper.GetTokenTotalSupply(ctx, name)
bz, err := codec.MarshalJSONIndent(keeper.cdc, tokenResp)
if err != nil {
panic("could not marshal result to JSON")
}
return bz, nil
}
func queryTokens(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
var tokens []types.Token
if len(path) > 0 && path[0] != "" {
ownerAddr, err := sdk.AccAddressFromBech32(path[0])
if err != nil {
return nil, sdk.ErrInvalidAddress(fmt.Sprintf("invalid address:%s", path[0]))
}
tokens = keeper.GetUserTokensInfo(ctx, ownerAddr)
} else {
tokens = keeper.GetTokensInfo(ctx)
}
var tokensResp types.Tokens
for _, token := range tokens {
tokenResp := types.GenTokenResp(token)
tokenResp.TotalSupply = keeper.GetTokenTotalSupply(ctx, token.Symbol)
tokensResp = append(tokensResp, tokenResp)
}
bz, err := codec.MarshalJSONIndent(keeper.cdc, tokensResp)
if err != nil {
panic("could not marshal result to JSON")
}
return bz, nil
}
func queryCurrency(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
tokens := keeper.GetCurrenciesInfo(ctx)
bz, err := codec.MarshalJSONIndent(keeper.cdc, tokens)
if err != nil {
panic("could not marshal result to JSON")
}
return bz, nil
}
func queryAccount(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
addr, err := sdk.AccAddressFromBech32(path[0])
if err != nil {
return nil, sdk.ErrInvalidAddress(fmt.Sprintf("invalid address:%s", path[0]))
}
//var queryPage QueryPage
var accountParam types.AccountParam
//var symbol string
err = codec.Cdc.UnmarshalJSON(req.Data, &accountParam)
if err != nil {
return nil, sdk.ErrUnknownRequest(err.Error())
}
coinsInfo := keeper.GetCoinsInfo(ctx, addr)
coinsInfoChoosen := make([]types.CoinInfo, 0)
if accountParam.Symbol == "" {
coinsInfoChoosen = coinsInfo
// show all or partial
if accountParam.Show == "all" {
tokens := keeper.GetTokensInfo(ctx)
for _, token := range tokens {
found := false
for _, coinInfo := range coinsInfo {
if coinInfo.Symbol == token.Symbol {
found = true
break
}
}
// not found
if !found {
ci := types.NewCoinInfo(token.Symbol, "0", "0")
coinsInfoChoosen = append(coinsInfoChoosen, *ci)
}
}
}
// page and pageSize
//coinsInfoChoosen = coinsInfoChoosen[Min((accountParam.Page-1)*accountParam.PerPage, len(coinsInfoChoosen)):Min(accountParam.Page*accountParam.PerPage, len(coinsInfoChoosen))]
} else {
for _, coinInfo := range coinsInfo {
if coinInfo.Symbol == accountParam.Symbol {
coinsInfoChoosen = append(coinsInfoChoosen, coinInfo)
}
}
}
accountResponse := types.NewAccountResponse(path[0])
accountResponse.Currencies = coinsInfoChoosen
bz, err := codec.MarshalJSONIndent(keeper.cdc, accountResponse)
if err != nil {
panic("could not marshal result to JSON")
}
return bz, nil
}
func queryParameters(ctx sdk.Context, keeper Keeper) ([]byte, sdk.Error) {
params := keeper.GetParams(ctx)
res, err := codec.MarshalJSONIndent(keeper.cdc, params)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
}
return res, nil
}
func queryKeysNum(ctx sdk.Context, keeper Keeper) ([]byte, sdk.Error) {
tokenStoreKeyNum, lockStoreKeyNum := keeper.getNumKeys(ctx)
res, err := codec.MarshalJSONIndent(keeper.cdc,
map[string]int64{"token": tokenStoreKeyNum,
"lock": lockStoreKeyNum})
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
}
return res, nil
}