0

I have this function, which is receiving a wallet address and an array of tokens, and returns the balances of every token for this wallet.

function getBalances(address wallet, IERC20[] tokens) external view returns (uint256[] memory balances) {

    uint size = tokens.length;

    uint256[] memory balances = new uint256[](size) 
    for (uint i=0; i < size; i++) {
            
        TokenContract tokenContract = TokenContract(tokens[i]);

        balances[i] = tokenContract.balanceOf(wallet);

    }
        
    return balances;

}

But I am concerned about the order of the input tokens. If the order is the same like the user sent them.

For example: user sends [t1, t2, t3, t4], for-loop is iterating in the same order, and the return is like [b1, b2, b3, b4].

Or the order is not 100% guaranteed? In this case, can you give me an idea about how to return the balances for the user to be able to know which token represents?

1 Answer 1

2

The order of tokens here is indeed the same as the one provided by the caller of getBalances, so as you said:

user sends [t1, t2, t3, t4], for-loop is iterating in the same order, and the return is [b1, b2, b3, b4].

where bi is the ti's balance for wallet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.