如何从 Hardhat 的主网分叉中获取 Uniswap 数据?
How to get Uniswap data from Hardhat's mainnet fork?
我正在使用 Uniswap SDK 通过 Hardhat 的主网分支从 Uniswap 获取 DAI/ETH 池中的 ETH 价格,除了查询返回 current ETH 的价格,而不是来自几个月前的挂钩块。
如何从 Hardhat 的主网分叉中获取 Uniswap 池信息?如果无法使用 Uniswap SDK,是否有任何解决方法?
提前致谢!
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 });
经过一番搜索,我认为 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`)
我正在使用 Uniswap SDK 通过 Hardhat 的主网分支从 Uniswap 获取 DAI/ETH 池中的 ETH 价格,除了查询返回 current ETH 的价格,而不是来自几个月前的挂钩块。
如何从 Hardhat 的主网分叉中获取 Uniswap 池信息?如果无法使用 Uniswap SDK,是否有任何解决方法?
提前致谢!
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 });
经过一番搜索,我认为 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`)