Skip to content

Instantly share code, notes, and snippets.

@wbobeirne
Last active October 20, 2018 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wbobeirne/77589c0448e16fb8f8bcdb964d64ce3e to your computer and use it in GitHub Desktop.
Save wbobeirne/77589c0448e16fb8f8bcdb964d64ce3e to your computer and use it in GitHub Desktop.
/*
Check the token balances of a wallet for multiple tokens.
Pass 0x0 as a "token" address to get ETH balance.
Possible error throws:
- extremely large arrays for user and or tokens (gas cost too high)
Returns a one-dimensional that's user.length * tokens.length long. The
array is ordered by all of the 0th users token balances, then the 1th
user, and so on.
*/
function balances(address[] users, address[] tokens) external view returns (uint[]) {
uint[] memory addrBalances = new uint[](tokens.length * users.length);
for(uint i = 0; i < users.length; i++) {
for (uint j = 0; j < tokens.length; j++) {
uint addrIdx = j + tokens.length * i;
if (tokens[j] != address(0x0)) {
addrBalances[addrIdx] = tokenBalance(users[i], tokens[j]);
} else {
addrBalances[addrIdx] = users[i].balance; // ETH balance
}
}
}
return addrBalances;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment