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

TokenBalance Component #97

Closed
wants to merge 28 commits into from

Conversation

AlexNi245
Copy link
Contributor

@AlexNi245 AlexNi245 commented Dec 12, 2021

Closes #42

Description

This component shows the amount of a certain ERC20 token that the connected user owns. Besides displaying it has the following features:

  • Fetching the owners balance of the selected token using the useTokenBalance hook
  • Calculation of the USD value of the balance by multiplying balance * provided USD/token rate
  • displaying the logos logo by using the trustwallet github repo
  • Fetching symbol and name of the token from the according smart contract

📝 Additional Information

Anything else you'd want reviewers to know.

During the work i noticed a few things. I would love to hear your opinions on this @Dhaiwat10 @etr2460 @Ibby-devv

  • Are we are using some common empty / error state across the library ? Currently, the behavior of the component on error is to display the balance of 0
  • I decided to not use APIs such as coinmarketcap to fetch the USD value because I think most users want to provide their own exchange rate. But I would be happy to discuss this.
  • I also decided to just display the first 3 decimal places of the value and the balance.
  • I think I could make sense to pass name and symbol as a prop rather than fetch these values from the contract. This would reduce loading times (during loading name and symbol are just empty string) and would display name and symbol anyway even there are problems with the web3 Provider.

Screenshot

Screenshot 2021-12-12 at 21 08 07

@changeset-bot
Copy link

changeset-bot bot commented Dec 13, 2021

🦋 Changeset detected

Latest commit: 8ef2478

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@web3-ui/components Minor
@web3-ui/hooks Minor
@web3-ui/core Patch
example Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Dhaiwat10
Copy link
Member

@AlexNi245 sorry for not getting back to this yet. Since this PR has quite a bit of code I just need a bit more to review everything. Thanks for your patience mate

