forked from BuildOnViction/erc20-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawlEtherScan.js
61 lines (49 loc) · 2.02 KB
/
crawlEtherScan.js
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
'use strict'
const db = require('./models/mongodb')
const request = require('request')
const q = require('./queues')
const mainUrl = 'https://api.etherscan.io/api?module=account&action=tokentx&contractaddress=0x8b353021189375591723e7384262f45709a3c3dc&offset=600&sort=asc&page='
async function processCrawl() {
let page = 1
while (true) {
let url = mainUrl + page.toString()
console.log('Crawl url: ', url)
let b = await crawlUrl(url)
let body = JSON.parse(b)
if (body.status === '0') {
console.log('Finish crawl')
break
}
for (let i=0; i < body.result.length; i++) {
let transaction = body.result[i]
let tran = await new db.Transaction({
hash: transaction.hash,
block: transaction.blockNumber,
fromAccount: transaction.from,
toAccount: transaction.to,
tokenAmount: parseFloat(transaction.value) / 10 ** 18,
isProcess: true
})
tran.save()
await db.Wallet.findOneAndUpdate({address: transaction.from}, {address: transaction.from}, {upsert: true, new: true})
await db.Wallet.findOneAndUpdate({address: transaction.to}, {address: transaction.to}, {upsert: true, new: true})
q.create('subAmountFromWallet', {fromWallet: transaction.from, tokenAmount: parseFloat(transaction.value) / 10 ** 18})
.priority('high').removeOnComplete(true).save()
q.create('addAmountToWallet', {toWallet: transaction.to, tokenAmount: parseFloat(transaction.value) / 10 ** 18})
.priority('high').removeOnComplete(true).save()
}
page++
}
}
function crawlUrl(url) {
return new Promise(function (resolve, reject) {
request(url, function (error, res, body) {
if (!error && res.statusCode == 200) {
resolve(body);
} else {
reject(error);
}
});
});
}
processCrawl()