如何使用本地币安智能链lightclient通过web3.js访问区块链

Posted

技术标签:

【中文标题】如何使用本地币安智能链lightclient通过web3.js访问区块链【英文标题】:How to use local binance smart chain lightclient to access the blockchain with web3.js 【发布时间】:2021-06-21 19:22:23 【问题描述】:

我已经下载并启动了 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 相同),但我不明白为什么在尝试使用本地轻节点时会引发错误?

【问题讨论】:

我已经看到,在 geth 的情况下,必须手动启用 api,但我在 binances 节点二进制文档中找不到任何相关内容。 【参考方案1】:

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

【讨论】:

所以不能使用币安链轻客户端?兼容 EVM lightnode 的前提条件是什么?

以上是关于如何使用本地币安智能链lightclient通过web3.js访问区块链的主要内容,如果未能解决你的问题,请参考以下文章

BSC(币安智能链)主网链部署

如何在币安智能链BSC上发行代币

如何将 BSCScan 中的合约部署到币安智能链上?

币安智能链查询账户余额-解码bech32失败

Roxe生态通证ROC将跨链至币安智能链(BSC)

Roxe生态通证ROC将跨链至币安智能链(BSC)