[Ethereum] Testing transfer of tokens with truffle

soliditytruffletruffle-test

I Have a Token contract:

pragma solidity ^0.4.24;

import "openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol";

contract MyToken is StandardToken{

    string public name = "MyToken";
    string public symbol = "MYT";
    uint8 public decimals = 10;
    uint public INITIAL_SUPPLY = 100000000000000000;

    constructor() public {
        totalSupply_ = INITIAL_SUPPLY;
        balances[msg.sender] = INITIAL_SUPPLY;
    }

}

And the following tests:

pragma solidity ^0.4.0;

import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/MyToken.sol";

contract TestMyToken{
    MyToken mytoken;

    constructor() public{
       mytoken  = MyToken(DeployedAddresses.MyToken());

    }

    // Testing the adopt() function
    function testTotalSupply() public {
      uint returned = mytoken.totalSupply();
      uint expected = 100000000000000000;
      Assert.equal(returned, expected, "Total Supply should be 100000000000000000.");
    }

    function testTransferFrom() public  {
        address _to = 0x89EAB984AbB3E7Cc0f847dc321fCD9B95a538f05;

        bool result = mytoken.transfer( _to, 1);
        Assert.isTrue(result, "Transfer should succeed.");
    }

}

I am using truffle with Ganache. When I execute the tests I get:

    truffle test
Using network 'development'.

Compiling ./test/TestMyToken.sol...


  TestMyToken
    ✓ testTotalSupply (58ms)
    1) testTransferFrom
    > No events were emitted


  1 passing (3s)
  1 failing

  1) TestMyToken
       testTransferFrom:
     Error: Returned error: VM Exception while processing transaction: revert
      at Object.ErrorResponse (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-core-helpers/src/errors.js:29:1)
      at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3-core-requestmanager/src/index.js:140:1
      at /usr/local/lib/node_modules/truffle/build/webpack:/packages/truffle-provider/wrapper.js:101:1
      at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-providers-http/src/index.js:79:1)
      at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request-event-target.js:34:1)
      at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request.js:208:1)
      at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request.js:318:1)
      at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request.js:289:47)
      at endReadableNT (_stream_readable.js:1092:12)
      at process._tickCallback (internal/process/next_tick.js:63:19

The address:

address _to = 0x89EAB984AbB3E7Cc0f847dc321fCD9B95a538f05;

is one of the test addresses that Ganache provides on startup.

From what I have read, I believe that
"Error: Returned error: VM Exception while processing transaction: revert" indicates that the address is not valid. Is that right? Is it a problem with the _to address?

How do I work with the test addresses provided by Ganache in my tests?

What is the correct way to test the transfer of MyToken tokens (not ether) from one address to another?

Update:

I modified MyToken to be Burnable. Then I added this test:

function testBurn() public {
    mytoken.burn(1000);
    uint balance = mytoken.balanceOf(msg.sender);
    uint expected = 100000000000000000 - 1000;
    Assert.equal(balance, expected, "Balance should be reduced.");

}

That test also fails. I feel like I'm missing something here. Reads work ok, but if I try and modify data it's reverting.

  1) TestMyToken
       testBurn:
     Error: Returned error: VM Exception while processing transaction: revert
      at Object.ErrorResponse (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-core-helpers/src/errors.js:29:1)
      at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3-core-requestmanager/src/index.js:140:1
      at /usr/local/lib/node_modules/truffle/build/webpack:/packages/truffle-provider/wrapper.js:101:1
      at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-providers-http/src/index.js:79:1)
      at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request-event-target.js:34:1)
      at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request.js:208:1)
      at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request.js:318:1)
      at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request.js:289:47)
      at endReadableNT (_stream_readable.js:1092:12)
      at process._tickCallback (internal/process/next_tick.js:63:19)

Best Answer

The problem was with the constructor:

constructor() public{
   mytoken  = MyToken(DeployedAddresses.MyToken());

}

Should be:

constructor() public{
   mytoken  = new MyToken();

}

Thanks to @dwarfu over at ConsenSys gitter who found the solution.

Related Topic