-
Notifications
You must be signed in to change notification settings - Fork 151
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a few comments. i think it's probably ok to not have unit tests for now since you added the storybook. but i'll need to look into other possible tests at some point. there's got to be a good way to mock the provider
. rename ERC20 to ERC20.json . change name of args of useTokenBalance.ts
. use rinkeby instead of mainnet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR @AlexNi245! I have requested a few changes. Can you please look into them?
export const useTokenBalance = (contractAddress: string, ownerAddress: string) => { | ||
const [isLoading, setIsLoading] = React.useState(true); | ||
const [balance, setBalance] = React.useState<string>(); | ||
const [errorMsg, setErrorMsg] = React.useState(false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const [errorMsg, setErrorMsg] = React.useState(false); | |
const [error, setError] = React.useState(false); |
I think error
is a better name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it okay for you @etr2460 ? If so I will rename it to "error"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My thought was that this shouldn't be a boolean at all, but instead be an errorMessage and a string type, so that we can pass a better error message back to the caller:
maybe it's better to make this errorMessage and set the type to string. Then, depending on the error, we can pass a different message up
I can iterate on this in a future PR though
@AlexNi245 the storybook setup needs some changes. I'm gonna be working on it and pushing commits to this PR. So if you're working on this PR rn, please keep that on hold! |
useEffect(() => { | ||
if (tokenAddress && accountAddress && provider) { | ||
setError(false); | ||
setLoading(true); | ||
try { | ||
getBalance(); | ||
} catch (error) { | ||
console.error(error); | ||
setError(true); | ||
} | ||
setLoading(false); | ||
} | ||
}, [tokenAddress, accountAddress]); |
There was a problem hiding this comment.
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.
formattedBalance: balance && ethers.utils.formatUnits(balance, decimals), // The balance in ethers eg. 0.01 ETH, 20 GTC, etc. | ||
balanceInBigNumber: balance, |
There was a problem hiding this comment.
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.
@@ -17,7 +17,8 @@ | |||
"pretty": true, | |||
"sourceMap": true, | |||
"strict": true, | |||
"target": "es5" | |||
"target": "es5", | |||
"resolveJsonModule": true |
There was a problem hiding this comment.
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
@allcontributors please add @AlexNi245 for code |
I've put up a pull request to add @AlexNi245! 🎉 |
Description
A simple hook that fetches the user's balance of a certain ERC20 token.
Aditional Information
I created 3 testcases in the storybook section
I want to write unit test in the first place but still not sure how to mock the web3context within a unit test :( so I decided to used the storybook instead