Some Example Methods of the Ethereum Provider API

Published by Mario Oettler on

Last Updated on 8. October 2024 by Mario Oettler

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: ['0x1102E22fF1F6F56Ed1B2E042959B5Ee90c62A29c', 'latest']}).then(function(res){
                        console.log("Balance: " + res);
                        document.getElementById("balance").innerHTML = "Balance: " + res;
                    })

                    // Requesting the transaction count sent from an address
                    params1 = ["0x1102E22fF1F6F56Ed1B2E042959B5Ee90c62A29c","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:

https://blockchain-academy.hs-mittweida.de/wp-content/uploads/2021/04/logo_bcam_rgb_gross.png

Welcome

Blockchain Academy

Continue with credential

No account yet?

Powered by Hidy

Register with Hidy


Register
Sign in

We need the following credential to register:

please select:

To create a new account, we need the following data from you:

We would appreciate the following additional information from you for the creation of your account:

Welcome

Blockchain Academy

Continue with credential

Already have an account?

Powered by Hidy

Sign in with Hidy


Register
Sign in

We need the following credential to log in:

please select:
Do you need assistance?
Click here to open chat.

Privacy Notice: This chat sends your questions to an external AI server.
How can we help you?
  • Please note: All questions, chat history, and feedback are sent to an external AI server for processing. Do not share sensitive personal information.

    All responses are generated by AI. Independently verify and fact-check all information before use.
  • No Chat History yet, start talking...