-
-
Save AlwaysBCoding/febd6d88341b5eeb06b29e96fffbb632 to your computer and use it in GitHub Desktop.
// 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"; | |
} | |
} |
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());
}
}
});
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?
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);
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());
}
}
});
From DecypherTV slack #ethereum channel: