1

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/

2

Can't believe I found answer on a youtube video comment :

Truffle change to call this way

HelloWorld.deployed().then(a => console.log(a.address))

link to the youtube video : link

Which mean in my case HelloWorld.deployed().then(a => console.log(a.balance)), or HelloWorld.deployed().then(a => console.log(a.balance.then(console.log))), to get answer directly So yes truffle changed their way of accessing contract in newer versions, they should update their doc

1

In order to make life a little bit easier you can also do it like this:

First you can assign the contract to variable:

truffle(development)> HelloWorld.deployed().then(function(instance){helloWorld=instance})

Since balance is a public variable the compiler will create a getter function for you. To get the balance you can call:

truffle(development)> helloWorld.balance.call()

or to assign the balance to a variable you can call:

truffle(development)> var balance = helloWorld.balance.call()

To call the function sayHi you would call:

truffle(development)> helloWorld.balance.sayHi()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.