VGLoic / metamask-react

A simple Context provider and consumer hook in order to provide a simple API for interacting with MetaMask in the browser
MIT License
136 stars 24 forks source link

How to identify the balance on the metamask account already connected? #8

Closed heras1991 closed 2 years ago

heras1991 commented 2 years ago

Hi VGLoic, thank you very much for this amazing library.

I have inspected the code without success to identify the balance of ERC20 and ERC721 tokens belonging to a connected account.

The only method I found is: eth_getBalance but only returns main balance of the chain.

Could you please lead me in the way to get another ERC721/ERC20 token balance? (not need to interact with it, just to know the balance)

Thank you again!

VGLoic commented 2 years ago

Hey @heras1991! Thanks a lot for the kind word!

So indeed this package only manages what is called the web3 provider layer, the exposed ethereum object is the one MetaMask injects directly in the window and does not come (intentionally) with a lot of features

In order to manage interactions with the chain, balance/block fetching or contract interactions, people generally use other libraries, the two major ones are ethersjs or web3js.

These libraries take as an entry param a web3 provider, so in the case of this package, the ethereum object :).

I would recommend you to take a look at this part of the ethersjs documentation which specifically describes the ERC20 case :). The ERC721 will be mostly similar at the exception of the ABI.

Feel free to tell me if I am not clear or you still have other questions :)

heras1991 commented 2 years ago

Thank you very much for the explication. I found the way to do it with web3.js 👍

I strongly suggest you implement the integration with web3.js to store the balance of an ERC20 token in the same context. This is a very popular use-case for blockchain devlopers and your library is almost there.

VGLoic commented 2 years ago

Thanks a lot for the idea! I will consider doing a separate package for these kind of use cases :)

I would love to keep this package far away from the battle of various use cases and/or choice of libraries such as web3.js/ethers.js

Are you okay if I close this issue? :)

heras1991 commented 2 years ago

sure! Please, close it. Thank you

El dom, 28 nov 2021 a las 14:00, Loïc Vincent-Genod (< @.***>) escribió:

Thanks a lot for the idea! I will consider doing a separate package for these kind of use cases :)

I would love to keep this package far away from the battle of various use cases and/or choice of libraries such as web3.js/ethers.js

Are you okay if I close this issue? :)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/VGLoic/metamask-react/issues/8#issuecomment-981081655, or unsubscribe https://github.com/notifications/unsubscribe-auth/AORR2WU6MXEBK2X73GTHYELUOIRYFANCNFSM5H5KNXTQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

VGLoic commented 2 years ago

Thanks!