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

web3.js:事件订阅

作者头像
孟斯特
发布2024-05-17 18:48:00
1070
发布2024-05-17 18:48:00
举报
文章被收录于专栏:code人生code人生

原文在这里[1]。

订阅智能合约事件

代码语言:javascript
复制
import { Web3 } from "web3";

// set a provider - MUST be a WebSocket(WSS) provider
const web3 = new Web3("wss://ethereum-rpc.publicnode.com");

async function subscribe() {
  // create a new contract object, providing the ABI and address
  const contract = new web3.eth.Contract(abi, address);

  // subscribe to the smart contract event
  const subscription = contract.events.EventName();

  // new value every time the event is emitted
  subscription.on("data", console.log);
}

// function to unsubscribe from a subscription
async function unsubscribe(subscription) {
    await subscription.unsubscribe();
}

subscribe();
unsubscribe(subscription);

订阅节点事件

像Geth这样的标准以太坊节点支持订阅特定的事件[2]。此外,还有一些以太坊节点提供额外的自定义订阅。如你在这个指南中所看到的,web3.js使你能够直接订阅标准事件。它还为你提供了订阅自定义订阅的能力,如你在自定义订阅[3]指南中所看到的。

重要提示 如果你是为用户提供自定义订阅的开发者。我们鼓励你在阅读下面的自定义订阅[4]部分后,开发一个web3.js插件。你可以在web3.js插件开发者指南[5]中找到如何开发插件的方法。

on("data") - 每当有新的日志进入时触发,日志对象作为参数。

代码语言:javascript
复制

on("changed") - 每当区块链中移除一个日志时触发。该日志将有额外的属性 "removed: true"。

代码语言:javascript
复制

on("error") - 当订阅中出现错误时触发。

代码语言:javascript
复制

on("connected") - 在订阅成功连接后触发一次。返回订阅id。

代码语言:javascript
复制

Logs

logs:在LogsSubscription类中实现

代码语言:javascript
复制
import { Web3 } from "web3";

const web3 = new Web3("wss://ethereum-rpc.publicnode.com");

async function subscribe() {
  //create subcription
  const subcription = await web3.eth.subscribe("logs");

  //print logs of the latest mined block
  subcription.on("data", (data) => console.log(data));
}

// function to unsubscribe from a subscription
async function unsubscribe(subscription) {
    await subscription.unsubscribe();
}

subscribe();
unsubscribe(subscription);

追加交易

newPendingTransactions:在NewPendingTransactionsSubscription[6]类中实现•pendingTransactions:与newPendingTransactions一样

代码语言:javascript
复制
import { Web3 } from "web3";

const web3 = new Web3("wss://ethereum-rpc.publicnode.com");

async function subscribe() {
  //create subcription
  const subcription = await web3.eth.subscribe("pendingTransactions"); //or ("newPendingTransactions")

  //print tx hashs of pending transactions
  subcription.on("data", (data) => console.log(data));
}

// function to unsubscribe from a subscription
async function unsubscribe(subscription) {
    await subscription.unsubscribe();
}

subscribe();
unsubscribe(subscription);

Block headers

newBlockHeader:在NewHeadsSubscription[7]类中实现•newHeads:与newBlockHeader一样

代码语言:javascript
复制
import { Web3 } from "web3";

const web3 = new Web3("wss://ethereum-rpc.publicnode.com");

async function subscribe() {
  //create subcription
  const subcription = await web3.eth.subscribe("newBlockHeaders"); //or ("newHeads")

  //print block header everytime a block is mined
  subcription.on("data", (data) => console.log(data));
}

// function to unsubscribe from a subscription
async function unsubscribe(subscription) {
    await subscription.unsubscribe();
}

subscribe();
unsubscribe(subscription);

Syncing

syncing:在SyncingSubscription[8]类中实现

代码语言:javascript
复制
import { Web3 } from "web3";

const web3 = new Web3("wss://ethereum-rpc.publicnode.com");

async function subscribe() {
  //create subcription
  const subcription = await web3.eth.subscribe("syncing");

  //this will return `true` when the node is syncing 
  //when it’s finished syncing will return `false`, for the `changed` event.
  subcription.on("data", (data) => console.log(data));
}

// function to unsubscribe from a subscription
async function unsubscribe(subscription) {
    await subscription.unsubscribe();
}

subscribe();
unsubscribe(subscription);

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

References

[1] 这里: https://docs.web3js.org/guides/events_subscriptions/ [2] 像Geth这样的标准以太坊节点支持订阅特定的事件: https://geth.ethereum.org/docs/interacting-with-geth/rpc/pubsub#supported-subscriptions [3] 自定义订阅: https://docs.web3js.org/guides/events_subscriptions/custom_subscriptions [4] 自定义订阅: https://docs.web3js.org/guides/events_subscriptions/custom_subscriptions [5] web3.js插件开发者指南: https://docs.web3js.org/guides/web3_plugin_guide/plugin_authors [6] NewPendingTransactionsSubscription: https://docs.web3js.org/api/web3-eth/class/NewPendingTransactionsSubscription [7] NewHeadsSubscription: https://docs.web3js.org/api/web3-eth/class/NewHeadsSubscription [8] SyncingSubscription: https://docs.web3js.org/api/web3-eth/class/SyncingSubscription [9] 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0): https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh [10] mengbin: mengbin1992@outlook.com [11] mengbin: https://mengbin.top [12] mengbin92: https://mengbin92.github.io/ [13] 恋水无意: https://www.cnblogs.com/lianshuiwuyi/ [14] 孟斯特: https://cloud.tencent.com/developer/user/6649301

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

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

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

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

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