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"
    },

2 Answers 2

4

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, ..).

4
  • Is it recommended to make function calls like that? I've seen it done in jquery plugins as well. Are there some benefits?
    – nulltron
    Commented May 30, 2019 at 18:29
  • 1
    There are not official recommendations, you can use any library/framework you are comfortable with.
    – Ismael
    Commented May 30, 2019 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
    Commented May 30, 2019 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
    Commented May 30, 2019 at 21:10
1

You can use Let balance = Token.balanceOf(accountB).then(b => { return b.toNumber() })

1
  • I think you are missing an await.
    – Ismael
    Commented Feb 21, 2022 at 6:02

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.