0

A am implementing a custom ERC20 token inheriting from OpenZeppelin's StandardToken. I override some of the functions but did not override approve or allowance. I write a test for approval:

contract('MyToken', async(accounts) => {
it("approve test", async() => {
    let token = await MyToken.deployed();

    let approveResult = await token.approve.call(accounts[1], 20, {from: accounts[0]});
    assert.isTrue(approveResult);

    let allowanceAccountZeroAccountOne = await token.allowance.call(accounts[0], accounts[1]);
    assert.equal(allowanceAccountZeroAccountOne.toNumber(), 20);
  });
});

I get the following result:

  Contract: MyToken
    1) approve test
    > No events were emitted

  1 failing

  1) Contract: MyToken approve test:
     AssertionError: expected 0 to equal 20
      at Context.it (test/mytoken.js:51:12)
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)

It seems that approve returns true, as expected, but the subsequent allowance returns 0 instead of 20. What could be the problem?

PS I tested the contract manually in Remix, and it works as expected.

  • remove .call from token.approve , call doesn;t make state changes – Aniket Mar 10 '18 at 12:40
1

Change

let approveResult = await token.approve.call(accounts[1], 20, {from: accounts[0]});

To

let approveResult = await token.approve.sendTransaction(accounts[1], 20, {from: accounts[0]});
  • Ok, but then I get AssertionError: expected <hash> to be true. Seems that sendTranscation returns the tx hash; how do I get the return value as defined in Solidity code? – Sergei Tikhomirov Mar 10 '18 at 12:54
  • True, if I comment out assert.isTrue(approveResult);, everything works :) Just a follow-up question, don't quite understand what I should use instead of toNumber for booleans. – Sergei Tikhomirov Mar 10 '18 at 13:07
  • @SergeiTikhomirov you check allowance later. sendTransaction doesn’t return solidity returned value. – Roman Frolov Mar 10 '18 at 13:10
  • @SergeiTikhomirov try toString. And in future just check with console.log returned value. – Roman Frolov Mar 10 '18 at 13:17

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.