微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何使用本地币安智能链轻客户端通过web3.js访问区块链

我已经下载并启动了轻客户端节点。

./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 provider(与 ETH 中的 infura 相同)时,一切正常,但我不明白为什么当我尝试使用本地轻节点时会抛出错误

解决方法

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

使用 geth

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。