next.js - 如何从 MetaMask 获得第二个余额?
解决方案
为了获得代币余额,您需要知道代币合约的地址,因为地址的代币余额存储在代币合约中。
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);
推荐阅读
- ios - 谷歌地图路线
- mysql - 使用 mysql join 选择所有成员
- php - 如何在 Jquery、PHP 和 AJax 中用多边形注释图像?
- spring-integration - Spring-Integration Webflux 异常处理
- ios - 将协议与关联对象绑定 - 使用 Swift 进行面向协议的编程
- ffmpeg - 为什么这条 ffmpeg 行会使 image_0 变灰?
- javascript - 有人知道如何解决此错误:无法在未安装的组件上调用 setState (or forceUpdate) ....?
- mysql - 未找到记录时出现错误 #2014 - 命令不同步;你现在不能运行这个命令
- docker - Docker 提交 - 不保留新设置?
- java - JavaFX 在后台线程中加载系统图标