Skip to content

Instantly share code, notes, and snippets.

@shoupn
Created July 12, 2018 21:13
Embed
What would you like to do?
Web3 Solidity Contract Deployment Script
const solc = require("solc");
const fs = require("fs");
const Web3 = require('web3');
const password = "test";
//web3 provider. can also use ws if started node as websocket
var web3 = new Web3(
new Web3.providers.HttpProvider('http://localhost:8545'));
//Read solidity contract file
const input = fs.readFileSync('./MinimalToken.sol');
console.log('Compiling Contract...');
const output = solc.compile(input.toString(),1);
const bytecode = output.contracts[':MinimalToken'].bytecode;
const abi = output.contracts[':MinimalToken'].interface;
fs.writeFile('contract_abi.json', abi, function (err) {
if(err){
return console.log(err);
}
console.log('Saved abi.json to disk');
})
console.log('Unlocking coinbase account');
let coinbase = web3.eth.getCoinbase((err, coinbase) => {
web3.eth.personal.unlockAccount(coinbase, password);
});
coinbase.then(function(acct, err){
console.log(acct);
const basicStorageContract = new web3.eth.Contract(JSON.parse(abi));
console.log('Deploying contract...');
basicStorageContract.deploy(
{
data:'0x' + bytecode,
arguments: [100000]//or whatever they are in contract constructor
}
).send({
from: acct,
},function(err, res){
if(err){
console.log('error' + err);
}
})
.on('receipt', function(receipt){
// receipt example
console.log(receipt);//txHash
})
.then(function(newContractInstance){
console.log(newContractInstance.options.address); // address from contract deployement
});
});
//catch any errors from coinbase promise
coinbase.catch((err)=>{
console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment