[英]Unable to retrieve the Ethereum balance of MetaMask account
const showAccount = document.querySelector('.showAccount');
const showBalance = document.querySelector('.showBalance');
const Web3 = require("web3");
getAccount();
getBalance();
getAccount
returns the wallet id getAccount
返回钱包 id
async function getAccount() {
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
const account = accounts[0];
showAccount.innerHTML = account;
}
getBalance
returns undefined instead of the amount of Ether from Metamask getBalance
返回 undefined 而不是 Metamask 中的 Ether 数量
async function getBalance() {
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
const account = accounts[0];
showBalance.innerHTML = account.balance;
}
Maybe someone knows a good API for retrieving more values than just these two with examples or got a nice video to learn from.也许有人知道一个很好的 API 用于检索比这两个更多的值,或者有一个很好的视频来学习。
I found the properties for my accounts
object: here我找到了我的
accounts
object 的属性: 这里
method: 'eth_requestAccounts'
will connect you to the metamask. method: 'eth_requestAccounts'
会将您连接到元掩码。
await ethereum.request({ method: 'eth_requestAccounts' })
After u connected, to get the accounts, use web3
:连接后,要获取帐户,请使用
web3
:
const accounts = await web3.eth.getAccounts();
const account = accounts[0];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.