Some Example Methods of the Ethereum Provider API

Published by Mario Oettler on

In this topic, we take a closer look at some methods of the Ethereum provider API. An extensive list of API endpoints can be found here https://eips.ethereum.org/EIPS/eip-1474.

  • get the accounts
  • get the coinbase address
  • estimate gas consumption of a transaction
  • get the balance of an address
  • get the used protocol

You can find the complete code here:

<!DOCTYPE html>
<html>
    <head>
        <script>
            window.addEventListener("load", (event)=>{
                if(window.ethereum){


                    // Get Accounts
                    ethereum.request({method: 'eth_requestAccounts'}).then(function(res){
                        console.log("Accounts: " + res);
                        document.getElementById("accounts").innerHTML = "Accounts: " + res;
                    })

                    // Coinbase address
                    ethereum.request({method: "eth_coinbase"}).then(function(res){
                        console.log("Coinbase address: " + res);
                        document.getElementById("coinbase_address").innerHTML = "Coinbase address: " + res;
                    })

                    // estimate Gas of a transaction
                    estGasParams = [{
                        "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675",
                        "from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
                        "gas": "0x76c0",
                        "gasPrice": "0x9184e72a000",
                        "to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
                        "value": "0x9184e72a"
                    }]

                    ethereum.request({method: 'eth_estimateGas', params: estGasParams}).then(function(res){
                        console.log("Gas estimation: " + res);
                        document.getElementById("gas_estimation").innerHTML = "Gas estimation: " + res;
                    })

                    // Requesting the balance of an address
                    ethereum.request({method: 'eth_getBalance', params: ['0xD354996e15E436c7c032A7be2d6218c38afEAc1a', 'latest']}).then(function(res){
                        console.log("Balance: " + res);
                        document.getElementById("balance").innerHTML = "Balance: " + res;
                    })

                    // Requesting the transaction count sent from an address
                    params1 = ["0xD354996e15E436c7c032A7be2d6218c38afEAc1a","latest",];
                    ethereum.request({method: 'eth_getTransactionCount', params: params1}).then(function(res){
                        console.log("TX Count: " + res);
                        document.getElementById("tx_count").innerHTML = "TX Count: " + res;
                    })

                    // Get the protocol of Ethereum
                    ethereum.request({method: 'eth_protocolVersion'}).then(function(res){
                        console.log("Protocol: " + res);
                        document.getElementById("protocol").innerHTML = "Protocol: " + res;
                    })

                }else{
                    return false
                }
            })
            
        </script>
    </head>
    <body>
        <h1>Using the Provider API</h1>
        <div id = "accounts"></div>
        <div id = "coinbase_address"></div>
        <div id = "gas_estimation"></div>
        <div id = "balance"></div>
        <div id = "tx_count"></div>
        <div id = "protocol"></div>
    </body>
</html>
Categories: