-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathmine.js
More file actions
73 lines (65 loc) · 1.58 KB
/
mine.js
File metadata and controls
73 lines (65 loc) · 1.58 KB
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
// TODO: Import these both from aragon/os
const mineBlock = require('./mineBlock')
const getBlock = require('./blockNumber')
// TODO: Correct errors
const getWeb3 = () => {
const Web3 = require('web3')
const PrivateKeyProvider = require('truffle-privatekey-provider')
const provider = new PrivateKeyProvider(
'A8A54B2D8197BC0B19BB8A084031BE71835580A01E70A45A13BABD16C9BC1563',
'http://localhost:8545'
)
return new Web3(provider)
}
// truffle script: can be run with `truffle exec`
const mine = async (cb) => {
const root = this
let activeWeb3
let argIdx
if (typeof cb !== 'function') {
activeWeb3 = getWeb3()
argIdx = 2
} else {
activeWeb3 = web3
argIdx = 4
}
const mineBlockFn = mineBlock(activeWeb3)
const getBlockFn = getBlock(activeWeb3)
const mineBlocks = async (blocks) => {
const blockArr = new Array(Number(blocks)).fill(0)
blockArr.forEach(async () => {
try {
await mineBlockFn()
}
catch(e) {
console.error('error: ',e)
}
})
}
const mineToBlock = async (blockNumber) => {
while (blockNumber > await getBlockFn()) {
try {
await mineBlockFn()
}
catch(e) {
console.error('error: ',e)
}
}
}
if (!Number(process.argv[argIdx])) {
await mineToBlock(process.argv[argIdx + 1])
}
else {
await mineBlocks(process.argv[argIdx])
}
console.log('\nblock',await getBlockFn(), 'mined')
if (typeof cb === 'function') {
cb()
} else {
process.exit()
}
}
module.exports = mine
if (!process.argv[1].includes('truffle')) {
mine()
}