forked from ranjbar-dev/tron-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.go
286 lines (225 loc) · 7.32 KB
/
crawler.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
package tronWallet
import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ranjbar-dev/tron-wallet/enums"
"github.com/ranjbar-dev/tron-wallet/grpcClient"
"github.com/ranjbar-dev/tron-wallet/grpcClient/proto/api"
"github.com/ranjbar-dev/tron-wallet/grpcClient/proto/core"
"github.com/ranjbar-dev/tron-wallet/util"
"strings"
"sync"
"time"
)
import (
"github.com/golang/protobuf/proto"
)
type Crawler struct {
Node enums.Node
Addresses []string
}
type CrawlResult struct {
Address string
Transactions []CrawlTransaction
}
type CrawlTransaction struct {
TxId string
Confirmations int64
FromAddress string
ToAddress string
Amount int64
Symbol string
}
func (c *Crawler) ScanBlocks(count int) ([]CrawlResult, error) {
var wg sync.WaitGroup
var allTransactions [][]CrawlTransaction
client, err := grpcClient.GetGrpcClient(c.Node)
if err != nil {
return nil, err
}
block, err := client.GetNowBlock()
if err != nil {
return nil, err
}
// check block for transaction
allTransactions = append(allTransactions, c.extractOurTransactionsFromBlock(block, 0))
if err != nil {
return nil, err
}
currentBlock := block.BlockHeader.RawData.Number
blockNumber := block.BlockHeader.RawData.Number
for i := count; i > 0; i-- {
// sleep to avoid 503 error
time.Sleep(100 * time.Millisecond)
blockNumber = blockNumber - 1
wg.Add(1)
go c.getBlockData(&wg, client, &allTransactions, blockNumber, currentBlock)
}
wg.Wait()
return c.prepareCrawlResultFromTransactions(allTransactions), nil
}
func (c *Crawler) ScanBlocksFromTo(from int, to int) ([]CrawlResult, error) {
if to-from < 1 {
return nil, errors.New("to number should be more than from number")
}
var wg sync.WaitGroup
var allTransactions [][]CrawlTransaction
client, err := grpcClient.GetGrpcClient(c.Node)
if err != nil {
return nil, err
}
block, err := client.GetNowBlock()
if err != nil {
return nil, err
}
currentBlock := block.BlockHeader.RawData.Number
for i := to; i > from; i-- {
// sleep to avoid 503 error
wg.Add(1)
time.Sleep(100 * time.Millisecond)
go c.getBlockData(&wg, client, &allTransactions, int64(i), currentBlock)
}
wg.Wait()
return c.prepareCrawlResultFromTransactions(allTransactions), nil
}
// ==================== private ==================== //
func (c *Crawler) getBlockData(wg *sync.WaitGroup, client *grpcClient.GrpcClient, allTransactions *[][]CrawlTransaction, num int64, currentBlock int64) {
defer wg.Done()
block, err := client.GetBlockByNum(num)
if err != nil {
fmt.Println(err)
return
}
// check block for transaction
*allTransactions = append(*allTransactions, c.extractOurTransactionsFromBlock(block, currentBlock))
}
func (c *Crawler) extractOurTransactionsFromBlock(block *api.BlockExtention, currentBlock int64) []CrawlTransaction {
var txs []CrawlTransaction
for _, t := range block.Transactions {
transaction := t.Transaction
// if transaction is not success
if transaction.Ret[0].ContractRet != core.Transaction_Result_SUCCESS {
fmt.Println("transaction is not success")
continue
}
// if transaction is not tron transfer or erc20 transfer
if transaction.RawData.Contract[0].Type != core.Transaction_Contract_TransferContract && transaction.RawData.Contract[0].Type != core.Transaction_Contract_TriggerSmartContract {
continue
}
var crawlTransaction *CrawlTransaction = nil
if transaction.RawData.Contract[0].Type == core.Transaction_Contract_TransferContract {
contract := &core.TransferContract{}
err := proto.Unmarshal(transaction.RawData.Contract[0].Parameter.Value, contract)
if err != nil {
fmt.Println(err)
continue
}
crawlTransaction = c.prepareTrxTransaction(t, contract)
} else if transaction.RawData.Contract[0].Type == core.Transaction_Contract_TriggerSmartContract {
contract := &core.TriggerSmartContract{}
err := proto.Unmarshal(transaction.RawData.Contract[0].Parameter.Value, contract)
if err != nil {
fmt.Println(err)
continue
}
crawlTransaction = c.prepareTrc20Transaction(t, contract)
}
if crawlTransaction != nil && currentBlock != 0 {
crawlTransaction.Confirmations = currentBlock - block.BlockHeader.RawData.Number
}
if crawlTransaction != nil {
for _, ourAddress := range c.Addresses {
if ourAddress == crawlTransaction.ToAddress || ourAddress == crawlTransaction.FromAddress {
txs = append(txs, *crawlTransaction)
}
}
}
}
return txs
}
func (c *Crawler) prepareTrxTransaction(t *api.TransactionExtention, contract *core.TransferContract) *CrawlTransaction {
// if address is hex convert to base58
toAddress := hexutil.Encode(contract.ToAddress)[2:]
if strings.HasPrefix(toAddress, "41") == true {
toAddress = util.HexToAddress(toAddress).String()
}
// if address is hex convert to base58
fromAddress := hexutil.Encode(contract.OwnerAddress)[2:]
if strings.HasPrefix(fromAddress, "41") == true {
fromAddress = util.HexToAddress(fromAddress).String()
}
return &CrawlTransaction{
TxId: hexutil.Encode(t.GetTxid())[2:],
FromAddress: fromAddress,
ToAddress: toAddress,
Amount: contract.Amount,
Symbol: "TRX",
}
}
func (c *Crawler) prepareTrc20Transaction(t *api.TransactionExtention, contract *core.TriggerSmartContract) *CrawlTransaction {
tokenTransferData, validTokenData := util.ParseTrc20TokenTransfer(util.ToHex(contract.Data)[2:])
if validTokenData == false {
return nil
}
// if contractAddress is hex convert to base58
contractAddress := hexutil.Encode(contract.ContractAddress)[2:]
if strings.HasPrefix(contractAddress, "41") == true {
contractAddress = util.HexToAddress(contractAddress).String()
}
// if address is hex convert to base58
toAddress := tokenTransferData.To
if strings.HasPrefix(toAddress, "41") == true {
toAddress = util.HexToAddress(toAddress).String()
}
// if address is hex convert to base58
fromAddress := hexutil.Encode(contract.OwnerAddress)[2:]
if strings.HasPrefix(fromAddress, "41") == true {
fromAddress = util.HexToAddress(fromAddress).String()
}
token := &Token{
ContractAddress: enums.CreateContractAddress(contractAddress),
}
symbol, _ := token.GetSymbol(c.Node, fromAddress)
return &CrawlTransaction{
TxId: hexutil.Encode(t.GetTxid())[2:],
FromAddress: fromAddress,
ToAddress: toAddress,
Amount: tokenTransferData.Value.Int64(),
Symbol: symbol,
}
}
func (c *Crawler) prepareCrawlResultFromTransactions(transactions [][]CrawlTransaction) []CrawlResult {
var result []CrawlResult
for _, transaction := range transactions {
for _, tx := range transaction {
if c.addressExistInResult(result, tx.ToAddress) {
id, res := c.getAddressCrawlInResultList(result, tx.ToAddress)
res.Transactions = append(res.Transactions, tx)
result[id] = res
} else {
result = append(result, CrawlResult{
Address: tx.ToAddress,
Transactions: []CrawlTransaction{tx},
})
}
}
}
return result
}
func (c *Crawler) addressExistInResult(result []CrawlResult, address string) bool {
for _, res := range result {
if res.Address == address {
return true
}
}
return false
}
func (c *Crawler) getAddressCrawlInResultList(result []CrawlResult, address string) (int, CrawlResult) {
for id, res := range result {
if res.Address == address {
return id, res
}
}
panic("crawl result not found")
}