-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
140 lines (110 loc) · 3.77 KB
/
index.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
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
const Web3 = require('web3')
const Utils = require('web3-utils')
const fs =require('fs')
const lineReader = require('line-reader');
const axios = require('axios').default
const url = 'https://http-testnet.hecochain.com'
const web3 = new Web3(url)
const privateKey = '0xffdee7f3fa4d2414b7bc2897dcc51817fd44f735e5c6b30346d75ce5d8f090aa'
async function broadcast(data) {
let resp = await axios.post(url, {
id: 1,
jsonrpc: "2.0",
method: "eth_sendRawTransaction",
params: [
data
]
}, {
timeout: 2000
})
// console.log(resp.data)
return resp
}
async function sign (from, to, nonce, value, data, extraConfig) {
let acc = web3.eth.accounts.privateKeyToAccount(privateKey)
if(acc.address !== from) {
throw new Error(`address:${from} is not Corresponding to the private key`)
}
let chainId = 256 // await web3.eth.getChainId()
let unsigned = {
nonce,
chainId,
from: acc.address,
to,
value: web3.utils.numberToHex(web3.utils.toWei(value, 'wei')),
data,
...extraConfig
}
let signed = await acc.signTransaction(unsigned)
signed.raw = signed.rawTransaction
return signed
}
const addrFile = 'address.txt'
const signFile = 'signed.txt'
async function main() {
let args = process.argv.slice(2)
if(args.length < 1) {
throw new Error("need args 'sign' or 'brocadcast'")
}
let from = '0xDd698C89c3fFe1D258c181526E611e825f78B410'
if(args[0] === 'sign') {
let logger = fs.createWriteStream(signFile, {
flags: 'a' // 'a' means appending (old data will be preserved)
})
let nonce = await web3.eth.getTransactionCount(from)
lineReader.eachLine(addrFile, async function(line) {
if(!line) {
return
}
let signed = await sign(from, line.trim(), nonce, '1', null, {gas: 21000, gasPrice: Utils.numberToHex(Utils.toWei('1', 'gwei'))})
//save to file
logger.write(`${nonce}, ${signed.rawTransaction}\n`)
nonce++
})
return
}
if(args[0] === 'broadcast') {
let cache =new Map()
let maxNonce = 0
lineReader.eachLine(signFile, async function(line, last) {
let data = line.split(',')
let nonce = parseInt(data[0])
cache.set(nonce, data[1])
if(nonce > maxNonce) {
maxNonce = nonce
}
if(last) {
console.log(`max nonce:${maxNonce}`)
console.log(`broadcast started:${new Date()}`)
while(true) {
try{
let nonce = await web3.eth.getTransactionCount(from)
if(nonce >= maxNonce) {
break
}
let requests = []
for(let i =nonce; i< nonce+200; i++) {
if(i > maxNonce) {
break
}
if(!cache.get(i)) {
console.log(`nonce ${i} not found tx`)
break
}
requests.push(broadcast(cache.get(i).trim()))
}
await Promise.all(requests)
await sleep(1000)
}catch(err) {
console.error(err)
}
}
console.log(`broadcast finished:${new Date()}`)
}
})
}
}
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
main().then(console.log).catch(console.error)