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

Conversation

AlexNi245
Copy link
Contributor

@AlexNi245 AlexNi245 commented Dec 4, 2021

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

  1. Success -> Owner and Contract Id's are valid
  2. Error -> undefined contract address
  3. Error -> undefined owner address

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

Copy link
Contributor

@etr2460 etr2460 left a 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

@Dhaiwat10 Dhaiwat10 self-requested a review December 5, 2021 04:17
. rename ERC20 to ERC20.json
. change name of args of useTokenBalance.ts
. use rinkeby instead of mainnet
Copy link
Member

@Dhaiwat10 Dhaiwat10 left a 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);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const [errorMsg, setErrorMsg] = React.useState(false);
const [error, setError] = React.useState(false);

I think error is a better name

Copy link
Contributor Author

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"

Copy link
Contributor

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

@Dhaiwat10
Copy link
Member

@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!

Comment on lines +29 to +41
useEffect(() => {
if (tokenAddress && accountAddress && provider) {
setError(false);
setLoading(true);
try {
getBalance();
} catch (error) {
console.error(error);
setError(true);
}
setLoading(false);
}
}, [tokenAddress, accountAddress]);
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.

Comment on lines +48 to +49
formattedBalance: balance && ethers.utils.formatUnits(balance, decimals), // The balance in ethers eg. 0.01 ETH, 20 GTC, etc.
balanceInBigNumber: balance,
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.

@@ -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

@Dhaiwat10 Dhaiwat10 merged commit 07e8546 into Developer-DAO:main Dec 7, 2021
@Dhaiwat10
Copy link
Member

@allcontributors please add @AlexNi245 for code

@allcontributors
Copy link
Contributor

@Dhaiwat10

I've put up a pull request to add @AlexNi245! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants