31 lines
1.2 KiB
JavaScript
31 lines
1.2 KiB
JavaScript
import {queryHelperContract} from "@/blockchain/contract.js";
|
|
import {Exchange} from "@/blockchain/orderlib.js";
|
|
import {useStore} from "@/store/store.js";
|
|
|
|
|
|
export async function findRoute(tokenA, tokenB) {
|
|
const helper = await queryHelperContract()
|
|
if (!helper)
|
|
throw Error('no helper')
|
|
const chainId = useStore().chainId
|
|
const rawRoutes = await helper.getRoutes(tokenA.address, tokenB.address)
|
|
// todo expose all available pools
|
|
console.log('raw routes', rawRoutes)
|
|
let result = null // we actually only find a single pool for now
|
|
for (let [exchange, fee, pool] of rawRoutes) {
|
|
exchange = Number(exchange)
|
|
fee = Number(fee)
|
|
if (!result || result.fee > fee) {
|
|
switch (exchange) {
|
|
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}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return result ? [result] : []
|
|
}
|