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"
    },
Aniket
  • 3,545
  • 2
  • 20
  • 42
Jorge
  • 309
  • 1
  • 5
  • 12

2 Answers2

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

Ismael
  • 30,570
  • 21
  • 53
  • 96
  • 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 '19 at 18:29
  • 1
    There are not official recommendations, you can use any library/framework you are comfortable with. – Ismael May 30 '19 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 '19 at 20:51
  • 1
    See this https://ethereum.stackexchange.com/questions/765/what-is-the-difference-between-a-transaction-and-a-call to understand when to use them. Create a new question if you have more doubts. – Ismael May 30 '19 at 21:10
1

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

Praneeth Pj
  • 111
  • 2