forked from c0mm4nd/tronetl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_token_transfers.go
245 lines (195 loc) · 6.89 KB
/
export_token_transfers.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"encoding/csv"
"io"
"log"
"sync"
"git.ngx.fi/c0mm4nd/tronetl/tron"
"github.com/jszwec/csvutil"
"golang.org/x/exp/slices"
)
type ExportTransferOptions struct {
// outputType string // failed to tfOutput to a conn with
tfOutput io.Writer
logOutput io.Writer
internalTxOutput io.Writer
receiptOutput io.Writer
Worker int
ProviderURI string `json:"provider_uri,omitempty"`
StartBlock uint64 `json:"start_block,omitempty"`
EndBlock uint64 `json:"end_block,omitempty"`
// extension
StartTimestamp string `json:"start_timestamp,omitempty"`
EndTimestamp string `json:"end_timestamp,omitempty"`
Contracts []string `json:"contracts,omitempty"`
}
// ExportTransfers is the main func for handling export_transfers command
func ExportTransfers(options *ExportTransferOptions) {
cli := tron.NewTronClient(options.ProviderURI)
var tfEncoder, logEncoder, internalTxEncoder, receiptEncoder *csvutil.Encoder
if options.tfOutput != nil {
tfWriter := csv.NewWriter(options.tfOutput)
defer tfWriter.Flush()
tfEncoder = csvutil.NewEncoder(tfWriter)
}
if options.logOutput != nil {
logWriter := csv.NewWriter(options.logOutput)
defer logWriter.Flush()
logEncoder = csvutil.NewEncoder(logWriter)
}
if options.internalTxOutput != nil {
internalTxWriter := csv.NewWriter(options.internalTxOutput)
defer internalTxWriter.Flush()
internalTxEncoder = csvutil.NewEncoder(internalTxWriter)
}
if options.receiptOutput != nil {
receiptWriter := csv.NewWriter(options.receiptOutput)
defer receiptWriter.Flush()
receiptEncoder = csvutil.NewEncoder(receiptWriter)
}
filterLogContracts := make([]string, len(options.Contracts))
for i, addr := range options.Contracts {
filterLogContracts[i] = tron.EnsureHexAddr(addr)[2:] // hex addr with 41 prefix
}
if options.StartTimestamp != "" {
// fast locate estimate start height
number, err := BlockNumberFromDateTime(cli, options.StartTimestamp, FirstAfterTimestamp)
if err != nil {
panic(err)
}
options.StartBlock = *number
}
if options.EndTimestamp != "" {
number, err := BlockNumberFromDateTime(cli, options.EndTimestamp, LastBeforeTimestamp)
if err != nil {
panic(err)
}
options.EndBlock = *number
}
log.Printf("try parsing token transfers from block %d to %d", options.StartBlock, options.EndBlock)
if options.StartBlock != 0 && options.EndBlock != 0 {
for number := options.StartBlock; number <= options.EndBlock; number++ {
txInfos := cli.GetTxInfosByNumber(number)
for txIndex, txInfo := range txInfos {
txHash := txInfo.ID
if receiptEncoder != nil {
receiptEncoder.Encode(NewCsvReceipt(number, txHash, uint(txIndex), txInfo.ContractAddress, txInfo.Fee, txInfo.Receipt))
}
for logIndex, log := range txInfo.Log {
if len(filterLogContracts) != 0 && !slices.Contains(filterLogContracts, log.Address) {
continue
}
if tfEncoder != nil {
tf := ExtractTransferFromLog(log.Topics, log.Data, log.Address, uint(logIndex), txHash, number, 0000)
if tf != nil {
err := tfEncoder.Encode(tf)
chk(err)
}
}
if logEncoder != nil {
err := logEncoder.Encode(NewCsvLog(number, txHash, uint(logIndex), log))
chk(err)
}
}
if internalTxEncoder != nil {
for internalIndex, internalTx := range txInfo.InternalTransactions {
for callInfoIndex, callInfo := range internalTx.CallValueInfo {
err := internalTxEncoder.Encode(NewCsvInternalTx(number, txHash, uint(internalIndex), internalTx, uint(callInfoIndex), callInfo.TokenID, callInfo.CallValue, 0000))
chk(err)
}
}
}
}
log.Printf("parsed block %d", number)
}
return
}
}
// ExportTransfers is the main func for handling export_transfers command
func ExportTransfersWithWorkers(options *ExportTransferOptions, workers uint) {
cli := tron.NewTronClient(options.ProviderURI)
var tfEncCh, logEncCh, internalTxEncCh, receiptEncCh chan any
var receiverWG sync.WaitGroup
if options.tfOutput != nil {
tfWriter := csv.NewWriter(options.tfOutput)
defer tfWriter.Flush()
tfEncoder := csvutil.NewEncoder(tfWriter)
tfEncCh = createCSVEncodeCh(&receiverWG, tfEncoder, workers)
}
if options.logOutput != nil {
logWriter := csv.NewWriter(options.logOutput)
defer logWriter.Flush()
logEncoder := csvutil.NewEncoder(logWriter)
logEncCh = createCSVEncodeCh(&receiverWG, logEncoder, workers)
}
if options.internalTxOutput != nil {
internalTxWriter := csv.NewWriter(options.internalTxOutput)
defer internalTxWriter.Flush()
internalTxEncoder := csvutil.NewEncoder(internalTxWriter)
internalTxEncCh = createCSVEncodeCh(&receiverWG, internalTxEncoder, workers)
}
if options.receiptOutput != nil {
receiptWriter := csv.NewWriter(options.receiptOutput)
defer receiptWriter.Flush()
receiptEncoder := csvutil.NewEncoder(receiptWriter)
receiptEncCh = createCSVEncodeCh(&receiverWG, receiptEncoder, workers)
}
filterLogContracts := make([]string, len(options.Contracts))
for i, addr := range options.Contracts {
filterLogContracts[i] = tron.EnsureHexAddr(addr)[2:] // hex addr with 41 prefix
}
log.Printf("try parsing token transfers from block %d to %d", options.StartBlock, options.EndBlock)
exportWork := func(wg *sync.WaitGroup, workerID uint) {
for number := options.StartBlock + uint64(workerID); number <= options.EndBlock; number += uint64(workers) {
txInfos := cli.GetTxInfosByNumber(number)
for txIndex, txInfo := range txInfos {
txHash := txInfo.ID
if options.receiptOutput != nil {
receiptEncCh <- NewCsvReceipt(number, txHash, uint(txIndex), txInfo.ContractAddress, txInfo.Fee, txInfo.Receipt)
}
for logIndex, log := range txInfo.Log {
if len(filterLogContracts) != 0 && !slices.Contains(filterLogContracts, log.Address) {
continue
}
if options.tfOutput != nil {
tf := ExtractTransferFromLog(log.Topics, log.Data, log.Address, uint(logIndex), txHash, number, 0000)
if tf != nil {
tfEncCh <- tf
}
}
if options.logOutput != nil {
logEncCh <- NewCsvLog(number, txHash, uint(logIndex), log)
}
}
if options.internalTxOutput != nil {
for internalIndex, internalTx := range txInfo.InternalTransactions {
for callInfoIndex, callInfo := range internalTx.CallValueInfo {
internalTxEncCh <- NewCsvInternalTx(number, txHash, uint(internalIndex), internalTx, uint(callInfoIndex), callInfo.TokenID, callInfo.CallValue, 0000)
}
}
}
}
log.Printf("parsed block %d", number)
}
wg.Done()
}
var senderWG sync.WaitGroup
for workerID := uint(0); workerID < workers; workerID++ {
senderWG.Add(1)
go exportWork(&senderWG, workerID)
}
senderWG.Wait()
if options.tfOutput != nil {
close(tfEncCh)
}
if options.logOutput != nil {
close(logEncCh)
}
if options.internalTxOutput != nil {
close(internalTxEncCh)
}
if options.receiptOutput != nil {
close(receiptEncCh)
}
receiverWG.Wait()
}