1

我已经下载并启动了 lightclient 节点。

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

我可以看到代理已启动。我用

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

设置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)

然后我想连接到智能合约以查看正在发出的事件。

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)
}

我可以看到该节点已收到 ws 连接

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

但是 node.js 处理崩溃并出现错误

[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
}

当我使用 ANKR 的 urls 提供程序(与 ETH 中的 infura 相同)时,一切正常,但我不明白为什么在尝试使用本地轻节点时会引发错误?

4

1 回答 1

2

所以答案是我试图使用与 EVM 不兼容的“Binance Chain”轻节点进行连接,为了连接到 BSC,必须根据https://github.com/binance-chain/bsc使用 geth

于 2021-04-01T06:07:34.997 回答