1

I'm attempting to calculate slippage of the past X number of blocks to determine if a potential trade is likely to slip beyond the threshold 1% level. If it does I will cancel the trade.

To do this, I have used web3.eth.getPastLogs() and have started to receive this error:

Error: CONNECTION ERROR: Couldn't connect to node on WS.
    at Object.ConnectionError (/Users/TrentKennelly/trading_bot_V2/node_modules/web3-core-helpers/lib/errors.js:66:23)
    at Object.InvalidConnection (/Users/TrentKennelly/trading_bot_V2/node_modules/web3-core-helpers/lib/errors.js:36:21)
    at /Users/TrentKennelly/trading_bot_V2/node_modules/web3-providers-ws/lib/index.js:161:37
    at Map.forEach (<anonymous>)
    at WebsocketProvider._onClose (/Users/TrentKennelly/trading_bot_V2/node_modules/web3-providers-ws/lib/index.js:160:28)
    at W3CWebSocket._dispatchEvent [as dispatchEvent] (/Users/TrentKennelly/trading_bot_V2/node_modules/yaeti/lib/EventTarget.js:115:12)
    at W3CWebSocket.onClose (/Users/TrentKennelly/trading_bot_V2/node_modules/websocket/lib/W3CWebSocket.js:228:10)
    at WebSocketConnection.<anonymous> (/Users/TrentKennelly/trading_bot_V2/node_modules/websocket/lib/W3CWebSocket.js:201:17)
    at WebSocketConnection.emit (node:events:513:28)
    at WebSocketConnection.drop (/Users/TrentKennelly/trading_bot_V2/node_modules/websocket/lib/WebSocketConnection.js:475:14)
    at /Users/TrentKennelly/trading_bot_V2/node_modules/websocket/lib/WebSocketConnection.js:303:18
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
  code: 1009,
  reason: 'Frame size of 5607436 bytes exceeds maximum accepted frame size'
}

I have attempted to increase the maxReceivedFrameSize in my truffle-config, which is a solution offered here like so:

networks: {
    mainnet: {
      provider: () => new HDWalletProvider(mnemonic, `wss://mainnet.infura.io/ws/v3/${process.env.INFURA_API_KEY}`,
        {
            clientConfig: {
                maxReceivedFrameSize: 100000000,
                maxReceivedMessageSize: 100000000
            }
        }),
      network_id: '*', 
      gasPrice: 100000000000
    }
}

Here is the function that is producing the error. :

const determineSlippage = async (_token0, _token1, _pairContract) => {

  console.log(`Calculating Slippage...\n`)

  const endpoint = 'https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2'

  // Set the token pair to analyze
  const token0 = _token0 
  const token1 = _token1 

  // Set the time interval to analyze (in blocks)
  const blocks = 500

async function getTradesForPair(token0, token1, blocks, uPairValue) {
  // Get the latest block number
  const latestBlockNumber = await web3.eth.getBlockNumber();

  // Determine the block range to search for trades
  const startBlockNumber = Math.max(latestBlockNumber - blocks, 0);
  const endBlockNumber = latestBlockNumber;
              
  const pair = _pairContract;

  const filter = {
    fromBlock: startBlockNumber,
    toBlock: endBlockNumber,
    topics: [web3.utils.sha3('Swap(address,uint256,uint256,uint256,uint256,address)')]
  };

  // Get the past Swap events from the Uniswap pair contract
  const events = await web3.eth.getPastLogs(filter);

  // Create an array of trades from the Swap events
  const trades = events.map(event => {
  const { amount0In, amount1In, amount0Out, amount1Out } = event.returnValues;
  const { token0, token1 } = pair.options;
  const trade = { 
    inputToken: token0.options.address === token0Address ? token0 : token1,
    outputToken: token0.options.address === token0Address ? token1 : token0,
    inputAmount: web3.utils.toBN(token0.options.address === token0Address ? amount0In : amount1In),
    outputAmount: web3.utils.toBN(token0.options.address === token0Address ? amount1Out : amount0Out)
  };

  return trade;

});

return trades;
}

As a final note, this error occurs whether blocks is 500 or 100. Doesn't seem to matter. Any thoughts?

1 Answer 1

1

I'm just asking for too much data at a time and have had to break it into smaller bits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.