获取Uniswap所有池子地址
使用Uniswap工厂合约0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f
,接口IUniswapV2Factory
,来查询池子地址。
interface IUniswapV2Factory {
function getPair(address tokenA, address tokenB) external view returns (address pair);
function allPairs(uint) external view returns (address pair);
function allPairsLength() external view returns (uint);
}
用Hardhat fork 区块号20710591,编写JavaScript代码调用上述接口。
const { ethers } = require("hardhat");
const factoryAddress = "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f"; //Uniswap工厂合约地址
//interface IUniswapV2Factory , 在ABI中列出用到的方法即可
const factoryABI =["function getPair(address tokenA, address tokenB) external view returns (address pair)",
"function allPairs(uint) external view returns (address pair)",
"function allPairsLength() external view returns (uint)"
];
async function main() {
const factoryContract = new ethers.Contract(factoryAddress, factoryABI, ethers.provider);
const allPairsLength = await factoryContract.allPairsLength();
console.log("当前Uniswap V2共有%s个池子", allPairsLength);
// 遍历获取前10个池子的地址
let pairs = [];
for (let i = 0; i < 10; i++) {
let pairAddress = await factoryContract.allPairs(i);
pairs.push(pairAddress);
console.log(pairAddress);
}
console.log(pairs.length);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
运行结果
npx hardhat run .\ignition\modules\uniswapv2.js --network hardhat
当前Uniswap V2共有364750n个池子
0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc
0x3139Ffc91B99aa94DA8A2dc13f1fC36F9BDc98eE
0x12EDE161c702D1494612d19f05992f43aa6A26FB
0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11
0x07F068ca326a469Fc1d87d85d448990C8cBa7dF9
0xAE461cA67B15dc8dc81CE7615e0320dA1A9aB8D5
0xCe407CD7b95B39d3B4d53065E711e713dd5C5999
0x33C2d48Bc95FB7D0199C5C693e7a9F527145a9Af
0xB6909B960DbbE7392D405429eB2b3649752b4838
0x30EB5E15476E6a80F4F3cd8479749b4881DAB1b8
364750个流动性池,两两组合,大约有800种Token