Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

19节mempool中,如有用ethers v6版本, 捕获 websocket异常是如何处理呢? #110

Open
256yangwei opened this issue Nov 3, 2023 · 2 comments

Comments

@256yangwei
Copy link

console.log("\n1. 连接 wss RPC")
// 准备 alchemy API 可以参考https://github.com/AmazingAng/WTFSolidity/blob/main/Topics/Tools/TOOL04_Alchemy/readme.md
const ALCHEMY_MAINNET_WSSURL = 'wss://eth-mainnet.g.alchemy.com/v2/oKmOQKbneVkxgHZfibs-iFhIlIAl6HDN';
const provider = new ethers.providers.WebSocketProvider(ALCHEMY_MAINNET_WSSURL);
let i =0
provider.on("pending", async (txHash) => {
if (txHash && i < 100) {
// 打印txHash
console.log([${(new Date).toLocaleTimeString()}] 监听Pending交易 ${i}: ${txHash} \r);
i++
}
});
provider ._websocket.on("error", async () => {
console.log(Unable to connect to ${ep.subdomain} retrying in 3s...);
setTimeout(listen_address, 3000);
});
provider ._websocket.on("close", async (code) => {
console.log(
Connection lost with code ${code}! Attempting reconnect in 3s...
);
provider ._websocket.terminate();
setTimeout(listen_address, 3000);
});

求问一下,在第19节 Mempool里,如果是在 ethers v5里,以上的 provider ._websocket.on("error", async () => {});代码是可以正常捕获错误,延时后继续推送交易的;
但是在ethers V6里provider 定义变了 const provider = new ethers.WebSocketProvider(ALCHEMY_MAINNET_WSSURL);然后这个捕获异常provider ._websocket.on("error", async () => {})就不能用了,请问V6里捕获websocket error是怎么用的呢?谢谢大佬,往不吝赐教!

@cykk
Copy link

cykk commented Jun 14, 2024

可以参考V6的官方文档: https://docs.ethers.org/v6/search/?search=websocket

@cykk
Copy link

cykk commented Jun 14, 2024

provider.websocket.onerror = async () => {
console.log(``);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants