-
Notifications
You must be signed in to change notification settings - Fork 2
/
tfextractor.go
74 lines (61 loc) · 2.11 KB
/
tfextractor.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
package main
import (
"encoding/hex"
"math/big"
"git.ngx.fi/c0mm4nd/tronetl/tron"
)
const TRANSFER_EVENT_TOPIC = "ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" // NO 0x Prefix for ANY topic!!!
type Transfer struct {
BlockNumber uint64 `json:"blockNumber" csv:"block_number"`
TransactionHash string `json:"transaction_hash" csv:"transaction_hash"`
LogIndex uint `json:"logIndex" csv:"log_index"`
// TxHashIdx string `csv:"id"`
TokenAddress string `json:"tokenAddress" csv:"token_address"`
FromAddress string `json:"fromAddress" csv:"from_address"`
ToAddress string `json:"toAddress" csv:"to_address"`
Value string `json:"value" csv:"value"`
}
// ExtractTransferFromLog is a helper to extract EVM smart contract token transfer from the event logs
func ExtractTransferFromLog(logTopics []string, logData string, logContractAddress string, logIndex uint, logTxHash string, logBlockNum uint64) *Transfer {
// topics := log.Topics
if logTopics == nil || len(logTopics) < 1 {
return nil
}
if logTopics[0] != TRANSFER_EVENT_TOPIC {
return nil
}
topics_with_data := append(logTopics, chunkDataToHashes(logData)...)
// txHash := log.TxHash
// logIndex := log.Index
if len(topics_with_data) != 4 {
return nil
}
valBytes, err := hex.DecodeString(topics_with_data[3])
chk(err)
value := new(big.Int).SetBytes(valBytes)
return &Transfer{
BlockNumber: logBlockNum,
TokenAddress: tron.EnsureTAddr(logContractAddress),
FromAddress: hash2Addr(topics_with_data[1]),
ToAddress: hash2Addr(topics_with_data[2]),
Value: value.String(),
LogIndex: logIndex,
TransactionHash: logTxHash,
// TxHashIdx: log.TxHash.String() + "_" + strconv.Itoa(int(log.Index)),
}
}
func chunkDataToHashes(b string) []string {
rtn := make([]string, 0, (len(b)+31*2)/64) // len hash str == 64
for i := 0; i+64 <= len(b); i += 64 {
rtn = append(rtn, b[i:i+64])
}
return rtn
}
func hash2Addr(hash string) string {
if len(hash) != 64 {
panic("not a hash")
}
// addr := common.Address{}
// copy(addr[:], )
return tron.EnsureTAddr(hash[12*2:])
}