-
Notifications
You must be signed in to change notification settings - Fork 1
/
adapter.go
106 lines (87 loc) · 3.06 KB
/
adapter.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
package keeper
import (
"context"
codectypes "github.com/FiboChain/fbc/libs/cosmos-sdk/codec/types"
"github.com/FiboChain/fbc/libs/cosmos-sdk/store/prefix"
sdk "github.com/FiboChain/fbc/libs/cosmos-sdk/types"
"github.com/FiboChain/fbc/libs/cosmos-sdk/types/query"
"github.com/FiboChain/fbc/libs/cosmos-sdk/x/auth/exported"
"github.com/FiboChain/fbc/libs/cosmos-sdk/x/auth/types"
internaltypes "github.com/FiboChain/fbc/libs/cosmos-sdk/x/auth/typesadapter"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var (
_ types.QueryServer = (*AccountKeeper)(nil)
)
func (ak AccountKeeper) Accounts(c context.Context, req *types.QueryAccountsRequest) (*types.QueryAccountsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
store := ctx.KVStore(ak.key)
accountsStore := prefix.NewStore(store, types.AddressStoreKeyPrefix)
var accounts []*codectypes.Any
pageRes, err := query.Paginate(accountsStore, req.Pagination, func(key, value []byte) error {
account := ak.decodeAccount(value)
ba := convEthAccountToBaseAccount(account)
any, err := codectypes.NewAnyWithValue(ba)
if err != nil {
return err
}
accounts = append(accounts, any)
return nil
})
if err != nil {
return nil, status.Errorf(codes.Internal, "paginate: %v", err)
}
return &types.QueryAccountsResponse{Accounts: accounts, Pagination: pageRes}, err
return nil, nil
}
func (ak AccountKeeper) Account(conte context.Context, req *types.QueryAccountRequest) (*types.QueryAccountResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
if req.Address == "" {
return nil, status.Error(codes.InvalidArgument, "Address cannot be empty")
}
ctx := sdk.UnwrapSDKContext(conte)
addr, err := sdk.AccAddressFromBech32(req.Address)
if err != nil {
return nil, err
}
account := ak.GetAccount(ctx, addr)
if account == nil {
return nil, status.Errorf(codes.NotFound, "account %s not found", req.Address)
}
//ethA:=account.(*ethermint.EthAccount)
ba := &internaltypes.BaseAccount{
Address: account.GetAddress().String(),
PubKey: nil,
AccountNumber: account.GetAccountNumber(),
Sequence: account.GetSequence(),
}
any, err := codectypes.NewAnyWithValue(ba)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
return &types.QueryAccountResponse{Account: any}, nil
}
// Params returns parameters of auth module
func (ak AccountKeeper) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
params := ak.GetParams(ctx)
return &types.QueryParamsResponse{Params: params}, nil
}
func convEthAccountToBaseAccount(account exported.Account) *internaltypes.BaseAccount {
ba := &internaltypes.BaseAccount{
Address: account.GetAddress().String(),
PubKey: nil,
AccountNumber: account.GetAccountNumber(),
Sequence: account.GetSequence(),
}
return ba
}