Guest User

Untitled

a guest
Oct 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. /*
  2. Check the token balances of a wallet for multiple tokens.
  3. Pass 0x0 as a "token" address to get ETH balance.
  4.  
  5. Possible error throws:
  6. - extremely large arrays for user and or tokens (gas cost too high)
  7.  
  8. Returns a one-dimensional that's user.length * tokens.length long. The
  9. array is ordered by all of the 0th users token balances, then the 1th
  10. user, and so on.
  11. */
  12. function balances(address[] users, address[] tokens) external view returns (uint[]) {
  13. uint[] memory addrBalances = new uint[](tokens.length * users.length);
  14.  
  15. for(uint i = 0; i < users.length; i++) {
  16. for (uint j = 0; j < tokens.length; j++) {
  17. uint addrIdx = j + tokens.length * i;
  18. if (tokens[j] != address(0x0)) {
  19. addrBalances[addrIdx] = tokenBalance(users[i], tokens[j]);
  20. } else {
  21. addrBalances[addrIdx] = users[i].balance; // ETH balance
  22. }
  23. }
  24. }
  25.  
  26. return addrBalances;
  27. }
Add Comment
Please, Sign In to add comment