-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
executable file
·170 lines (137 loc) · 4.77 KB
/
client.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package scorumgo
import (
"context"
"encoding/hex"
"fmt"
"time"
"github.com/scorum/scorum-go/apis/account_history"
"github.com/scorum/scorum-go/apis/betting"
"github.com/scorum/scorum-go/apis/blockchain_history"
"github.com/scorum/scorum-go/apis/chain"
"github.com/scorum/scorum-go/apis/database"
"github.com/scorum/scorum-go/apis/network_broadcast"
"github.com/scorum/scorum-go/caller"
"github.com/scorum/scorum-go/key"
"github.com/scorum/scorum-go/sign"
"github.com/scorum/scorum-go/types"
)
// Client can be used to access Scorum remote APIs.
//
// There is a public field for every Scorum API available,
// e.g. Client.Database corresponds to database_api.
type Client struct {
cc caller.CallCloser
// Database represents database_api
Database *database.API
// AccountHistory represents account_history_api
AccountHistory *account_history.API
// NetworkBroadcast represents network_broadcast_api
NetworkBroadcast *network_broadcast.API
// BlockchainHistory represents blockchain_history_api
BlockchainHistory *blockchain_history.API
// Betting represents betting_api
Betting *betting.API
// Chain represents chain_api
Chain *chain.API
getReferenceBlock getReferenceBlock
}
type reference struct {
time types.Time
number uint16
prefix uint32
}
type getReferenceBlock func(ctx context.Context) (reference, error)
type Option func(c *Client)
func WithHeadBlockReferenceSign() Option {
return func(c *Client) {
c.getReferenceBlock = c.getHeadBlockReference
}
}
// NewClient creates a new RPC client that use the given CallCloser internally.
func NewClient(cc caller.CallCloser, opts ...Option) *Client {
client := &Client{
cc: cc,
}
client.Database = database.NewAPI(client.cc)
client.Chain = chain.NewAPI(client.cc)
client.AccountHistory = account_history.NewAPI(client.cc)
client.NetworkBroadcast = network_broadcast.NewAPI(client.cc)
client.BlockchainHistory = blockchain_history.NewAPI(client.cc)
client.Betting = betting.NewAPI(client.cc)
client.getReferenceBlock = client.getLastIrreversibleBlockReference
for _, opt := range opts {
opt(client)
}
return client
}
// Close should be used to close the client when no longer needed.
// It simply calls Close() on the underlying CallCloser.
func (client *Client) Close() error {
return client.cc.Close()
}
func (client *Client) BroadcastTransactionSynchronous(ctx context.Context, chainID []byte, operations []types.Operation, keys ...*key.PrivateKey) (*network_broadcast.BroadcastResponse, error) {
stx, err := client.createSignedTransaction(ctx, chainID, operations, keys...)
if err != nil {
return nil, err
}
return client.NetworkBroadcast.BroadcastTransactionSynchronous(ctx, stx.Transaction)
}
func (client *Client) BroadcastTransaction(ctx context.Context, chainID []byte, operations []types.Operation, keys ...*key.PrivateKey) (string, error) {
stx, err := client.createSignedTransaction(ctx, chainID, operations, keys...)
if err != nil {
return "", err
}
id, _ := stx.ID()
return hex.EncodeToString(id), client.NetworkBroadcast.BroadcastTransaction(ctx, stx.Transaction)
}
func (client *Client) createSignedTransaction(ctx context.Context, chainID []byte, operations []types.Operation, keys ...*key.PrivateKey) (*sign.SignedTransaction, error) {
refBlock, err := client.getReferenceBlock(ctx)
if err != nil {
return nil, err
}
expiration := refBlock.time.Add(10 * time.Minute)
stx := sign.NewSignedTransaction(&types.Transaction{
Operations: operations,
RefBlockNum: refBlock.number,
RefBlockPrefix: refBlock.prefix,
Expiration: &types.Time{Time: &expiration},
})
if err = stx.Sign(chainID, keys...); err != nil {
return nil, fmt.Errorf("sign transaction: %w", err)
}
return stx, nil
}
func (client *Client) getLastIrreversibleBlockReference(ctx context.Context) (reference, error) {
props, err := client.Chain.GetChainProperties(ctx)
if err != nil {
return reference{}, fmt.Errorf("get chainID properties: %w", err)
}
block, err := client.BlockchainHistory.GetBlock(ctx, props.LastIrreversibleBlockNumber)
if err != nil {
return reference{}, fmt.Errorf("blockchain history get block: %w", err)
}
number, prefix, err := sign.ParseBlockID(block.Previous)
if err != nil {
return reference{}, fmt.Errorf("ref block prefix: %w", err)
}
return reference{
time: props.Time,
number: number,
prefix: prefix,
}, nil
}
func (client *Client) getHeadBlockReference(ctx context.Context) (reference, error) {
props, err := client.Chain.GetChainProperties(ctx)
if err != nil {
return reference{}, fmt.Errorf("get chainID properties: %w", err)
}
number, prefix, err := sign.ParseBlockID(props.HeadBlockID)
if err != nil {
return reference{}, fmt.Errorf("ref block prefix: %w", err)
}
return reference{
time: props.Time,
number: number,
prefix: prefix,
}, nil
}