-
Notifications
You must be signed in to change notification settings - Fork 0
/
querier.go
61 lines (50 loc) · 1.46 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
package account
import (
"context"
"fmt"
"time"
"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/app/encoding"
"github.com/celestiaorg/celestia-app/pkg/appconsts"
sdk "github.com/cosmos/cosmos-sdk/types"
auth "github.com/cosmos/cosmos-sdk/x/auth/types"
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
"google.golang.org/grpc"
)
type Querier struct {
conn *grpc.ClientConn
cdc encoding.Config
}
func NewQuerier(conn *grpc.ClientConn) *Querier {
return &Querier{
conn: conn,
cdc: encoding.MakeConfig(app.ModuleEncodingRegisters...),
}
}
func (q *Querier) GetAccount(ctx context.Context, address string) (auth.AccountI, error) {
client := auth.NewQueryClient(q.conn)
timeOutCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
resp, err := client.Account(timeOutCtx, &auth.QueryAccountRequest{Address: address})
if err != nil {
return nil, err
}
var acc auth.AccountI
err = q.cdc.InterfaceRegistry.UnpackAny(resp.Account, &acc)
if err != nil {
return nil, err
}
return acc, nil
}
func (q *Querier) GetBalance(ctx context.Context, address string) (uint64, error) {
_, err := sdk.AccAddressFromBech32(address)
if err != nil {
return 0, fmt.Errorf("invalid address: %w", err)
}
client := bank.NewQueryClient(q.conn)
resp, err := client.Balance(ctx, &bank.QueryBalanceRequest{Address: address, Denom: appconsts.BondDenom})
if err != nil {
return 0, err
}
return resp.Balance.Amount.Uint64(), nil
}