I'm using the Uniswap SDK to fetch the price of ETH in the DAI/ETH pool from Uniswap using Hardhat's mainnet fork, except the query is returning the current price of ETH, instead of from the pegged block which is from a few months ago.
How do I fetch Uniswap pool information from Hardhat's mainnet fork? If it's not possible using the Uniswap SDK, is there any workarounds?
Thanks in advance!
After some searching I don't think the Uniswap SDK can be connected to a local mainnet fork. However, a good workaround is to call methods directly from the Uniswap contract.
For example, this would fetch the correct price of ETH from a pegged block:
const UNISWAPV2_ROUTER02_ADDRESS = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D";
const UNISWAPV2_ROUTER02_ABI = [{ "inputs": [{ "internalType": "uint256", "name": "amountIn", "type": "uint256" }, { "internalType": "address[]", "name": "path", "type": "address[]" }], "name": "getAmountsOut", "outputs": [{ "internalType": "uint256[]", "name": "amounts", "type": "uint256[]" }], "stateMutability": "view", "type": "function" }]
const DAI_ADDRESS = "0x6b175474e89094c44da98b954eedeac495271d0f";
const WETH_ADDRESS = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
const uniswap = new ethers.Contract(
UNISWAPV2_ROUTER02_ADDRESS,
UNISWAPV2_ROUTER02_ABI,
ethers.provider,
);
let amountEthFromContract = await uniswap.getAmountsOut(
1, // 1 ETH
[WETH_ADDRESS, DAI_ADDRESS]
)
console.log(`1 Eth = ${amountEthFromContract[1].toString()} USD`)