-
Notifications
You must be signed in to change notification settings - Fork 0
/
querier.go
123 lines (108 loc) · 4.07 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
package keeper
import (
"encoding/json"
"fmt"
ethcmm "github.com/ethereum/go-ethereum/common"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/codec"
sdk "github.com/furyaxyz/fuxchain/libs/cosmos-sdk/types"
sdkerrors "github.com/furyaxyz/fuxchain/libs/cosmos-sdk/types/errors"
transfertypes "github.com/furyaxyz/fuxchain/libs/ibc-go/modules/apps/transfer/types"
abci "github.com/furyaxyz/fuxchain/libs/tendermint/abci/types"
"github.com/furyaxyz/fuxchain/x/common"
"github.com/furyaxyz/fuxchain/x/erc20/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) ([]byte, error) {
if len(path) < 1 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 1 parameter is required")
}
switch path[0] {
case types.QueryParameters:
return queryParams(ctx, keeper)
case types.QueryTokenMapping:
return queryTokenMapping(ctx, keeper)
case types.QueryDenomByContract:
return queryDenomByContract(ctx, req, keeper)
case types.QueryContractByDenom:
return queryContractByDenom(ctx, req, keeper)
case types.QueryContractTem:
return queryContractTemplate(ctx, req, keeper)
default:
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown query endpoint")
}
}
}
func queryParams(ctx sdk.Context, keeper Keeper) (res []byte, err sdk.Error) {
params := keeper.GetParams(ctx)
res, errUnmarshal := codec.MarshalJSONIndent(types.ModuleCdc, params)
if errUnmarshal != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to marshal result to JSON", errUnmarshal.Error()))
}
return res, nil
}
func queryTokenMapping(ctx sdk.Context, keeper Keeper) ([]byte, error) {
var mappings []types.QueryTokenMappingResponse
keeper.IterateMapping(ctx, func(denom, contract string) bool {
mapping := types.QueryTokenMappingResponse{
Denom: denom,
Contract: contract,
}
if types.IsValidIBCDenom(denom) {
hexHash := denom[len(transfertypes.DenomPrefix+"/"):]
hash, err := transfertypes.ParseHexHash(hexHash)
if err == nil {
denomTrace, found := keeper.transferKeeper.GetDenomTrace(ctx, hash)
if found {
mapping.Path = denomTrace.Path
mapping.BaseDenom = denomTrace.BaseDenom
}
}
}
mappings = append(mappings, mapping)
return false
})
res, errUnmarshal := codec.MarshalJSONIndent(types.ModuleCdc, mappings)
if errUnmarshal != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to marshal result to JSON", errUnmarshal.Error()))
}
return res, nil
}
func queryDenomByContract(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
var params types.DenomByContractRequest
err := keeper.cdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, common.ErrUnMarshalJSONFailed(sdk.AppendMsgToErr("incorrectly formatted request data", err.Error()))
}
denom, found := keeper.GetDenomByContract(ctx, ethcmm.HexToAddress(params.Contract))
if !found {
return nil, fmt.Errorf("coin denom for contract %s is not found", params.Contract)
}
return []byte(denom), nil
}
func queryContractByDenom(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
var params types.ContractByDenomRequest
err := keeper.cdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, common.ErrUnMarshalJSONFailed(sdk.AppendMsgToErr("incorrectly formatted request data", err.Error()))
}
contract, found := keeper.GetContractByDenom(ctx, params.Denom)
if !found {
return nil, fmt.Errorf("contract for the coin denom %s is not found", params.Denom)
}
return []byte(contract.String()), nil
}
func queryContractTemplate(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
ret := types.ContractTemplate{}
proxy, found := keeper.GetProxyTemplateContract(ctx)
if found {
ret.Proxy = string(types.MustMarshalCompileContract(proxy))
}
imple, found := keeper.GetImplementTemplateContract(ctx)
if found {
ret.Implement = string(types.MustMarshalCompileContract(imple))
}
data, _ := json.Marshal(ret)
return data, nil
}