【问题标题】:Unable to retrieve the Ethereum balance of MetaMask account无法检索 MetaMask 账户的以太坊余额
【发布时间】:2021-12-29 13:07:46
【问题描述】:
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 对象的属性:here

【问题讨论】:

    标签: javascript ethereum web3 web3js metamask


    【解决方案1】:

    我自定义了您的代码,现在它显示了帐户中的以太币数量:

    const showAccount = document.querySelector('.showAccount');
    const showBalance = document.querySelector('.showBalance');
    
    getAccount();
    loadBalance();
    
    
    async function getAccount() {
        const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
        const account = accounts[0];
        showAccount.innerHTML = account;
    }
    
    function loadBalance(){
        web3Provider = null;
        contracts = {};
        account = '0x0';
        const Web3 = require("web3");
        const ethEnabled = async () => {
            if (window.ethereum) {
                await window.ethereum.send('eth_requestAccounts');
                window.web3 = new Web3(window.ethereum);
                return true;
            }
        }
        if (typeof web3 !== 'undefined') {
            // If a web3 instance is already provided by Meta Mask.
            web3Provider = web3.currentProvider;
            web3 = new Web3(web3.currentProvider);
        } else {
            // Specify default instance if no web3 instance provided
            web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
            web3 = new Web3(App.web3Provider);
        }
        $.getJSON("Market.json", function (market) {
            console.log("initializing Market contract")
    
            // Instantiate a new truffle contract from the artifact
            contracts.Market = TruffleContract(market);
            // Connect provider to interact with contract
            contracts.Market.setProvider(web3Provider);
        });
        $.getJSON("Users.json", function (users) {
            console.log("initializing User contract")
            // Instantiate a new truffle contract from the artifact
            contracts.Users = TruffleContract(users);
            // Connect provider to interact with contract
            contracts.Users.setProvider(App.web3Provider);
        });
    
        var marketInstance;
        var userInstance;
    
        var loader = $("#loader");
        var content = $("#content");
    
        //loader.show();
        content.show();
    
        // Load account data
        console.log("loading account data")
        var currentAccount;
        web3.eth.getCoinbase(function (err, account) {
            if (err === null) {
                console.log("Your Account: " + account)
                account = account;
                currentAccount = account;
                web3.eth.getBalance(account, function(err, balance) {
                    if (err === null) {  //Note:set id="accountBalance" in your html page
                        $("#accountBalance").text(web3.fromWei(balance, "ether") + " ETH");
                    }
                });
            }
        });
    }  
    

    【讨论】:

      猜你喜欢
      • 2015-11-25
      • 2022-11-16
      • 2019-03-29
      • 2020-03-31
      • 2021-11-15
      • 2019-04-06
      • 2022-10-05
      • 1970-01-01
      • 2021-05-19
      相关资源
      最近更新 更多