From 0e443b1da6f9edd30a80cc8da5413ac6187100e6 Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Wed, 11 Dec 2024 12:44:08 +0900 Subject: [PATCH] remove: compute_routes --- router/comptue_routes.gno | 227 -------------------------------------- 1 file changed, 227 deletions(-) delete mode 100644 router/comptue_routes.gno diff --git a/router/comptue_routes.gno b/router/comptue_routes.gno deleted file mode 100644 index f73b2944..00000000 --- a/router/comptue_routes.gno +++ /dev/null @@ -1,227 +0,0 @@ -package router - -import ( - "sort" - - pl "gno.land/r/gnoswap/v1/pool" - u256 "gno.land/p/gnoswap/uint256" - - "gno.land/p/demo/ufmt" -) - -// PoolWithMeta is a struct that contains poolPath, token0Path, token1Path, fee, tokenPair, and liquidity -// It's used to store the pool information and sort the pools by liquidity -type PoolWithMeta struct { - poolPath string - token0Path string - token1Path string - fee int - tokenPair string - liquidity *u256.Uint -} - -func (pm PoolWithMeta) hasToken(token string) bool { - return pm.token0Path == token || pm.token1Path == token -} - -type ByLiquidity []PoolWithMeta - -func (p ByLiquidity) Len() int { return len(p) } -func (p ByLiquidity) Swap(i, j int) { p[i], p[j] = p[j], p[i] } -func (p ByLiquidity) Less(i, j int) bool { return p[i].liquidity.Gt(p[j].liquidity) } - -// BuildRoute is a struct that contains route, tokenIn, and tokenOut -// It's used to store the route information -type BuildRoute struct { - route []PoolWithMeta - tokenIn string - tokenOut string -} - -func computeAllRoutes( - inputTokenPath string, - outputTokenPath string, - maxHops int, - pools []PoolWithMeta, -) []BuildRoute { - - routes := _computeAllRoutes( - inputTokenPath, - outputTokenPath, - []BuildRoute{}, - pools, - maxHops, - ) - - return routes -} - -func _computeAllRoutes( - inputTokenPath string, - outputTokenPath string, - buildRoute []BuildRoute, // BuildRoute - pools []PoolWithMeta, - maxHops int, -) []BuildRoute { - poolUsed := make([]bool, len(pools)) - - routes := []BuildRoute{} - - tokenVisited := make(map[string]bool, 0) - tokenVisited[inputTokenPath] = true - - computeRoutes( - inputTokenPath, - outputTokenPath, - []PoolWithMeta{}, // currentRoute - poolUsed, - tokenVisited, - "", // _previousTokenOut - maxHops, - pools, - &routes, - ) - - return routes -} - -// TOOD: overcomplicated parameters -func computeRoutes( - inputTokenPath string, - outputTokenPath string, - currentRoute []PoolWithMeta, - poolsUsed []bool, - tokenVisited map[string]bool, - _previousTokenOut string, - maxHops int, - pools []PoolWithMeta, - routes *[]BuildRoute, -) *[]BuildRoute { - - routeLen := len(currentRoute) - - if routeLen > maxHops { - return routes - } - - if (routeLen > 0) && (currentRoute[routeLen-1].hasToken(outputTokenPath)) { - buildRoute := BuildRoute{} - buildRoute.route = append([]PoolWithMeta{}, currentRoute...) - buildRoute.tokenIn = inputTokenPath - buildRoute.tokenOut = outputTokenPath - *routes = append(*routes, buildRoute) - return routes - } - - for i, pool := range pools { - if poolsUsed[i] { - continue - } - - curPool := pool - - var previousTokenOut string - if _previousTokenOut == "" { // first iteration - previousTokenOut = inputTokenPath - } else { - previousTokenOut = _previousTokenOut - } - - if !curPool.hasToken(previousTokenOut) { - continue - } - - var currentTokenOut string - if curPool.token0Path == previousTokenOut { - currentTokenOut = curPool.token1Path - } else { - currentTokenOut = curPool.token0Path - } - - if tokenVisited[currentTokenOut] { - continue - } - - tokenVisited[currentTokenOut] = true - currentRoute = append(currentRoute, curPool) - poolsUsed[i] = true - - computeRoutes( - inputTokenPath, - outputTokenPath, - currentRoute, - poolsUsed, - tokenVisited, - currentTokenOut, - // - maxHops, - pools, - // - routes, - ) - - poolsUsed[i] = false - currentRoute = currentRoute[:len(currentRoute)-1] - - delete(tokenVisited, currentTokenOut) - } - - return routes -} - -func findCandidatePools() []PoolWithMeta { - poolList := pl.PoolGetPoolList() - - poolWithMetas := []PoolWithMeta{} - for _, poolPath := range poolList { - token0Path, token1Path, pFee := poolPathWithFeeDivide(poolPath) - - pool := pl.GetPoolFromPoolPath(poolPath) - liquidity := pool.PoolGetLiquidity() - poolWithMetas = append(poolWithMetas, PoolWithMeta{ - poolPath, - token0Path, - token1Path, - pFee, - ufmt.Sprintf("%s:%s", token0Path, token1Path), - liquidity, - }) - } - - groupedPools := groupPoolsByTokenPair(poolWithMetas) - top2ByGroup := selectTop2ByGroup(groupedPools) - - candidatePools := []PoolWithMeta{} - for _, pools := range top2ByGroup { - candidatePools = append(candidatePools, pools...) - } - - return candidatePools -} - -// group pools by tokenPair -func groupPoolsByTokenPair(pools []PoolWithMeta) map[string][]PoolWithMeta { - groupedPools := make(map[string][]PoolWithMeta) - - for _, pool := range pools { - groupedPools[pool.tokenPair] = append(groupedPools[pool.tokenPair], pool) - } - - return groupedPools -} - -// select the top 2 liquidity values per each group -func selectTop2ByGroup(groupedPools map[string][]PoolWithMeta) map[string][]PoolWithMeta { - top2ByGroup := make(map[string][]PoolWithMeta) - - for tokenPair, pools := range groupedPools { - // Use sort.Sort with ByLiquidity interface - sort.Sort(ByLiquidity(pools)) - - // Select the top 2 liquidity values - top2 := pools[:min(2, len(pools))] - top2ByGroup[tokenPair] = top2 - } - - return top2ByGroup -}