web3.js:事件订阅

2024-05-17 18:48:00 浏览数 (2)

原文在这里[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

0 人点赞