Solidity

如何獲取使用者交易的雜湊?

  • July 1, 2022

請幫幫我 。如果我們將使用: await provider.getTransaction(‘0xeed4fadf09e9560f01056f240f86b515dce717087a42c7266bf659edef555861’) —> 在這種情況下,我將只得到這個雜湊的結果。

但我需要為任何交易獲取動態使用者的雜湊值,例如: const tx = provider.getTransaction(anyTransactionHash) ;

那麼如何獲得 anyTransactionHash 呢?謝謝

可能您想要的是監控待處理的交易?如果是這種情況,您需要通過 websocket 連接到節點並訂閱“PendingTransactions”。像這樣的東西:

var Web3 = require('web3')

const main = async () => {
 // reconnect options
 const options = {
   reconnect: {
     auto: true,
     delay: 2000,
     maxAttempts: 3,
     onTimeout: false,
   },
 }
 const web3 = new Web3(
   new Web3.providers.WebsocketProvider('wss://your-node-endpoint/12345',
     options
   )
 )

 // subscribe to pendingTransactions events
 web3.eth
   .subscribe('pendingTransactions', async (error, result) => {
     if (error) console.log('error', error)
   })
   .on('data', async (trxId) => {
     // receives the transaction id
     console.log('TRX ID >> ', trxId)
     // query all transaction details using its id
     const trxDetails = await web3.eth.getTransaction(trxId)
     console.log(`TRX ${trxId} DETAILS >> `, trxDetails)
   })
}

main()

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