如何从 Hardhat 主网分叉获取 Uniswap 数据?

发布于 2025-01-11 23:46:00 字数 200 浏览 3 评论 0原文

我正在使用 Uniswap SDK 使用 Hardhat 的主网分叉从 Uniswap 获取 DAI/ETH 池中 ETH 的价格,但查询返回 ETH 的当前价格,而不是从挂钩区块中获取这是几个月前的事。

如何从 Hardhat 的主网分支获取 Uniswap 池信息?如果无法使用 Uniswap SDK,是否有任何解决方法?

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神妖 2025-01-18 23:46:00

经过一番搜索,我认为 Uniswap SDK 无法连接到本地主网分支。然而,一个好的解决方法是直接从 Uniswap 合约调用方法。

例如,这将从挂钩区块中获取 ETH 的正确价格:

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`)

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`)
把人绕傻吧 2025-01-18 23:46:00

https://github.com/defi- Wonderland/solidity-boilerplate/blob/main/test/utils/uniswap.ts

代码在这里

import UniswapV2FactoryContract from '@uniswap/v2-core/build/UniswapV2Factory.json';
import UniswapV2Router02Contract from '@uniswap/v2-periphery/build/UniswapV2Router02.json';
import IUniswapV2Pair from '@uniswap/v2-core/build/IUniswapV2Pair.json';
import WETHContract from '@uniswap/v2-periphery/build/WETH9.json';
import { deployContract } from 'ethereum-waffle';

uniswapV2Factory = await deployContract(owner, UniswapV2FactoryContract, [await owner.getAddress()]);
uniswapV2Router02 = await deployContract(owner, UniswapV2Router02Contract, [uniswapV2Factory.address, WETH.address], { gasLimit: 9500000 });

https://github.com/defi-wonderland/solidity-boilerplate/blob/main/test/utils/uniswap.ts

code here

import UniswapV2FactoryContract from '@uniswap/v2-core/build/UniswapV2Factory.json';
import UniswapV2Router02Contract from '@uniswap/v2-periphery/build/UniswapV2Router02.json';
import IUniswapV2Pair from '@uniswap/v2-core/build/IUniswapV2Pair.json';
import WETHContract from '@uniswap/v2-periphery/build/WETH9.json';
import { deployContract } from 'ethereum-waffle';

uniswapV2Factory = await deployContract(owner, UniswapV2FactoryContract, [await owner.getAddress()]);
uniswapV2Router02 = await deployContract(owner, UniswapV2Router02Contract, [uniswapV2Factory.address, WETH.address], { gasLimit: 9500000 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文