/
connection.go
104 lines (82 loc) · 2.11 KB
/
connection.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
// Copyright 2020 Snowfork
// SPDX-License-Identifier: LGPL-3.0-only
package parachain
import (
"context"
"github.com/sirupsen/logrus"
gsrpc "github.com/snowfork/go-substrate-rpc-client/v4"
"github.com/snowfork/go-substrate-rpc-client/v4/signature"
"github.com/snowfork/go-substrate-rpc-client/v4/types"
log "github.com/sirupsen/logrus"
)
type Connection struct {
endpoint string
kp *signature.KeyringPair
api *gsrpc.SubstrateAPI
metadata types.Metadata
genesisHash types.Hash
}
func (co *Connection) API() *gsrpc.SubstrateAPI {
return co.api
}
func (co *Connection) Metadata() *types.Metadata {
return &co.metadata
}
func (co *Connection) Keypair() *signature.KeyringPair {
return co.kp
}
func NewConnection(endpoint string, kp *signature.KeyringPair) *Connection {
return &Connection{
endpoint: endpoint,
kp: kp,
}
}
func (co *Connection) Connect(_ context.Context) error {
// Initialize API
api, err := gsrpc.NewSubstrateAPI(co.endpoint)
if err != nil {
return err
}
co.api = api
// Fetch metadata
meta, err := api.RPC.State.GetMetadataLatest()
if err != nil {
return err
}
co.metadata = *meta
// Fetch genesis hash
genesisHash, err := api.RPC.Chain.GetBlockHash(0)
if err != nil {
return err
}
co.genesisHash = genesisHash
log.WithFields(logrus.Fields{
"endpoint": co.endpoint,
"metaVersion": meta.Version,
}).Info("Connected to chain")
return nil
}
func (co *Connection) Close() {
// TODO: Fix design issue in GSRPC preventing on-demand closing of connections
}
func (co *Connection) GenesisHash() types.Hash {
return co.genesisHash
}
func (co *Connection) GetFinalizedHeader() (*types.Header, error) {
finalizedHash, err := co.api.RPC.Chain.GetFinalizedHead()
if err != nil {
return nil, err
}
finalizedHeader, err := co.api.RPC.Chain.GetHeader(finalizedHash)
if err != nil {
return nil, err
}
return finalizedHeader, nil
}
func (co *Connection) GetLatestBlockNumber() (*types.BlockNumber, error) {
latestBlock, err := co.api.RPC.Chain.GetBlockLatest()
if err != nil {
return nil, err
}
return &latestBlock.Block.Header.Number, nil
}