3

I have ERC20 standard token functions in my code...I´m trying to test the function balanceOf(address _owner) with a javascript test on truffle but I have the following problem:

 TypeError: ovxet.balanceOf(...).call is not a function
  at test/test1.js:48:46

It´s weird, because I always pay attention on how I define the functions in the code and how I have to call them...

I´ve tried in my test in both ways (async/await and promise chaning) but the result is the same "is not a function":

it("should call balance", function() {
    account1 = accounts[0];
    return OVXET.deployed().then(function(instance) {
        ovxet = instance;
        return ovxet.balanceOf(account1).call({from: account1});
    }).then(function(balance){
        console.log(balance);
    });
});

And this async/await call inside another promise:

    var balance2 = await ovxet.balanceOf(account1).call({from: account1});
    console.log("balance using erc20 function: " + balance2.toNumber());

Here you have the code inside the .sol script:

function balanceOf(address _owner) public constant returns (uint balance) {
    return _sumBalances(_owner);
}

It´s really strange because It´s so easy coding-function...so I am really frustrating with this situation...

This is the ABI part related to this function:

{
      "constant": true,
      "inputs": [
        {
          "name": "_owner",
          "type": "address"
        }
      ],
      "name": "balanceOf",
      "outputs": [
        {
          "name": "balance",
          "type": "uint256"
        }
      ],
      "payable": false,
      "stateMutability": "view",
      "type": "function"
    },
3

In Truffle v4.xx you should be able to call a functions like this

return OVXET.deployed().then(function(instance) {
    ovxet = instance;
    return ovxet.balanceOf.call(account1, {from: account1});
}).then(function(balance){
    console.log(balance);
});

The syntax is contract.method.call(param1, param2, ..).

  • Is it recommended to make function calls like that? I've seen it done in jquery plugins as well. Are there some benefits? – nulltron May 30 at 18:29
  • There are not official recommendations, you can use any library/framework you are comfortable with. – Ismael May 30 at 19:08
  • I've done some digging, and while writing tests if you use contract.method.call() then a transaction is not actually executed on the test network. However if you call the method like contract.method() then a transaction will be executed on the test network. So using .call() will actually allow you to run tests on return values without executing transactions on the network. – nulltron May 30 at 20:51
  • 1
    See this ethereum.stackexchange.com/questions/765/… to understand when to use them. Create a new question if you have more doubts. – Ismael May 30 at 21:10

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.