From 7293c101f8ebbe4c163553eb54a3e25c6f5ca7ca Mon Sep 17 00:00:00 2001 From: Tim Olson <> Date: Sat, 18 Nov 2023 15:57:50 -0400 Subject: [PATCH] route price lookup fixes --- src/blockchain/contract.js | 1 + src/blockchain/prices.js | 10 +++++++--- src/blockchain/wallet.js | 13 +++++++++---- src/components/RoutePrice.vue | 5 ++--- src/socket.js | 2 +- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/blockchain/contract.js b/src/blockchain/contract.js index 083f514..1e2aeab 100644 --- a/src/blockchain/contract.js +++ b/src/blockchain/contract.js @@ -4,6 +4,7 @@ import {useStore} from "@/store/store.js"; export function vaultAddress( owner, num=0) { + console.log('va', owner, num) if( !owner ) return null const s = useStore() diff --git a/src/blockchain/prices.js b/src/blockchain/prices.js index 316368f..e1fd53c 100644 --- a/src/blockchain/prices.js +++ b/src/blockchain/prices.js @@ -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 diff --git a/src/blockchain/wallet.js b/src/blockchain/wallet.js index 648f154..b1c3f1e 100644 --- a/src/blockchain/wallet.js +++ b/src/blockchain/wallet.js @@ -109,7 +109,7 @@ function discoverVaults() { if( result.length ) flushOrders(result[0]) else - ensureVault() + ensureVault2(s.chainId, owner, 0) } }) } @@ -136,15 +136,20 @@ async function _discoverVaults(owner) { export function ensureVault() { const s = useStore() - if( !s.chain ) { + ensureVault2(s.chainId, s.account, 0) +} + + +export function ensureVault2(chainId, owner, num) { + if( !chainId ) { console.log('cannot create vault: no chain selected') return } - if( !s.account ) { + if( !owner ) { console.log('cannot create vault: no account logged in') return } - socket.emit('ensureVault', s.chainId, s.account, 0) + socket.emit('ensureVault', chainId, owner, num) } diff --git a/src/components/RoutePrice.vue b/src/components/RoutePrice.vue index 4f3d430..642f123 100644 --- a/src/components/RoutePrice.vue +++ b/src/components/RoutePrice.vue @@ -21,11 +21,10 @@ const price = computed(()=>{ if( !route || !(route.pool in s.poolPrices) ) return '' let p = s.poolPrices[route.pool] + console.log('pool price is',typeof p, p) if( !p ) return '' - p = Number(p) // TODO: Temporary workaround. Better to fix reason for p being set to string. - console.log('pool price is',typeof p, p) - p = FixedNumber.fromString(p) + p = FixedNumber.fromString(p).toUnsafeFloat() if( props.inverted ) p = 1/p return p.toPrecision(props.precision) diff --git a/src/socket.js b/src/socket.js index 03fe0f7..f04e495 100644 --- a/src/socket.js +++ b/src/socket.js @@ -31,7 +31,7 @@ socket.on('p', async (chainId, pool, price) => { return const prices = {} prices[pool] = price - console.log('pool price from message', pool, price) + console.log('pool price from message', pool, typeof price, price) const poolPrices = s.poolPrices poolPrices[pool] = price s.poolPrices = poolPrices