0

Hi i have an issue i created a function that fetches prices from dex.This is looped every 5 sec and the prices should be up to date every 5 sec.The issues start because the price refreshes every few iterations(this take around 30 sec to update).I expect that the price should be updated every new function iteration of the function.The code dont have any issues i using Alchemy and i am fetching price directly from mainnet.

I dont know what may cause the issue maybe some kind of latency on Alchemy site or its my fault. Maybe fetches price directly from blockchain is bad idea but i dont know what may cause this troubles.

Shorter version of code:


var provider = new ethers.JsonRpcProvider(
  `https://eth-mainnet.g.alchemy.com/v2/${process.env.API_KEY}`
);
var WETH_USDT_V3 = "0x4e68Ccd3E89f51C3074ca5072bbAC773960dFa36"; // replace with actual address

var v3PoolArtifact = require("@uniswap/v3-core/artifacts/contracts/UniswapV3Pool.sol/UniswapV3Pool.json");

var v3Pool = new ethers.Contract(WETH_USDT_V3, v3PoolArtifact.abi, provider);

async function PriceUpdate() {
  try {
    var { sqrtPriceX96 } = await v3Pool.slot0();
    var uniswapV3Price = sqrtToPrice(sqrtPriceX96);

    console.log(
      "Uni V3",
      "|",
      "pair:",
      "WETH/USDT",
      "|",
      "price:",
      uniswapV3Price
    );
  } catch (error) {
    console.error(error);
  }
}

const interval = 5000; // 5 seconds

setInterval(PriceUpdate, interval);

1 Answer 1

2

Ethereum generates a new block every ~12 seconds. All on-chain data remains the same during the interval. So, it is normal to have no updates in 5 seconds.

1
  • So the prices on Uniswap also refresh every 12s ?
    – Xardesso
    Dec 8, 2023 at 13:25

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.