Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement