Skip to content

Instantly share code, notes, and snippets.

@bobquest33
Created May 29, 2018 10:43
Show Gist options
  • Save bobquest33/f5343f1cc787b698f2ff9b48cc47cb38 to your computer and use it in GitHub Desktop.
Save bobquest33/f5343f1cc787b698f2ff9b48cc47cb38 to your computer and use it in GitHub Desktop.
// Copyright 2017 https://tokenmarket.net - MIT licensed
//
// Run with Node 7.x as:
//
// node --harmony-async-await deploy.js
//
let fs = require("fs");
let Web3 = require('web3'); // https://www.npmjs.com/package/web3
// Create a web3 connection to a running geth node over JSON-RPC running at
// http://localhost:8545
// For geth VPS server + SSH tunneling see
// https://gist.github.com/miohtama/ce612b35415e74268ff243af645048f4
let web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
// Read the compiled contract code
// Compile with
// solc SampleContract.sol --combined-json abi,asm,ast,bin,bin-runtime,clone-bin,devdoc,interface,opcodes,srcmap,srcmap-runtime,userdoc > contracts.json
let source = fs.readFileSync("contracts.json");
let contracts = JSON.parse(source)["contracts"];
// ABI description as JSON structure
let abi = JSON.parse(contracts.SampleContract.abi);
// Smart contract EVM bytecode as hex
let code = '0x' + contracts.SampleContract.bin;
// Create Contract proxy class
let SampleContract = web3.eth.contract(abi);
// Unlock the coinbase account to make transactions out of it
console.log("Unlocking coinbase account");
var password = "";
try {
web3.personal.unlockAccount(web3.eth.coinbase, password);
} catch(e) {
console.log(e);
return;
}
console.log("Deploying the contract");
let contract = SampleContract.new({from: web3.eth.coinbase, gas: 1000000, data: code});
// Transaction has entered to geth memory pool
console.log("Your contract is being deployed in transaction at http://testnet.etherscan.io/tx/" + contract.transactionHash);
// http://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// We need to wait until any miner has included the transaction
// in a block to get the address of the contract
async function waitBlock() {
while (true) {
let receipt = web3.eth.getTransactionReceipt(contract.transactionHash);
if (receipt && receipt.contractAddress) {
console.log("Your contract has been deployed at http://testnet.etherscan.io/address/" + receipt.contractAddress);
console.log("Note that it might take 30 - 90 sceonds for the block to propagate befor it's visible in etherscan.io");
break;
}
console.log("Waiting a mined block to include your contract... currently in block " + web3.eth.blockNumber);
await sleep(4000);
}
}
waitBlock();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment