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 2 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/ERC20.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
[
AlexNi245 marked this conversation as resolved.
Show resolved Hide resolved
{
"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
45 changes: 45 additions & 0 deletions packages/hooks/src/hooks/useTokenBalance/useTokenBalance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React, { useContext } from 'react';
import { ethers } from 'ethers';
import { Web3Context } from '../../Provider';
import ERC20 from './ERC20.json';

export const useTokenBalance = (address: string, owner: string) => {
AlexNi245 marked this conversation as resolved.
Show resolved Hide resolved
const [isLoading, setIsLoading] = React.useState(true);
AlexNi245 marked this conversation as resolved.
Show resolved Hide resolved
const [balance, setBalance] = React.useState<null | string>(null);
AlexNi245 marked this conversation as resolved.
Show resolved Hide resolved
const [hasError, setHasError] = React.useState(false);
AlexNi245 marked this conversation as resolved.
Show resolved Hide resolved

const { provider } = useContext(Web3Context);

React.useEffect(() => {
getTokenBalance();
});

const getTokenBalance = async () => {
if (provider === undefined) {
setHasError(true);
setIsLoading(false);
return;
}
try {
const contract = new ethers.Contract(address, ERC20, provider);
const balanceFuture = contract.balanceOf(owner);
const decimalsFuture = contract.decimals();

const [resolvedBalance, resolvedDecimals] = await Promise.all([
balanceFuture,
decimalsFuture,
]);

const balanceAsNumber = Number.parseInt(resolvedBalance);
const displayBalance = (balanceAsNumber / 10 ** resolvedDecimals).toFixed(3);

setBalance(displayBalance);
setIsLoading(false);
} catch (e) {
setHasError(true);
setIsLoading(false);
}
};

return [isLoading, balance, hasError];
};
62 changes: 62 additions & 0 deletions packages/hooks/src/stories/UseTokenBalance.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { storiesOf } from '@storybook/react';
import React from 'react';
import { Provider, useWallet } from '..';
import { useTokenBalance } from '../hooks/useTokenBalance';

const Default = () => {
const { connectWallet, connected } = useWallet();

if (connected) {
return (
<>
<BalanceOfDai />
<UnknownOwner />
<UnknownContract />
</>
);
}

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>
));

const BalanceOfDai = () => {
const owner = '0x503828976D22510aad0201ac7EC88293211D23Da';
const address = '0x6B175474E89094C44Da98b954EedeAC495271d0F';
AlexNi245 marked this conversation as resolved.
Show resolved Hide resolved
const [loading, balance] = useTokenBalance(address, owner);

return (
<>
<p>{loading ? 'Loading...' : balance}</p>
</>
);
};

const UnknownOwner = () => {
const owner = '';
const address = '0x6B175474E89094C44Da98b954EedeAC495271d0F';
const [loading, balance, hasError] = useTokenBalance(address, owner);

return (
<>
<p>{loading ? 'Loading...' : 'Error ' + hasError}</p>
</>
);
};

const UnknownContract = () => {
const owner = '0x503828976D22510aad0201ac7EC88293211D23Da';
const address = '';
const [loading, balance, hasError] = useTokenBalance(address, owner);

return (
<>
<p>{loading ? 'Loading...' : 'Error ' + hasError}</p>
</>
);
};