/
auth.go
88 lines (73 loc) · 2.1 KB
/
auth.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
package client
import (
"fmt"
"github.com/LampardNguyen234/astra-go-sdk/account"
"github.com/cosmos/cosmos-sdk/types/query"
authTypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/gogo/protobuf/grpc"
)
type AuthClient struct {
authTypes.QueryClient
}
// NewAuthClient creates a new client for interacting with the `auth` module.
func NewAuthClient(conn grpc.ClientConn) *AuthClient {
return &AuthClient{
QueryClient: authTypes.NewQueryClient(conn),
}
}
// AccountInfo returns a authTypes.AccountI for the given address.
func (c *CosmosClient) AccountInfo(addr string) (AccountInfoI, error) {
resp, err := c.auth.QueryClient.Account(c.ctx, &authTypes.QueryAccountRequest{Address: addr})
if err != nil || resp == nil {
return nil, err
}
if resp.Account == nil {
return nil, fmt.Errorf("no account found")
}
var ret authTypes.AccountI
err = c.Codec.UnpackAny(resp.Account, &ret)
if err != nil {
return nil, err
}
return ret, nil
}
// GetModuleAccount returns the account of the given module.
func (c *CosmosClient) GetModuleAccount(name string) (AccountInfoI, error) {
resp, err := c.auth.ModuleAccountByName(c.ctx, &authTypes.QueryModuleAccountByNameRequest{
Name: name,
})
if err != nil {
return nil, fmt.Errorf("failed to query module address of `%v`: %v", name, err)
}
var ret authTypes.AccountI
err = c.Codec.UnpackAny(resp.Account, &ret)
if err != nil {
return nil, err
}
return ret, nil
}
// AccountExists checks if an account exists given its address.
func (c *CosmosClient) AccountExists(addr string) error {
accAddr, err := account.NewCosmosAddressFromStr(addr)
if err != nil {
return err
}
err = c.BaseClient.AccountRetriever.EnsureExists(c.BaseClient.Context, accAddr)
if err != nil {
return err
}
return nil
}
// TotalAccounts returns the number of accounts on the blockchain.
func (c *CosmosClient) TotalAccounts() (uint64, error) {
resp, err := c.auth.Accounts(c.ctx, &authTypes.QueryAccountsRequest{
Pagination: &query.PageRequest{
Limit: 1,
CountTotal: true,
},
})
if err != nil {
return 0, err
}
return resp.Pagination.Total, nil
}