1

I'm testing to create a token in Truffle; it compiles fine, but it won't load the balance of the token in to Metamask. I've set the connection correct: it does load the ETH balances of the Ganache chain correctly.

I'm not sure as to what is going on. This is the contract:

pragma solidity ^0.5.0;

contract TestToken{
    // Track how many tokens are owned by each address.
    mapping (address => uint256) public balanceOf;

    string public name = "TestToken1";
    string public symbol = "TTO";
    uint8 public decimals = 18;

    uint256 public totalSupply = 1000000 * (uint256(10) ** decimals);

    event Transfer(address indexed from, address indexed to, uint256 value);

    constructor() public {

        balanceOf[msg.sender] = totalSupply;

    }


    function isContract(address _addr) private view returns (bool is_contract) {
        uint length;
        assembly {
            //retrieve the size of the code on target address, this needs assembly
            length := extcodesize(_addr)
        }
        return length > 0;
    }

    function transfer(address to, uint256 value) public returns (bool success) {
        bytes memory empty;
        return transfer(to, value, empty);
    }

    function transfer(address to, uint256 value, bytes memory data) public returns (bool) {
        require(balanceOf[msg.sender] >= value);

        balanceOf[msg.sender] -= value;
        balanceOf[to] += value;
        emit Transfer(msg.sender, to, value);

       // if (isContract(to)) {
       //     ITokenReceiver(to).tokenFallback(msg.sender, value, data);
       // }
        return true;
    }

    event Approval(address indexed owner, address indexed spender, uint256 value);

    mapping(address => mapping(address => uint256)) public allowance;

    function approve(address spender, uint256 value)
        public
        returns (bool success)
    {
        allowance[msg.sender][spender] = value;
        emit Approval(msg.sender, spender, value);
        return true;
    }

    function transferFrom(address from, address to, uint256 value)
        public
        returns (bool success)
    {
        require(value <= balanceOf[from]);
        require(value <= allowance[from][msg.sender]);

        balanceOf[from] -= value;
        balanceOf[to] += value;
        allowance[from][msg.sender] -= value;
        emit Transfer(from, to, value);
        return true;
    }
}

As you can see its as basic as it gets. But in the Metamask console I see this:

ui.js:1 failed to load balance for token at 0xaec97fa2a3bd75ac77a431e06e5fa0aeb7e3083e (anonymous) @........

token updating failed Error: [ethjs-query] while formatting outputs from RPC '{"value":{"code":-32603,"message":"Internal JSON-RPC error.","data":{"originalError":{}},"stack":"Error: \"message\" must be a nonempty string.\n at new......

What is causing that RPC error? Is it me that made a mistake or some type of issue between ganache and Metamask?

  • As an update to this, I changed compiler from Truffle to Remix and it worked. Hence I believe Truffle have some issue. with how it compiles or push. – Andy Sep 6 at 14:48
0

I am not sure if this will solve it completely, but your typecasting for totalSupply is being done incorrectly. You should be casting decimals to uint256, not 10. It should be:

uint256 public totalSupply = 1000000 * (10 ** uint256(decimals));

  • Thanks for the catch, it did compile though. In remix, it did show the full supply as intended. Not sure why '10' worked in place of decimals for the compiler. Strange. – Andy Sep 5 at 22:51
0

This might work: MetaMask settings -> Advanced -> Reset Account.

New contributor
1077 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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.