0

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)
  • when you are deploying it with ganache the first account is used for deploying.if 0x89EAB984AbB3E7Cc0f847dc321fCD9B95a538f05 is the first address it won't transfer.As in the trasfer function there is this statement require(_to != address(0)); .so let me know after changing the address to some other are you able to run test cases. – AVATAR Oct 5 '18 at 14:10
  • Account 0 is 0x7D89759dA24D97B1497d5EB7260E33D30b24c040. I am using the second account (index=1) to transfer to. 0x89EAB984AbB3E7Cc0f847dc321fCD9B95a538f05 – Mark Irvine Oct 5 '18 at 14:19
  • which version of truffle are you using ? – AVATAR Oct 5 '18 at 14:59
  • Truffle v5.0.0-beta.1 – Mark Irvine Oct 5 '18 at 15:06
  • I updated the description with another test. Seems like I can't burn tokens either. Any reads seem to work. I can test balanceOf on msg.sender and it passes. I can test balanceOf the 0x89... address and it passes. but can't transfer tokens or burn tokens, – Mark Irvine Oct 5 '18 at 15:09
0

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.

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.