I am attempting to get the current prices for some tokens from Uniswap and SushiSwap.
I am using this function:
async getBuyPrice() {
const pathToBuy = await this.routerContract.methods.getAmountsIn(
this.swapAmount,
[ this.swapTo, this.swapFrom ],
).call();
return Math.floor(ethers.utils.parseUnits(pathToBuy[0], this.decimal) * 100) / 100
}
For example for USDT I am passing in a swap amount of '1000000000000000000' to equal one Eth a decimal of 6 to equal what the USDT contract says and then the contract addresses.
For example:
swapAmount : '1000000000000000000',
swapTo:'0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', // weth address
swapFrom: '0xdac17f958d2ee523a2206206994597c13d831ec7', // USDT address
decimal: 6
This should give me how much 1 WETH is in USDT
This should be something like 1673 USDT
however I get a really long number with no decimal place WETH / USDT 1678898786000000
1678898786000000 / 1e12
is what you want. Why 12? Because WETH has 18 decimals and USDT have 6, and 18 - 6 = 12.