Skip to content

Instantly share code, notes, and snippets.

@AlwaysBCoding
Last active November 2, 2018 20:57
Show Gist options
  • Save AlwaysBCoding/febd6d88341b5eeb06b29e96fffbb632 to your computer and use it in GitHub Desktop.
Save AlwaysBCoding/febd6d88341b5eeb06b29e96fffbb632 to your computer and use it in GitHub Desktop.
Ethereum Ðapp Development - Video 7 | Smart Contracts - Hello World
// package.json
{
"dependencies": {
"web3": "0.17.0-alpha",
"solc": "^0.4.4"
}
}
// HelloWorld.sol
contract HelloWorld {
function displayMessage() constant returns (string) {
return "Hello from a smart contract";
}
}
@alx
Copy link

alx commented Apr 19, 2017

From DecypherTV slack #ethereum channel:

the solc API changed in the latest version to include the semicolon in the destructuring code.

compiled.contracts.HelloWorld.bytecode

needs to be replaced with

compiled.contracts[“:HelloWorld”].bytecode

@ryanhagerty
Copy link

ryanhagerty commented Aug 12, 2017

If anyone is using a JS file instead of the node console and runs into the error TypeError: Cannot read property 'call' of undefined, you need to check for the address first with no need to use call(). For example:

const deployed = helloWorldContract.new({
  from: web3.eth.accounts[0],
  data: compiled.contracts[':HelloWorld'].bytecode,
  gas: 151972,
  gasPrice: 5
}, (error, contract) => {
    if (error) {
      console.error(error);
    } else {
      if (contract.address) {
        console.log(contract.displayMessage());
      }
    }
});

@hugoroussel
Copy link

Even using
compiled.contracts[“:HelloWorld”].bytecode
I still get an error, and I haven't managed to paste the bytecode from an external compiler. Any help?

@haidoan
Copy link

haidoan commented Nov 19, 2017

the variable 'abi' need to be JSON object not a string, or else creating deployed variable will raise error "abi.filter is not a function" .
code : var abi = JSON.parse(compiled.contracts[':HelloWorld'].interface);

@zachgoll
Copy link

zachgoll commented Nov 20, 2017

pragma solidity ^0.4.0;
contract HelloWorld {
	function displayMessage() public pure returns(string){
		return "hello from smart contract";
	}
}

If getting errors while compiling, this is the code that worked for me.

@NathanMaton
Copy link

@ndsvw
Copy link

ndsvw commented Mar 3, 2018

I combined your ideas and it worked:

const Web3 = require("web3");
const solc = require("solc");
let web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

let source = `pragma solidity ^0.4.20;
contract HelloWorld {
  function displayMessage() pure public returns (string) { return "Hello from a smart contract"; }
}
`

var compiled = solc.compile(source);



// console.log(compiled.contracts[":HelloWorld"].bytecode);

// console.log();

// console.log(compiled.contracts[":HelloWorld"].opcodes);

// console.log();

// console.log(compiled.contracts[":HelloWorld"].interface);



//save public interface of contract
var abi = JSON.parse(compiled.contracts[":HelloWorld"].interface)

//create var with contract
var hwContract = web3.eth.contract(abi)


const deployed = hwContract.new({
  from: web3.eth.accounts[0],
  data: compiled.contracts[':HelloWorld'].bytecode,
  gas: 4700000,
  gasPrice: 10
}, (error, contract) => {
    if (error) {
      console.error(error);
    } else {
      if (contract.address) {
        console.log(contract.displayMessage());
      }
    }
});

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