-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexport.go
82 lines (62 loc) · 1.73 KB
/
export.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"bufio"
"fmt"
"os"
"time"
"ethereum-blockchain-transaction-csv-export/work"
"github.com/mgutz/logxi/v1"
)
func exportAsCSV(jobs chan *work.Job) {
now := time.Now().Format("2006-01-02-15-04-05")
f, err := os.Create(fmt.Sprintf("/output/geth_tx_export_%s.csv", now))
if err != nil {
log.Fatal("failed to open output file", "err", err.Error())
}
w := bufio.NewWriter(f)
w.WriteString("tx_hash,tx_nonce,tx_block_hash,tx_block_number,tx_index,tx_from,tx_to,tx_value,tx_gas,tx_gas_price,tx_input,tx_timestamp\n")
w.Flush()
lineBuf := 0
for job := range jobs {
tx := job.Tx
lineBuf++
line := fmt.Sprintf(
"%s,%d,%s,%d,%d,%s,%s,%s,%d,%s,%s,%d\n",
tx.Hash, tx.Nonce, tx.BlockHash, *tx.BlockNumber, *tx.TransactionIndex, tx.From, tx.To,
tx.Value.String(), tx.Gas, tx.GasPrice.String(), tx.Input, job.Timestamp,
)
w.WriteString(line)
if lineBuf > 50 {
lineBuf = 0
w.Flush()
}
}
log.Info("finished writing to file", "file", f.Name())
w.Flush()
}
func exportFailedBlockJobs(jobs chan *work.Job) {
f, err := os.Create("/output/failedBlocks.txt")
if err != nil {
log.Fatal("failed to create block error file", "err", err.Error())
}
w := bufio.NewWriter(f)
for job := range jobs {
w.WriteString(fmt.Sprintf("%d\n", job.BlockHeight))
w.Flush()
}
log.Info("finished writing to block error file")
w.Flush()
}
func exportFailedTxJobs(jobs chan *work.Job) {
f, err := os.Create("/output/failedTransactions.txt")
if err != nil {
log.Fatal("failed to create transaction error file", "err", err.Error())
}
w := bufio.NewWriter(f)
for job := range jobs {
w.WriteString(fmt.Sprintf("%s\n", job.TxHash))
w.Flush()
}
log.Info("finished writing to transaction error file")
w.Flush()
}