简体   繁体   English

如何使用 Web3.js 库将服务器连接到币安智能链?

[英]How to connect server to Binance Smart Chain using the Web3.js library?

I'm trying to connect my small server web app in ExpressJS to the BSC blockchain, but without success.我正在尝试将 ExpressJS 中的小型服务器 Web 应用程序连接到 BSC 区块链,但没有成功。

As per my understanding, the Web3 library provides all the required stuff under the hood, and I can connect by calling根据我的理解,Web3 库提供了所有必需的东西,我可以通过调用连接

new Web3(new Web3.providers.HttpProvider(urlToRpcNode))

By following this, I put the following into my function, which serves as a Web3 object init:通过遵循这一点,我将以下内容放入我的函数中,该函数用作 Web3 对象 init:

return new Web3(new Web3.providers.HttpProvider(https://bsc-dataseed1.binance.org));

According to BscScan, the given argument is the URL to the RPC Node for BSC https://bscscan.com/apis#rpc .根据 BscScan,给定的参数是 BSC https://bscscan.com/apis#rpc的 RPC 节点的 URL。

Unfortunately, awaiting this call just gets stuck forever and doesn't return anything.不幸的是,等待这个调用会永远卡住并且不会返回任何东西。

What am I doing wrong?我究竟做错了什么? Any help is appreciated.任何帮助表示赞赏。

Edit:编辑:

I found another link to Binance Docs, where it outlines the specific process for connecting to BSC.我找到了另一个指向 Binance Docs 的链接,其中概述了连接到 BSC 的具体过程。 Here is how they do it:以下是他们的做法:

const web3 = new Web3('https://bsc-dataseed1.binance.org:443');

https://docs.binance.org/smart-chain/developer/create-wallet.html#connect-to-bsc-network https://docs.binance.org/smart-chain/developer/create-wallet.html#connect-to-bsc-network

Unfortunately, this gives me the same result.不幸的是,这给了我同样的结果。

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

var Web3 = require('web3');
const web3_bsc = new Web3('https://bsc-dataseed1.binance.org:443');

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