简体   繁体   English

如何使用 web3 在 nodejs 中连接币安网络

[英]How to connect binance network in nodejs using web3

I tried to connect with binance testnet in Nodejs.我尝试在 Nodejs 中连接 binance 测试网。 This is my code.这是我的代码。

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('https://data-seed-prebsc-1-s2.binance.org:8545'));

But I got the error code like但我得到了错误代码

Error: The current provider doesn't support subscriptions: HttpProvider
    at Timeout._onTimeout (E:\test\share1\node_modules\web3-core-subscriptions\lib\subscription.js:176:24)

Please let me know how I can fix it.请让我知道如何解决它。

HTTP provider, does not support subscriptions. HTTP 提供程序,不支持订阅。

Source: https://web3js.readthedocs.io/en/v1.7.0/web3.html#value来源: https://web3js.readthedocs.io/en/v1.7.0/web3.html#value


If you want to use subscriptions, you need to connect to a websocket node - not HTTP.如果要使用订阅,则需要连接到 websocket 节点 - 而不是 HTTP。

There is a list of 3rd party node providers recommended by Binance in their docs page . Binance 在其文档页面中推荐了 3rd 方节点提供商列表。 Some of them support websocket connection as well.其中一些还支持 websocket 连接。

The above web3 part works fine.上面的 web3 部分工作正常。 Please try the below lines,请尝试以下几行,

var Web3 = require('web3');
const web3_bsc = new Web3('https://data-seed-prebsc-1-s2.binance.org:8545');

Then using the web3_bsc, access the functions in your contract or perform any functions that web3 supports.然后使用 web3_bsc,访问合约中的函数或执行 web3 支持的任何函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM