-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
151 lines (125 loc) · 3.52 KB
/
client.go
File metadata and controls
151 lines (125 loc) · 3.52 KB
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
149
150
151
package client
import (
"context"
"fmt"
"github.com/holiman/uint256"
mmnpb "github.com/mezonai/mmn-sdk/go-sdk/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/encoding"
vtgrpc "github.com/planetscale/vtprotobuf/codec/grpc"
)
func init() {
encoding.RegisterCodec(vtgrpc.Codec{})
}
type Config struct {
Endpoint string
UseTLS bool
}
type MmnClient struct {
cfg Config
conn *grpc.ClientConn
healthClient mmnpb.HealthServiceClient
txClient mmnpb.TxServiceClient
accClient mmnpb.AccountServiceClient
}
func NewClient(cfg Config) (*MmnClient, error) {
var creds credentials.TransportCredentials
if cfg.UseTLS {
creds = credentials.NewTLS(nil)
} else {
creds = insecure.NewCredentials()
}
conn, err := grpc.NewClient(
cfg.Endpoint,
grpc.WithTransportCredentials(creds),
)
if err != nil {
return nil, err
}
return &MmnClient{
cfg: cfg,
conn: conn,
healthClient: mmnpb.NewHealthServiceClient(conn),
txClient: mmnpb.NewTxServiceClient(conn),
accClient: mmnpb.NewAccountServiceClient(conn),
}, nil
}
func (c *MmnClient) CheckHealth(ctx context.Context) (*mmnpb.HealthCheckResponse, error) {
health, err := c.healthClient.Check(ctx, &mmnpb.Empty{})
if err != nil {
return nil, err
}
return health, nil
}
func (c *MmnClient) AddTx(ctx context.Context, tx SignedTx) (AddTxResponse, error) {
txMsg := ToProtoSigTx(&tx)
res, err := c.txClient.AddTx(ctx, txMsg)
if err != nil {
return AddTxResponse{}, err
}
if !res.Ok {
return AddTxResponse{}, fmt.Errorf("add-tx failed: %s", res.Error)
}
return AddTxResponse{
Ok: res.Ok,
TxHash: res.TxHash,
Error: res.Error,
}, nil
}
func (c *MmnClient) GetAccount(ctx context.Context, addr string) (Account, error) {
res, err := c.accClient.GetAccount(ctx, &mmnpb.GetAccountRequest{Address: addr})
if err != nil {
return Account{Address: addr, Balance: uint256.NewInt(0), Nonce: 0}, err
}
return FromProtoAccount(res), nil
}
func (c *MmnClient) SubscribeTransactionStatus(ctx context.Context) (mmnpb.TxService_SubscribeTransactionStatusClient, error) {
stream, err := c.txClient.SubscribeTransactionStatus(ctx, &mmnpb.SubscribeTransactionStatusRequest{})
if err != nil {
fmt.Printf("SubscribeTransactionStatus error: %v", err)
return nil, err
}
return stream, nil
}
func (c *MmnClient) GetTxByHash(ctx context.Context, txHash string) (TxInfo, error) {
res, err := c.txClient.GetTxByHash(ctx, &mmnpb.GetTxByHashRequest{TxHash: txHash})
if err != nil {
return TxInfo{}, err
}
if res.Error != "" {
return TxInfo{}, fmt.Errorf("get-tx-by-hash failed: %s", res.Error)
}
return TxInfo{
Sender: res.Tx.Sender,
Recipient: res.Tx.Recipient,
Amount: Uint256FromString(res.Tx.Amount),
Timestamp: res.Tx.Timestamp,
TextData: res.Tx.TextData,
Nonce: res.Tx.Nonce,
Slot: res.Tx.Slot,
Blockhash: res.Tx.Blockhash,
Status: int32(res.Tx.Status),
ErrMsg: res.Tx.ErrMsg,
ExtraInfo: res.Tx.ExtraInfo,
TxHash: res.Tx.TxHash,
}, nil
}
func (c *MmnClient) GetCurrentNonce(ctx context.Context, addr string, tag string) (uint64, error) {
res, err := c.accClient.GetCurrentNonce(ctx, &mmnpb.GetCurrentNonceRequest{Address: addr, Tag: tag})
if err != nil {
return 0, err
}
return res.Nonce, nil
}
func (c *MmnClient) Conn() *grpc.ClientConn {
return c.conn
}
// Close closes the gRPC connection
func (c *MmnClient) Close() error {
if c.conn != nil {
return c.conn.Close()
}
return nil
}