forked from YashP16/smart-order-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.ts
84 lines (75 loc) · 2.44 KB
/
routes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { Protocol } from '@uniswap/router-sdk';
import { Percent } from '@uniswap/sdk-core';
import { Pair } from '@uniswap/v2-sdk';
import { Pool } from '@uniswap/v3-sdk';
import _ from 'lodash';
import { CurrencyAmount } from '.';
import { RouteWithValidQuote } from '../routers/alpha-router';
import { MixedRoute, V2Route, V3Route } from '../routers/router';
export const routeToString = (
route: V3Route | V2Route | MixedRoute
): string => {
const routeStr = [];
const tokens =
route.protocol === Protocol.V3
? route.tokenPath
: // MixedRoute and V2Route have path
route.path;
const tokenPath = _.map(tokens, (token) => `${token.symbol}`);
const pools =
route.protocol === Protocol.V3 || route.protocol === Protocol.MIXED
? route.pools
: route.pairs;
const poolFeePath = _.map(pools, (pool) => {
return `${
pool instanceof Pool
? ` -- ${pool.fee / 10000}% [${Pool.getAddress(
pool.token0,
pool.token1,
pool.fee
)}]`
: ` -- [${Pair.getAddress(
(pool as Pair).token0,
(pool as Pair).token1
)}]`
} --> `;
});
for (let i = 0; i < tokenPath.length; i++) {
routeStr.push(tokenPath[i]);
if (i < poolFeePath.length) {
routeStr.push(poolFeePath[i]);
}
}
return routeStr.join('');
};
export const routeAmountsToString = (
routeAmounts: RouteWithValidQuote[]
): string => {
const total = _.reduce(
routeAmounts,
(total: CurrencyAmount, cur: RouteWithValidQuote) => {
return total.add(cur.amount);
},
CurrencyAmount.fromRawAmount(routeAmounts[0]!.amount.currency, 0)
);
const routeStrings = _.map(routeAmounts, ({ protocol, route, amount }) => {
const portion = amount.divide(total);
const percent = new Percent(portion.numerator, portion.denominator);
/// @dev special case for MIXED routes we want to show user friendly V2+V3 instead
return `[${
protocol == Protocol.MIXED ? 'V2 + V3' : protocol
}] ${percent.toFixed(2)}% = ${routeToString(route)}`;
});
return _.join(routeStrings, ', ');
};
export const routeAmountToString = (
routeAmount: RouteWithValidQuote
): string => {
const { route, amount } = routeAmount;
return `${amount.toExact()} = ${routeToString(route)}`;
};
export const poolToString = (p: Pool | Pair): string => {
return `${p.token0.symbol}/${p.token1.symbol}${
p instanceof Pool ? `/${p.fee / 10000}%` : ``
}`;
};