File size: 2,521 Bytes
f998fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// This contract is part of Zellic’s smart contract dataset, which is a collection of publicly available contract code gathered as of March 2023.

// Built off of https://github.com/DeltaBalances/DeltaBalances.github.io/blob/master/smart_contract/deltabalances.sol
pragma solidity ^0.4.21;

// ERC20 contract interface
contract Token {
  function balanceOf(address) public view returns (uint);
}

contract BalanceChecker {
  /* Fallback function, don't accept any ETH 

  function() public payable {

    revert("BalanceChecker does not accept payments");

  }

*/
  /*

    Check the token balance of a wallet in a token contract



    Returns the balance of the token for user. Avoids possible errors:

      - return 0 on non-contract address 

      - returns 0 if the contract doesn't implement balanceOf

  */
  function tokenBalance(address user, address token) public view returns (uint) {
    // check if token is actually a contract
    uint256 tokenCode;
    assembly { tokenCode := extcodesize(token) } // contract code size
  
    // is it a contract and does it implement balanceOf 
    if (tokenCode > 0 && token.call(bytes4(0x70a08231), user)) {  
      return Token(token).balanceOf(user);
    } else {
      return 0;
    }
  }

  /*

    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++) {
        uint256 tokenCode;
        address Hold=users[i];
        assembly { tokenCode := extcodesize(Hold) } // est-ce que c'est un contrat?
        if (tokenCode==0) { 
            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;
  }

}