Skip to content

Instantly share code, notes, and snippets.

@iisaint
Created March 1, 2017 05:28
Show Gist options
  • Save iisaint/ccd7f538eb63703579ff8455a33d355b to your computer and use it in GitHub Desktop.
Save iisaint/ccd7f538eb63703579ff8455a33d355b to your computer and use it in GitHub Desktop.
Deploy a compiled smart contract
---- 前面省略 ----
/*
* deploy contract
*/
let gasEstimate = web3.eth.estimateGas({data: '0x' + bytecode});
console.log('gasEstimate = ' + gasEstimate);
let MyContract = web3.eth.contract(abi);
console.log('deploying contract...');
let myContractReturned = MyContract.new([], {
from: address,
data: '0x'+ bytecode,
gas: gasEstimate + 50000
}, function (err, myContract) {
if (!err) {
// NOTE: The callback will fire twice!
// Once the contract has the transactionHash property set and once its deployed on an address.
// e.g. check tx hash on the first call (transaction send)
if (!myContract.address) {
console.log(`myContract.transactionHash = ${myContract.transactionHash}`); // The hash of the transaction, which deploys the contract
// check address on the second call (contract deployed)
} else {
console.log(`myContract.address = ${myContract.address}`); // the contract address
global.contractAddress = myContract.address;
}
// Note that the returned "myContractReturned" === "myContract",
// so the returned "myContractReturned" object will also get the address set.
} else {
console.log(err);
}
});
(function wait () {
setTimeout(wait, 1000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment