-
Notifications
You must be signed in to change notification settings - Fork 805
/
grpc_query.go
148 lines (136 loc) · 4.73 KB
/
grpc_query.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
package keeper
import (
"context"
"errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
"github.com/sei-protocol/sei-chain/x/evm/artifacts/cw20"
"github.com/sei-protocol/sei-chain/x/evm/artifacts/cw721"
"github.com/sei-protocol/sei-chain/x/evm/artifacts/erc20"
"github.com/sei-protocol/sei-chain/x/evm/artifacts/erc721"
"github.com/sei-protocol/sei-chain/x/evm/artifacts/native"
"github.com/sei-protocol/sei-chain/x/evm/types"
)
var _ types.QueryServer = Querier{}
// Querier defines a wrapper around the x/mint keeper providing gRPC method
// handlers.
type Querier struct {
*Keeper
}
func NewQuerier(k *Keeper) Querier {
return Querier{Keeper: k}
}
func (q Querier) SeiAddressByEVMAddress(c context.Context, req *types.QuerySeiAddressByEVMAddressRequest) (*types.QuerySeiAddressByEVMAddressResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
if req.EvmAddress == "" {
return nil, sdkerrors.ErrInvalidRequest
}
evmAddr := common.HexToAddress(req.EvmAddress)
addr, found := q.Keeper.GetSeiAddress(ctx, evmAddr)
if !found {
return &types.QuerySeiAddressByEVMAddressResponse{Associated: false}, nil
}
return &types.QuerySeiAddressByEVMAddressResponse{SeiAddress: addr.String(), Associated: true}, nil
}
func (q Querier) EVMAddressBySeiAddress(c context.Context, req *types.QueryEVMAddressBySeiAddressRequest) (*types.QueryEVMAddressBySeiAddressResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
if req.SeiAddress == "" {
return nil, sdkerrors.ErrInvalidRequest
}
seiAddr, err := sdk.AccAddressFromBech32(req.SeiAddress)
if err != nil {
return nil, err
}
addr, found := q.Keeper.GetEVMAddress(ctx, seiAddr)
if !found {
return &types.QueryEVMAddressBySeiAddressResponse{Associated: false}, nil
}
return &types.QueryEVMAddressBySeiAddressResponse{EvmAddress: addr.Hex(), Associated: true}, nil
}
func (q Querier) StaticCall(c context.Context, req *types.QueryStaticCallRequest) (*types.QueryStaticCallResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
if req.To == "" {
return nil, errors.New("cannot use static call to create contracts")
}
if ctx.GasMeter().Limit() == 0 {
ctx = ctx.WithGasMeter(sdk.NewGasMeterWithMultiplier(ctx, q.QueryConfig.GasLimit))
}
to := common.HexToAddress(req.To)
res, err := q.Keeper.StaticCallEVM(ctx, q.Keeper.AccountKeeper().GetModuleAddress(types.ModuleName), &to, req.Data)
if err != nil {
return nil, err
}
return &types.QueryStaticCallResponse{Data: res}, nil
}
func (q Querier) Pointer(c context.Context, req *types.QueryPointerRequest) (*types.QueryPointerResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
switch req.PointerType {
case types.PointerType_NATIVE:
p, v, e := q.Keeper.GetERC20NativePointer(ctx, req.Pointee)
return &types.QueryPointerResponse{
Pointer: p.Hex(),
Version: uint32(v),
Exists: e,
}, nil
case types.PointerType_CW20:
p, v, e := q.Keeper.GetERC20CW20Pointer(ctx, req.Pointee)
return &types.QueryPointerResponse{
Pointer: p.Hex(),
Version: uint32(v),
Exists: e,
}, nil
case types.PointerType_CW721:
p, v, e := q.Keeper.GetERC721CW721Pointer(ctx, req.Pointee)
return &types.QueryPointerResponse{
Pointer: p.Hex(),
Version: uint32(v),
Exists: e,
}, nil
case types.PointerType_ERC20:
p, v, e := q.Keeper.GetCW20ERC20Pointer(ctx, common.HexToAddress(req.Pointee))
return &types.QueryPointerResponse{
Pointer: p.String(),
Version: uint32(v),
Exists: e,
}, nil
case types.PointerType_ERC721:
p, v, e := q.Keeper.GetCW721ERC721Pointer(ctx, common.HexToAddress(req.Pointee))
return &types.QueryPointerResponse{
Pointer: p.String(),
Version: uint32(v),
Exists: e,
}, nil
default:
return nil, errors.ErrUnsupported
}
}
func (q Querier) PointerVersion(c context.Context, req *types.QueryPointerVersionRequest) (*types.QueryPointerVersionResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
switch req.PointerType {
case types.PointerType_NATIVE:
return &types.QueryPointerVersionResponse{
Version: uint32(native.CurrentVersion),
}, nil
case types.PointerType_CW20:
return &types.QueryPointerVersionResponse{
Version: uint32(cw20.CurrentVersion(ctx)),
}, nil
case types.PointerType_CW721:
return &types.QueryPointerVersionResponse{
Version: uint32(cw721.CurrentVersion),
}, nil
case types.PointerType_ERC20:
return &types.QueryPointerVersionResponse{
Version: uint32(erc20.CurrentVersion),
CwCodeId: q.GetStoredPointerCodeID(ctx, types.PointerType_ERC20),
}, nil
case types.PointerType_ERC721:
return &types.QueryPointerVersionResponse{
Version: uint32(erc721.CurrentVersion),
CwCodeId: q.GetStoredPointerCodeID(ctx, types.PointerType_ERC721),
}, nil
default:
return nil, errors.ErrUnsupported
}
}