前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >web3.js事件监听

web3.js事件监听

作者头像
孟斯特
发布2024-05-11 10:25:51
1220
发布2024-05-11 10:25:51
举报
文章被收录于专栏:code人生code人生

原文在这里[1]

在通过web3.eth.sendTransaction, web3.eth.sendSignedTransaction, contractDeployed.methods['methodName'](...methodParams).send函数发送交易时,我们可以使用Web3PromiEvent来监听时间:

代码语言:javascript
复制
web3.eth.sendTransaction({...})
    .on('sending', (sending) => {
        // Sending example
        // 0x02f86d82053903849502f900849a9a0d16830186a0947ab80aeb6bb488b7f6c41c58e83ef248eb39c8828080c080a0b0fce643a6ca3077ee6b83590b1798d00edef99e2c65c1837daab88d46860887a07ca449a31b2430dbf21310b8c4491386762ade23e48c7cd0b70d315576374c7c
    })
    .on('sent', (sent) => {
        // Sent example
        // 0x02f86d82053903849502f900849a9a0d16830186a0947ab80aeb6bb488b7f6c41c58e83ef248eb39c8828080c080a0b0fce643a6ca3077ee6b83590b1798d00edef99e2c65c1837daab88d46860887a07ca449a31b2430dbf21310b8c4491386762ade23e48c7cd0b70d315576374c7c
    })
    .on('transactionHash', (transactionHash) => {
        // Transaction hash example
        // 0x6d85b2f07e7c8f2a7ce90a5bcfa3100c528f173f0707164434fb42d397d92d50
    })
    .on('confirmation', (confirmation) => {
        // Confirmation example
        // {
        //     confirmations: 1n,
        //         receipt: {
        //          blockHash: '0x947b8c95dea7f0c643f2be0e9d1c3bec76c7f5146fdf34f5f1efe6d2cab5f568',
        //               blockNumber: 22n,
        //               cumulativeGasUsed: 21000n,
        //               effectiveGasPrice: 2553565308n,
        //               from: '0xe2597eb05cf9a87eb1309e86750c903ec38e527e',
        //               gasUsed: 21000n,
        //               logs: [],
        //               logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
        //               status: 1n,
        //               to: '0x7ab80aeb6bb488b7f6c41c58e83ef248eb39c882',
        //               transactionHash: '0x3ec198ae10cf289b91210b4fd86a3b22cc9bcef16bca6beee21c35b76a2b7073',
        //               transactionIndex: 0n,
        //               type: 2n
        //          },
        //     latestBlockHash: '0x947b8c95dea7f0c643f2be0e9d1c3bec76c7f5146fdf34f5f1efe6d2cab5f568'
        // }

    })
    .on('receipt', (receipt) => {
        // Receipt example
        // {
        //     blockHash: '0x135d14b724d90b97feec1e96df590ce9af762d424aea49d29e11feaa24fe02f1',
        //     blockNumber: 23n,
        //     cumulativeGasUsed: 21000n,
        //     effectiveGasPrice: 2546893579n,
        //     from: '0xe2597eb05cf9a87eb1309e86750c903ec38e527e',
        //     gasUsed: 21000n,
        //     logs: [],
        //     logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
        //     status: 1n,
        //     to: '0x7ab80aeb6bb488b7f6c41c58e83ef248eb39c882',
        //     transactionHash: '0x9a6497fe4028d716e66a24ab7dfd3d1bcf136ba2ec26f427719b4ddaaff76fb7',
        //     transactionIndex: 0n,
        //     type: 2n
        // }

    })
    .on('error', (error) => {
        // Error example
        // InvalidResponseError: Returned error: exceeds block gas limit
        // at Web3RequestManager._processJsonRpcResponse (.../web3_request_manager.js:193:23)
        // at Web3RequestManager.<anonymous> (.../web3_request_manager.js:112:29)
        // at Generator.next (<anonymous>)
        // at fulfilled (.../web3_request_manager.js:5:58)
        // at processTicksAndRejections (node:internal/process/task_queues:96:5) {
        //             cause: { code: -32000, message: 'exceeds block gas limit' },
        //             code: 101,
        //             data: undefined,
        //             request: {
        //             jsonrpc: '2.0',
        //             id: 'ea1f8fb4-fe86-4492-9d89-c6e31bf1c036',
        //             method: 'eth_sendRawTransaction',
        //             params: [
        //             '0x02f86e82053903849502f900849a9a0d168405f7c1f0947ab80aeb6bb488b7f6c41c58e83ef248eb39c8828080c001a0ddd93f5ce9a6a0de130dc660e65d2cdf8784148b8c91b83635b8458e96a767a3a028c48b048bf041e530ded63a0d2198855043f782ef0aa47391a2afa9c50a5ff1'
        //             ]
        // }
    });

声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)[2]进行许可,使用时请注明出处。 Author: mengbin[3] blog: mengbin[4] Github: mengbin92[5] cnblogs: 恋水无意[6] 腾讯云开发者社区:孟斯特[7]

References

[1] 这里: https://docs.web3js.org/guides/wallet/promi_event [2] 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0): https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh [3] mengbin: mengbin1992@outlook.com [4] mengbin: https://mengbin.top [5] mengbin92: https://mengbin92.github.io/ [6] 恋水无意: https://www.cnblogs.com/lianshuiwuyi/ [7] 孟斯特: https://cloud.tencent.com/developer/user/6649301

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-05-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 孟斯特 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • References
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档