原文在这里。
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这样的标准以太坊节点支持订阅特定的事件。此外,还有一些以太坊节点提供额外的自定义订阅。如你在这个指南中所看到的,web3.js使你能够直接订阅标准事件。它还为你提供了订阅自定义订阅的能力,如你在自定义订阅指南中所看到的。
重要提示 如果你是为用户提供自定义订阅的开发者。我们鼓励你在阅读下面的自定义订阅部分后,开发一个web3.js插件。你可以在web3.js插件开发者指南中找到如何开发插件的方法。
on("data") - 每当有新的日志进入时触发,日志对象作为参数。
subcription.on("data", (data) => console.log(data));
on("changed") - 每当区块链中移除一个日志时触发。该日志将有额外的属性 “removed: true”。
subcription.on("changed", (changed) => console.log(changed));
on("error") - 当订阅中出现错误时触发。
subcription.on("error", (error) => console.log(error));
on("connected") - 在订阅成功连接后触发一次。返回订阅id。
subcription.on("connected", (connected) => console.log(connected));
logs:在LogsSubscription类中实现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类中实现pendingTransactions:与newPendingTransactions一样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);
newBlockHeader:在NewHeadsSubscription类中实现newHeads:与newBlockHeader一样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:在SyncingSubscription类中实现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)进行许可,使用时请注明出处。
Author: mengbin
blog: mengbin
Github: mengbin92
cnblogs: 恋水无意
腾讯云开发者社区:孟斯特