Web3js

如何獲取交易雜湊的完整資訊?Web3.py 或 Web3.js

  • December 20, 2021

我是 Aditya,我想知道如何通過 Web3 獲取交易的詳細資訊。

請參閱此 BscScan 的 TX 雜湊:https : //bscscan.com/tx/0xa826ea39430a7d77f1b0ba31eb0de9f54662c8a7b891ca0bec8affce96bbb312 非常漂亮,對嗎?

在此處輸入圖像描述

但在我的情況下,使用此程式碼會給出瘋狂的結果

tx_id = '0xa826ea39430a7d77f1b0ba31eb0de9f54662c8a7b891ca0bec8affce96bbb312'
result = web3.eth.getTransactionReceipt(tx_id)

print(web3.toJSON(result))

輸出

{"transactionIndex": 251, "blockNumber": 13614675, "gasUsed": 130167, "to": "0x10ED43C718714eb63d5aA57B78B54704E256024E", "from": "0xdde85FF49AB49615A9864a5e2aFB04b3bd2F9199", "logs": [{"address": "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c", "data": "0x00000000000000000000000000000000000000000000000001e01c2029dc614d", "transactionHash": 
"0xa826ea39430a7d77f1b0ba31eb0de9f54662c8a7b891ca0bec8affce96bbb312", "blockHash": "0x60048fc2c731929fd8fffccac3707922e0e0b74cf64b503efbba759b6827600a", "topics": ["0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c", "0x00000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e"], "blockNumber": 13614675, "transactionIndex": 251, "logIndex": 782, "removed": false}, {"address": "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c", "topics": ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x00000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e", "0x00000000000000000000000058f876857a02d6762e0101bb5c46a8c1ed44dc16"], "blockNumber": 13614675, "transactionHash": "0xa826ea39430a7d77f1b0ba31eb0de9f54662c8a7b891ca0bec8affce96bbb312", "blockHash": "0x60048fc2c731929fd8fffccac3707922e0e0b74cf64b503efbba759b6827600a", "data": "0x00000000000000000000000000000000000000000000000001e01c2029dc614d", "transactionIndex": 251, "logIndex": 783, "removed": false}, {"logIndex": 
784, "transactionHash": "0xa826ea39430a7d77f1b0ba31eb0de9f54662c8a7b891ca0bec8affce96bbb312", "topics": ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x00000000000000000000000058f876857a02d6762e0101bb5c46a8c1ed44dc16", "0x000000000000000000000000dde85ff49ab49615a9864a5e2afb04b3bd2f9199"], "data": "0x000000000000000000000000000000000000000000000003e733628714200000", "blockNumber": 13614675, "transactionIndex": 251, "blockHash": "0x60048fc2c731929fd8fffccac3707922e0e0b74cf64b503efbba759b6827600a", "removed": false, "address": "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56"}, {"blockNumber": 13614675, "transactionHash": "0xa826ea39430a7d77f1b0ba31eb0de9f54662c8a7b891ca0bec8affce96bbb312", "removed": false, "topics": ["0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1"], "data": "0x000000000000000000000000000000000000000000005e227acf35ec4d5086e4000000000000000000000000000000000000000000c4673dd7c66fd693a21f0d", "transactionIndex": 251, "blockHash": "0x60048fc2c731929fd8fffccac3707922e0e0b74cf64b503efbba759b6827600a", "logIndex": 785, "address": "0x58F876857a02D6762E0101bb5C46A8c1ED44Dc16"}, {"transactionIndex": 251, "logIndex": 786, "topics": ["0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822", "0x00000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e", "0x000000000000000000000000dde85ff49ab49615a9864a5e2afb04b3bd2f9199"], "blockNumber": 13614675, "transactionHash": "0xa826ea39430a7d77f1b0ba31eb0de9f54662c8a7b891ca0bec8affce96bbb312", "removed": false, "address": "0x58F876857a02D6762E0101bb5C46A8c1ED44Dc16", "data": "0x00000000000000000000000000000000000000000000000001e01c2029dc614d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003e733628714200000", "blockHash": "0x60048fc2c731929fd8fffccac3707922e0e0b74cf64b503efbba759b6827600a"}], "logsBloom": "0x00200200000000100000000080000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000008000000200000000000000000800400008000000800001000000000004000000000000000000000008000000000200010000000000000000000000000000000000000000000240001000000080000004000000800000000000000000000000000020000000000000000000000000000000000000000000002000000020000000000001000000000000000001000000000000080000000000000000000000000000001000000000000000000400000000000000000", "status": 1, "transactionHash": "0xa826ea39430a7d77f1b0ba31eb0de9f54662c8a7b891ca0bec8affce96bbb312", "blockHash": "0x60048fc2c731929fd8fffccac3707922e0e0b74cf64b503efbba759b6827600a", "contractAddress": null, "cumulativeGasUsed": 41055238, "type": "0x0"}

那麼有人知道如何獲取所有這些資訊嗎?或者它被編碼或其他東西。我想得到這個 TX 中大約 71 美元的交易價值。

方法web3.eth.getTransaction(<TX_HASH>);將返回一個具有屬性的對象,value這是傳遞給事務的值參數,但表示為 wei。要將其轉換為 ETH(在您的情況下為 BNB),您需要使用web3.utils.fromWei(<WEI_AMOUNT>, 'ether');. 現在你得到了 ETH(或 BNB)的金額,如果你想找到它的美元價值,你必須使用像 coinmarketcap 或 coingecko 這樣的外部 API 來獲得與交易價值等值的美元價值。

例子:

web3.eth.getTransaction(<TX_HASH>, function(err, result) {
   if (result.value) {
       console.log(web3.utils.fromWei(result.value, 'ether'));
   }
});

引用自:https://ethereum.stackexchange.com/questions/116945