Etherscan api:如何獲得交易費?
例如:打開這個https://etherscan.io/tx/0xcb1e3530950cf2c43a307bcb5645ae71a12c76a60831617badd04aea3efe68aa
這是一個隨機的
我在網頁上看到這些資訊:
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)
我知道
fee = gas_price x used_gas
=>0.000000008 x 35,531 = 0.000284248 eth
完美的。
我有興趣閱讀,通過 api 以真實貨幣轉換的交易費,就像你看到的那樣
$0.05
我無法通過單個 api 呼叫在 Etherscan 上返回此資訊。
我該怎麼做才能獲得這些資訊?
打電話
我只得到這些資訊
{ "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" } }
在哪裡
"gas": "0x21534"
=> 十進制是136,500
$$ gas limit $$
"gasPrice"○: "0x1dcd65000"
=> 十進制是8,000,000,000
$$ gas price $$ 我也嘗試呼叫 https://api.etherscan.io/api?module=proxy&action=eth_getTransactionReceipt&txhash=0xcb1e3530950cf2c43a307bcb5645ae71a12c76a60831617badd04aea3efe68aa&apikey=your_api_key
這是結果
{ "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" } }
在這裡我有
"cumulativeGasUsed": "0x778412" => 7,832,594 [ what ??? ] "gasUsed": "0x8acb" => 35,531 [ gas used by this transaction]
因此,要重建網頁資訊,我需要 2 個 api 呼叫,以檢索 gas 價格和使用的 gas。
但我沒有引用 eth,這確實是我需要的
您可以使用 CryptoCompare API,例如:
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 */)); }
我不知道在此期間您是否能夠解決問題,但截至2022 年 1 月 7 日,API 呼叫
eth_getTransactionReceipt
返回一個名為的欄位,該欄位effectiveGasPrice
對應於從 API 呼叫返回的相同值,eth_getTransactionByHash
即"0x1dcd65000"
.讓我們進行另一筆交易,在這種情況下:
0xf23766719c61461673d9e6f5cdc1e9ba42c958c07b924bfb9809cd58c244b4c0
effectiveGasPrice
來自eth_getTransactionReceipt
:0x19bbced2d3
gasPrice
來自eth_getTransactionByHash
:0x19bbced2d3換句話說
eth_getTransactionReceipt
,足以檢索 gas 價格和使用的 gas。