I am having difficulty understanding getting the price within the contract. So I created 2 different methods (one using SDK and other one using ethers.js)
- When I try the SDK, I get
3839.62
- When I try with ethers.js, I get
55561073443426097
so it's not just decimal difference :/
let chainId = ChainId.MAINNET;
const dai = await Fetcher.fetchTokenData(chainId, '0x6B175474E89094C44Da98b954EedeAC495271d0F')
const weth = WETH[chainId]
const pair = await Fetcher.fetchPairData(dai, weth);
const route = new Route([pair], weth)
const trade = new Trade(route, new TokenAmount(weth, '100000000000000000'), TradeType.EXACT_INPUT);
console.log(trade.executionPrice.toSignificant(6)); // 3839.62
const provider = new ethers.providers.getDefaultProvider();
const weth = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'
const dai = '0x6B175474E89094C44Da98b954EedeAC495271d0F'
let uniswapRouter = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D';
const routerABI = [
'function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB)',
'function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut)'
]
let factoryContract = new ethers.Contract(uniswapRouter, routerABI, provider);
let price = await factoryContract.getAmountOut('100000000000000000', weth, dai)
console.log(price.toString()); // 55561073443426097
By integrating the contract, how can I get the price? Is there a method in contracts like: getPrice(tokenAAmount, tokenA, tokenB) returns (tokenBAmount)
Update: I can get the midprice using
let pairAddress = '';
const pairABI = [
'function getReserves() public view returns (uint112 _reserve0, uint112 _reserve1, uint32 _blockTimestampLast)'
]
let pairContract = new ethers.Contract(pairAddress, pairABI, provider);
let reserves = await pairContract.getReserves();
let price = Number(reserves._reserve0) / Number(reserves._reserve1)
console.log(price.toPrecision(6)); // mid price