Get token balance

JSON-RPC API

I use Postman to do HTTP request. My IP address 192.168.xx.xx, and the RPC port of miner2 node is 8487, so I enter IP address 192.168.xx.xx:8487 to be the request URL. Then do the HTTP POST request with below body.

{
  "jsonrpc":"2.0",
  "method":"eth_call",
  "params":[
    {
      "to": "0xf13c9660190749124ad90c8defc77cc6c5d36494",
      "data": "0x70a08231000000000000000000000000aeF72A6461e5392e9fa1537372b98701780a1819"
    },
    "latest"
  ],
  "id":63
}

Then after about 26ms, I got below response.

{
  "jsonrpc": "2.0",
  "id": 63,
  "result": "0x0000000000000000000000000000000000000000000000361abe817294d24000"
}

I can use Web3 to parse that balance of the account of given address.

> web3.fromWei(web3.toBigNumber("0x0000000000000000000000000000000000000000000000361abe817294d24000"), "ether")
998.0513

eth_getBalance

Geth API

I also can use Geth to request token balance.

  • TokenBalanceFetcher.java

    public class TokenBalanceFetcher implements Balance {
      /**
       * Fetch the balance of Erc20 Token from ethereum.
       *
       * @return balance is possible less than zero.
       * @throws IllegalStateException when fetch balance failed, such as still trying connect to ethereum.
       */
      @Override
      public BigDecimal fetchBalance() {
        byte[] response;
        final CallMsg callMsg = Geth.newCallMsg();
        try {
          callMsg.setTo(Geth.newAddressFromHex(tokenAddress));
          callMsg.setData(InputData.makeInputDataForBalanceOfMethod(accountAddress));
          Log.d(TAG, "'toAddress is " + callMsg.getTo().getHex());
          Log.d(TAG, "input data is " + InputData.toHexWith0x(callMsg.getData()));
          response = ethereum.pendingCallContract(callMsg);
        } catch (IllegalStateException e) {
          throw e;
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
        if (response == null) {
          throw new IllegalStateException("cannot fetch balance, possible trying connect to other peers in ethereum.");
        }
        Log.d(TAG, "response is " + toHexWith0x(response));
        return toValue(toHexWith0x(response));
      }
    }
    
  • GoEthereum.java

    public class GoEthereum implements Ethereum {
    
      byte[] pendingCallContract(@NonNull CallMsg callMsg) throws Exception {
        if (ethereumClient != null) {
          return ethereumClient.pendingCallContract(Geth.newContext(), callMsg);
        } else {
          throw new IllegalStateException("you have to call Ethereum#connect() before invokes this method.");
        }
      }
    }
    
  • InputData.java

    import okio.ByteString;
    
    public class InputData {
      public static final String PREFIX_HEX = "0x";
    
      @NonNull
      static String toHexWith0x(@NonNull byte[] data) {
        final String result = ByteString.of(data).hex();
        return hasPrefixWith0x(result) ? result : PREFIX_HEX + result;
      }
    
      public static BigDecimal toValue(@NonNull String hex) {
        final String hexWithoutPrefix0x = hasPrefixWith0x(hex) ? hex.replace(PREFIX_HEX, "") : hex;
        if (hexWithoutPrefix0x.length() > 0) {
          return new BigDecimal(new BigInteger(hexWithoutPrefix0x, 16).toString());
        } else {
          return new BigDecimal(0);
        }
      }
    }
    

EthereumClient#pendingCallContract(Context, CallMsg)

results matching ""

    No results matching ""