chart-based route finding
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user