Last active
November 2, 2018 20:57
-
-
Save AlwaysBCoding/febd6d88341b5eeb06b29e96fffbb632 to your computer and use it in GitHub Desktop.
Ethereum Ðapp Development - Video 7 | Smart Contracts - Hello World
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
// 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"; | |
} | |
} |
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.
Slightly more robust gist for this lesson: https://gist.github.com/NathanMaton/c4b435d1381f196ed4d89a7240874b9a
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
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);