【问题标题】:How do I get all the tokens inside my Metamask wallet by web3 or others?如何通过 web3 或其他人获取 Metamask 钱包中的所有代币?
【发布时间】:2021-07-25 13:40:20
【问题描述】:

我在我的网站(Laravel 和 Vuejs)上使用元掩码登录。我安装了 Web3 并执行了 getAccounts、Sign、getBalance 等操作。

但我想在 metamask 中获取所有代币(例如 BNB、RARI 等)的余额。我写了这段代码:

//     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");
});

但只返回 ETH 代币余额不是所有代币

我如何获得所有代币余额?你能帮帮我吗?

【问题讨论】:

  • 有关于这个问题的消息吗?
  • 您必须通过每个令牌地址和钱包地址检查金额

标签: javascript laravel vue.js web3 metamask


【解决方案1】:

正如您在 cmets 中已经发现的那样,没有人知道您帐户的所有代币余额,甚至 MetaMask 也不知道。这是因为代币并不存在于您的账户中,而是存在于跟踪您的代币余额的代币智能合约中。

因此,您必须检查您要查询的帐户的每个代币的合约以获取代币余额。这就引出了下一个问题:我们如何知道每个代币的合约地址?

MyCrypto 或 MetaMask 等钱包维护自己的知名代币合约白名单。 ethereum-lists 集体为您提供 ERC-20 代币:

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

它目前列出了 2000 多个以太坊代币,您可以选择自己喜欢的代币,也可以全部解析。每个令牌都有一个 JSON 规范定义,其中包含最重要的参数,例如:

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

来源:https://github.com/ethereum-lists/tokens/blob/c11d278944dc66e95b3b1c44786676b697c84b0a/tokens/eth/0x0000000000085d4780B73119b644AE5ecd22b376.json

【讨论】:

    猜你喜欢
    • 2021-09-06
    • 2022-08-13
    • 2018-05-04
    • 2021-06-29
    • 2022-06-27
    • 2022-11-04
    • 2022-06-20
    • 2022-06-30
    • 2021-10-07
    相关资源
    最近更新 更多