admin管理员组

文章数量:1401149

I make login with metamask on my website (Laravel & Vuejs). I installed Web3 and doing actions such as getAccounts, Sign , getBalance and etc.

But I want to get all tokens(Such as BNB, RARI and etc) balance in metamask. I wrote this code:

//     accounts[0] => default wallet number
window.web3.eth.getBalance(accounts[0], (err, b) => {
  if (err)
      console.log(err);
  let balance = {};
  balance.ether = window.web3.utils.fromWei(b, "ether");
});

But just return ETH Token Balance not all tokens.

How do i get all tokens balance? Can you help me?

I make login with metamask on my website (Laravel & Vuejs). I installed Web3 and doing actions such as getAccounts, Sign , getBalance and etc.

But I want to get all tokens(Such as BNB, RARI and etc) balance in metamask. I wrote this code:

//     accounts[0] => default wallet number
window.web3.eth.getBalance(accounts[0], (err, b) => {
  if (err)
      console.log(err);
  let balance = {};
  balance.ether = window.web3.utils.fromWei(b, "ether");
});

But just return ETH Token Balance not all tokens.

How do i get all tokens balance? Can you help me?

Share Improve this question edited Aug 17, 2022 at 18:29 miken32 42.8k16 gold badges125 silver badges174 bronze badges asked May 3, 2021 at 5:35 AminAmin 3971 gold badge6 silver badges19 bronze badges 1
  • You must check the amount by each token address and wallet address – Amin Commented Jun 19, 2021 at 19:15
Add a ment  | 

1 Answer 1

Reset to default 5

As you already figured out in the ments, nobody knows all the token balances of your account, not even MetaMask. This is due to the fact that the tokens do not reside in your account, but in the token smart contract which tracks your token balance.

Therefore, you have to check each token's contract for the account you are querying to get the token balance. Which brings us to the next issue: How do we know each token's contract address?

Wallets, such as MyCrypto or MetaMask maintain their own whitelists of well-known token contracts. The ethereum-lists collective has you covered for ERC-20 tokens:

https://github./ethereum-lists/tokens

It currently lists more than 2000 tokens for Ethereum and you can either pick your favorites or parse them all. Each token has a JSON spec definition containing the most important parameters, e.g.:

{
  "symbol": "TUSD",
  "name": "TrueUSD",
  "type": "ERC20",
  "address": "0x0000000000085d4780B73119b644AE5ecd22b376",
  "ens_address": "",
  "decimals": 18,
  "website": "https://www.trusttoken.",
  "logo": {
    "src": "",
    "width": "",
    "height": "",
    "ipfs_hash": ""
  },
  "support": {
    "email": "[email protected]",
    "url": ""
  },
  "social": {
    "blog": "https://blog.trusttoken.",
    "chat": "",
    "facebook": "",
    "forum": "",
    "github": "https://github./trusttoken",
    "gitter": "",
    "instagram": "",
    "linkedin": "",
    "reddit": "https://www.reddit./r/TrustToken/",
    "slack": "",
    "telegram": "https://t.me/joinchat/HihkMkTja1gIyBRM1J1_vg",
    "twitter": "https://twitter./TrustToken",
    "youtube": ""
  }
}

Source: https://github./ethereum-lists/tokens/blob/c11d278944dc66e95b3b1c44786676b697c84b0a/tokens/eth/0x0000000000085d4780B73119b644AE5ecd22b376.json

本文标签: javascriptHow do I get all the tokens inside my Metamask wallet by web3 or othersStack Overflow