简体   繁体   中英

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.

As per my understanding, the Web3 library provides all the required stuff under the hood, and I can connect by calling

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

By following this, I put the following into my function, which serves as a Web3 object 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 .

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. 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

Unfortunately, this gives me the same result.

The above web3 part works fine. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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