forked from arijoon/web3-6900-reproduction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
120 lines (100 loc) · 2.84 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
const { createServer, RequestListener, Server } = require('http')
const Web3 = require('web3')
const { utils, providers } = Web3
const privateKey = process.env.PRIVATE_KEY
|| '0xd707027b24dccf126549191c008744e03a7dd17c50dccf56c3271ed0746ace5f'
const run = async ({
rpcUrl,
}) => {
const provider = new providers.http.HttpProvider(rpcUrl)
const web3 = new Web3.default(provider)
const account = web3.eth.accounts.privateKeyToAccount(privateKey)
const publicKey = account.address
const tx = {
gasLimit: 50000,
gasPrice: 1000000000,
from: publicKey,
to: publicKey,
chainId: 1,
networkId: 1,
nonce: 0,
data: '0x0000000000000000000000000000000000000000',
}
const { rawTransaction, transactionHash } = await account.signTransaction(tx)
console.error(`Signed message with hash: [${transactionHash}]`)
const txSent = web3.eth.sendSignedTransaction(rawTransaction, {}, {
checkRevertBeforeSending: false,
ignoreGasPricing: true,
ignoreFillingGasLimit: true,
})
// With this promise caught, there should be no unhandled rejection
txSent.catch(error => {
console.error('Error in sendSignedTransaction response', error)
})
return new Promise((resolve, reject) => {
txSent.once('sent', () => {
console.error('Transction was sent')
resolve()
})
txSent.on('error', error => {
console.error('Failed sending tx', error)
reject(error)
})
})
}
// Rpc responses in order
const result = result => ({
result,
})
const responses = [
// eth_blockNumber
utils.toHex(100),
// eth_sendRawTransaction
'0x2c01432606c9ed429b56af5c30ea4e7a02ccd25e8ed400296d58000f6f89649e',
].map(result)
const withServer = async fn => {
const onRequest = (req, res) => {
let body = ''
req.on('data', chunk => {
body += chunk
})
req.on('end', () => {
console.error('Request body', body)
const { id } = JSON.parse(body)
const response = responses.shift()
res.setHeader('Content-Type', 'application/json')
res.writeHead(200)
res.end(JSON.stringify({ id, jsonrpc: '2.0', ...response }))
})
}
const port = process.env.PORT || 36448
const host = process.env.HOST || 'localhost'
const server = createServer(onRequest)
server.listen(port, host)
// Wait for server to start
await new Promise((resolve, reject) => {
server.once('error', reject)
server.once('listening', resolve)
})
await fn({
rpcUrl: `http://${host}:${port}`,
})
// Kill the server
server.closeAllConnections()
await new Promise((resolve, reject) => {
server.close(error => {
if (!error) {
resolve()
} else {
reject(error)
}
})
})
}
withServer(opts =>
run(opts).catch(error => {
console.error('Application exception', error)
})
).catch(error => {
console.error('Server failure error', error)
})