chart-based route finding

This commit is contained in:
Tim
2024-02-27 22:20:23 -04:00
parent fc126955b6
commit 9a5d7a1ce5
12 changed files with 126 additions and 46 deletions

View File

@@ -1,9 +1,12 @@
import {Exchange} from "@/blockchain/orderlib.js";
import {useOrderStore, useStore} from "@/store/store.js";
import {queryHelperContract} from "@/blockchain/contract.js";
import {SingletonCoroutine} from "@/misc.js";
export async function findRoute(helper, chainId, tokenA, tokenB) {
console.log('getting raw routes', helper, tokenA.address, tokenB.address)
const rawRoutes = await helper.getRoutes(tokenA.address, tokenB.address)
console.log('getting raw routes', helper, tokenA.a, tokenB.a)
const rawRoutes = await helper.getRoutes(tokenA.a, tokenB.a)
// todo expose all available pools
console.log('raw routes', rawRoutes)
let result = null // we actually only find a single pool for now
@@ -15,11 +18,41 @@ export async function findRoute(helper, chainId, tokenA, tokenB) {
case 0: // UniswapV2
throw Error('Uniswap V2 not yet supported')
case 1: // UniswapV3
const [token0, token1] = tokenA.address < tokenB.address ? [tokenA, tokenB] : [tokenB, tokenA]
result = {chainId, exchange: Exchange.UniswapV3, pool, fee, token0, token1}
const inverted = tokenA.a < tokenB.a;
const [token0, token1] = inverted ? [tokenA, tokenB] : [tokenB, tokenA]
result = {chainId, exchange: Exchange.UniswapV3, pool, fee, token0, token1, inverted}
break
}
}
}
return result ? [result] : []
}
async function componentFindRoute() {
const s = useStore()
const os = useOrderStore()
const tokenA = os.tokenA
const tokenB = os.tokenB
os.routes = []
if (!tokenA || !tokenB)
return
console.log('finding route', s.chainId.value, tokenA, tokenB)
os.routesPending = true
try {
console.log('getting query helper')
const helper = await queryHelperContract(s.helper, s.provider)
if (!helper) {
console.log('no helper')
} else {
const result = await findRoute(helper, s.chainId.value, tokenA, tokenB)
console.log('found route', result)
os.routes = result
}
} catch (e) {
console.log('ignoring routes exception', e)
} finally {
os.routesPending = false
}
}
export const routeFinder = new SingletonCoroutine(componentFindRoute, 10)

View File

@@ -131,7 +131,10 @@ async function _discoverVaults(owner) {
console.error(`bad vault version ${version}`)
}
catch (e) {
console.log(`no vault ${num} at ${addr}`, e)
if( e.value==='0x' && e.code==='BAD_DATA' )
console.log(`no vault ${num} at ${addr}`)
else
console.error(`routeFinder failed`, e)
}
if( s.account == owner ) { // double-check the account since it could have changed during our await
s.vaults = result