Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/use token balance #69

Merged
merged 7 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
222 changes: 222 additions & 0 deletions packages/hooks/src/hooks/useTokenBalance/ERC20ABI.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
[
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_spender",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_from",
"type": "address"
},
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [
{
"name": "",
"type": "uint8"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "balance",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
},
{
"name": "_spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"payable": true,
"stateMutability": "payable",
"type": "fallback"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "owner",
"type": "address"
},
{
"indexed": true,
"name": "spender",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "from",
"type": "address"
},
{
"indexed": true,
"name": "to",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
}
]
1 change: 1 addition & 0 deletions packages/hooks/src/hooks/useTokenBalance/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useTokenBalance } from './useTokenBalance';
AlexNi245 marked this conversation as resolved.
Show resolved Hide resolved
51 changes: 51 additions & 0 deletions packages/hooks/src/hooks/useTokenBalance/useTokenBalance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { ethers } from 'ethers';
import { useContext, useEffect, useState } from 'react';
import { Web3Context } from '../..';
import ERC20ABI from './ERC20ABI.json';
import { BigNumber } from '@ethersproject/bignumber';

interface Props {
tokenAddress: string;
accountAddress: string;
}

export function useTokenBalance({ tokenAddress, accountAddress }: Props) {
const context = useContext(Web3Context);
const provider = context?.provider;

const [balance, setBalance] = useState<BigNumber>();
const [decimals, setDecimals] = useState();
const [loading, setLoading] = useState(true);
const [error, setError] = useState(false);

const getBalance = async () => {
const contract = new ethers.Contract(tokenAddress, ERC20ABI, provider!);
const balance = await contract.balanceOf(accountAddress);
const decimals = await contract.decimals();
setBalance(balance);
setDecimals(decimals);
};

useEffect(() => {
if (tokenAddress && accountAddress && provider) {
setError(false);
setLoading(true);
try {
getBalance();
} catch (error) {
console.error(error);
setError(true);
}
setLoading(false);
}
}, [tokenAddress, accountAddress]);
Comment on lines +29 to +41
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexNi245 this is one thing that I want you to look at. I just fixed this but you didn't have a dependency array attached to your useEffect which was causing it to run in an infinite loop.


return {
balance: balance?.toString(), // The balance in wei
loading,
error,
decimals,
formattedBalance: balance && ethers.utils.formatUnits(balance, decimals), // The balance in ethers eg. 0.01 ETH, 20 GTC, etc.
balanceInBigNumber: balance,
Comment on lines +48 to +49
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added these two additional helper variables.

};
}
1 change: 1 addition & 0 deletions packages/hooks/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { useWallet } from './hooks';
export { useTokenBalance } from './hooks/useTokenBalance';
export { Provider, Web3Context } from './Provider';
export type { ProviderProps, Web3ContextType } from './Provider';
40 changes: 40 additions & 0 deletions packages/hooks/src/stories/UseTokenBalance.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { storiesOf } from '@storybook/react';
import React from 'react';
import { Button } from '@chakra-ui/react';
import { Provider, useTokenBalance, useWallet } from '..';

const GTC_ADDRESS = '0xde30da39c46104798bb5aa3fe8b9e0e1f348163f';

const Default = () => {
const { connection, connectWallet, disconnectWallet, connected } = useWallet();
const { balance, formattedBalance, loading, error } = useTokenBalance({
tokenAddress: GTC_ADDRESS,
accountAddress: connection.userAddress!, // you can test using 0x0ED6Cec17F860fb54E21D154b49DAEFd9Ca04106
});

if (connected) {
return (
<div>
<Button onClick={disconnectWallet}>Disconnect wallet</Button>
<p>{connection.ens || connection.userAddress}</p>
{error ? (
<p>Error occured while trying to fetch balance.</p>
) : loading ? (
<p>Loading...</p>
) : (
<p>
GTC balance: {balance} wei, {formattedBalance} GTC
</p>
)}
</div>
);
}

return <button onClick={connectWallet}>Connect Wallet</button>;
};

storiesOf('UseTokenBalance', module).add('Default', () => (
<Provider network='mainnet'>
AlexNi245 marked this conversation as resolved.
Show resolved Hide resolved
<Default />
</Provider>
));
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"pretty": true,
"sourceMap": true,
"strict": true,
"target": "es5"
"target": "es5",
"resolveJsonModule": true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this to support .json imports

},
"exclude": ["node_modules"],
"include": ["./packages/*/src"]
Expand Down