Skip to content

Commit 2c5597a

Browse files
authored
feat: add block timestamp to substrate events (#40)
1 parent 77e7770 commit 2c5597a

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

chains/substrate/connection/connection.go

+43
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
package connection
55

66
import (
7+
"bytes"
78
"math/big"
89
"sync"
10+
"time"
911

1012
"github.com/centrifuge/go-substrate-rpc-client/v4/client"
13+
"github.com/centrifuge/go-substrate-rpc-client/v4/registry"
1114
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/parser"
1215
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/retriever"
1316
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/state"
17+
"github.com/vedhavyas/go-subkey/scale"
1418

1519
"github.com/centrifuge/go-substrate-rpc-client/v4/rpc"
1620
"github.com/centrifuge/go-substrate-rpc-client/v4/rpc/chain"
@@ -85,9 +89,48 @@ func (c *Connection) GetBlockEvents(hash types.Hash) ([]*parser.Event, error) {
8589
if err != nil {
8690
return nil, err
8791
}
92+
93+
timestamp, err := c.GetBlockTimestamp(hash)
94+
if err != nil {
95+
return nil, err
96+
}
97+
98+
for _, e := range evts {
99+
e.Fields = append(e.Fields, &registry.DecodedField{
100+
Value: timestamp,
101+
Name: "block_timestamp",
102+
})
103+
}
88104
return evts, nil
89105
}
90106

107+
func (c *Connection) GetBlockTimestamp(hash types.Hash) (time.Time, error) {
108+
callIndex, err := c.meta.FindCallIndex("Timestamp.set")
109+
if err != nil {
110+
return time.Now(), err
111+
}
112+
113+
block, err := c.GetBlock(hash)
114+
if err != nil {
115+
return time.Now(), err
116+
}
117+
118+
timestamp := new(big.Int)
119+
for _, extrinsic := range block.Block.Extrinsics {
120+
if extrinsic.Method.CallIndex != callIndex {
121+
continue
122+
}
123+
timeDecoder := scale.NewDecoder(bytes.NewReader(extrinsic.Method.Args))
124+
timestamp, err = timeDecoder.DecodeUintCompact()
125+
if err != nil {
126+
return time.Now(), err
127+
}
128+
break
129+
}
130+
msec := timestamp.Int64()
131+
return time.Unix(msec/1e3, (msec%1e3)*1e6), nil
132+
}
133+
91134
func (c *Connection) FetchEvents(startBlock, endBlock *big.Int) ([]*parser.Event, error) {
92135
evts := make([]*parser.Event, 0)
93136
for i := new(big.Int).Set(startBlock); i.Cmp(endBlock) <= 0; i.Add(i, big.NewInt(1)) {

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/rs/zerolog v1.25.0
1111
github.com/stretchr/testify v1.8.3
1212
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a
13+
github.com/vedhavyas/go-subkey v1.0.4
1314
go.opentelemetry.io/otel v1.16.0
1415
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v0.39.0
1516
go.opentelemetry.io/otel/metric v1.16.0
@@ -45,7 +46,6 @@ require (
4546
github.com/rs/cors v1.8.2 // indirect
4647
github.com/stretchr/objx v0.5.0 // indirect
4748
github.com/supranational/blst v0.3.11 // indirect
48-
github.com/vedhavyas/go-subkey v1.0.4 // indirect
4949
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
5050
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.39.0 // indirect
5151
golang.org/x/crypto v0.12.0 // indirect

0 commit comments

Comments
 (0)