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";
}
}
@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