I made my first very simple HelloWorld smart contract :
pragma solidity ^0.4.4; contract HelloWorld { uint public balance; function sayHi() returns (bool success) { balance = 1000; return true; } } Trying to deploy all using truffle in a testrpc local network My deploy contracts looks like this :
var HelloWorld = artifacts.require("./HelloWorld.sol"); module.exports = function(deployer) { deployer.deploy(HelloWorld); }; I truffle build && truffle migrate and then try to use my contract, I can see that my contract is correctly deployed, I can access my testrpc network (accounts available etc ...) but I try to get my balance :
truffle(development)> var aHelloWorld = HelloWorld.deployed() undefined truffle(development)> aHelloWorld.balance.call().then(console.log) TypeError: Cannot read property 'call' of undefined It doesn't find my balance property ... however if I console.log my aHelloWorld object, I can see that it has a balance property.
and then I called :
truffle(development)> aHelloWorld.toString() '[object Promise]' So it seems that my contract stays as a promise, I tried :
truffle(development)> aHelloWorld.then(function (myContract) { myContract.sayHi().then(); console.log(myContract.balance.call().then(console.log)) }) Promise { <pending> } undefined truffle(development)> { [String: '1000'] s: 1, e: 3, c: [ 1000 ] } It correctly changed my balance, I am happy but reaally confused... I've watched few tutorials and also read some answers here about basic steps like Truffle error: Cannot read property 'call' of undefined It works for them all by doing HelloWorld.deployed().balance.call()...
I mean accessing the contract directly through HelloWorld.deployed() without waiting for the promise ... Is it because the version of truffle I use or am I doing something wrong ?
UPDATE : Even truffle tutorial shows direct access to the deployed() object : http://truffle.readthedocs.io/en/latest/getting_started/testing/