-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrawler.go
238 lines (184 loc) · 5.22 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
package ethereumWallet
import (
"context"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/params"
"github.com/ranjbar-dev/ethereum-wallet/enums"
"github.com/ranjbar-dev/ethereum-wallet/geth"
"github.com/ranjbar-dev/ethereum-wallet/util"
"math/big"
"strings"
"sync"
"time"
)
type Crawler struct {
Node Node
Addresses []string
}
type CrawlResult struct {
Address string
Transactions []CrawlTransaction
}
type CrawlTransaction struct {
TxId string
Confirmations int64
FromAddress string
ToAddress string
Amount uint64
Symbol string
}
func (c *Crawler) ScanBlocks(count int) ([]CrawlResult, error) {
var wg sync.WaitGroup
var allTransactions [][]CrawlTransaction
client, err := geth.GetGETHClient(c.Node.Http)
if err != nil {
return nil, err
}
block, err := client.BlockByNumber(context.Background(), nil)
if err != nil {
return nil, err
}
// check block for transaction
allTransactions = append(allTransactions, c.extractOurTransactionsFromBlock(block, block))
if err != nil {
return nil, err
}
blockNumber := block.Number().Int64()
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, block, &allTransactions, blockNumber)
}
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 := geth.GetGETHClient(c.Node.Http)
if err != nil {
return nil, err
}
block, err := client.BlockByNumber(context.Background(), nil)
if err != nil {
return nil, err
}
for i := to; i > from; i-- {
// sleep to avoid 503 error
time.Sleep(100 * time.Millisecond)
wg.Add(1)
go c.getBlockData(&wg, client, block, &allTransactions, int64(i))
}
wg.Wait()
return c.prepareCrawlResultFromTransactions(allTransactions), nil
}
func (c *Crawler) getBlockData(wg *sync.WaitGroup, client *ethclient.Client, currentBlock *types.Block, allTransactions *[][]CrawlTransaction, num int64) {
defer wg.Done()
block, err := client.BlockByNumber(context.Background(), big.NewInt(num))
if err != nil {
fmt.Println(err)
return
}
// check block for transaction
*allTransactions = append(*allTransactions, c.extractOurTransactionsFromBlock(currentBlock, block))
}
func (c *Crawler) extractOurTransactionsFromBlock(currentBlock *types.Block, block *types.Block) []CrawlTransaction {
chainConfig := params.MainnetChainConfig
if strings.Contains(c.Node.Http, "goerli") {
chainConfig = params.GoerliChainConfig
}
if strings.Contains(c.Node.Http, "sepolia") {
chainConfig = params.SepoliaChainConfig
}
blockSigner := types.MakeSigner(chainConfig, block.Number())
var txs []CrawlTransaction
for _, transaction := range block.Transactions() {
symbol := "ETH"
txMsg, errMessage := transaction.AsMessage(blockSigner, nil)
if errMessage != nil {
continue
}
fromAddress := txMsg.From().Hex()
if txMsg.To() == nil {
continue
}
toAddress := txMsg.To().Hex()
if txMsg.Value() == nil {
continue
}
amount := txMsg.Value().Int64()
// is ERC20 token transfer
if len(transaction.Data()) > 0 {
tokenData, exist := util.ParsDataErc20TokenTransfer(transaction.Data())
if !exist {
continue
}
// toAddress -> contract address
var err error
token := Token{ContractAddress: enums.CreateContractAddress(toAddress)}
symbol, err = token.GetSymbol(c.Node)
if err != nil {
continue
}
amount = tokenData.Value.Int64()
toAddress = tokenData.To
}
txId := transaction.Hash().Hex()
confirmations := int64(currentBlock.NumberU64() - block.NumberU64())
for _, ourAddress := range c.Addresses {
if ourAddress == toAddress || ourAddress == fromAddress {
txs = append(txs, CrawlTransaction{
TxId: txId,
FromAddress: fromAddress,
ToAddress: toAddress,
Amount: uint64(amount),
Confirmations: confirmations,
Symbol: symbol,
})
}
}
}
return txs
}
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")
}