Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add block timestamp to substrate events #40

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading