-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.go
63 lines (60 loc) · 1.81 KB
/
db.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
package main
import (
"context"
"encoding/json"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
)
func storeLogs(logs []types.Log) {
// Iterate on all the logs found to create the mongo log
for _, log := range logs {
logJSON, err := log.MarshalJSON()
if err != nil {
logger.Errorf("error while marshaling log: %v", err)
continue
}
logJSONString := string(logJSON)
logger.Infof("currenty iterating: %s", logJSONString)
topics := []string{}
for _, topic := range log.Topics {
topics = append(topics, topic.String())
}
JSONlog := JSONLog{}
mongoLog := Log{}
err = json.Unmarshal(logJSON, &JSONlog)
if err != nil {
logger.Errorf("error while unmarshaling log: %v", err)
continue
}
mongoLog.Address = JSONlog.Address
mongoLog.Removed = JSONlog.Removed
mongoLog.BlockHash = JSONlog.BlockHash
mongoLog.Data = JSONlog.Data
mongoLog.Topics = JSONlog.Topics
logIndex, err := hexutil.DecodeBig(JSONlog.LogIndex)
if err != nil {
logger.Errorf("error while decoding log index: %v", err)
continue
}
mongoLog.LogIndex = int(logIndex.Int64())
transactionIndex, err := hexutil.DecodeBig(JSONlog.TransactionIndex)
if err != nil {
logger.Errorf("error while decoding transaction index: %v", err)
continue
}
mongoLog.TransactionIndex = int(transactionIndex.Int64())
blockNumber, err := hexutil.DecodeUint64(JSONlog.BlockNumber)
if err != nil {
logger.Errorf("error while decoding block number: %v", err)
continue
}
mongoLog.BlockNumber = int(blockNumber)
collection := mongoDatabase.Collection("logs")
logger.Infof("inserting log in the collection: %v", mongoLog)
_, err = collection.InsertOne(context.Background(), mongoLog)
if err != nil {
logger.Errorf("error while inserting log: %v", err)
continue
}
}
}