SHARE
TWEET

Untitled

a guest Sep 17th, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { gql } from 'apollo-boost';
  2. import { useQuery } from '@apollo/react-hooks';
  3. import web3 from '../lib/web3';
  4.  
  5. export const GET_TOKEN_BALANCE = gql`
  6.   query TokenBalance($tokenAddress: Address!, $callData: Bytes!) {
  7.     block {
  8.       call(data: { to: $tokenAddress, data: $callData }) {
  9.         data
  10.       }
  11.     }
  12.   }
  13. `;
  14.  
  15. export default function TokenBalance({ forAddress, token }) {
  16.   const callData =
  17.     '0x70a08231000000000000000000000000' + forAddress.substring(2); // balanceOf(address)
  18.  
  19.   const { loading, error, data } = useQuery(GET_TOKEN_BALANCE, {
  20.     variables: { tokenAddress: token.address, callData }
  21.   });
  22.  
  23.   if (loading) return 'Loading balance';
  24.   if (error) return error.toString();
  25.  
  26.   const balance = web3.utils.toBN(data.block.call.data);
  27.   let balanceDecimal = balance;
  28.   if (token.decimal) {
  29.     balanceDecimal = balance.div(token.decimal);
  30.   }
  31.   return `${balanceDecimal.toLocaleString()} ${token.symbol}`;
  32. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top