0

I made a Wallet Contract to store all the tokens an account possess.

There is a mapping(address=>TokenInfo[]), and 'address' refers to the account, TokenInfo[] stored tokens which this account holds. TokenInfo is an array of TokenInfo struct, in which 'token' is the token address, 'exist' means whether the token has been added to the array.

contract Wallet {
    struct TokenInfo {
        IERC20Token token;
        uint exist;//0 means unregistered
    }

    mapping(address=>TokenInfo[]) public tokenList;

    function Wallet() {

    }

    function addToken(address _account, IERC20Token _token) public {
        for (uint i = 0; i < tokenList[_account].length; i++) {//seems didn't enter the loop
            if (tokenList[_account][i].exist == 0) {
                var tokenInfo = TokenInfo(_token, 1);
                tokenList[_account].push(tokenInfo);
            }
        }
    }

    function getTokenListLength(address _account) public constant returns (uint) {
        return tokenList[_account].length;
    }
}

But when I call 'addToken' function, it didn't work. It seems that it never entered the loop.

1

The default value in a mapping for an array is an empty array.

In your case this means tokenList[_account].length is equal to 0.

And the loop will never be entered because the condition i < tokenList[_account].length becames 0 < 0 which is false.

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.