0

When I run tests with the following code, I get the following error:

Error

Error: Returned error: VM Exception while processing transaction: revert

Tests

  it('Handles delegated token transfers', () => {
    return DappToken.deployed().then(instance => {
      tokenInstance = instance
      fromAccount = accounts[2]
      toAccount = accounts[3]
      spendingAccount = accounts[4]
      return tokenInstance.transfer(fromAccount, 100, { from: accounts[0] })
    }).then(receipt => {
      // Approve spendingAccount to spend 10 tokens to fromAccount
      return tokenInstance.approve(spendingAccount, 10, { from: accounts[0] })
    }).then(receipt => {
      // Try transferrin something larger than the sender´s balance
      return tokenInstance.transferFrom(fromAccount, toAccount, 9999, { from: spendingAccount })
    }).then(assert.fail).catch(error => {
      assert(error.message.indexOf('revert') >= 0, 'Cannot transfer value larger than balance')
      // Try transfering something larger than the approved amount
      return tokenInstance.transferFrom(fromAccount, toAccount, 20, { from: spendingAccount }) // larger than 10 smaller than 100
    }).then(assert.fail).catch(error => {
      assert(error.message.indexOf('revert') >= 0, 'Cannot transfer value larger than approved amount')

      // ================ Fail from here ======================
      return tokenInstance.transferFrom.call(fromAccount, toAccount, 6, { from: spendingAccount })
    }).then(success => {
      assert.equal(success, true, 'it returns true')
    })
  })

Lines that produce error
If I delete the last lines the tests works:

      ... // THIS CODE PRODUCE THE ERROR
      return tokenInstance.transferFrom.call(fromAccount, toAccount, 6, { from: spendingAccount })
    }).then(success => {
      assert.equal(success, true, 'it returns true')
    })

TransferFrom code in solidity
This is my function in solidity it´s very simple

  function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
    require(_value <= balanceOf[_from]);
    require(_value <= allowance[_from][msg.sender]);

    return true;
  }

I have been looking for a while but I can't find how I can solve it, thank you very much in advance.

2
  • Not answer but an advice, get rid of the .then and use ‘await’ instead :)
    – Majd TL
    Apr 8 at 18:14
  • Thanks for the tip. I change the code using await and it´s look better and I fix the code. :) Apr 9 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.