0

Let's say my solidity code is

contract MetaCoin {

uint256 public balance = 0;

function getBalance() public view returns(uint) {
    return balance
   }

}

Let's say I want to call the getBalance function in my javascript tests; I have 2 ways to do it

const MetaCoin = artifacts.require("MetaCoin");
const metaCoinInstance = await MetaCoin.deployed();
// Way 1 using call
const metaCoinBalance = (await metaCoinInstance.getBalance.call()).toNumber();
// Way 2 without using call
const metaCoinBalance = (await metaCoinInstance.getBalance()).toNumber();

Which way is better and what's the difference between the 2 ways of calling?

0

I would say it's mostly a preference, but I would go with the .call(...) version. The effect is exactly the same, but the .call(...) version clearly indicates to the reader that this is a constant method call that doesn't modify the state, where as you would use .send(...) for calling a non-constant method that does modify the state.

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.