forked from austintgriffith/clevis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clevis.js
152 lines (123 loc) · 4.27 KB
/
clevis.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
141
142
143
144
145
146
147
148
149
150
151
152
const Command = require('commander').Command
const fs = require('fs')
const Web3 = require('web3')
const winston = require('winston')
require('./initLogger')()
require('dotenv').config()
async function execute(name, ...args) {
winston.debug(`🗜️ Clevis [${name}]`)
winston.debug(`${name.toUpperCase()}`)
let config = readConfig()
let provider = getWeb3Provider(name, config)
let web3 = new Web3(provider)
let params = {
config: config,
web3: web3
}
let fn = require(`./commands/${name}.js`)
try {
let result = fn(...args, params)
if(web3.currentProvider.engine) {
params.web3.currentProvider.engine.stop()
}
return result
} catch(e) {
winston.error(e)
}
}
function getWeb3Provider(name, config) {
//If the user is using infura, they need to have a mnemonic defined
if(name !== 'new' && config.USE_INFURA && !process.env.mnemonic) {
winston.error("ERROR: No Mnemonic Generated. In order to use Infura, you need one. Run 'clevis new' to create a local account.")
process.exit(1)
}
if(config.USE_INFURA) {
const HDWalletProvider = require("truffle-hdwallet-provider")
return new HDWalletProvider(
process.env.mnemonic,
config.provider
)
} else {
return new Web3.providers.HttpProvider(config.provider)
}
}
function readConfig() {
return JSON.parse(fs.readFileSync("clevis.json").toString())
}
async function init() {
console.log(await require(`./commands/init.js`)())
}
//Usage: new Runner().runCommand(process.argv)
class Runner {
constructor() {
this.program = this.setupProgram()
}
runCommand(args) {
let p = new Promise((resolve, reject) => {
this.resolve = resolve
this.reject = reject
})
this.program.parse(args)
if(this.program.args.length == 0) {
this.program.help()
}
return p
}
async standard(...args) {
let cmdr = args.pop()
let name = cmdr.name()
execute(name, ...args).then(result => {
this.resolve(result)
}).catch(e => {
this.reject(e)
})
}
setupProgram() {
let standard = this.standard.bind(this)
let program = new Command()
program
.option('--debug', 'Turns on Debugging output')
program
.version('0.1.0')
program.command('accounts').action(standard)
program.command('balance <address> [units]').action(standard)
program.command('block <blockNumber>').action(standard)
program.command('blockNumber').action(standard)
program.command('build').action(standard)
program.command('compile <contractName>').action(standard)
program.command('contract <scriptName> <contractName> [accountIndex] [contractArguments...]').action(standard)
program.command('create <contractName>').action(standard)
program.command('deploy <contractName> <accountIndex>').action(standard)
program.command('explain <contractName>').action(standard)
program.command('fromhex <hexString>').action(standard)
program.command('fromwei <amount> <symbol>').action(standard)
program.command('init').action(init)
program.command('invalidate <target>').action(standard)
program.command('new [password]').action(standard)
program.command('randomhex <size>').action(standard)
program.command('recover <string> <signature>').action(standard)
program.command('send <amount> <fromAddress> <toAddress> [data]').action(standard)
program.command('sha3 <string>').action(standard)
program.command('sign <string> <accountIndex> <password>').action(standard)
program.command('start').action(standard)
program.command('test <testName>').action(standard)
program.command('tohex <textString>').action(standard)
program.command('towei <amount> <symbol>').action(standard)
program.command('transaction <hash>').action(standard)
program.command('unlock <accountIndex> <password>').action(standard)
program.command('update').action(standard)
program.command('upload <site>').action(standard)
program.command('version').action(standard)
program.on('command:*', () => {
console.error('Invalid command: %s\nSee --help for a list of available commands.', program.args.join(' '))
process.exit(1)
})
program.on('option:debug', () => {
winston.level = 'debug'
})
return program
}
}
module.exports = {
Runner
}