0

I've successfully deployed on Ganache test network a contract with this test method in it:

function test() public pure returns (uint) {
    return 7;
}

In my JS script I try to call this method in this way:

var abi = JSON.parse('...'); // 
var contract = new web3.eth.Contract(abi, '0xf1447c6d7Bff526411fFfC68502017Ce9a0AE54a'); // contract mined at this address
contract.methods.test().call().then(function(e,r) {
    console.log(e,r);
});

The console.log gives me this result (instead of 7):

false, undefined

I don't think it's an address problem (i've tried to change it and tthe script raises an error before call the method).

Any idea?

  • This doesn't answer why you're getting false instead of 7, but your callback will only receive one parameter: the result of the call. If you want to catch errors, add a .catch(function (error) { ... }). – user19510 Mar 6 '18 at 18:06
  • You're right @smarx, thank's for the clarification – imfabio Mar 6 '18 at 19:11
1

It's hard to detect what's wrong from your code. Formally seems right, if the address is correct what about the abi? If you can post the whole code (or a way to reproduce it) might be more easy to understand what's wrong.

I've used web3 1.0 recently and I had no issue on calling contract methods as per doc

// Solidity
contract MyContract {
    function myFunction() returns(string myString) {
        return "Hello!%";
    }
}

// web3.js
var MyContract = new web3.eth.contract(abi, address);
MyContract.methods.myFunction().call()
.then(console.log);
> "Hello!%"

https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#id15

0

Loading the contract using abi and address is asynchronous. For the safer side use async-await-Promise or then(). Below is example of using Promise.

foo()
async foo():Promise<return_type>{
 var myContract = await new web3.eth.contract(abi, address);
 return new Promise((resolve, reject) => {
  let _web3 = this._web3;
  myContract.function.call(account, function (err, result) {
    if(err != null) {
      reject(err);
    }

    resolve(result);
  });
})as Promise<return_type>;
}
0

The call method you used is correct and should work fine.

If you are not able to call the function the only thing that might cause an issue here is the deployment of the contract (in case you are deploying it from web3@1.0). Did you deploy it correctly? In case you want to deploy the contract, I would suggest you to use the query mentioned below.

var sampleContract = new web3.eth.Contract(JSON.parse(<your contract abi>));
sampleContract.options.data = '0x'+<your contract byteCode>; //No Need to include '0x' if you have already included it as prefix in the byteCode.
var contractAddress;
sampleContract.deploy({arguments:[<in case any>]}).send({from : accountAddress, gas: <your gas limit for contract>}).then(function(receipt) => {
  if(receipt) {
     contractAddress = receipt.options.address;
     //here receipt is nothing but contractInstanceFromDeployedContract
  }
}).catch(function(error){
   console.log(error);
});

I hope this should resolve the issue in case your deployment is from web3 and now you should be able to call the function.
In case this fails to execute try putting more gas than estimatedGas because sometimes it runs out of gas while calling. And try to include a callback in call() function call((err, res) => {});.

0

I got this error when trying to access a contract that didn't exist. I had accidentally deployed my contract to Remix-IDE's Javascript VM rather than ganache-cli.

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.