Hice un contrato de billetera para almacenar todos los tokens que posee una cuenta.
Hay un mapping(address=>TokenInfo[])
, y address
se refiere a la cuenta, TokenInfo[] tokens almacenados que contiene esta cuenta. TokenInfo es una matriz de la estructura TokenInfo, en la que token
está la dirección del token, exist
lo que significa si el token se ha agregado a la matriz.
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;
}
}
Pero cuando llamo a addToken()
la función, no funcionó. Parece que nunca entró en el bucle.
El valor predeterminado en una asignación para una matriz es una matriz vacía.
En su caso, esto significa tokenList[_account].length
que es igual a 0.
Y nunca se ingresará al bucle porque la condición i < tokenList[_account].length
se convierte 0 < 0
en falsa.