Create a gist now

Instantly share code, notes, and snippets.

Embed
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 = contract.new({
data: '0x' + bytecode,
from: web3.eth.coinbase,
gas: 90000*2
}, (err, res) => {
if (err) {
console.log(err);
return;
}
// Log the tx, you can explore status with eth.getTransaction()
console.log(res.transactionHash);
// 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
testContract(res.address);
}
});
// Quick test the contract
function testContract(address) {
// Reference to the deployed contract
const token = contract.at(address);
// Destination account for test
const dest_account = '0x002D61B362ead60A632c0e6B43fCff4A7a259285';
// Assert initial account balance, should be 100000
const balance1 = token.balances.call(web3.eth.coinbase);
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 = token.balances.call(dest_account);
console.log(balance2 == 100);
});
}
@cgoliver

This comment has been minimized.

Show comment
Hide comment
@cgoliver

cgoliver Dec 18, 2016

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!

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!

@Shauneoo

This comment has been minimized.

Show comment
Hide comment
@Shauneoo

Shauneoo Feb 24, 2017

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

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

This comment has been minimized.

Show comment
Hide comment
@linneacfritz

linneacfritz Mar 9, 2017

@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 :)

linneacfritz commented Mar 9, 2017

@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

This comment has been minimized.

Show comment
Hide comment
@base698

base698 May 26, 2017

Who is the from in this case: https://gist.github.com/tomconte/4edb83cf505f1e7faf172b9252fff9bf#file-web3-solc-contract-compile-deploy-js-L20

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....

base698 commented May 26, 2017

Who is the from in this case: https://gist.github.com/tomconte/4edb83cf505f1e7faf172b9252fff9bf#file-web3-solc-contract-compile-deploy-js-L20

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

This comment has been minimized.

Show comment
Hide comment
@ar412

ar412 Jul 5, 2017

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

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

This comment has been minimized.

Show comment
Hide comment
@ar412

ar412 Jul 5, 2017

@base698 Can you help me out?

ar412 commented Jul 5, 2017

@base698 Can you help me out?

@themandalore

This comment has been minimized.

Show comment
Hide comment
@themandalore

themandalore Aug 26, 2017

For those wondering how to run this:

https://github.com/ethereum/web3.js/

open a command prompt or terminal and type node....then all of the commands should work

For those wondering how to run this:

https://github.com/ethereum/web3.js/

open a command prompt or terminal and type node....then all of the commands should work

@duanyytop

This comment has been minimized.

Show comment
Hide comment
@duanyytop

duanyytop Nov 8, 2017

The problem can be resolved by changing the name of Token. If you print the json string ,you will find the property of 'Token' should be ':Token'. So just change '[Token]' to '[:Token]'. Best wishes!

The problem can be resolved by changing the name of Token. If you print the json string ,you will find the property of 'Token' should be ':Token'. So just change '[Token]' to '[:Token]'. Best wishes!

@streamerd

This comment has been minimized.

Show comment
Hide comment
@streamerd

streamerd Jan 3, 2018

Hi all, I am currently having a TypeError;
...

$ node app.js

TypeError: web3.eth.contract is not a function
at Object. (/Users/foo/Developer/btc/contracts/app.js:15:27)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:974:3

--
node -v : v4.6.0
npm -v : 2.15.9
on OSX : 10.12.6 (Sierra)

streamerd commented Jan 3, 2018

Hi all, I am currently having a TypeError;
...

$ node app.js

TypeError: web3.eth.contract is not a function
at Object. (/Users/foo/Developer/btc/contracts/app.js:15:27)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:974:3

--
node -v : v4.6.0
npm -v : 2.15.9
on OSX : 10.12.6 (Sierra)

@streamerd

This comment has been minimized.

Show comment
Hide comment
@streamerd

streamerd Jan 3, 2018

I've changed the code a little bit, seems we need to use 'Contract'' instead of 'contract' while instance generation.

// app.js
// Deploy contract instance

const abi = JSON.parse(output.contracts[':Token'].interface);
const contract = new web3.eth.Contract(abi, '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe', {
    data: '0x' + bytecode,
    from: web3.eth.coinbase,
    gas: 90000*2
}, (err, res) => {
    if (err) {
        console.log(err);
        return;
    }

now there is no error back.
I've referenced from https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html for the address 0x..BAe

streamerd commented Jan 3, 2018

I've changed the code a little bit, seems we need to use 'Contract'' instead of 'contract' while instance generation.

// app.js
// Deploy contract instance

const abi = JSON.parse(output.contracts[':Token'].interface);
const contract = new web3.eth.Contract(abi, '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe', {
    data: '0x' + bytecode,
    from: web3.eth.coinbase,
    gas: 90000*2
}, (err, res) => {
    if (err) {
        console.log(err);
        return;
    }

now there is no error back.
I've referenced from https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html for the address 0x..BAe

@VanThanh1812

This comment has been minimized.

Show comment
Hide comment
@VanThanh1812

VanThanh1812 Apr 12, 2018

Anyone can help me, when I use solc and build in webpack, I get error: Uncaught TypeError: Cannot read property 'prototype' of undefined
at patch (graceful-fs.js:166) and load web fail? Thank you

VanThanh1812 commented Apr 12, 2018

Anyone can help me, when I use solc and build in webpack, I get error: Uncaught TypeError: Cannot read property 'prototype' of undefined
at patch (graceful-fs.js:166) and load web fail? Thank you

@ADitya024

This comment has been minimized.

Show comment
Hide comment
@ADitya024

ADitya024 Apr 19, 2018

Hello guys,
I am facing a error : TypeError: Cannot read property 'bytecode' of undefined
occuring on line : const bytecode = output.contracts['Token'].bytecode;

ADitya024 commented Apr 19, 2018

Hello guys,
I am facing a error : TypeError: Cannot read property 'bytecode' of undefined
occuring on line : const bytecode = output.contracts['Token'].bytecode;

@izotx

This comment has been minimized.

Show comment
Hide comment
@izotx

izotx Jun 19, 2018

@ADitya024
Change it to:
output.contracts['Token']["bytecode"];

izotx commented Jun 19, 2018

@ADitya024
Change it to:
output.contracts['Token']["bytecode"];

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