@alcorexchange/alcor-swap-sdk 1.0.393 → 1.0.394

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  import { Token, Pool, Route } from '../entities';
2
2
  export declare function computeAllRoutes(tokenIn: Token, tokenOut: Token, pools: Pool[], maxHops: number): Route<Token, Token>[];
3
+ export declare function computeAllRoutesOld(tokenIn: Token, tokenOut: Token, pools: Pool[], maxHops: number): Route<Token, Token>[];
3
4
  export declare function computeAllRoutesFromMap(tokenIn: Token, tokenOut: Token, poolMap: {
4
5
  [tokenId: string]: Pool[];
5
6
  }, maxHops: number): Route<Token, Token>[];
@@ -5,8 +5,67 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.computeAllRoutes = computeAllRoutes;
7
7
  exports.computeAllRoutesFromMap = computeAllRoutesFromMap;
8
+ exports.computeAllRoutesOld = computeAllRoutesOld;
8
9
  var _entities = require("../entities");
9
10
  function computeAllRoutes(tokenIn, tokenOut, pools, maxHops) {
11
+ // Создаем карту пулов для каждого токена
12
+ const poolMap = new Map();
13
+ pools.forEach((pool, index) => {
14
+ const tokenA = pool.tokenA.id;
15
+ const tokenB = pool.tokenB.id;
16
+ if (!poolMap.has(tokenA)) poolMap.set(tokenA, []);
17
+ if (!poolMap.has(tokenB)) poolMap.set(tokenB, []);
18
+ poolMap.get(tokenA).push(pool);
19
+ poolMap.get(tokenB).push(pool);
20
+ });
21
+
22
+ // Инициализируем стек для итеративного обхода
23
+ const routes = [];
24
+ const stack = [{
25
+ currentToken: tokenIn,
26
+ currentRoute: [],
27
+ usedPools: new Set()
28
+ }];
29
+
30
+ // Основной цикл обработки маршрутов
31
+ while (stack.length > 0) {
32
+ const {
33
+ currentToken,
34
+ currentRoute,
35
+ usedPools
36
+ } = stack.pop();
37
+
38
+ // Ранняя остановка: превышен лимит хопов
39
+ if (currentRoute.length > maxHops) continue;
40
+
41
+ // Если маршрут завершен (достигнут tokenOut), добавляем его в результат
42
+ if (currentRoute.length > 0 && currentRoute[currentRoute.length - 1].involvesToken(tokenOut)) {
43
+ routes.push(new _entities.Route([...currentRoute], tokenIn, tokenOut));
44
+ continue;
45
+ }
46
+
47
+ // Получаем доступные пулы для текущего токена
48
+ const availablePools = poolMap.get(currentToken.id) || [];
49
+ for (const pool of availablePools) {
50
+ const poolIndex = pools.indexOf(pool);
51
+ if (usedPools.has(poolIndex)) continue; // Пропускаем уже использованные пулы
52
+
53
+ // Определяем следующий токен
54
+ const nextToken = pool.tokenA.equals(currentToken) ? pool.tokenB : pool.tokenA;
55
+
56
+ // Создаем новый маршрут и добавляем его в стек
57
+ const newRoute = [...currentRoute, pool];
58
+ const newUsedPools = new Set(usedPools).add(poolIndex);
59
+ stack.push({
60
+ currentToken: nextToken,
61
+ currentRoute: newRoute,
62
+ usedPools: newUsedPools
63
+ });
64
+ }
65
+ }
66
+ return routes;
67
+ }
68
+ function computeAllRoutesOld(tokenIn, tokenOut, pools, maxHops) {
10
69
  const poolsUsed = Array(pools.length).fill(false);
11
70
  const routes = [];
12
71
  const computeRoutes = (tokenIn, tokenOut, currentRoute, poolsUsed, _previousTokenOut) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alcorexchange/alcor-swap-sdk",
3
- "version": "1.0.393",
3
+ "version": "1.0.394",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
@@ -1,10 +1,79 @@
1
1
  import { Token, Pool, Route } from '../entities';
2
2
 
3
+ interface RouteNode {
4
+ currentToken: Token;
5
+ currentRoute: Pool[];
6
+ usedPools: Set<number>;
7
+ }
8
+
3
9
  export function computeAllRoutes(
4
10
  tokenIn: Token,
5
11
  tokenOut: Token,
6
12
  pools: Pool[],
7
13
  maxHops: number
14
+ ): Route<Token, Token>[] {
15
+ // Создаем карту пулов для каждого токена
16
+ const poolMap: Map<string, Pool[]> = new Map();
17
+ pools.forEach((pool, index) => {
18
+ const tokenA = pool.tokenA.id;
19
+ const tokenB = pool.tokenB.id;
20
+ if (!poolMap.has(tokenA)) poolMap.set(tokenA, []);
21
+ if (!poolMap.has(tokenB)) poolMap.set(tokenB, []);
22
+ poolMap.get(tokenA)!.push(pool);
23
+ poolMap.get(tokenB)!.push(pool);
24
+ });
25
+
26
+ // Инициализируем стек для итеративного обхода
27
+ const routes: Route<Token, Token>[] = [];
28
+ const stack: RouteNode[] = [
29
+ {
30
+ currentToken: tokenIn,
31
+ currentRoute: [],
32
+ usedPools: new Set(),
33
+ },
34
+ ];
35
+
36
+ // Основной цикл обработки маршрутов
37
+ while (stack.length > 0) {
38
+ const { currentToken, currentRoute, usedPools } = stack.pop()!;
39
+
40
+ // Ранняя остановка: превышен лимит хопов
41
+ if (currentRoute.length > maxHops) continue;
42
+
43
+ // Если маршрут завершен (достигнут tokenOut), добавляем его в результат
44
+ if (currentRoute.length > 0 && currentRoute[currentRoute.length - 1].involvesToken(tokenOut)) {
45
+ routes.push(new Route([...currentRoute], tokenIn, tokenOut));
46
+ continue;
47
+ }
48
+
49
+ // Получаем доступные пулы для текущего токена
50
+ const availablePools = poolMap.get(currentToken.id) || [];
51
+ for (const pool of availablePools) {
52
+ const poolIndex = pools.indexOf(pool);
53
+ if (usedPools.has(poolIndex)) continue; // Пропускаем уже использованные пулы
54
+
55
+ // Определяем следующий токен
56
+ const nextToken = pool.tokenA.equals(currentToken) ? pool.tokenB : pool.tokenA;
57
+
58
+ // Создаем новый маршрут и добавляем его в стек
59
+ const newRoute = [...currentRoute, pool];
60
+ const newUsedPools = new Set(usedPools).add(poolIndex);
61
+ stack.push({
62
+ currentToken: nextToken,
63
+ currentRoute: newRoute,
64
+ usedPools: newUsedPools,
65
+ });
66
+ }
67
+ }
68
+
69
+ return routes;
70
+ }
71
+
72
+ export function computeAllRoutesOld(
73
+ tokenIn: Token,
74
+ tokenOut: Token,
75
+ pools: Pool[],
76
+ maxHops: number
8
77
  ): Route<Token , Token>[] {
9
78
  const poolsUsed = Array<boolean>(pools.length).fill(false);
10
79
  const routes: Route<Token, Token>[] = [];