|
4 | 4 | package connection
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "bytes" |
7 | 8 | "math/big"
|
8 | 9 | "sync"
|
| 10 | + "time" |
9 | 11 |
|
10 | 12 | "github.com/centrifuge/go-substrate-rpc-client/v4/client"
|
| 13 | + "github.com/centrifuge/go-substrate-rpc-client/v4/registry" |
11 | 14 | "github.com/centrifuge/go-substrate-rpc-client/v4/registry/parser"
|
12 | 15 | "github.com/centrifuge/go-substrate-rpc-client/v4/registry/retriever"
|
13 | 16 | "github.com/centrifuge/go-substrate-rpc-client/v4/registry/state"
|
| 17 | + "github.com/vedhavyas/go-subkey/scale" |
14 | 18 |
|
15 | 19 | "github.com/centrifuge/go-substrate-rpc-client/v4/rpc"
|
16 | 20 | "github.com/centrifuge/go-substrate-rpc-client/v4/rpc/chain"
|
@@ -85,9 +89,48 @@ func (c *Connection) GetBlockEvents(hash types.Hash) ([]*parser.Event, error) {
|
85 | 89 | if err != nil {
|
86 | 90 | return nil, err
|
87 | 91 | }
|
| 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, ®istry.DecodedField{ |
| 100 | + Value: timestamp, |
| 101 | + Name: "block_timestamp", |
| 102 | + }) |
| 103 | + } |
88 | 104 | return evts, nil
|
89 | 105 | }
|
90 | 106 |
|
| 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 | + |
91 | 134 | func (c *Connection) FetchEvents(startBlock, endBlock *big.Int) ([]*parser.Event, error) {
|
92 | 135 | evts := make([]*parser.Event, 0)
|
93 | 136 | for i := new(big.Int).Set(startBlock); i.Cmp(endBlock) <= 0; i.Add(i, big.NewInt(1)) {
|
|
0 commit comments