[Ethereum] Etherscan api: how to get transaction fee

etherscanexternal-apifees

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

Best Answer

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 */));
}