CRYPTO NEWS

IERC20 balanceOf() function doesn’t renew data with every new block

I’m trying to get balances of USDC and WETH from Uniswap V3 Pool and USDC/WETH price with every new block. This is my code:

async function getPrice(inputTokenA,inputTokenB){     let today = new Date()   let time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();      pairAddress = "0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8"    // Get ERC20 Token contract instance   pairAddress = "0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8"    smallToken0 = await ethers.getContractAt("IERC20","0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48")   smallToken1 = await ethers.getContractAt("IERC20","0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2")   let reserve0 = await smallToken0.balanceOf(pairAddress);   let reserve1 = await smallToken1.balanceOf(pairAddress);      let formattedUSDC = Number(       ethers.utils.formatUnits(reserve0, 6)     );       //     const quoterAddress = "0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6";   const quoterContract = new ethers.Contract(quoterAddress, QuoterABI, provider)   let quotedAmountOut = 0   let amtIn = "1"   let amountIn = ethers.utils.parseUnits(amtIn, 18).toString()       try {     quotedAmountOut = await quoterContract.callStatic.quoteExactInputSingle(       inputTokenA,       inputTokenB,       500,       amountIn,       0)   } catch (err) {     return 0   }      // Format Output     let outputAmount = ethers.utils.formatUnits(quotedAmountOut, 6)     let outputAmount2 = parseFloat(outputAmount).toFixed(0)     console.log(  formattedUSDC,reserve1, outputAmount2, time )  async function web(){      web3Ws = new Web3(new Web3.providers.WebsocketProvider("wss://mainnet.infura.io/ws/v3/infura"));       var subscription = web3Ws.eth.subscribe('newBlockHeaders', function(error, result){    })   .on("connected", function(subscriptionId){       console.log("ID",subscriptionId);   })   .on("data", function(blockHeader){     getPrice("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2","0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48")      let today = new Date()      if (today.getHours()>=18 && today.getMinutes()>=15){          subscription.unsubscribe(function(error, success){              console.log('Successfully unsubscribed!');       if(success) return   })};    })   

}

Everything works, but this is what I get:

52244518.857732 BigNumber { value: "95628828301060254089480" } 1279 17:28:20 52244518.857732 BigNumber { value: "95628828301060254089480" } 1278 17:28:25 52244518.857732 BigNumber { value: "95628828301060254089480" } 1278 17:28:37 .....     52244518.857732 BigNumber { value: "95628828301060254089480" } 1284 17:55:40  52244518.857732 BigNumber { value: "95628828301060254089480" } 1284 17:55:50  52244518.857732 BigNumber { value: "95628828301060254089480" } 1284 17:56:2 

My function doesn’t renew balances. But if I stop and launch it again I receive the new data:

54258304.239956 BigNumber { value: "94292726075356721409097" } 1285 17:58:3 

May be I don’t understand totally how this balanceOf() function works or smth wrong with variables so it gives me the same value every block.

IERC20 balanceOf() function doesn’t renew data with every new block

Shopping cart
There are no products in the cart!
Continue shopping
0