Skip to content

Commit

Permalink
feat: add block timestamp to substrate events (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 authored Oct 28, 2024
1 parent 77e7770 commit 2c5597a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
43 changes: 43 additions & 0 deletions chains/substrate/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
package connection

import (
"bytes"
"math/big"
"sync"
"time"

"github.com/centrifuge/go-substrate-rpc-client/v4/client"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/parser"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/retriever"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/state"
"github.com/vedhavyas/go-subkey/scale"

"github.com/centrifuge/go-substrate-rpc-client/v4/rpc"
"github.com/centrifuge/go-substrate-rpc-client/v4/rpc/chain"
Expand Down Expand Up @@ -85,9 +89,48 @@ func (c *Connection) GetBlockEvents(hash types.Hash) ([]*parser.Event, error) {
if err != nil {
return nil, err
}

timestamp, err := c.GetBlockTimestamp(hash)
if err != nil {
return nil, err
}

for _, e := range evts {
e.Fields = append(e.Fields, &registry.DecodedField{
Value: timestamp,
Name: "block_timestamp",
})
}
return evts, nil
}

func (c *Connection) GetBlockTimestamp(hash types.Hash) (time.Time, error) {
callIndex, err := c.meta.FindCallIndex("Timestamp.set")
if err != nil {
return time.Now(), err
}

block, err := c.GetBlock(hash)
if err != nil {
return time.Now(), err
}

timestamp := new(big.Int)
for _, extrinsic := range block.Block.Extrinsics {
if extrinsic.Method.CallIndex != callIndex {
continue
}
timeDecoder := scale.NewDecoder(bytes.NewReader(extrinsic.Method.Args))
timestamp, err = timeDecoder.DecodeUintCompact()
if err != nil {
return time.Now(), err
}
break
}
msec := timestamp.Int64()
return time.Unix(msec/1e3, (msec%1e3)*1e6), nil
}

func (c *Connection) FetchEvents(startBlock, endBlock *big.Int) ([]*parser.Event, error) {
evts := make([]*parser.Event, 0)
for i := new(big.Int).Set(startBlock); i.Cmp(endBlock) <= 0; i.Add(i, big.NewInt(1)) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/rs/zerolog v1.25.0
github.com/stretchr/testify v1.8.3
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a
github.com/vedhavyas/go-subkey v1.0.4
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v0.39.0
go.opentelemetry.io/otel/metric v1.16.0
Expand Down Expand Up @@ -45,7 +46,6 @@ require (
github.com/rs/cors v1.8.2 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/vedhavyas/go-subkey v1.0.4 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.39.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
Expand Down

0 comments on commit 2c5597a

Please sign in to comment.