Ether.js and Uniswap V3
This is Thanos Polychronakis. He is very smart and compact developer. He is working on uniswap v3 and developed ether.js query module for uniswap.
Firstly we install this module in terminal
npm install @thanpolas/uniswap-chain-queries — save
Secondly we add this folder to the workspace.
Start.
getToken(tokenAddress, provider)
We create new file in this folder and we paste the following code.
const { ethers } = require(‘ethers’);
const { getToken } = require(‘@thanpolas/uniswap-chain-queries’);
async function run() {
// Define the DAI address
const erc20Address = ‘0x6B175474E89094C44Da98b954EedeAC495271d0F’;
const provider = ethers.getDefaultProvider();
const tokenData = await getToken(erc20Address, provider);
console.log(tokenData);
}
run();
Result:
getLPTokensDataByLpAddress(lpContract, provider)
const { ethers } = require(‘ethers’);
const {
getLPTokensDataByLpAddress,
} = require(‘@thanpolas/uniswap-chain-queries’);
async function run() {
const provider = ethers.getDefaultProvider();
// LP of DAI/WETH
const lpAddress = ‘0xa478c2975ab1ea89e8196811f51a7b7ade33eb11’;
const tokensData = await getLPTokensDataByLpAddress(lpAddress, provider);
console.log(tokensData);
}
run();
Result:
getPriceUniswapV3(lpAddress, provider, optTokenDecimals)
const { ethers } = require(‘ethers’);
const { getPriceUniswapV3 } = require(‘@thanpolas/uniswap-chain-queries’);
async function run() {
// Uni V3 LP of DAI/WETH at 0.3% Fee
const lpAddress = ‘0xc2e9f25be6257c210d7adf0d4cd6e3e881ba25f8’;
const provider = ethers.getDefaultProvider();
const tokenPairPrice = await getPriceUniswapV3(lpAddress, provider, [18, 18]);
console.log(tokenPairPrice);
}
run();
Result: