Search code examples
ethereumweb3jsbinancebinance-chain

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.


Solution

  • Few months later, I remembered asking this. I ended up using an external RPC node provider (fe. QuickNode, Ankr, Infura).

    I don't have my exact code on hand, but I know that the steps were as follows:

    • Open an account on a platform which offers RPC services
    • Eg. Infura, Ankr, QuickNode (these were the ones that I found)
    • These services will let you create your own node for a certain blockchain (I saw Eth, Bsc, Polygon, AvalancheC and many others)
    • After you create your own node, which is of course, paid service, you get an HTTP link for your node, which looks something like this https://mynode2241235.ankr.com/, let's call that LINK from now.
    • Back on my server, I took the regular Web3 connection code snippet and just modified it by putting this LINK as an HttpProvider, so sth like this:

    const instance = new Web3(new Web3.providers.HttpProvider(LINK));