/
grpc_query.go
70 lines (61 loc) · 2.2 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
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/functionx/fx-core/v5/x/migrate/types"
)
var _ types.QueryServer = Keeper{}
func (k Keeper) MigrateRecord(ctx context.Context, req *types.QueryMigrateRecordRequest) (*types.QueryMigrateRecordResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.Address == "" {
return nil, status.Error(codes.InvalidArgument, "address cannot be empty")
}
var addr []byte
if common.IsHexAddress(req.Address) {
addr = common.HexToAddress(req.Address).Bytes()
} else {
if acc, err := sdk.AccAddressFromBech32(req.Address); err == nil {
addr = acc.Bytes()
}
}
if len(addr) == 0 {
return nil, status.Error(codes.InvalidArgument, "must be bech32 or hex address")
}
record, found := k.GetMigrateRecord(sdk.UnwrapSDKContext(ctx), addr)
return &types.QueryMigrateRecordResponse{MigrateRecord: record, Found: found}, nil
}
func (k Keeper) MigrateCheckAccount(goCtx context.Context, req *types.QueryMigrateCheckAccountRequest) (*types.QueryMigrateCheckAccountResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.From == "" || req.To == "" {
return nil, status.Error(codes.InvalidArgument, "address cannot be empty")
}
from, err := sdk.AccAddressFromBech32(req.From)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "form")
}
if !common.IsHexAddress(req.To) {
return nil, status.Error(codes.InvalidArgument, "not hex address")
}
to := common.HexToAddress(req.To)
ctx := sdk.UnwrapSDKContext(goCtx)
// check migrated
if k.HasMigrateRecord(ctx, from) {
return nil, status.Errorf(codes.AlreadyExists, "address %s has been migrated", req.From)
}
if k.HasMigrateRecord(ctx, to.Bytes()) {
return nil, status.Errorf(codes.AlreadyExists, "address %s has been migrated", req.To)
}
for _, m := range k.GetMigrateI() {
if err := m.Validate(ctx, k.cdc, from, to); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
}
return &types.QueryMigrateCheckAccountResponse{}, nil
}