-
Notifications
You must be signed in to change notification settings - Fork 2
/
csv.go
512 lines (448 loc) · 16 KB
/
csv.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
package main
import (
"encoding/hex"
"encoding/json"
"log"
"math/big"
"strings"
"git.ngx.fi/c0mm4nd/tronetl/tron"
"golang.org/x/crypto/sha3"
)
// CsvTransaction represents a tron tx csv output, not trc10
// 1 TRX = 1000000 sun
type CsvTransaction struct {
Hash string `csv:"hash"`
Nonce string `csv:"nonce"`
BlockHash string `csv:"block_hash"`
BlockNumber uint64 `csv:"block_number"`
TransactionIndex int `csv:"transaction_index"`
FromAddress string `csv:"from_address"`
ToAddress string `csv:"to_address"`
Value string `csv:"value"`
Gas string `csv:"gas"`
GasPrice string `csv:"gas_price"`
Input string `csv:"input"`
BlockTimestamp uint64 `csv:"block_timestamp"`
MaxFeePerGas string `csv:"max_fee_per_gas"`
MaxPriorityFeePerGas string `csv:"max_priority_fee_per_gas"`
TransactionType string `csv:"transaction_type"`
Status string
// appendix
TransactionTimestamp int64 `csv:"transaction_timestamp"`
TransactionExpiration int64 `csv:"transaction_expiration"`
FeeLimit int64 `csv:"fee_limit"`
}
// NewCsvTransaction creates a new CsvTransaction
func NewCsvTransaction(blockTimestamp uint64, txIndex int, jsontx *tron.JSONTransaction, httptx *tron.HTTPTransaction) *CsvTransaction {
to := ""
if jsontx.To != "" {
to = tron.EnsureTAddr(jsontx.To[2:])
}
txType := "Unknown"
if len(httptx.RawData.Contract) > 0 {
txType = httptx.RawData.Contract[0].ContractType
}
status := ""
if len(httptx.Ret) > 0 {
status = httptx.Ret[0].ContractRet
}
return &CsvTransaction{
Hash: jsontx.Hash[2:],
Nonce: "", //tx.Nonce,
BlockHash: jsontx.BlockHash[2:],
BlockNumber: uint64(*jsontx.BlockNumber),
TransactionIndex: txIndex,
FromAddress: tron.EnsureTAddr(jsontx.From[2:]),
ToAddress: to,
Value: jsontx.Value.ToInt().String(),
Gas: jsontx.Gas.ToInt().String(),
GasPrice: jsontx.GasPrice.ToInt().String(), // https://support.ledger.com/hc/en-us/articles/6331588714141-How-do-Tron-TRX-fees-work-?support=true
Input: jsontx.Input[2:],
BlockTimestamp: blockTimestamp / 1000, // unit: sec
MaxFeePerGas: "", //tx.MaxFeePerGas.String(),
MaxPriorityFeePerGas: "", //tx.MaxPriorityFeePerGas.String(),
TransactionType: txType, //jsontx.Type[2:],
Status: status, // can be SUCCESS REVERT
// appendix
TransactionTimestamp: httptx.RawData.Timestamp / 1000, // float64(httptx.RawData.Timestamp) * 1 / 1000,
TransactionExpiration: httptx.RawData.Expiration / 1000, // float64(httptx.RawData.Expiration) * 1 / 1000,
FeeLimit: httptx.RawData.FeeLimit,
}
}
// CsvBlock represents a tron block output
type CsvBlock struct {
Number uint64 `csv:"number"`
Hash string `csv:"hash"`
ParentHash string `csv:"parent_hash"`
Nonce string `csv:"nonce"`
Sha3Uncles string `csv:"sha3_uncles"`
LogsBloom string `csv:"logs_bloom"`
TransactionsRoot string `csv:"transaction_root"`
StateRoot string `csv:"state_root"`
ReceiptsRoot string `csv:"receipts_root"`
Miner string `csv:"miner"`
Difficulty string `csv:"difficulty"`
TotalDifficulty string `csv:"total_difficulty"`
Size uint64 `csv:"size"`
ExtraData string `csv:"extra_data"`
GasLimit string `csv:"gas_limit"`
GasUsed string `csv:"gas_used"`
Timestamp uint64 `csv:"timestamp"`
TansactionCount int `csv:"transaction_count"`
BaseFeePerGas string `csv:"base_fee_per_gas"`
// append
WitnessSignature string `csv:"witness_signature"`
}
// NewCsvBlock creates a new CsvBlock
func NewCsvBlock(jsonblock *tron.JSONBlockWithTxs, httpblock *tron.HTTPBlock) *CsvBlock {
return &CsvBlock{
Number: uint64(*jsonblock.Number),
Hash: jsonblock.Hash[2:],
ParentHash: jsonblock.ParentHash[2:],
Nonce: "",
Sha3Uncles: "", // block.Sha3Uncles,
LogsBloom: jsonblock.LogsBloom[2:],
TransactionsRoot: jsonblock.TransactionsRoot[2:],
StateRoot: jsonblock.StateRoot[2:],
ReceiptsRoot: "", // block.ReceiptsRoot
Miner: tron.EnsureTAddr(jsonblock.Miner[2:]), // = WitnessAddress
Difficulty: "",
TotalDifficulty: "",
Size: uint64(*jsonblock.Size),
ExtraData: "",
GasLimit: jsonblock.GasLimit.ToInt().String(),
GasUsed: jsonblock.GasUsed.ToInt().String(),
Timestamp: uint64(httpblock.BlockHeader.RawData.Timestamp) / 1000,
TansactionCount: len(jsonblock.Transactions),
BaseFeePerGas: "", // block.BaseFeePerGas,
//append
WitnessSignature: httpblock.BlockHeader.WitnessSignature,
}
}
// CsvTRC10Transfer is a trc10 transfer output
// https://developers.tron.network/docs/trc10-transfer-in-smart-contracts
// https://tronprotocol.github.io/documentation-en/mechanism-algorithm/system-contracts/
// It represents:
// - TransferContract
// - TransferAssetContract
type CsvTRC10Transfer struct {
BlockNumber uint64 `csv:"block_number"`
BlockHash string `csv:"block_hash"`
TransactionHash string `csv:"transaction_hash"`
TransactionIndex int `csv:"transaction_index"`
ContractCallIndex int `csv:"contract_call_index"`
AssetName string `csv:"asset_name"` // do not omit => empty means trx
FromAddress string `csv:"from_address"`
ToAddress string `csv:"to_address"`
Value string `csv:"value"`
}
// NewCsvTRC10Transfer creates a new CsvTRC10Transfer
func NewCsvTRC10Transfer(blockHash string, blockNum uint64, txIndex, callIndex int, httpTx *tron.HTTPTransaction, tfParams *tron.TRC10TransferParams) *CsvTRC10Transfer {
return &CsvTRC10Transfer{
TransactionHash: httpTx.TxID,
BlockHash: blockHash,
BlockNumber: blockNum,
TransactionIndex: txIndex,
ContractCallIndex: callIndex,
AssetName: tfParams.AssetName,
FromAddress: tron.EnsureTAddr(tfParams.OwnerAddress),
ToAddress: tron.EnsureTAddr(tfParams.ToAddress),
Value: tfParams.Amount.String(),
}
}
// CsvLog is a EVM smart contract event log output
type CsvLog struct {
BlockNumber uint64 `csv:"block_number"`
TransactionHash string `csv:"transaction_hash"`
LogIndex uint `csv:"log_index"`
Address string `csv:"address"`
Topics string `csv:"topics"`
Data string `csv:"data"`
}
// NewCsvLog creates a new CsvLog
func NewCsvLog(blockNumber uint64, txHash string, logIndex uint, log *tron.HTTPTxInfoLog) *CsvLog {
return &CsvLog{
BlockNumber: blockNumber,
TransactionHash: txHash,
LogIndex: logIndex,
Address: tron.EnsureTAddr(log.Address),
Topics: strings.Join(log.Topics, ";"),
Data: log.Data,
}
}
// CsvInternalTx is a EVM smart contract internal transaction
type CsvInternalTx struct {
BlockNumber uint64 `csv:"block_number"`
TransactionHash string `csv:"transaction_hash"`
Index uint `csv:"internal_index"`
InternalTransactionHash string `csv:"internal_hash"`
CallerAddress string `csv:"caller_address"`
TransferToAddress string `csv:"transferTo_address"`
CallInfoIndex uint `csv:"call_info_index"`
CallTokenID string `csv:"call_token_id"`
CallValue int64 `csv:"call_value"`
Note string `csv:"note"`
Rejected bool `csv:"rejected"`
}
// NewCsvInternalTx creates a new CsvInternalTx
func NewCsvInternalTx(blockNum uint64, txHash string, index uint, itx *tron.HTTPInternalTransaction, callInfoIndex uint, tokenID string, value int64) *CsvInternalTx {
return &CsvInternalTx{
BlockNumber: blockNum,
TransactionHash: txHash,
Index: index,
InternalTransactionHash: itx.InternalTransactionHash,
CallerAddress: tron.EnsureTAddr(itx.CallerAddress),
TransferToAddress: tron.EnsureTAddr(itx.TransferToAddress),
// CallValueInfo: strings.Join(callValues, ";"),
CallInfoIndex: callInfoIndex,
CallTokenID: tokenID,
CallValue: value,
Note: itx.Note,
Rejected: itx.Rejected,
}
}
// CsvReceipt is a receipt for tron transaction
type CsvReceipt struct {
TxHash string `csv:"transaction_hash"`
TxIndex uint `csv:"transaction_index"`
// BlockHash string `csv:"block_hash"` // cannot get this
BlockNumber uint64 `csv:"block_number"`
ContractAddress string `csv:"contract_address"`
EnergyUsage int64 `csv:"energy_usage,omitempty"`
EnergyFee int64 `csv:"energy_fee,omitempty"`
OriginEnergyUsage int64 `csv:"origin_energy_usage,omitempty"`
EnergyUsageTotal int64 `csv:"energy_usage_total,omitempty"`
NetUsage int64 `csv:"net_usage,omitempty"`
NetFee int64 `csv:"net_fee,omitempty"`
Result string `csv:"result"`
}
func NewCsvReceipt(blockNum uint64, txHash string, txIndex uint, contractAddr string, r *tron.HTTPReceipt) *CsvReceipt {
return &CsvReceipt{
TxHash: txHash,
TxIndex: txIndex,
// BlockHash: blockHash,
BlockNumber: blockNum,
ContractAddress: contractAddr,
EnergyUsage: r.EnergyUsage,
EnergyFee: r.EnergyFee,
OriginEnergyUsage: r.OriginEnergyUsage,
EnergyUsageTotal: r.EnergyUsageTotal,
NetUsage: r.NetUsage,
NetFee: r.NetFee,
Result: r.Result,
}
}
// CsvAccount is a tron account
type CsvAccount struct {
AccountName string `csv:"account_name"`
Address string `csv:"address"`
Type string `csv:"type"`
CreateTime int64 `csv:"create_time"`
// DecodedName string `csv:decoded_name`
}
func NewCsvAccount(acc *tron.HTTPAccount) *CsvAccount {
name, _ := hex.DecodeString(acc.AccountName)
return &CsvAccount{
AccountName: string(name),
Address: tron.EnsureTAddr(acc.Address),
Type: acc.AccountType,
CreateTime: acc.CreateTime / 1000,
}
}
// CsvContract is a standard EVM contract
type CsvContract struct {
Address string `csv:"address"`
Bytecode string `csv:"bytecode"`
FunctionSighashes string `csv:"function_sighashes"`
IsErc20 bool `csv:"is_erc20"`
IsErc721 bool `csv:"is_erc721"`
BlockNumber uint64 `csv:"block_number"`
// append some...
ContractName string
ConsumeUserResourcePercent int
OriginAddress string
OriginEnergyLimit int64
}
var keccakHasher = sha3.NewLegacyKeccak256()
func NewCsvContract(c *tron.HTTPContract) *CsvContract {
hashes := make([]string, 0, len(c.Abi.Entrys))
for _, abi := range c.Abi.Entrys {
if strings.ToLower(abi.Type) == "function" {
content := abi.Name + "("
types := make([]string, 0, len(abi.Inputs))
for _, input := range abi.Inputs {
types = append(types, input.Type)
}
funchash := keccakHasher.Sum([]byte(content + strings.Join(types, ",") + ")"))
hashes = append(hashes, hex.EncodeToString(funchash))
}
}
isErc20 := implementsAnyOf(hashes, "totalSupply()") &&
implementsAnyOf(hashes, "balanceOf(address)") &&
implementsAnyOf(hashes, "transfer(address,uint256)") &&
implementsAnyOf(hashes, "transferFrom(address,address,uint256)") &&
implementsAnyOf(hashes, "approve(address,uint256)") &&
implementsAnyOf(hashes, "allowance(address,address)")
isErc721 := implementsAnyOf(hashes, "balanceOf(address)") &&
implementsAnyOf(hashes, "ownerOf(uint256)") &&
implementsAnyOf(hashes, "transfer(address,uint256)", "transferFrom(address,address,uint256)") &&
implementsAnyOf(hashes, "approve(address,uint256)")
return &CsvContract{
Address: tron.EnsureTAddr(c.ContractAddress),
Bytecode: c.Bytecode,
FunctionSighashes: strings.Join(hashes, ";"),
IsErc20: isErc20,
IsErc721: isErc721,
BlockNumber: 0,
// append
ContractName: c.Name,
ConsumeUserResourcePercent: c.ConsumeUserResourcePercent,
OriginAddress: tron.EnsureTAddr(c.OriginAddress),
OriginEnergyLimit: c.OriginEnergyLimit,
}
}
func implementsAnyOf(hashes []string, sigStrs ...string) bool {
for i := range sigStrs {
hash := hex.EncodeToString(keccakHasher.Sum([]byte(sigStrs[i])))
for j := range hashes {
if hashes[j] == hash {
return true
}
}
}
return false
}
// CsvTokens is a standard EVM contract token
type CsvTokens struct {
Address string `csv:"address"`
Symbol string `csv:"symbol"`
Name string `csv:"name"`
Decimals uint64 `csv:"decimals"`
TotalSupply uint64 `csv:"total_supply"`
BlockNumber uint64 `csv:"block_number"`
}
func NewCsvTokens(cli *tron.TronClient, contract *tron.HTTPContract) *CsvTokens {
contractAddr := contract.ContractAddress
callerAddr := contract.OriginAddress
symbolResult := cli.CallContract(contractAddr, callerAddr, 0, 1000,
"symbol()",
)
symbol := ParseSymbol(symbolResult.ConstantResult)
if symbol == nil {
log.Println("failed to parse symbol for contract", tron.EnsureHexAddr(contractAddr))
result, _ := json.Marshal(symbolResult)
log.Println(string(result))
return nil
}
nameResult := cli.CallContract(contractAddr, callerAddr, 0, 1000,
"name()",
)
name := ParseName(nameResult.ConstantResult)
if name == nil {
log.Println("failed to parse name for contract", tron.EnsureHexAddr(contractAddr))
result, _ := json.Marshal(nameResult)
log.Println(string(result))
return nil
}
decimalsResult := cli.CallContract(contractAddr, callerAddr, 0, 1000,
"decimals()",
)
decimals := ParseDecimals(decimalsResult.ConstantResult)
if decimals == nil {
log.Println("failed to parse decimals for contract", tron.EnsureHexAddr(contractAddr))
result, _ := json.Marshal(decimalsResult)
log.Println(string(result))
return nil
}
totalSupplyResult := cli.CallContract(contractAddr, callerAddr, 0, 1000,
"totalSupply()",
)
totalSupply := ParseTotalSupply(totalSupplyResult.ConstantResult)
if totalSupply == nil {
log.Println("failed to parse totalSupply for contract", tron.EnsureHexAddr(contractAddr))
result, _ := json.Marshal(totalSupplyResult)
log.Println(string(result))
return nil
}
block := cli.GetJSONBlockByNumberWithTxIDs(nil)
return &CsvTokens{
Address: contractAddr,
Symbol: *symbol,
Name: *name,
Decimals: *decimals,
TotalSupply: *totalSupply,
BlockNumber: uint64(*block.Number),
}
}
func ParseSymbol(contractResults []string) *string {
if len(contractResults) == 0 {
return nil
}
result := contractResults[0]
bigLlen, ok := new(big.Int).SetString(result[0:64], 16)
if !ok {
// TODO: warn log here
return nil
}
l, ok := new(big.Int).SetString(result[64:64+bigLlen.Int64()*2], 16)
if !ok {
// TODO: warn log here
return nil
}
hexStr := result[64+bigLlen.Int64()*2 : 64+bigLlen.Int64()*2+l.Int64()*2]
decoded, err := hex.DecodeString(hexStr)
if err != nil {
// TODO: err log here
return nil
}
rtn := string(decoded)
return &rtn
}
func ParseName(contractResults []string) *string {
if len(contractResults) == 0 {
return nil
}
result := contractResults[0]
bigLlen, ok := new(big.Int).SetString(result[0:64], 16)
if !ok {
// TODO: warn log here
return nil
}
l, ok := new(big.Int).SetString(result[64:64+bigLlen.Int64()*2], 16)
if !ok {
// TODO: warn log here
return nil
}
hexStr := result[64+bigLlen.Int64()*2 : 64+bigLlen.Int64()*2+l.Int64()*2]
decoded, err := hex.DecodeString(hexStr)
if err != nil {
// TODO: err log here
return nil
}
rtn := string(decoded)
return &rtn
}
func ParseDecimals(contractResults []string) *uint64 {
if len(contractResults) == 0 {
panic("failed to parse symbol")
}
result, ok := new(big.Int).SetString(contractResults[0], 16)
if !ok {
// TODO: err log here
return nil
}
rtn := result.Uint64()
return &rtn
}
func ParseTotalSupply(contractResults []string) *uint64 {
if len(contractResults) == 0 {
panic("failed to parse symbol")
}
result, ok := new(big.Int).SetString(contractResults[0], 16)
if !ok {
// TODO: err log here
return nil
}
rtn := result.Uint64()
return &rtn
}