Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
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. 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. 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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Just to clear up the terms: You're opening a websocket connection to the WSS node provider - not to the smart contracts. But yes, your JS snippet subscribes to multiple channels (one for each contract) within this one connection (to the node provider).

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. 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]). 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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!