22

I´m having problem with the basic HelloWorld example coded in Solidity and managed with truffle with testrpc.

pragma solidity ^0.4.2;

contract HelloWorld { uint public balance;

function HelloWorld() { balance = 1000; } }

enter image description here

>  truffle(development)>
> HelloWorld.deployed().balance.call().then(console.log) TypeError:
> Cannot read property 'call' of undefined
>     at evalmachine.<anonymous>:1:30
>     at ContextifyScript.Script.runInContext (vm.js:35:29)
>     at Object.exports.runInContext (vm.js:67:17)
>     at TruffleInterpreter.interpret (c:\Users\whha\AppData\Roaming\npm\node_modules\truffle\lib\repl.js:128:17)
>     at bound (domain.js:280:14)
>     at REPLServer.runBound [as eval] (domain.js:293:12)
>     at REPLServer.<anonymous> (repl.js:545:10)
>     at emitOne (events.js:96:13)
>     at REPLServer.emit (events.js:188:7)
>     at REPLServer.Interface._onLine (readline.js:239:10) truffle(development)>

The problem looked similar to this thread, but it does not solve my problem:

The contract is properly deployed and has a valid JSON document, I really don´t understand why I cannot access its members. The only one that return value is a constructor.

truffle(development)> HelloWorld.deployed().constructor
[Function: Object]

Any ideas??

2 Answers 2

24

If you are running the latest version of Truffle (v.3.x), the way to call a function or variable in your contract has evolved a little bit according to the Upgrading documentation

In Truffle 2.0, your contract abstractions managed your networks in a naive way, and added constructs like a "default network" that opened up the possibility of using the wrong network artifacts and deployed addresses at the wrong time. This provided a fancy and easy to use syntax – i.e., MyContract.deployed().myFunction(...) – but it left your code open to errors. Truffle 3.0 changes this syntax, where .deployed() is now thennable, like a promise (see example below). Similarly, this makes your contract abstractions seamlessly integrate with EIP 190, Ethereum's package management standard. All of this is for the better, but it means you'll have some changes to make all across the board.

So in your case, if you want to call a function, you have to do this:

HelloWorld.deployed().then(function(instance) {
    return instance.balance.call();
}).then(function(balance) {
    console.log(balance);
});

Please refer to the latest documentation for more details.

0
11

you could also use :

HelloWorld.deployed().then(helloworld=> console.log(helloworld.balance.call()))

or

HelloWorld.deployed().then(function(instance){helloworld=instance})

then

helloworld.balance.call()
2
  • 1
    this answer is what you need to get the results from the console that they demonstrate here: youtube.com/watch?v=8jI1TuEaTro @BadrBellaj nice work :) Commented Jul 27, 2017 at 0:55
  • Even it calls a getter function I see an Gas usage: 27034 on the ganache console, shouldn't be free to call view() functions? @Badr Bellaj♦
    – alper
    Commented Apr 26, 2019 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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