簡體   English   中英

無法檢索 MetaMask 賬戶的以太坊余額

[英]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返回錢包 id

async function getAccount() {  
    const accounts = await ethereum.request({ method: 'eth_requestAccounts' });  
    const account = accounts[0];  
    showAccount.innerHTML = account;  
}  

getBalance返回 undefined 而不是 Metamask 中的 Ether 數量

async function getBalance() {  
    const accounts = await ethereum.request({ method: 'eth_requestAccounts' });  
    const account = accounts[0];  
    showBalance.innerHTML = account.balance;  
}  

也許有人知道一個很好的 API 用於檢索比這兩個更多的值,或者有一個很好的視頻來學習。

我找到了我的accounts object 的屬性: 這里

method: 'eth_requestAccounts'會將您連接到元掩碼。

await ethereum.request({ method: 'eth_requestAccounts' })

連接后,要獲取帳戶,請使用web3

  const accounts = await web3.eth.getAccounts();
  const account = accounts[0];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM