1

For example: open this https://etherscan.io/tx/0xcb1e3530950cf2c43a307bcb5645ae71a12c76a60831617badd04aea3efe68aa

It's a random one

I see these infos on webpage:

Gas Limit:
136,500

Gas Used by Transaction:
35,531 (26.03%)

Gas Price:
0.000000008 Ether (8 Gwei)

Transaction Fee:
0.000284248 Ether ($0.05)

I know that fee = gas_price x used_gas => 0.000000008 x 35,531 = 0.000284248 eth

Perfect.

I am interested in reading, via api the Transaction Fee converted in real money, like you see there is a $0.05

I am not able a single api call the returns this info on Etherscan.

What can I do to have this info?

Calling

https://api.etherscan.io/api?module=proxy&action=eth_getTransactionByHash&txhash=0xcb1e3530950cf2c43a307bcb5645ae71a12c76a60831617badd04aea3efe68aa&apikey=your_api_key

I get only these infos

{
"jsonrpc": "2.0",
"id": 1,
"result": {
  "blockHash": "0xd8d3b1532b376dcb34c6c36fc7bc167cfc048cfcb3cc53a9ab7dab7f383d4240",
  "blockNumber": "0x961a86",
  "from": "0xcc5ce245a296667aca1b5855f1a05ca950017e68",
  "gas": "0x21534",
  "gasPrice": "0x1dcd65000",
  "hash": "0xcb1e3530950cf2c43a307bcb5645ae71a12c76a60831617badd04aea3efe68aa",
  "input": "0x1cff79cd00000000000000000000000050b881ceafece2034b899205fbbd7bafc0c0d23000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000104a70e78c1000000000000000000000000f173214c720f58e03e194085b1db28b50acdeead000000000000000000000000514910771af9ca656af840dff83e8264ecf986ca00000000000000000000000056178a0d5f301baf6cf3e1cd53d9863437345bf9000000000000000000000000000000000000000000000000a688906bd8b000000000000000000000000000000000000000000000000002bd494127192db3364000000000000000000000000000000000000000000000000000450ed6b3b61f2e000000000000000000000000000000000000000000000000000000005e8ee87a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000",
  "nonce": "0x514",
  "to": "0xa57bd00134b2850b2a1c55860c9e9ea100fdd6cf",
  "transactionIndex": "0x4e",
  "value": "0x0",
  "v": "0x1c",
  "r": "0xa03627e5bc49b850afb92774a4dc21c436da8bfe569688dfbd14fc9c58a2bdbb",
  "s": "0x59ddaf4c96b60f413d713902abb1f802fbe3266afd3a61e82a8e635b44522fed"
  }
}

Where

"gas": "0x21534" => in decimal is 136,500 [gas limit]

"gasPrice"○: "0x1dcd65000" => in decimal is 8,000,000,000 [gas price]

I tried also calling https://api.etherscan.io/api?module=proxy&action=eth_getTransactionReceipt&txhash=0xcb1e3530950cf2c43a307bcb5645ae71a12c76a60831617badd04aea3efe68aa&apikey=your_api_key

This is the result

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "blockHash": "0xd8d3b1532b376dcb34c6c36fc7bc167cfc048cfcb3cc53a9ab7dab7f383d4240",
    "blockNumber": "0x961a86",
    "contractAddress": null,
    "cumulativeGasUsed": "0x778412",
    "from": "0xcc5ce245a296667aca1b5855f1a05ca950017e68",
    "gasUsed": "0x8acb",
    "logs": [],
    "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    "status": "0x1",
    "to": "0xa57bd00134b2850b2a1c55860c9e9ea100fdd6cf",
    "transactionHash": "0xcb1e3530950cf2c43a307bcb5645ae71a12c76a60831617badd04aea3efe68aa",
    "transactionIndex": "0x4e"
  }
}

Here I have

"cumulativeGasUsed": "0x778412"  => 7,832,594 [ what  ??? ]
"gasUsed": "0x8acb"              => 35,531    [ gas used by this transaction]

So to reconstruct the webpage info I need 2 api call, to retrieve gas price and gas used.

but i have no quotation of eth, and this is really what I need

1
  • Did you find a solution ?
    – Ɛɔıs3
    Commented Nov 21, 2021 at 14:10

3 Answers 3

1

You can use CryptoCompare API, for example:

const ETH_AMOUNT = "0.000284248";

const request = require("request");
const Decimal = require("decimal.js");

request.get("https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD", function(error, response, body) {
    if (error)
        throw new Error(error);
    else if (!response)
        throw new Error("no response");
    else if (response.statusCode != 200)
        throw new Error("bad response");
    else
        convert(JSON.parse(body).USD);
});

function convert(usdRate) {
    const USD_AMOUNT = new Decimal(ETH_AMOUNT).mul(usdRate);
    console.log(USD_AMOUNT.toFixed(2 /* decimal places */));
}
6
  • Thanks for suggestion, but in this wayI need 2 api call to etherscan (or using web3js, are 2 calls anyway) and 1 api call to cryptocompare; I'll wait then I'll start having doing web scraping...
    – realtebo
    Commented Apr 9, 2020 at 9:50
  • @realtebo: You need 1 api call to etherscan and another api call to cryptocompare. AFAIK, etherscan doesn't provide FIAT to ETH prices, so it's not like you can do this via etherscan alone anyway. Commented Apr 9, 2020 at 11:17
  • No, we need 2 call to etherscan, because I need not only gasUsed but also gasPrice, and they need 2 calls. Actually, we've already switched to web3.js methods equivalent (getTransaction and getTransactionReceipt). Well'try cryptocompare soon
    – realtebo
    Commented Apr 9, 2020 at 13:19
  • @realtebo did you ever found workaround to not do two api calls to etherscan ?. Is it possible to get Transaction fee by just one call ? Commented Nov 11, 2021 at 10:13
  • No, I didn't found a way, sorrry
    – realtebo
    Commented Nov 11, 2021 at 18:39
1

I do not know if in the meantime you were able to solve the issue, but as of January 7th 2022 the API call eth_getTransactionReceiptreturns a field named effectiveGasPrice which corresponds to the same value that is being returned from the eth_getTransactionByHash API call which is "0x1dcd65000".

Let's take another transaction, in this case:

0xf23766719c61461673d9e6f5cdc1e9ba42c958c07b924bfb9809cd58c244b4c0

effectiveGasPrice from eth_getTransactionReceipt: 0x19bbced2d3

gasPrice from eth_getTransactionByHash: 0x19bbced2d3

In other words eth_getTransactionReceipt is sufficient to retrieve both the gas price and the gas used.

2
  • A litte misunderstood. I have gasPrice I need gas price in EURO or DOLLARS
    – realtebo
    Commented Jan 10, 2022 at 9:25
  • 1
    For that you have to do another api call, either you pay the api pro service to get the ether price in USD, from the ethdailyprice api call, or you use another service like coingecko for the price in USD and EURO but it will not be the same value as on the etherscan page
    – Garantees
    Commented Jan 10, 2022 at 11:27
1

I was just doing a similar thing (but calc before i send a tx).

The process is to

  1. Get the average price for the currency you want the value in. i.e. USD. Coingecko has a free api that returns a list of exchanges' prices. I take the average of those to arrive at a current price.
  2. Get the price per unit. i.e. to get USD to Gwei you would do avgPriceUSD / 1000000000. This will give you USD per Gwei. With this you can now do a lookup.
  3. Calc the gas used. It looks like you've got that covered with a bunch of these answers.
  4. Simply multiply the gas per gwei to how much gas (in gwei) you've spent.

If you are trying to figure this out for tx in the past, you're gonna need to fetch the price at a certain point in time. (you can use the free coingecko api or your preference.)

I hope i understood your original question correctly. Reading back, i might have. I'm gonna leave this and delete if not cause i gotta run. lol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.