简体   繁体   中英

How to use local binance smart chain lightclient to access the blockchain with web3.js

I have downloaded and launched the lightclient node.

./lightd --chain-id "Binance-Chain-Tigris" --node tcp://dataseed1.binance.org:80

I[2021-03-25|03:19:19.040] Connecting to source HTTP client...
I[2021-03-25|03:19:19.041] Constructing Verifier...
I[2021-03-25|03:19:19.041] lite/proxy/NewVerifier()...                  module=lite/proxy chainID=Binance-Chain-Tigris rootDir=.binance-lite client=WSEvents
I[2021-03-25|03:19:19.098] DBProvider.LatestFullCommit()...             module=lite label=trusted.mem chainID=Binance-Chain-Tigris minHeight=1 maxHeight=9223372036854775807
I[2021-03-25|03:19:19.098] DBProvider.LatestFullCommit()...             module=lite label=trusted.lvl chainID=Binance-Chain-Tigris minHeight=1 maxHeight=9223372036854775807
I[2021-03-25|03:19:19.099] DBProvider.LatestFullCommit() found latest.  module=lite label=trusted.lvl height=1
I[2021-03-25|03:19:19.100] Starting proxy...
I[2021-03-25|03:19:19.697] Starting RPC HTTP server on 127.0.0.1:27147

I can see that the proxy is started. I use

WSS_URL=tcp://0.0.0.0:27147/websocket
HTTPS_URL=http://0.0.0.0:27147

to setup the web3.

const httpsUrl = process.env.HTTPS_URL
const wssUrl = process.env.WSS_URL
const jsonRpcUrl = process.env.JSON_RPC_URL
const web3 = Helpers.initWeb3(httpsUrl)
const web3Socket = Helpers.initWeb3Socket(wssUrl)

Then I want to connect to smart contract in order to see the events being emitted.

let factoryContract = Helpers.initContract(web3Socket, factoryABI, factoryAddress)
trackPairCreatedEvent(factoryContract)

function trackPairCreatedEvent(factoryContract) {
  trackEvent(
    factoryContract.events.PairCreated(),
    handlePairCreatedEvent
  )
}

function trackEvent(event, callback) {
  event
    .on('data', callback)
    .on('error', console.error)
}

I can see that node has received the ws connection

I[2021-03-25|03:27:17.455] New websocket connection                     remote=127.0.0.1:59376
I[2021-03-25|03:27:17.455] Starting wsConnection                        remote=127.0.0.1:59376 impl=wsConnection

But the node.js process the crashes with an error

[25.03.2021 03:21.34.630] [ERROR] Error: Returned error: Method not found
    at Object.ErrorResponse (/Users/ok/Workspace/aimbot/node_modules/web3-core-helpers/lib/errors.js:28:19)
    at Object.callback (/Users/ok/Workspace/aimbot/node_modules/web3-core-requestmanager/lib/index.js:303:36)
    at /Users/ok/Workspace/aimbot/node_modules/web3-providers-ws/lib/index.js:114:45
    at Array.forEach (<anonymous>)
    at WebsocketProvider._onMessage (/Users/ok/Workspace/aimbot/node_modules/web3-providers-ws/lib/index.js:102:69)
    at W3CWebSocket._dispatchEvent [as dispatchEvent] (/Users/ok/Workspace/aimbot/node_modules/yaeti/lib/EventTarget.js:115:12)
    at W3CWebSocket.onMessage (/Users/ok/Workspace/aimbot/node_modules/websocket/lib/W3CWebSocket.js:234:14)
    at WebSocketConnection.<anonymous> (/Users/ok/Workspace/aimbot/node_modules/websocket/lib/W3CWebSocket.js:205:19)
    at WebSocketConnection.emit (node:events:376:20)
    at WebSocketConnection.processFrame (/Users/ok/Workspace/aimbot/node_modules/websocket/lib/WebSocketConnection.js:554:26)
    at /Users/ok/Workspace/aimbot/node_modules/websocket/lib/WebSocketConnection.js:323:40
    at processTicksAndRejections (node:internal/process/task_queues:75:11) {
  data: null
}

Everything works perfectly when I used urls provider by ANKR (same as infura in ETH), but I dont understand why an error is thrown when I try to use local light node?

So the answer is that I was trying to connect using "Binance Chain" lightnode which is not EVM compatible, in order to connect to BSC geth must be used according to https://github.com/binance-chain/bsc

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