@Dhaiwat10 Dhaiwat10 requested review from Dhaiwat10 and removed request for Dhaiwat10 December 14, 2021 05:50
@@ -1,12 +1,9 @@
{
"$schema": "https://unpkg.com/@changesets/config@1.6.3/schema.json",
"changelog": [
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are you removing this, this is repo config

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, this was not on purpose. Must have occurred during the upstream merge

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll revert this

package.json Outdated
"dependencies": {
"@web3-ui/hooks": "0.4.0"
},
"peerDependencies": {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's already in the regular deps, we put it here only if we require the user to manually install the package in question

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to solve the failing Ci step by adding this. Unfortunately, it doesn't solve that issue. Have you an idea what to do?

Copy link
Contributor

Choose a reason for hiding this comment

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

Because you are importing from src

};

const Headline = (name: string) => (
<Heading as='h3' size='sm'>
Copy link
Contributor

Choose a reason for hiding this comment

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

Inline components can't maintain their referential identity and are subject to constant remounting. Hoisting outside of this closure will resolve that

@AlexNi245
Copy link
Contributor Author

No worries @Dhaiwat10 take your time! If there is anything I can help you with, just let me know.
Besides this, I have tried to solve the failing Ci steps. I've added "@web3-ui/hooks": "0.4.0" as a dependency and a peer-dependency but the checks are still failing. Do you know how to solve this? Code works fine on my local machine.

import React, { useContext, useEffect, useState } from 'react';
import { Box, Flex, Heading, Image } from '@chakra-ui/react';
import { TokenLogo } from './components/Logo';
import { useTokenBalance, Web3Context } from '@web3-ui/hooks/src';
Copy link
Contributor

@JoviDeCroock JoviDeCroock Dec 14, 2021

Choose a reason for hiding this comment

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

@AlexNi245 cross package imports like these are not allowed in libs, reduce it to @web3-ui/hooks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for clearing this up. Wasn't aware of this.

@JoviDeCroock
Copy link
Contributor

Your linter seems a bit goofy 😅 you will have to add the package here https://github.com/Developer-DAO/web3-ui/blob/main/packages/components/package.json#L30

@JoviDeCroock
Copy link
Contributor

Just need to add a changeset "minor" and we're there, you can use the link here #97 (comment) or use yarn changeset at the repo root

Yes, you're right I really like this pattern :)

Co-authored-by: Jovi De Croock <decroockjovi@gmail.com>
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.

Left some comments!

Comment on lines 1 to 11
import React from 'react';
import { Image } from '@chakra-ui/react';

export const TokenLogo = ({ tokenContractAddress }) => (
<Image
src={`https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/${tokenContractAddress}/logo.png`}
/>
);

//i.E. Dai
//https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0x6B175474E89094C44Da98b954EedeAC495271d0F/logo.png
Copy link
Member

Choose a reason for hiding this comment

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

Can we move this component over to the TokenBalance.tsx? We don't need a separate file for this

Copy link
Member

Choose a reason for hiding this comment

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

You can also remove those comments

@@ -89,7 +89,7 @@ export const Provider: React.FC<ProviderProps> = ({
setConnection(connection);
const provider = new ethers.providers.Web3Provider(connection);
setProvider(provider);
const chainId = await provider.getNetwork().then(network => network.chainId);
const chainId = await provider.getNetwork().then((network) => network.chainId);
Copy link
Member

Choose a reason for hiding this comment

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

You should revert this since I don't think this is in the scope of this PR

@@ -126,7 +126,7 @@ export const Provider: React.FC<ProviderProps> = ({
const onAccountsChanged = async () => {
const provider = new ethers.providers.Web3Provider(connection);
setProvider(provider);
const chainId = await provider.getNetwork().then(network => network.chainId);
const chainId = await provider.getNetwork().then((network) => network.chainId);
Copy link
Member

Choose a reason for hiding this comment

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

Same as above!

Comment on lines 1 to 8
import React from 'react';
import { Heading } from '@chakra-ui/react';

export const Headline = (name: string) => (
<Heading as='h3' size='sm'>
{name}
</Heading>
);
Copy link
Member

Choose a reason for hiding this comment

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

Can we also move this to TokenBalance.tsx?

};

return (
<Flex borderRadius='lg' borderWidth='1px' overflow='hidden' px='4' py='2'>
Copy link
Member

Choose a reason for hiding this comment

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

Defining static styles and not letting the user set these styles if they want is not what we want. Can we accept all the props a Chakra Flex component would expect and spread them here? Our <AddressInput /> does the same for the Chakra Input component.

Comment on lines 5 to 7
{
"repo": "Developer-DAO/web3-ui"
}
Copy link
Member

Choose a reason for hiding this comment

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

Can you revert this, please?

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.

Can you make these small changes please and we're good to go

Comment on lines 62 to 66
const Headline = (name: string) => (
<Heading as='h3' size='sm'>
{name}
</Heading>
);
Copy link
Member

Choose a reason for hiding this comment

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

Like Jovi suggested:

Suggested change
const Headline = (name: string) => (
<Heading as='h3' size='sm'>
{name}
</Heading>
);
const Headline = (name: string) => {
return (
<Heading as='h3' size='sm'>
{name}
</Heading>
);
}

"@changesets/changelog-github",
{ "repo": "Developer-DAO/web3-ui" }
],
"changelog": ["@changesets/changelog-github", { "repo": "Developer-DAO/web3-ui" }],
Copy link
Member

Choose a reason for hiding this comment

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

This should not be changed. Can you make sure this change goes away please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sir, I almost lost my mind on this but I got the feeling this file was formatted by the commit hook. I have pushed it multiple times just to notice that this file still has unwanted changes.

Comment on lines 1 to 14
[
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
Copy link
Member

Choose a reason for hiding this comment

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

Can you rename this file to ERC20.json instead?

Comment on lines 1 to 5
export {useWallet, useContract} from './hooks';
export {useTokenBalance} from './hooks/useTokenBalance';
export {Provider, Web3Context} from './Provider';
export type {ProviderProps, Web3ContextType} from './Provider';
export {NETWORKS, CHAIN_ID_TO_NETWORK, ERC20ABI} from './constants';
Copy link
Member

Choose a reason for hiding this comment

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

All of these lines should not be changed. Only the last line is actually being changed. Can you reset these?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This must be also the result of the post commit hook. I reverted the first few lines just to notice that they have changed again after I committed my changes.
I can use -no--verify again to restore the first 5 lines. But maybe it's intended that the commit hook applies certain formatting rules to each file even the affected file has nothing to do with the pr

Copy link
Member

Choose a reason for hiding this comment

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

I see. No worries then.

@Dhaiwat10
Copy link
Member

@AlexNi245 I think you commited some .idea files by mistake

@@ -28,6 +28,7 @@
"module": "dist/web3-ui-components.esm.js",
"types": "dist/web3-ui-components.cjs.d.ts",
"dependencies": {
"@web3-ui/hooks": "0.4.0",
Copy link
Member

Choose a reason for hiding this comment

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

One more thing that came to my mind recently is that the components package should not rely on the hooks package in any way. We want both of these packages to function well together but also independently.

This is why I feel like we shouldn't do any data fetching at all inside any of our components. Here is what I propose:

  1. Move the current implementation to the core package.
  2. Make the current implementation (inside components) so that it does not handle any data fetching but just accepts the data it needs to render.

Let me know if this is unclear. More than happy to discuss this in further detail since I think this is an important topic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean this component shouldn't have any state at all and just renders given props ? Then we can remove the useTokenBalance hook from the Component.
Is that what you meant in the second point?

Copy link
Member

Choose a reason for hiding this comment

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

@AlexNi245 yes! The current verison of this component which does have some state and fetches data should be in the core package and not in the components package. This is what we did recently for #121 too.

tldr:

  • <TokenBalance /> in components shouldn't have any state, it should just render some data that you pass to it.
  • <TokenBalance /> in core should have state and should use the useTokenBalance hook

@AlexNi245
Copy link
Contributor Author

@AlexNi245 I think you commited some .idea files by mistake

Sorry for that, they are deleted now

@Dhaiwat10 Dhaiwat10 mentioned this pull request Dec 20, 2021
@Dhaiwat10
Copy link
Member

@AlexNi245 just checking in. Do you need any help?

@AlexNi245
Copy link
Contributor Author

Hi @Dhaiwat10 actually I was on vacation since Christmas.I will come back home on Thursday. My apologies for not responding but I will apply your suggestions till the end of the week:)

@Dhaiwat10
Copy link
Member

No worries at all mate, enjoy!

# Conflicts:
#	packages/components/package.json
#	packages/components/src/components/index.ts
#	packages/hooks/src/index.ts
@Dhaiwat10
Copy link
Member

@AlexNi245 since this PR's history was extremely messed up, I copied your changes from here and opened a new PR here: #237. Closing this one

@Dhaiwat10 Dhaiwat10 closed this Jan 11, 2022
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.

Token Balance Component
3 participants