This repository has been archived by the owner on Dec 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
deploy.js
105 lines (85 loc) · 3.07 KB
/
deploy.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
const fs = require('fs');
var util = require('util');
const solc = require('solc');
const Web3 = require('web3');
var utils = require('./utils');
var callAsyncFunc = utils.callAsyncFunc;
const SUCCESS_EXIT_CODE = 0;
const MISSING_ARGUMENT_EXIT_CODE = 1;
const GENERAL_ERROR_EXIT_CODE = 2;
var deployContract = async (opts) => {
try {
var contractName = opts.contractName;
var rpcEndpoint = opts.rpcEndpoint;
var accountPassword = opts.accountPassword;
console.log(`deploying contract on ${rpcEndpoint}`);
const web3 = new Web3(new Web3.providers.HttpProvider(rpcEndpoint));
// compile contract
const input = fs.readFileSync(`contracts/${contractName}.sol`);
const output = solc.compile(input.toString(), 1);
var compiledContract = output.contracts[`:${contractName}`];
const bytecode = '0x' + compiledContract.bytecode;
const abi = JSON.parse(compiledContract.interface);
const contract = web3.eth.contract(abi);
// get coinbase address
var getCoinbaseRequest = await callAsyncFunc(web3.eth, 'getCoinbase');
var accountAddress = getCoinbaseRequest.result;
// unlock the account
var unlockRes = await callAsyncFunc(web3.personal, 'unlockAccount', accountAddress, accountPassword);
if (!unlockRes.result) {
throw new Error(`error unlocking account: ${accountAddress}`);
}
// deploy contract
var deployResult = contract.new(accountAddress, {
from: accountAddress,
password: accountPassword,
data: bytecode,
gas: 2000000 });
var txHash = deployResult.transactionHash;
// lock the account
var lockRes = await callAsyncFunc(web3.personal, 'lockAccount', accountAddress, accountPassword);
if (!lockRes) {
throw new Error(`error locking account: ${opts.config.from}`);
}
// wait until the contract is mined
var receipt;
while (!receipt)
{
console.log(`waiting for contract to be mined...`);
await utils.sleep(5);
var receiptRequest = await callAsyncFunc(web3.eth, 'getTransactionReceipt', txHash);
receipt = receiptRequest.result;
}
// get contract address
var contractAddress = receipt.contractAddress;
console.log(`contract deployed on address: ${contractAddress}`);
// success exit and send result to output
return exit(SUCCESS_EXIT_CODE, {
accountAddress,
contractAddress
});
}
catch(err) {
return exit(GENERAL_ERROR_EXIT_CODE, {
error: `error deploying contract: ${err.message}`
});
}
}
function exit(exitCode, obj) {
var logLevel = exitCode ? 'error' : 'info';
console[logLevel](JSON.stringify(obj));
process.nextTick(() => process.exit(exitCode));
}
var contractName = process.argv[2];
var rpcEndpoint = process.argv[3];
var accountPassword = process.argv[4];
if (!contractName || !rpcEndpoint || !accountPassword) {
return exit(MISSING_ARGUMENT_EXIT_CODE, {
error: `missing arguments, command line usage: node deploy.js <CONTRACT_NAME> <RPC_ENDPOINT> <COINBASE_PASSWORD>`
});
}
deployContract({
contractName,
rpcEndpoint,
accountPassword
});