首页 > 解决方案 > 如何从 MetaMask 获得第二个余额?

问题描述

请给我一个解决方案以从帐户中获得 2 余额(HPB,ESR)。 在此处输入图像描述

标签: next.jsblockchainethereum

解决方案


为了获得代币余额,您需要知道代币合约的地址,因为地址的代币余额存储在代币合约中。

MetaMask 仅将保存的帐户共享给请求的应用程序(并且用户需要先手动确认此操作)。但它不共享用户添加到其 MM UI 的令牌地址。

因此,如果您知道令牌地址,则可以使用简单的 web3 调用,例如:

const jsonAbi = {}; // JSON ABI of the token contract
const contractAddress = "0x123"; // address of the token contract
const tokenAddress = "0xfF3d"; // address of which you want to get the token balance

const token = new web3.eth.Contract(jsonAbi, contractAddress);
const balance = await token.balanceOf(tokenAddress);

推荐阅读