Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Compiling and deploying an Ethereum Smart Contract, using solc and web3.
const fs = require('fs');
const solc = require('solc');
const Web3 = require('web3');
// Connect to local Ethereum node
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
// Compile the source code
const input = fs.readFileSync('Token.sol');
const output = solc.compile(input.toString(), 1);
const bytecode = output.contracts['Token'].bytecode;
const abi = JSON.parse(output.contracts['Token'].interface);
// Contract object
const contract = web3.eth.contract(abi);
// Deploy contract instance
const contractInstance ={
data: '0x' + bytecode,
from: web3.eth.coinbase,
gas: 90000*2
}, (err, res) => {
if (err) {
// Log the tx, you can explore status with eth.getTransaction()
// If we have an address property, the contract was deployed
if (res.address) {
console.log('Contract address: ' + res.address);
// Let's test the deployed contract
// Quick test the contract
function testContract(address) {
// Reference to the deployed contract
const token =;
// Destination account for test
const dest_account = '0x002D61B362ead60A632c0e6B43fCff4A7a259285';
// Assert initial account balance, should be 100000
const balance1 =;
console.log(balance1 == 1000000);
// Call the transfer function
token.transfer(dest_account, 100, {from: web3.eth.coinbase}, (err, res) => {
// Log transaction, in case you want to explore
console.log('tx: ' + res);
// Assert destination account balance, should be 100
const balance2 =;
console.log(balance2 == 100);

thanks! this is very nice. I'm very new to this and I have a question. I am running a testrpc node on the 8545 port. I am just wondering now how I can actually run this javascript? I installed all the required node modules. Do I just put it in an html and open it with the browser? Thanks!

Hi @tomconte, does this still work for you since the Solidity 0.4.9 update? I keep getting "Cannot read property 'bytecode' of undefined". Cheers

linneacfritz commented Mar 9, 2017 edited

@Shauneoo Hi, I had the same problem you did. And I discovered what I think may be the reason. The output becomes {"contracts":{":NameOfContract":{"assembly"... and so on, with an extra colon in front of NameOfContract. So when you call it in your code, you have to write "const bytecode = output.contracts[':Token'].bytecode;" instead of as before, without the colon. Same thing with "const abi = JSON.parse(output.contracts[':Token'].interface);" Then, the name parses correctly. HTH :)

base698 commented May 26, 2017

Who is the from in this case:

I deployed and it said I don't have enough gas. Using the greeter example contract. I also had to use geth to run rpc unlocked....

ar412 commented Jul 5, 2017 edited

I have Token.sol that it is referring to.
When deployed with node it is giving error:

Error: Cannot find module 'web3'

And with npm
TypeError: Cannot read property 'bytecode' of undefined

ar412 commented Jul 5, 2017 edited

@base698 Can you help me out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment