This repository has been archived by the owner on Mar 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
deployContract.js
144 lines (123 loc) · 4.96 KB
/
deployContract.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
'use strict';
/**
* @fileoverview Nodejs Program to deploy ethereum smart contracts. See deploy method for sample code.
* @author [email protected] (Rachin Kapoor)
*/
const fs = require('fs');
const path = require('path');
const PerformerBase = require('./PerformerBase');
class Performer extends PerformerBase {
constructor(program) {
super(program);
let config = this.getSetupConfig();
let openST = this.openST;
let contractArgs = this.parseArguments(program.args || []),
jsonInterfacePath = program.abi,
byteCodePath = program.bin;
this.jsonInterface = this.buildAbi(jsonInterfacePath);
this.byteCode = this.buildBin(byteCodePath);
//Set the deployer params.
this.deployParams = {
from: config.deployerAddress,
gasPrice: config.gasPrice,
gas: config.gas
};
this.contractArgs = contractArgs;
}
deploy() {
//1. Get web3 object from openST.
let web3 = this.openST.web3();
//2. Create an instance of contract.
let contract = new web3.eth.Contract(this.jsonInterface);
//3. Deploy the contract.
try {
this.log('Deploying Contract.');
contract
.deploy({
data: this.byteCode,
arguments: this.contractArgs || []
})
.send(this.deployParams)
.on('receipt', (receipt) => {
this.logReceipt(receipt);
if (receipt.status && receipt.contractAddress) {
this.exitWithoutError('Deployed Contract Address:', receipt.contractAddress);
} else {
this.exitWithError('Failed to deploy contract. See receipt for details.');
}
})
.catch((reason) => {
this.logError(e);
this.exitWithError('Failed to deploy contract. See error for details.');
});
} catch (e) {
this.logError(e);
this.exitWithError('Failed to deploy contract. See error details.');
}
}
buildAbi(abiInPath) {
let abiPath = null;
try {
abiPath = path.resolve(abiInPath);
let abiContent = fs.readFileSync(abiPath, { encoding: 'utf8' });
let abi = JSON.parse(abiContent);
return abi;
} catch (e) {
this.logError(e);
let error = new Error('Invalid ABI Path: ' + (abiPath || abiInPath) + '\nPlease provide contract Abi using --abi flag');
this.exitWithError(error);
}
}
buildBin(binInPath) {
let binPath;
try {
binPath = path.resolve(binInPath);
let binContent = fs.readFileSync(binPath, { encoding: 'utf8' });
if (binContent.indexOf('0x') !== 0) {
binContent = '0x' + binContent;
}
return binContent;
} catch (e) {
this.logError(e);
let error = new Error('Invalid BIN Path: ' + (binPath || binInPath) + '\nPlease provide contract Bin using --bin flag');
this.exitWithError(error);
}
}
}
let fileName = 'deployContract.js';
const program = PerformerBase.getProgram();
program
.usage('[constructor_arguments...] [options]')
.option('--abi [file]', 'Required. Path to smart-contract Abi (Application Binary Interface) file.')
.option('--bin [file]', 'Required. Path to smart-contract Bin (Binary) file.');
program.on('--help', function() {
console.log('');
console.log('');
console.log(' \x1b[1m Deploy contract without any constructor arguments :\x1b[0m');
console.log(
` $ node ${fileName} --abi ./node_modules/\\@openstfoundation/openst.js/contracts/abi/MockToken.abi --bin ./node_modules/\\@openstfoundation/openst.js/contracts/bin/MockToken.bin`
);
console.log('');
console.log('');
console.log(' \x1b[1m Deploy contract with multiple constructor arguments:\x1b[0m');
console.log(
` $ node ${fileName} 0x00ebec794aa82bc98e753865a5ed9f339c8fd81d 0xe34d081dC576B04DDEDAf1087BB803dea256AE89 \x1b[2m --abi ./node_modules/\\@openstfoundation/openst.js/contracts/abi/TokenRules.abi --bin ./node_modules/\\@openstfoundation/openst.js/contracts/bin/TokenRules.bin \x1b[0m`
);
console.log('');
console.log('');
console.log(' \x1b[1m Deploy contract with single constructor argument:\x1b[0m');
console.log(
` $ node ${fileName} 0xa502c51c8213A4e61Dc59dF914e252EB6354A8c0 \x1b[2m --abi ./node_modules/\\@openstfoundation/openst.js/contracts/abi/TransferRule.abi --bin ./node_modules/\\@openstfoundation/openst.js/contracts/bin/TransferRule.bin \x1b[0m`
);
console.log('');
console.log('');
console.log(' \x1b[1m Deploy contract with an argument of type array:\x1b[0m');
console.log(
` $ node ${fileName} \x1b[2m 0x00ebec794aa82bc98e753865a5ed9f339c8fd81d 0xa502c51c8213A4e61Dc59dF914e252EB6354A8c0 2 \x1b[0m\x1b[1m '["0xbba2c47be3add4fd302d9a8122442ca9d65ad9a3","0x39e76d2c955462674cd2dab10dbf46135dd2af24"]' \x1b[0m\x1b[2m --abi ./node_modules/\\@openstfoundation/openst.js/contracts/abi/TokenHolder.abi --bin ./node_modules/\\@openstfoundation/openst.js/contracts/bin/TokenHolder.bin \x1b[0m`
);
console.log('');
console.log('');
});
program.parse(process.argv);
let performer = new Performer(program);
performer.deploy();