2

I am struggling with a simple hello world contract:

Contract:

pragma solidity ^0.4.4;

contract Greeter {

    function Greeter() {}

    function sayHello() constant returns(string) {
        return "hello";
    }
}

web3 - Javascript code:

if (typeof web3 !== 'undefined') {
  web3 = new Web3(web3.currentProvider);
} else {
  // set the provider you want from Web3.providers
  web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}

var contractArray = JSON.parse('{"contract_name": "Greeter","abi": [{"constant": false,"inputs": [],"name": "sayHello","outputs": [{"name": "","type": "string"}],"payable": false,"type": "function"},{"inputs": [],"payable": false,"type": "constructor"}],"unlinked_binary": "0x6060604052341561000c57fe5b5b5b5b6101118061001e6000396000f300606060405263ffffffff60e060020a600035041663ef5fb05b81146020575bfe5b3415602757fe5b602d60a9565b6040805160208082528351818301528351919283929083019185019080838382156070575b805182526020831115607057601f1990920191602091820191016052565b505050905090810190601f168015609b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60af60d3565b50604080518082019091526005815260d860020a6468656c6c6f0260208201525b90565b604080516020810190915260008152905600a165627a7a723058205c8157cef185e2881f9f3c49092242ef4418bb30363100e00c373e157ebb6a540029","networks": {"1496042639874": {"events": {},"links": {},"address": "0xc90f7594a02ecc8fe27dad58023537da569ebbc6","updated_at": 1496042652979}},"schema_version": "0.0.5","updated_at": 1496042652979}');

var MyContract = web3.eth.contract(contractArray.abi);
var contractAddress = '0x95c9aa274cc63ec2a0b52f1b19e0e67c3d059194';
var contractInstance = MyContract.at(contractAddress);

var result = contractInstance.sayHello.call();
console.log(result);

The call contractInstance.sayHello.call(); ends up throwing this error:

bignumber.js:1190 Uncaught Error: new BigNumber() not a base 16 number: 
    at raise (http://localhost:3000/bower_components/web3/dist/web3.js:14728:25)
    at http://localhost:3000/bower_components/web3/dist/web3.js:14716:33
    at new BigNumber (http://localhost:3000/bower_components/web3/dist/web3.js:13750:28)
    at SolidityTypeString.formatOutputString [as _outputFormatter] (http://localhost:3000/bower_components/web3/dist/web3.js:1110:19)
    at http://localhost:3000/bower_components/web3/dist/web3.js:1634:25
    at SolidityTypeString.SolidityType.decode (http://localhost:3000/bower_components/web3/dist/web3.js:1635:11)
    at http://localhost:3000/bower_components/web3/dist/web3.js:826:29
    at Array.map (native)
    at SolidityCoder.decodeParams (http://localhost:3000/bower_components/web3/dist/web3.js:825:26)
    at SolidityFunction.unpackOutput (http://localhost:3000/bower_components/web3/dist/web3.js:4064:24)

UPDATE:

I am seeing a POST to my testrpc instance (http://localhost:8545/) with this payload:

{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"0x95c9aa274cc63ec2a0b52f1b19e0e67c3d059194","data":"0xef5fb05b"},"latest"]}

This is the result of the call:

{"id":1,"jsonrpc":"2.0","result":"0x"}
1

You need a promise (represents the result of an asynchronous operation)

With call().then(...

http://truffleframework.com/docs/getting_started/contracts#making-a-call

  • Hi! I am calling from the browser, not from the truffle console. Anyway I tried with a promise and the error is the same: contractInstance.sayHello.call().then(function(result){ alert(result); }); – codependent May 29 '17 at 11:21
  • Ok no problem, maybe you should accept your own answer to put forward the solution :) Good day! – vincentLg May 29 '17 at 12:54
0

The problem was I wasn't using the address of the contract but one of the autocreated accounts created by testrcp.

I simply had to change it to the addrest gotten when the contract is deployed:

Running migration: 2_deploy_contracts.js
  Deploying Greeter...
  Greeter: 0x5b0b6ec4fdb542210bcfc9bc7a278a4dd6d40e0c
Saving successful migration to network...
Saving artifacts...

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.