-
Notifications
You must be signed in to change notification settings - Fork 5
/
deploy.js
56 lines (52 loc) · 2.06 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
import Promise from 'bluebird';
import * as fs from 'fs';
import solc from 'solc';
import Web3 from 'web3';
// Connect to a local Ethereum node over RPC.
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
// Create a promise from the web3.js contract instance
// in order to use other async methods on it.
const deployContractPromise = (contractName, contractInstance, params) => new Promise((resolve, reject) => {
const deployCallback = (error, contract) => {
if (error) {
return reject(error);
} else {
if (contract.address) {
return resolve({
name: contractName,
address: contract.address,
abi: JSON.stringify(contract.abi),
command: `var myContract = web3.eth.contract(${JSON.stringify(contract.abi)}).at("${contract.address}");`,
});
} else {
console.log(`TransactionHash: ${contract.transactionHash} waiting to be mined...`);
}
}
};
const deployParams = params.concat([deployCallback]);
contractInstance.new(...deployParams);
});
// Build an options object with contract metadata
// for web3.js to consume for deployment and mining
// into the blockchain.
const buildDeployOptions = (compiledContract) => {
// prepend 0x so geth can parse
const bytecode = `0x${compiledContract.bytecode}`;
const gasEstimate = web3.eth.estimateGas({data: bytecode});
return {
from: web3.eth.accounts[0],
data: bytecode,
gas: (gasEstimate*2),
};
};
const folder = process.env.CONTRACT_PATH;
const contractConfig = require(`./${folder}/config`);
const source = fs.readFileSync(`./${folder}/contract.sol`, 'utf8');
const compiled = solc.compile(source, 1);
const compiledContract = compiled.contracts[`:${contractConfig.contractName}`];
const abiObject = JSON.parse(compiledContract.interface);
const contract = web3.eth.contract(abiObject);
const options = buildDeployOptions(compiledContract);
const args = contractConfig.params.concat([options]);
deployContractPromise(contractConfig.contractName, contract, args)
.then((results) => console.log(results));