Created
April 4, 2019 08:30
-
-
Save dbezrukov/a104ba406746ea6ea2218ce3ccca6a5a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Web3 = require('web3'); | |
const solc = require('solc'); | |
// For localhost | |
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); | |
let source = 'contract Hello{ string h = "hello"; function g() constant returns(string){ return h; } }'; | |
let helloCompiled = solc.compile(source, 1).contracts[':Hello']; | |
You may need to unlock the address which is going to deploy the contract (Make sure there is enough funds on this address): | |
web3.eth.personal.unlockAccount(<address>, <password>). | |
then(() => { console.log('Account unlocked.'); }). | |
catch(console.error); | |
let Hello = new web3.eth.Contract(JSON.parse(helloCompiled.interface), null, { | |
data: '0x' + helloCompiled.bytecode | |
}); | |
OPTIONAL: Use average gas price to deploy (If you use too low gas price, the transaction may get stuck): | |
web3.eth.getGasPrice(). | |
then((averageGasPrice) => { | |
console.log("Average gas price: " + averageGasPrice); | |
gasPrice = averageGasPrice; | |
}). | |
catch(console.error); | |
Estimate gas to deploy: | |
Hello.deploy().estimateGas(). | |
then((estimatedGas) => { | |
console.log("Estimated gas: " + estimatedGas); | |
gas = estimatedGas; | |
}). | |
catch(console.error); | |
And finally deploy: | |
Hello.deploy().send({ | |
from: <address>, | |
gasPrice: gasPrice, | |
gas: gas | |
}).then((instance) => { | |
console.log("Contract mined at " + instance.options.address); | |
helloInstance = instance; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment