route price lookup fixes
This commit is contained in:
@@ -2,7 +2,7 @@ import {socket} from "@/socket.js";
|
||||
import {useStore} from "@/store/store.js";
|
||||
import {Exchange} from "@/blockchain/orderlib.js";
|
||||
import {uniswapV3PoolAddress} from "@/blockchain/uniswap.js";
|
||||
import {ethers} from "ethers";
|
||||
import {ethers, FixedNumber} from "ethers";
|
||||
import {uniswapV3PoolAbi} from "@/blockchain/abi.js";
|
||||
|
||||
const subscriptionCounts = {} // key is route and value is a subscription counter
|
||||
@@ -82,8 +82,12 @@ async function getPriceForRoute(route) {
|
||||
const pool = new ethers.Contract(addr, uniswapV3PoolAbi, provider)
|
||||
const got = await pool.slot0()
|
||||
const [sqrtPrice,,,,,,] = got
|
||||
const spn = Number(sqrtPrice)
|
||||
const price = spn*spn/2**(96*2) * 10**(route.token0.decimals-route.token1.decimals)
|
||||
const spn = BigInt(sqrtPrice)
|
||||
let price = spn*spn * 10n**BigInt(route.token0.decimals-route.token1.decimals)
|
||||
const format = {decimals:38,width:512,signed:false}; // 38 decimals is 127 bits
|
||||
price = FixedNumber.fromValue(price,0,format)
|
||||
price = price.div(FixedNumber.fromValue(2n**(96n*2n),0,format))
|
||||
price = price.round(18).toString()
|
||||
console.log(`price for ${route.token0.symbol}/${route.token1.symbol}`,price)
|
||||
store.poolPrices[addr] = price
|
||||
return price
|
||||
|
||||
Reference in New Issue
Block a user