-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.js
More file actions
85 lines (76 loc) · 2.93 KB
/
deploy.js
File metadata and controls
85 lines (76 loc) · 2.93 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
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Author: Vu Duy Tuan - tuanvd@gmail.com
* Date: 5/29/19
* Time: 10:04 AM
*/
const fs = require('fs-extra');
const path = require('path');
const config = require('./config');
const Web3 = require('web3');
const argv = require('minimist')(process.argv.slice(2));
if (argv['contract'] === undefined || argv['contract'] === null) {
console.log('Undefined contract');
process.exit();
}
const contractName = argv['contract'];
let networkId = 'default';
if (argv['network'] !== undefined && argv['network'] !== null) {
networkId = argv['network'];
}
if (config.networks[networkId] === undefined || config.networks[networkId] === null) {
console.log('Undefined network');
process.exit();
}
const networkConfig = config.networks[networkId];
let params = [];
if (argv['params'] !== undefined && argv['params'] !== null) {
params = JSON.parse(argv['params']);
}
const builtContractPath = path.resolve(__dirname, config.builtContractPath, `${contractName}.json`);
const {abi, bytecode} = fs.readJsonSync(builtContractPath);
if (abi && bytecode) {
console.log(`Used contract def file: ${builtContractPath}`);
} else {
console.log(`Invalid contract def file: ${builtContractPath}`);
process.exit();
}
console.log(`Used network provider: ${networkConfig['url']}`);
const web3 = new Web3(networkConfig['url']);
//web3.eth.accounts.wallet.add(networkConfig.defaultAccount.privateKey);
web3.eth.accounts.wallet.add({
address: networkConfig.defaultAccount.address,
privateKey: networkConfig.defaultAccount.privateKey
});
const contract = new web3.eth.Contract(abi);
contract
.deploy({data: bytecode, arguments: params})
.send({from: networkConfig.defaultAccount.address, gas: networkConfig.gas})
.on('receipt', function (receipt) {
console.log(`Contract ${contractName} deployed successfully`);
console.log(' address: ' + receipt.contractAddress);
console.log(' txn: ' + receipt.transactionHash);
// Save deployment info
const deployedContractPath = path.resolve(__dirname, config.deployedContractPath, `${contractName}_${networkId}.json`);
fs.removeSync(deployedContractPath);
fs.outputJsonSync(
deployedContractPath,
{
contractName: contractName,
networkId: networkId,
contractAddress: receipt.contractAddress,
transactionIndex: receipt.transactionIndex,
blockHash: receipt.blockHash,
blockNumber: receipt.blockNumber,
cumulativeGasUsed: receipt.cumulativeGasUsed,
gasUsed: receipt.gasUsed,
transactionHash: receipt.transactionHash,
abi: abi
}
);
console.log(`Deployment info saved to ${deployedContractPath}`);
process.exit();
})
.on('error', function (error) {
console.log('Error on creating contract: ' + error);
process.exit();
});