Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use web3.js to obtain the number of transaction confirmations in Ethernet Fong

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use web3.js to obtain the transaction confirmation number of Ethernet Fong". In the daily operation, I believe that many people have doubts about how to use web3.js to obtain the transaction confirmation number of Ethernet Fong. The editor consulted all kinds of data and sorted out a simple and useful operation method. I hope it will be helpful for you to answer the doubt of "how to use web3.js to obtain the transaction confirmation number of Ethernet Fong". Next, please follow the editor to study!

To get an Ethernet Fong transaction confirmation, you need to use two RPC calls:

Eth_getTransactionReceipt: get transaction receipts

Eth_blockNumber: get the latest block number

First, use the eth_getTransactionReceipt call to get the specified transaction receipt, for example, the following command gets the receipt for the transaction 0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238:

~ $curl-X POST-- data'{"jsonrpc": "2.0", "method": "eth_getTransactionReceipt", "params": ["0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"], "id": 1} 'http://localhost:8545

The block number of the exchange can be obtained in the result:

{"id": 1, "jsonrpc": "2.0", "result": {. BlockNumber: '0xbaked, / / Block 11...}}

You can then use the eth_blockNumber call at any subsequent time to get the current latest block information, such as:

~ $curl-X POST-- data'{"jsonrpc": "2.0", "method": "eth_blockNumber", "params": [], "id": 83} 'http://localhost:8545

You can get the latest block number in the result:

{"id": 83, "jsonrpc": "2.0", "result": "0xf" / / Block 15}

Use the number of the last block in the block chain, subtract the block number of the exchange, and add 1 to get a confirmation number of transactions in Ethernet Fong:

Number of transaction confirmations = latest block number-Exchange at block number + 1 = 15-11 + 1 = 5 use web3.js to obtain the number of transaction confirmations in Ethernet Square

Based on the above principle, it is easy to write web3.js code to obtain the number of transaction confirmations in Ethernet Fong:

Async function getTxConfirms (txhash) {const receipt = await web3.eth.getTransactionReceipt (txhash) / / later... Const latest = await web3.eth.getBlockNumber () / / confirms return latest-receipt.number + 1}

By calling the getTxConfirms () function implemented above, you can get the current number of confirmations for the specified transaction:

GetTxConfirms ('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b'). Then (confirms = > console.log (confirms)), the study on "how to use web3.js to obtain the transaction confirmation number of Ethernet place" is over. I hope to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report