简体   繁体   English

WebSocket 使用 NodeJS 在客户端进行缩放

[英]WebSocket scaling on the client side with NodeJS

I have written the script below which creates multiple WebSocket connections with a smart contract to listen to events.我编写了下面的脚本,该脚本创建多个 WebSocket 连接与智能合约以监听事件。 it's working fine but I feel this is not an optimized solution and probably this could be done in a better way.它工作正常,但我觉得这不是一个优化的解决方案,可能这可以以更好的方式完成。

 const main = async (PAIR_NAME, PAIR_ADDRESS_UNISWAP, PAIR_ADDRESS_SUSHISWAP) => { const PairContractHTTPUniswap = new Blockchain.web3http.eth.Contract( UniswapV2Pair.abi, PAIR_ADDRESS_UNISWAP ); const PairContractWSSUniswap = new Blockchain.web3ws.eth.Contract( UniswapV2Pair.abi, PAIR_ADDRESS_UNISWAP ); const PairContractHTTPSushiswap = new Blockchain.web3http.eth.Contract( UniswapV2Pair.abi, PAIR_ADDRESS_SUSHISWAP ); const PairContractWSSSushiswap = new Blockchain.web3ws.eth.Contract( UniswapV2Pair.abi, PAIR_ADDRESS_SUSHISWAP ); var Price_Uniswap = await getReserves(PairContractHTTPUniswap); var Price_Sushiswap = await getReserves(PairContractHTTPSushiswap); // subscribe to Sync event of Pair PairContractWSSUniswap.events.Sync({}).on("data", (data) => { Price_Uniswap = (Big(data.returnValues.reserve0)).div(Big(data.returnValues.reserve1)); priceDifference(Price_Uniswap, Price_Sushiswap, PAIR_NAME); }); PairContractWSSSushiswap.events.Sync({}).on("data", (data) => { Price_Sushiswap = (Big(data.returnValues.reserve0)).div(Big(data.returnValues.reserve1)); priceDifference(Price_Uniswap, Price_Sushiswap, PAIR_NAME); }); }; for (let i = 0; i < pairsArray.length; i++){ main(pairsArray[i].tokenPair, pairsArray[i].addressUniswap, pairsArray[i].addressSushiswap); }

In the end, I instantiate the main function multiple times for each pair from a pair array, in a for-loop.最后,我在for循环中为一对数组中的每一对多次实例化主function。 I think this way of solving is brute force and there is a better way of doing this.我认为这种解决方法是蛮力的,有更好的方法来做到这一点。

Any suggestions/opinions would be really appreciated.任何建议/意见将不胜感激。

Just to clear up the terms: You're opening a websocket connection to the WSS node provider - not to the smart contracts.只是为了澄清条款:您正在打开 websocket 连接到 WSS 节点提供程序 - 而不是智能合约。 But yes, your JS snippet subscribes to multiple channels (one for each contract) within this one connection (to the node provider).但是,是的,您的 JS 片段在这个连接(到节点提供者)中订阅了多个通道(每个合同一个)。

You can collect event logs from multiple contracts through just one WSS channel using the web3.eth.subscribe("logs") function ( docs ), passing it the list of contract addresses as a param.您可以使用web3.eth.subscribe("logs") function ( docs ) 仅通过一个 WSS 通道从多个合约收集事件日志,并将合约地址列表作为参数传递给它。 Example:例子:

const options = {
    // list of contract addresses that you want to subscribe to their event logs
    address: ["0x123", "0x456"]
};
web3.eth.subscribe("logs", options, (err, data) => {
    console.log(data);
});

But it has a drawback - it doesn't decode the event log data for you.但它有一个缺点 - 它不会为您解码事件日志数据。 So your code will need to find the expected data types based on the event signature (returned in data.topics[0] ).因此,您的代码将需要根据事件签名(在data.topics[0]中返回)找到预期的数据类型。 Once you know which event log is emitted based on the topics[0] event signature (real-life example value in this answer ), you can use the decodeLog() function ( docs ) to get the decoded values.一旦您知道根据topics[0]事件签名(此答案中的实际示例值)发出了哪个事件日志,您就可以使用decodeLog() function ( docs ) 来获取解码值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM