@marko00/routing-finder-mare 1.0.0
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.
- package/.idea/copilot.data.migration.agent.xml +6 -0
- package/.idea/copilot.data.migration.ask.xml +6 -0
- package/.idea/copilot.data.migration.ask2agent.xml +6 -0
- package/.idea/copilot.data.migration.edit.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/.idea/php.xml +19 -0
- package/.idea/ratex-sdk.iml +8 -0
- package/.idea/vcs.xml +6 -0
- package/LICENSE-MIT +21 -0
- package/README.md +209 -0
- package/contracts/abi/BalancerHelperAbi.ts +1 -0
- package/contracts/abi/CamelotHelperAbi.ts +1 -0
- package/contracts/abi/RateXAbi.ts +482 -0
- package/contracts/abi/SushiSwapHelperAbi.ts +1 -0
- package/contracts/abi/UniswapHelperAbi.ts +1 -0
- package/contracts/abi/UniswapV2HelperAbi.ts +1 -0
- package/contracts/addresses-arbitrum.ts +8 -0
- package/contracts/addresses-mainnet.ts +7 -0
- package/contracts/addresses-polkadot.ts +9 -0
- package/contracts/addresses-sei.ts +9 -0
- package/contracts/rateX/BalancerHelper.ts +13 -0
- package/contracts/rateX/CamelotHelper.ts +13 -0
- package/contracts/rateX/SushiSwapHelper.ts +13 -0
- package/contracts/rateX/UniswapHelper.ts +19 -0
- package/contracts/rateX/UniswapV2Helper.ts +19 -0
- package/dexes/dexIdsList.ts +9 -0
- package/dexes/graph_queries/BalancerV2.ts +150 -0
- package/dexes/graph_queries/CamelotV2.ts +202 -0
- package/dexes/graph_queries/SushiSwapV2.ts +283 -0
- package/dexes/graph_queries/UniswapV2.ts +252 -0
- package/dexes/graph_queries/UniswapV3.ts +229 -0
- package/dexes/graph_queries/graphQueryFilters.ts +41 -0
- package/dexes/graph_queries/x_template.ts +67 -0
- package/dexes/pools/Balancer/BalancerState.ts +34 -0
- package/dexes/pools/Balancer/BalancerWeightedPool.ts +96 -0
- package/dexes/pools/Camelot.ts +164 -0
- package/dexes/pools/SushiSwapV2.ts +35 -0
- package/dexes/pools/UniswapV2.ts +36 -0
- package/dexes/pools/uniswap/UniswapV3.ts +40 -0
- package/dexes/pools/uniswap/testUniswapOffchainQuoter.ts +169 -0
- package/dexes/pools/uniswap/types.ts +174 -0
- package/dexes/pools/uniswap/uniswapOffchainQuoter.ts +173 -0
- package/dexes/pools/uniswap/uniswapState.ts +56 -0
- package/dexes/pools/uniswap/utils.ts +71 -0
- package/dist/contracts/abi/BalancerHelperAbi.d.ts +25 -0
- package/dist/contracts/abi/BalancerHelperAbi.js +4 -0
- package/dist/contracts/abi/CamelotHelperAbi.d.ts +45 -0
- package/dist/contracts/abi/CamelotHelperAbi.js +4 -0
- package/dist/contracts/abi/RateXAbi.d.ts +71 -0
- package/dist/contracts/abi/RateXAbi.js +485 -0
- package/dist/contracts/abi/SushiSwapHelperAbi.d.ts +45 -0
- package/dist/contracts/abi/SushiSwapHelperAbi.js +4 -0
- package/dist/contracts/abi/UniswapHelperAbi.d.ts +39 -0
- package/dist/contracts/abi/UniswapHelperAbi.js +4 -0
- package/dist/contracts/abi/UniswapV2HelperAbi.d.ts +45 -0
- package/dist/contracts/abi/UniswapV2HelperAbi.js +4 -0
- package/dist/contracts/addresses-arbitrum.d.ts +6 -0
- package/dist/contracts/addresses-arbitrum.js +10 -0
- package/dist/contracts/addresses-mainnet.d.ts +6 -0
- package/dist/contracts/addresses-mainnet.js +10 -0
- package/dist/contracts/addresses-polkadot.d.ts +6 -0
- package/dist/contracts/addresses-polkadot.js +10 -0
- package/dist/contracts/addresses-sei.d.ts +6 -0
- package/dist/contracts/addresses-sei.js +10 -0
- package/dist/contracts/rateX/BalancerHelper.d.ts +26 -0
- package/dist/contracts/rateX/BalancerHelper.js +14 -0
- package/dist/contracts/rateX/CamelotHelper.d.ts +46 -0
- package/dist/contracts/rateX/CamelotHelper.js +14 -0
- package/dist/contracts/rateX/SushiSwapHelper.d.ts +46 -0
- package/dist/contracts/rateX/SushiSwapHelper.js +14 -0
- package/dist/contracts/rateX/UniswapHelper.d.ts +40 -0
- package/dist/contracts/rateX/UniswapHelper.js +22 -0
- package/dist/contracts/rateX/UniswapV2Helper.d.ts +46 -0
- package/dist/contracts/rateX/UniswapV2Helper.js +22 -0
- package/dist/dexes/dexIdsList.d.ts +9 -0
- package/dist/dexes/dexIdsList.js +12 -0
- package/dist/dexes/graph_queries/BalancerV2.d.ts +14 -0
- package/dist/dexes/graph_queries/BalancerV2.js +141 -0
- package/dist/dexes/graph_queries/CamelotV2.d.ts +14 -0
- package/dist/dexes/graph_queries/CamelotV2.js +183 -0
- package/dist/dexes/graph_queries/SushiSwapV2.d.ts +14 -0
- package/dist/dexes/graph_queries/SushiSwapV2.js +263 -0
- package/dist/dexes/graph_queries/UniswapV2.d.ts +14 -0
- package/dist/dexes/graph_queries/UniswapV2.js +217 -0
- package/dist/dexes/graph_queries/UniswapV3.d.ts +14 -0
- package/dist/dexes/graph_queries/UniswapV3.js +198 -0
- package/dist/dexes/graph_queries/graphQueryFilters.d.ts +19 -0
- package/dist/dexes/graph_queries/graphQueryFilters.js +40 -0
- package/dist/dexes/graph_queries/x_template.d.ts +12 -0
- package/dist/dexes/graph_queries/x_template.js +57 -0
- package/dist/dexes/pools/Balancer/BalancerState.d.ts +6 -0
- package/dist/dexes/pools/Balancer/BalancerState.js +32 -0
- package/dist/dexes/pools/Balancer/BalancerWeightedPool.d.ts +12 -0
- package/dist/dexes/pools/Balancer/BalancerWeightedPool.js +109 -0
- package/dist/dexes/pools/Camelot.d.ts +12 -0
- package/dist/dexes/pools/Camelot.js +135 -0
- package/dist/dexes/pools/SushiSwapV2.d.ts +9 -0
- package/dist/dexes/pools/SushiSwapV2.js +34 -0
- package/dist/dexes/pools/UniswapV2.d.ts +9 -0
- package/dist/dexes/pools/UniswapV2.js +34 -0
- package/dist/dexes/pools/uniswap/UniswapV3.d.ts +7 -0
- package/dist/dexes/pools/uniswap/UniswapV3.js +36 -0
- package/dist/dexes/pools/uniswap/types.d.ts +76 -0
- package/dist/dexes/pools/uniswap/types.js +111 -0
- package/dist/dexes/pools/uniswap/uniswapOffchainQuoter.d.ts +13 -0
- package/dist/dexes/pools/uniswap/uniswapOffchainQuoter.js +121 -0
- package/dist/dexes/pools/uniswap/uniswapState.d.ts +14 -0
- package/dist/dexes/pools/uniswap/uniswapState.js +51 -0
- package/dist/dexes/pools/uniswap/utils.d.ts +3 -0
- package/dist/dexes/pools/uniswap/utils.js +41 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +41 -0
- package/dist/routes.d.ts +1 -0
- package/dist/routes.js +20 -0
- package/dist/routing/iterative_spliting/main.d.ts +3 -0
- package/dist/routing/iterative_spliting/main.js +104 -0
- package/dist/routing/iterative_spliting/multiHopSwap.d.ts +4 -0
- package/dist/routing/iterative_spliting/multiHopSwap.js +83 -0
- package/dist/routing/main.d.ts +2 -0
- package/dist/routing/main.js +22 -0
- package/dist/routing/uni_like_algo/algo_config.d.ts +2 -0
- package/dist/routing/uni_like_algo/algo_config.js +8 -0
- package/dist/routing/uni_like_algo/amount_distribution.d.ts +2 -0
- package/dist/routing/uni_like_algo/amount_distribution.js +17 -0
- package/dist/routing/uni_like_algo/compute_routes_backtrack.d.ts +3 -0
- package/dist/routing/uni_like_algo/compute_routes_backtrack.js +44 -0
- package/dist/routing/uni_like_algo/main.d.ts +2 -0
- package/dist/routing/uni_like_algo/main.js +49 -0
- package/dist/routing/uni_like_algo/routes_quoter.d.ts +21 -0
- package/dist/routing/uni_like_algo/routes_quoter.js +53 -0
- package/dist/routing/uni_like_algo/swap_finder.d.ts +25 -0
- package/dist/routing/uni_like_algo/swap_finder.js +154 -0
- package/dist/routing/uni_like_algo/types.d.ts +40 -0
- package/dist/routing/uni_like_algo/types.js +12 -0
- package/dist/swap/graph_communication.d.ts +5 -0
- package/dist/swap/graph_communication.js +187 -0
- package/dist/swap/my_local_storage.d.ts +8 -0
- package/dist/swap/my_local_storage.js +16 -0
- package/dist/utils/addresses.d.ts +24 -0
- package/dist/utils/addresses.js +60 -0
- package/dist/utils/math/fixed-points.d.ts +14 -0
- package/dist/utils/math/fixed-points.js +123 -0
- package/dist/utils/math/log-exp.d.ts +5 -0
- package/dist/utils/math/log-exp.js +385 -0
- package/dist/utils/math/math.d.ts +12 -0
- package/dist/utils/math/math.js +50 -0
- package/dist/utils/types/types.d.ts +51 -0
- package/dist/utils/types/types.js +25 -0
- package/dist/utils/utils.d.ts +20 -0
- package/dist/utils/utils.js +72 -0
- package/images/decenter_logo.png +0 -0
- package/index.ts +50 -0
- package/package.json +39 -0
- package/routes.ts +27 -0
- package/routing/iterative_spliting/main.ts +131 -0
- package/routing/iterative_spliting/multiHopSwap.ts +98 -0
- package/routing/main.ts +22 -0
- package/routing/uni_like_algo/algo_config.ts +7 -0
- package/routing/uni_like_algo/amount_distribution.ts +16 -0
- package/routing/uni_like_algo/compute_routes_backtrack.ts +81 -0
- package/routing/uni_like_algo/main.ts +65 -0
- package/routing/uni_like_algo/routes_quoter.ts +63 -0
- package/routing/uni_like_algo/swap_finder.ts +185 -0
- package/routing/uni_like_algo/types.ts +54 -0
- package/swap/graph_communication.ts +212 -0
- package/swap/my_local_storage.ts +27 -0
- package/tsconfig.json +26 -0
- package/utils/addresses.ts +64 -0
- package/utils/math/fixed-points.ts +88 -0
- package/utils/math/log-exp.ts +469 -0
- package/utils/math/math.ts +46 -0
- package/utils/types/types.ts +100 -0
- package/utils/utils.ts +125 -0
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Ported from Solidity:
|
|
3
|
+
// https://github.com/balancer-labs/balancer-core-v2/blob/70843e6a61ad11208c1cfabf5cfe15be216ca8d3/pkg/solidity-utils/contracts/math/LogExpMath.sol
|
|
4
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
6
|
+
};
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.ln = exports.log = exports.exp = exports.pow = void 0;
|
|
9
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
10
|
+
// All fixed point multiplications and divisions are inlined
|
|
11
|
+
// This means we need to divide by ONE when multiplying two numbers, and multiply by ONE when dividing them
|
|
12
|
+
// All arguments and return values are 18 decimal fixed point numbers
|
|
13
|
+
const ONE_18 = new bignumber_js_1.default("1000000000000000000"); // 1e18
|
|
14
|
+
// Internally, intermediate values are computed with higher precision as 20 decimal fixed point numbers, and in the case of ln36, 36 decimals
|
|
15
|
+
const ONE_20 = new bignumber_js_1.default("100000000000000000000"); // 1e20
|
|
16
|
+
const ONE_36 = new bignumber_js_1.default("1000000000000000000000000000000000000"); // 1e36
|
|
17
|
+
// The domain of natural exponentiation is bound by the word size and number of decimals used
|
|
18
|
+
// Because internally the result will be stored using 20 decimals, the largest possible result is
|
|
19
|
+
// (2^255 - 1) / 10^20, which makes the largest exponent ln((2^255 - 1) / 10^20) = 130.700829182905140221
|
|
20
|
+
// The smallest possible result is 10^(-18), which makes largest negative argument
|
|
21
|
+
// ln(10^(-18)) = -41.446531673892822312.
|
|
22
|
+
// We use 130.0 and -41.0 to have some safety margin
|
|
23
|
+
const MAX_NATURAL_EXPONENT = new bignumber_js_1.default("130000000000000000000"); // 130e18
|
|
24
|
+
const MIN_NATURAL_EXPONENT = new bignumber_js_1.default("-41000000000000000000"); // (-41)e18
|
|
25
|
+
// Bounds for ln_36's argument
|
|
26
|
+
// Both ln(0.9) and ln(1.1) can be represented with 36 decimal places in a fixed point 256 bit integer
|
|
27
|
+
const LN_36_LOWER_BOUND = ONE_18.minus(new bignumber_js_1.default("100000000000000000")); // 1e18 - 1e17
|
|
28
|
+
const LN_36_UPPER_BOUND = ONE_18.plus(new bignumber_js_1.default("100000000000000000")); // 1e18 + 1e17
|
|
29
|
+
const MILD_EXPONENT_BOUND = new bignumber_js_1.default(2).pow(254).idiv(ONE_20);
|
|
30
|
+
// 18 decimal constants
|
|
31
|
+
const x0 = new bignumber_js_1.default("128000000000000000000"); // 2ˆ7
|
|
32
|
+
const a0 = new bignumber_js_1.default("38877084059945950922200000000000000000000000000000000000"); // eˆ(x0) (no decimals)
|
|
33
|
+
const x1 = new bignumber_js_1.default("64000000000000000000"); // 2ˆ6
|
|
34
|
+
const a1 = new bignumber_js_1.default("6235149080811616882910000000"); // eˆ(x1) (no decimals)
|
|
35
|
+
// 20 decimal constants
|
|
36
|
+
const x2 = new bignumber_js_1.default("3200000000000000000000"); // 2ˆ5
|
|
37
|
+
const a2 = new bignumber_js_1.default("7896296018268069516100000000000000"); // eˆ(x2)
|
|
38
|
+
const x3 = new bignumber_js_1.default("1600000000000000000000"); // 2ˆ4
|
|
39
|
+
const a3 = new bignumber_js_1.default("888611052050787263676000000"); // eˆ(x3)
|
|
40
|
+
const x4 = new bignumber_js_1.default("800000000000000000000"); // 2ˆ3
|
|
41
|
+
const a4 = new bignumber_js_1.default("298095798704172827474000"); // eˆ(x4)
|
|
42
|
+
const x5 = new bignumber_js_1.default("400000000000000000000"); // 2ˆ2
|
|
43
|
+
const a5 = new bignumber_js_1.default("5459815003314423907810"); // eˆ(x5)
|
|
44
|
+
const x6 = new bignumber_js_1.default("200000000000000000000"); // 2ˆ1
|
|
45
|
+
const a6 = new bignumber_js_1.default("738905609893065022723"); // eˆ(x6)
|
|
46
|
+
const x7 = new bignumber_js_1.default("100000000000000000000"); // 2ˆ0
|
|
47
|
+
const a7 = new bignumber_js_1.default("271828182845904523536"); // eˆ(x7)
|
|
48
|
+
const x8 = new bignumber_js_1.default("50000000000000000000"); // 2ˆ(-1)
|
|
49
|
+
const a8 = new bignumber_js_1.default("164872127070012814685"); // eˆ(x8)
|
|
50
|
+
const x9 = new bignumber_js_1.default("25000000000000000000"); // 2ˆ(-2)
|
|
51
|
+
const a9 = new bignumber_js_1.default("128402541668774148407"); // eˆ(x9)
|
|
52
|
+
const x10 = new bignumber_js_1.default("12500000000000000000"); // 2ˆ(-3)
|
|
53
|
+
const a10 = new bignumber_js_1.default("113314845306682631683"); // eˆ(x10)
|
|
54
|
+
const x11 = new bignumber_js_1.default("6250000000000000000"); // 2ˆ(-4)
|
|
55
|
+
const a11 = new bignumber_js_1.default("106449445891785942956"); // eˆ(x11)
|
|
56
|
+
const pow = (x, y) => {
|
|
57
|
+
if (y.isZero()) {
|
|
58
|
+
// We solve the 0^0 indetermination by making it equal one.
|
|
59
|
+
return ONE_18;
|
|
60
|
+
}
|
|
61
|
+
if (x.isZero()) {
|
|
62
|
+
return new bignumber_js_1.default(0);
|
|
63
|
+
}
|
|
64
|
+
// Instead of computing x^y directly, we instead rely on the properties of logarithms and exponentiation to
|
|
65
|
+
// arrive at that result. In particular, exp(ln(x)) = x, and ln(x^y) = y * ln(x). This means
|
|
66
|
+
// x^y = exp(y * ln(x)).
|
|
67
|
+
// The ln function takes a signed value, so we need to make sure x fits in the signed 256 bit range.
|
|
68
|
+
if (x.gte(new bignumber_js_1.default(2).pow(255))) {
|
|
69
|
+
throw new Error("X_OUT_OF_BOUNDS");
|
|
70
|
+
}
|
|
71
|
+
// We will compute y * ln(x) in a single step. Depending on the value of x, we can either use ln or ln_36. In
|
|
72
|
+
// both cases, we leave the division by ONE_18 (due to fixed point multiplication) to the end.
|
|
73
|
+
// This prevents y * ln(x) from overflowing, and at the same time guarantees y fits in the signed 256 bit range.
|
|
74
|
+
if (y.gte(MILD_EXPONENT_BOUND)) {
|
|
75
|
+
throw new Error("Y_OUT_OF_BOUNDS");
|
|
76
|
+
}
|
|
77
|
+
let logx_times_y;
|
|
78
|
+
if (LN_36_LOWER_BOUND.lt(x) && x.lt(LN_36_UPPER_BOUND)) {
|
|
79
|
+
let ln_36_x = _ln_36(x);
|
|
80
|
+
// ln_36_x has 36 decimal places, so multiplying by y_int256 isn't as straightforward, since we can't just
|
|
81
|
+
// bring y_int256 to 36 decimal places, as it might overflow. Instead, we perform two 18 decimal
|
|
82
|
+
// multiplications and add the results: one with the first 18 decimals of ln_36_x, and one with the
|
|
83
|
+
// (downscaled) last 18 decimals.
|
|
84
|
+
logx_times_y = ln_36_x
|
|
85
|
+
.idiv(ONE_18)
|
|
86
|
+
.times(y)
|
|
87
|
+
.plus(ln_36_x.mod(ONE_18).times(y).idiv(ONE_18));
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
logx_times_y = _ln(x).times(y);
|
|
91
|
+
}
|
|
92
|
+
logx_times_y = logx_times_y.idiv(ONE_18);
|
|
93
|
+
// Finally, we compute exp(y * ln(x)) to arrive at x^y
|
|
94
|
+
if (logx_times_y.lt(MIN_NATURAL_EXPONENT) ||
|
|
95
|
+
logx_times_y.gt(MAX_NATURAL_EXPONENT)) {
|
|
96
|
+
throw new Error("PRODUCT_OUT_OF_BOUNDS");
|
|
97
|
+
}
|
|
98
|
+
return (0, exports.exp)(logx_times_y);
|
|
99
|
+
};
|
|
100
|
+
exports.pow = pow;
|
|
101
|
+
const exp = (x) => {
|
|
102
|
+
if (x.lt(MIN_NATURAL_EXPONENT) || x.gt(MAX_NATURAL_EXPONENT)) {
|
|
103
|
+
throw new Error("INVALID_EXPONENT");
|
|
104
|
+
}
|
|
105
|
+
if (x.lt(0)) {
|
|
106
|
+
// We only handle positive exponents: e^(-x) is computed as 1 / e^x. We can safely make x positive since it
|
|
107
|
+
// fits in the signed 256 bit range (as it is larger than MIN_NATURAL_EXPONENT).
|
|
108
|
+
// Fixed point division requires multiplying by ONE_18.
|
|
109
|
+
return ONE_18.times(ONE_18).idiv((0, exports.exp)(x.negated()));
|
|
110
|
+
}
|
|
111
|
+
// First, we use the fact that e^(x+y) = e^x * e^y to decompose x into a sum of powers of two, which we call x_n,
|
|
112
|
+
// where x_n == 2^(7 - n), and e^x_n = a_n has been precomputed. We choose the first x_n, x0, to equal 2^7
|
|
113
|
+
// because all larger powers are larger than MAX_NATURAL_EXPONENT, and therefore not present in the
|
|
114
|
+
// decomposition.
|
|
115
|
+
// At the end of this process we will have the product of all e^x_n = a_n that apply, and the remainder of this
|
|
116
|
+
// decomposition, which will be lower than the smallest x_n.
|
|
117
|
+
// exp(x) = k_0 * a_0 * k_1 * a_1 * ... + k_n * a_n * exp(remainder), where each k_n equals either 0 or 1.
|
|
118
|
+
// We mutate x by subtracting x_n, making it the remainder of the decomposition.
|
|
119
|
+
// The first two a_n (e^(2^7) and e^(2^6)) are too large if stored as 18 decimal numbers, and could cause
|
|
120
|
+
// intermediate overflows. Instead we store them as plain integers, with 0 decimals.
|
|
121
|
+
// Additionally, x0 + x1 is larger than MAX_NATURAL_EXPONENT, which means they will not both be present in the
|
|
122
|
+
// decomposition.
|
|
123
|
+
// For each x_n, we test if that term is present in the decomposition (if x is larger than it), and if so deduct
|
|
124
|
+
// it and compute the accumulated product.
|
|
125
|
+
let firstAN;
|
|
126
|
+
if (x.gte(x0)) {
|
|
127
|
+
x = x.minus(x0);
|
|
128
|
+
firstAN = a0;
|
|
129
|
+
}
|
|
130
|
+
else if (x.gte(x1)) {
|
|
131
|
+
x = x.minus(x1);
|
|
132
|
+
firstAN = a1;
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
firstAN = new bignumber_js_1.default(1); // One with no decimal places
|
|
136
|
+
}
|
|
137
|
+
// We now transform x into a 20 decimal fixed point number, to have enhanced precision when computing the
|
|
138
|
+
// smaller terms.
|
|
139
|
+
x = x.times(100);
|
|
140
|
+
// `product` is the accumulated product of all a_n (except a0 and a1), which starts at 20 decimal fixed point
|
|
141
|
+
// one. Recall that fixed point multiplication requires dividing by ONE_20.
|
|
142
|
+
let product = ONE_20;
|
|
143
|
+
if (x.gte(x2)) {
|
|
144
|
+
x = x.minus(x2);
|
|
145
|
+
product = product.times(a2).idiv(ONE_20);
|
|
146
|
+
}
|
|
147
|
+
if (x.gte(x3)) {
|
|
148
|
+
x = x.minus(x3);
|
|
149
|
+
product = product.times(a3).idiv(ONE_20);
|
|
150
|
+
}
|
|
151
|
+
if (x.gte(x4)) {
|
|
152
|
+
x = x.minus(x4);
|
|
153
|
+
product = product.times(a4).idiv(ONE_20);
|
|
154
|
+
}
|
|
155
|
+
if (x.gte(x5)) {
|
|
156
|
+
x = x.minus(x5);
|
|
157
|
+
product = product.times(a5).idiv(ONE_20);
|
|
158
|
+
}
|
|
159
|
+
if (x.gte(x6)) {
|
|
160
|
+
x = x.minus(x6);
|
|
161
|
+
product = product.times(a6).idiv(ONE_20);
|
|
162
|
+
}
|
|
163
|
+
if (x.gte(x7)) {
|
|
164
|
+
x = x.minus(x7);
|
|
165
|
+
product = product.times(a7).idiv(ONE_20);
|
|
166
|
+
}
|
|
167
|
+
if (x.gte(x8)) {
|
|
168
|
+
x = x.minus(x8);
|
|
169
|
+
product = product.times(a8).idiv(ONE_20);
|
|
170
|
+
}
|
|
171
|
+
if (x.gte(x9)) {
|
|
172
|
+
x = x.minus(x9);
|
|
173
|
+
product = product.times(a9).idiv(ONE_20);
|
|
174
|
+
}
|
|
175
|
+
// x10 and x11 are unnecessary here since we have high enough precision already.
|
|
176
|
+
// Now we need to compute e^x, where x is small (in particular, it is smaller than x9). We use the Taylor series
|
|
177
|
+
// expansion for e^x: 1 + x + (x^2 / 2!) + (x^3 / 3!) + ... + (x^n / n!).
|
|
178
|
+
let seriesSum = ONE_20; // The initial one in the sum, with 20 decimal places.
|
|
179
|
+
let term; // Each term in the sum, where the nth term is (x^n / n!).
|
|
180
|
+
// The first term is simply x.
|
|
181
|
+
term = x;
|
|
182
|
+
seriesSum = seriesSum.plus(term);
|
|
183
|
+
// Each term (x^n / n!) equals the previous one times x, divided by n. Since x is a fixed point number,
|
|
184
|
+
// multiplying by it requires dividing by ONE_20, but dividing by the non-fixed point n values does not.
|
|
185
|
+
term = term.times(x).idiv(ONE_20).idiv(2);
|
|
186
|
+
seriesSum = seriesSum.plus(term);
|
|
187
|
+
term = term.times(x).idiv(ONE_20).idiv(3);
|
|
188
|
+
seriesSum = seriesSum.plus(term);
|
|
189
|
+
term = term.times(x).idiv(ONE_20).idiv(4);
|
|
190
|
+
seriesSum = seriesSum.plus(term);
|
|
191
|
+
term = term.times(x).idiv(ONE_20).idiv(5);
|
|
192
|
+
seriesSum = seriesSum.plus(term);
|
|
193
|
+
term = term.times(x).idiv(ONE_20).idiv(6);
|
|
194
|
+
seriesSum = seriesSum.plus(term);
|
|
195
|
+
term = term.times(x).idiv(ONE_20).idiv(7);
|
|
196
|
+
seriesSum = seriesSum.plus(term);
|
|
197
|
+
term = term.times(x).idiv(ONE_20).idiv(8);
|
|
198
|
+
seriesSum = seriesSum.plus(term);
|
|
199
|
+
term = term.times(x).idiv(ONE_20).idiv(9);
|
|
200
|
+
seriesSum = seriesSum.plus(term);
|
|
201
|
+
term = term.times(x).idiv(ONE_20).idiv(10);
|
|
202
|
+
seriesSum = seriesSum.plus(term);
|
|
203
|
+
term = term.times(x).idiv(ONE_20).idiv(11);
|
|
204
|
+
seriesSum = seriesSum.plus(term);
|
|
205
|
+
term = term.times(x).idiv(ONE_20).idiv(12);
|
|
206
|
+
seriesSum = seriesSum.plus(term);
|
|
207
|
+
// 12 Taylor terms are sufficient for 18 decimal precision.
|
|
208
|
+
// We now have the first a_n (with no decimals), and the product of all other a_n present, and the Taylor
|
|
209
|
+
// approximation of the exponentiation of the remainder (both with 20 decimals). All that remains is to multiply
|
|
210
|
+
// all three (one 20 decimal fixed point multiplication, dividing by ONE_20, and one integer multiplication),
|
|
211
|
+
// and then drop two digits to return an 18 decimal value.
|
|
212
|
+
return product.times(seriesSum).idiv(ONE_20).times(firstAN).idiv(100);
|
|
213
|
+
};
|
|
214
|
+
exports.exp = exp;
|
|
215
|
+
const log = (arg, base) => {
|
|
216
|
+
// This performs a simple base change: log(arg, base) = ln(arg) / ln(base).
|
|
217
|
+
// Both logBase and logArg are computed as 36 decimal fixed point numbers, either by using ln_36, or by
|
|
218
|
+
// upscaling.
|
|
219
|
+
let logBase;
|
|
220
|
+
if (LN_36_LOWER_BOUND.lt(base) && base.lt(LN_36_UPPER_BOUND)) {
|
|
221
|
+
logBase = _ln_36(base);
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
logBase = _ln(base).times(ONE_18);
|
|
225
|
+
}
|
|
226
|
+
let logArg;
|
|
227
|
+
if (LN_36_LOWER_BOUND.lt(arg) && arg.lt(LN_36_UPPER_BOUND)) {
|
|
228
|
+
logArg = _ln_36(arg);
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
logArg = _ln(arg).times(ONE_18);
|
|
232
|
+
}
|
|
233
|
+
// When dividing, we multiply by ONE_18 to arrive at a result with 18 decimal places
|
|
234
|
+
return logArg.times(ONE_18).idiv(logBase);
|
|
235
|
+
};
|
|
236
|
+
exports.log = log;
|
|
237
|
+
const ln = (a) => {
|
|
238
|
+
// The real natural logarithm is not defined for negative numbers or zero.
|
|
239
|
+
if (a.lte(0)) {
|
|
240
|
+
throw new Error("OUT_OF_BOUNDS");
|
|
241
|
+
}
|
|
242
|
+
if (LN_36_LOWER_BOUND.lt(a) && a.lt(LN_36_UPPER_BOUND)) {
|
|
243
|
+
return _ln_36(a).idiv(ONE_18);
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
return _ln(a);
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
exports.ln = ln;
|
|
250
|
+
const _ln = (a) => {
|
|
251
|
+
if (a.lt(ONE_18)) {
|
|
252
|
+
// Since ln(a^k) = k * ln(a), we can compute ln(a) as ln(a) = ln((1/a)^(-1)) = - ln((1/a))
|
|
253
|
+
// If a is less than one, 1/a will be greater than one, and this if statement will not be entered in the recursive call
|
|
254
|
+
// Fixed point division requires multiplying by ONE_18
|
|
255
|
+
return _ln(ONE_18.times(ONE_18).idiv(a)).negated();
|
|
256
|
+
}
|
|
257
|
+
// First, we use the fact that ln^(a * b) = ln(a) + ln(b) to decompose ln(a) into a sum of powers of two, which
|
|
258
|
+
// we call x_n, where x_n == 2^(7 - n), which are the natural logarithm of precomputed quantities a_n (that is,
|
|
259
|
+
// ln(a_n) = x_n). We choose the first x_n, x0, to equal 2^7 because the exponential of all larger powers cannot
|
|
260
|
+
// be represented as 18 fixed point decimal numbers in 256 bits, and are therefore larger than a.
|
|
261
|
+
// At the end of this process we will have the sum of all x_n = ln(a_n) that apply, and the remainder of this
|
|
262
|
+
// decomposition, which will be lower than the smallest a_n.
|
|
263
|
+
// ln(a) = k_0 * x_0 + k_1 * x_1 + ... + k_n * x_n + ln(remainder), where each k_n equals either 0 or 1
|
|
264
|
+
// We mutate a by subtracting a_n, making it the remainder of the decomposition
|
|
265
|
+
// For reasons related to how `exp` works, the first two a_n (e^(2^7) and e^(2^6)) are not stored as fixed point
|
|
266
|
+
// numbers with 18 decimals, but instead as plain integers with 0 decimals, so we need to multiply them by
|
|
267
|
+
// ONE_18 to convert them to fixed point.
|
|
268
|
+
// For each a_n, we test if that term is present in the decomposition (if a is larger than it), and if so divide
|
|
269
|
+
// by it and compute the accumulated sum.
|
|
270
|
+
let sum = new bignumber_js_1.default(0);
|
|
271
|
+
if (a.gte(a0.times(ONE_18))) {
|
|
272
|
+
a = a.idiv(a0); // Integer, not fixed point division
|
|
273
|
+
sum = sum.plus(x0);
|
|
274
|
+
}
|
|
275
|
+
if (a.gte(a1.times(ONE_18))) {
|
|
276
|
+
a = a.idiv(a1); // Integer, not fixed point division
|
|
277
|
+
sum = sum.plus(x1);
|
|
278
|
+
}
|
|
279
|
+
// All other a_n and x_n are stored as 20 digit fixed point numbers, so we convert the sum and a to this format.
|
|
280
|
+
sum = sum.times(100);
|
|
281
|
+
a = a.times(100);
|
|
282
|
+
// Because further a_n are 20 digit fixed point numbers, we multiply by ONE_20 when dividing by them.
|
|
283
|
+
if (a.gte(a2)) {
|
|
284
|
+
a = a.times(ONE_20).idiv(a2);
|
|
285
|
+
sum = sum.plus(x2);
|
|
286
|
+
}
|
|
287
|
+
if (a.gte(a3)) {
|
|
288
|
+
a = a.times(ONE_20).idiv(a3);
|
|
289
|
+
sum = sum.plus(x3);
|
|
290
|
+
}
|
|
291
|
+
if (a.gte(a4)) {
|
|
292
|
+
a = a.times(ONE_20).idiv(a4);
|
|
293
|
+
sum = sum.plus(x4);
|
|
294
|
+
}
|
|
295
|
+
if (a.gte(a5)) {
|
|
296
|
+
a = a.times(ONE_20).idiv(a5);
|
|
297
|
+
sum = sum.plus(x5);
|
|
298
|
+
}
|
|
299
|
+
if (a.gte(a6)) {
|
|
300
|
+
a = a.times(ONE_20).idiv(a6);
|
|
301
|
+
sum = sum.plus(x6);
|
|
302
|
+
}
|
|
303
|
+
if (a.gte(a7)) {
|
|
304
|
+
a = a.times(ONE_20).idiv(a7);
|
|
305
|
+
sum = sum.plus(x7);
|
|
306
|
+
}
|
|
307
|
+
if (a.gte(a8)) {
|
|
308
|
+
a = a.times(ONE_20).idiv(a8);
|
|
309
|
+
sum = sum.plus(x8);
|
|
310
|
+
}
|
|
311
|
+
if (a.gte(a9)) {
|
|
312
|
+
a = a.times(ONE_20).idiv(a9);
|
|
313
|
+
sum = sum.plus(x9);
|
|
314
|
+
}
|
|
315
|
+
if (a.gte(a10)) {
|
|
316
|
+
a = a.times(ONE_20).idiv(a10);
|
|
317
|
+
sum = sum.plus(x10);
|
|
318
|
+
}
|
|
319
|
+
if (a.gte(a11)) {
|
|
320
|
+
a = a.times(ONE_20).idiv(a11);
|
|
321
|
+
sum = sum.plus(x11);
|
|
322
|
+
}
|
|
323
|
+
// a is now a small number (smaller than a_11, which roughly equals 1.06). This means we can use a Taylor series
|
|
324
|
+
// that converges rapidly for values of `a` close to one - the same one used in ln_36.
|
|
325
|
+
// Let z = (a - 1) / (a + 1).
|
|
326
|
+
// ln(a) = 2 * (z + z^3 / 3 + z^5 / 5 + z^7 / 7 + ... + z^(2 * n + 1) / (2 * n + 1))
|
|
327
|
+
// Recall that 20 digit fixed point division requires multiplying by ONE_20, and multiplication requires
|
|
328
|
+
// division by ONE_20.
|
|
329
|
+
const z = a.minus(ONE_20).times(ONE_20).idiv(a.plus(ONE_20));
|
|
330
|
+
const z_squared = z.times(z).idiv(ONE_20);
|
|
331
|
+
// num is the numerator of the series: the z^(2 * n + 1) term
|
|
332
|
+
let num = z;
|
|
333
|
+
// seriesSum holds the accumulated sum of each term in the series, starting with the initial z
|
|
334
|
+
let seriesSum = num;
|
|
335
|
+
// In each step, the numerator is multiplied by z^2
|
|
336
|
+
num = num.times(z_squared).idiv(ONE_20);
|
|
337
|
+
seriesSum = seriesSum.plus(num.idiv(3));
|
|
338
|
+
num = num.times(z_squared).idiv(ONE_20);
|
|
339
|
+
seriesSum = seriesSum.plus(num.idiv(5));
|
|
340
|
+
num = num.times(z_squared).idiv(ONE_20);
|
|
341
|
+
seriesSum = seriesSum.plus(num.idiv(7));
|
|
342
|
+
num = num.times(z_squared).idiv(ONE_20);
|
|
343
|
+
seriesSum = seriesSum.plus(num.idiv(9));
|
|
344
|
+
num = num.times(z_squared).idiv(ONE_20);
|
|
345
|
+
seriesSum = seriesSum.plus(num.idiv(11));
|
|
346
|
+
// 6 Taylor terms are sufficient for 36 decimal precision.
|
|
347
|
+
// Finally, we multiply by 2 (non fixed point) to compute ln(remainder)
|
|
348
|
+
seriesSum = seriesSum.times(2);
|
|
349
|
+
// We now have the sum of all x_n present, and the Taylor approximation of the logarithm of the remainder (both
|
|
350
|
+
// with 20 decimals). All that remains is to sum these two, and then drop two digits to return a 18 decimal
|
|
351
|
+
// value.
|
|
352
|
+
return sum.plus(seriesSum).idiv(100);
|
|
353
|
+
};
|
|
354
|
+
const _ln_36 = (x) => {
|
|
355
|
+
// Since ln(1) = 0, a value of x close to one will yield a very small result, which makes using 36 digits worthwhile
|
|
356
|
+
// First, we transform x to a 36 digit fixed point value
|
|
357
|
+
x = x.times(ONE_18);
|
|
358
|
+
// We will use the following Taylor expansion, which converges very rapidly. Let z = (x - 1) / (x + 1)
|
|
359
|
+
// ln(x) = 2 * (z + z^3 / 3 + z^5 / 5 + z^7 / 7 + ... + z^(2 * n + 1) / (2 * n + 1))
|
|
360
|
+
// Recall that 36 digit fixed point division requires multiplying by ONE_36, and multiplication requires division by ONE_36
|
|
361
|
+
const z = x.minus(ONE_36).times(ONE_36).idiv(x.plus(ONE_36));
|
|
362
|
+
const z_squared = z.times(z).idiv(ONE_36);
|
|
363
|
+
// num is the numerator of the series: the z^(2 * n + 1) term
|
|
364
|
+
let num = z;
|
|
365
|
+
// seriesSum holds the accumulated sum of each term in the series, starting with the initial z
|
|
366
|
+
let seriesSum = num;
|
|
367
|
+
// In each step, the numerator is multiplied by z^2
|
|
368
|
+
num = num.times(z_squared).idiv(ONE_36);
|
|
369
|
+
seriesSum = seriesSum.plus(num.idiv(3));
|
|
370
|
+
num = num.times(z_squared).idiv(ONE_36);
|
|
371
|
+
seriesSum = seriesSum.plus(num.idiv(5));
|
|
372
|
+
num = num.times(z_squared).idiv(ONE_36);
|
|
373
|
+
seriesSum = seriesSum.plus(num.idiv(7));
|
|
374
|
+
num = num.times(z_squared).idiv(ONE_36);
|
|
375
|
+
seriesSum = seriesSum.plus(num.idiv(9));
|
|
376
|
+
num = num.times(z_squared).idiv(ONE_36);
|
|
377
|
+
seriesSum = seriesSum.plus(num.idiv(11));
|
|
378
|
+
num = num.times(z_squared).idiv(ONE_36);
|
|
379
|
+
seriesSum = seriesSum.plus(num.idiv(13));
|
|
380
|
+
num = num.times(z_squared).idiv(ONE_36);
|
|
381
|
+
seriesSum = seriesSum.plus(num.idiv(15));
|
|
382
|
+
// 8 Taylor terms are sufficient for 36 decimal precision
|
|
383
|
+
// All that remains is multiplying by 2 (non fixed point)
|
|
384
|
+
return seriesSum.times(2);
|
|
385
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import BigNumber from "bignumber.js";
|
|
2
|
+
export declare const ZERO: BigNumber;
|
|
3
|
+
export declare const ONE: BigNumber;
|
|
4
|
+
export declare const TWO: BigNumber;
|
|
5
|
+
export declare const add: (a: BigNumber, b: BigNumber) => BigNumber;
|
|
6
|
+
export declare const sub: (a: BigNumber, b: BigNumber) => BigNumber;
|
|
7
|
+
export declare const max: (a: BigNumber, b: BigNumber) => BigNumber;
|
|
8
|
+
export declare const min: (a: BigNumber, b: BigNumber) => BigNumber;
|
|
9
|
+
export declare const mul: (a: BigNumber, b: BigNumber) => BigNumber;
|
|
10
|
+
export declare const div: (a: BigNumber, b: BigNumber, roundUp: boolean) => BigNumber;
|
|
11
|
+
export declare const divDown: (a: BigNumber, b: BigNumber) => BigNumber;
|
|
12
|
+
export declare const divUp: (a: BigNumber, b: BigNumber) => BigNumber;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Ported from Solidity:
|
|
3
|
+
// https://github.com/balancer-labs/balancer-v2-monorepo/blob/ce70f7663e0ac94b25ed60cb86faaa8199fd9e13/pkg/solidity-utils/contracts/math/Math.sol
|
|
4
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
6
|
+
};
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.divUp = exports.divDown = exports.div = exports.mul = exports.min = exports.max = exports.sub = exports.add = exports.TWO = exports.ONE = exports.ZERO = void 0;
|
|
9
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
10
|
+
exports.ZERO = new bignumber_js_1.default(0);
|
|
11
|
+
exports.ONE = new bignumber_js_1.default(1);
|
|
12
|
+
exports.TWO = new bignumber_js_1.default(2);
|
|
13
|
+
const add = (a, b) => {
|
|
14
|
+
return a.plus(b);
|
|
15
|
+
};
|
|
16
|
+
exports.add = add;
|
|
17
|
+
const sub = (a, b) => {
|
|
18
|
+
if (b.gt(a))
|
|
19
|
+
throw new Error("SUB_OVERFLOW");
|
|
20
|
+
return a.minus(b);
|
|
21
|
+
};
|
|
22
|
+
exports.sub = sub;
|
|
23
|
+
const max = (a, b) => {
|
|
24
|
+
return a.gte(b) ? a : b;
|
|
25
|
+
};
|
|
26
|
+
exports.max = max;
|
|
27
|
+
const min = (a, b) => {
|
|
28
|
+
return a.lt(b) ? a : b;
|
|
29
|
+
};
|
|
30
|
+
exports.min = min;
|
|
31
|
+
const mul = (a, b) => {
|
|
32
|
+
return a.times(b);
|
|
33
|
+
};
|
|
34
|
+
exports.mul = mul;
|
|
35
|
+
const div = (a, b, roundUp) => {
|
|
36
|
+
return roundUp ? (0, exports.divUp)(a, b) : (0, exports.divDown)(a, b);
|
|
37
|
+
};
|
|
38
|
+
exports.div = div;
|
|
39
|
+
const divDown = (a, b) => {
|
|
40
|
+
if (b.isZero())
|
|
41
|
+
throw new Error("ZERO_DIVISION");
|
|
42
|
+
return a.idiv(b);
|
|
43
|
+
};
|
|
44
|
+
exports.divDown = divDown;
|
|
45
|
+
const divUp = (a, b) => {
|
|
46
|
+
if (b.isZero())
|
|
47
|
+
throw new Error("ZERO_DIVISION");
|
|
48
|
+
return a.isZero() ? exports.ZERO : exports.ONE.plus(a.minus(exports.ONE).idiv(b));
|
|
49
|
+
};
|
|
50
|
+
exports.divUp = divUp;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import Web3 from "web3";
|
|
2
|
+
export interface ResponseType {
|
|
3
|
+
isSuccess: boolean;
|
|
4
|
+
txHash: string;
|
|
5
|
+
errorMessage: string;
|
|
6
|
+
}
|
|
7
|
+
export interface DEXGraphFunctionality {
|
|
8
|
+
setEndpoint: (chainId: number, graphApiKey: string) => void;
|
|
9
|
+
getTopPools: (numPools: number) => Promise<PoolInfo[]>;
|
|
10
|
+
getPoolsWithTokenPair: (tokenA: string, tokenB: string, first: number) => Promise<PoolInfo[]>;
|
|
11
|
+
getPoolsWithToken: (token: string, first: number) => Promise<PoolInfo[]>;
|
|
12
|
+
getAdditionalPoolDataFromSolidity: (poolInfos: PoolInfo[], rpcProvider: Web3) => Promise<Pool[]>;
|
|
13
|
+
}
|
|
14
|
+
export type Token = {
|
|
15
|
+
_address: string;
|
|
16
|
+
decimals: number;
|
|
17
|
+
name?: string;
|
|
18
|
+
};
|
|
19
|
+
export type PoolInfo = {
|
|
20
|
+
poolId: string;
|
|
21
|
+
dexId: string;
|
|
22
|
+
tokens: Token[];
|
|
23
|
+
};
|
|
24
|
+
export declare abstract class Pool {
|
|
25
|
+
poolId: string;
|
|
26
|
+
dexId: string;
|
|
27
|
+
tokens: Token[];
|
|
28
|
+
protected constructor(poolId: string, dexId: string, tokens: Token[]);
|
|
29
|
+
abstract calculateExpectedOutputAmount(tokenIn: string, tokenOut: string, amountIn: bigint): bigint;
|
|
30
|
+
abstract reset(): void;
|
|
31
|
+
abstract update(tokenIn: string, tokenOut: string, amountIn: bigint, amountOut: bigint): void;
|
|
32
|
+
containsToken(token: string): boolean;
|
|
33
|
+
getToken0(): Token;
|
|
34
|
+
getToken1(): Token;
|
|
35
|
+
}
|
|
36
|
+
export type SwapStep = {
|
|
37
|
+
poolId: string;
|
|
38
|
+
dexId: string;
|
|
39
|
+
tokenIn: string;
|
|
40
|
+
tokenOut: string;
|
|
41
|
+
};
|
|
42
|
+
export type Route = {
|
|
43
|
+
swaps: SwapStep[];
|
|
44
|
+
amountIn: bigint;
|
|
45
|
+
percentage: number;
|
|
46
|
+
quote: bigint;
|
|
47
|
+
};
|
|
48
|
+
export type Quote = {
|
|
49
|
+
routes: Route[];
|
|
50
|
+
quote: bigint;
|
|
51
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Pool = void 0;
|
|
4
|
+
// we get from Solidity (extra info)
|
|
5
|
+
class Pool {
|
|
6
|
+
constructor(poolId, dexId, tokens) {
|
|
7
|
+
this.poolId = poolId;
|
|
8
|
+
this.dexId = dexId;
|
|
9
|
+
this.tokens = tokens.map((token) => ({
|
|
10
|
+
_address: token._address.toLowerCase(),
|
|
11
|
+
decimals: token.decimals,
|
|
12
|
+
name: token.name,
|
|
13
|
+
}));
|
|
14
|
+
}
|
|
15
|
+
containsToken(token) {
|
|
16
|
+
return this.tokens.some((t) => t._address.toLowerCase() === token.toLowerCase());
|
|
17
|
+
}
|
|
18
|
+
getToken0() {
|
|
19
|
+
return this.tokens[0];
|
|
20
|
+
}
|
|
21
|
+
getToken1() {
|
|
22
|
+
return this.tokens[1];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.Pool = Pool;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Quote, SwapStep } from "./types/types";
|
|
2
|
+
export declare function prepareSwapParams(quote: Quote, slippagePercentage: number, deadlineInMinutes: number, tokenIn: string, tokenOut: string, amountIn: bigint, recipient: string): {
|
|
3
|
+
adjustedQuote: {
|
|
4
|
+
swaps: {
|
|
5
|
+
data: string;
|
|
6
|
+
dexId: number;
|
|
7
|
+
}[];
|
|
8
|
+
amountIn: bigint;
|
|
9
|
+
}[];
|
|
10
|
+
tokenIn: string;
|
|
11
|
+
tokenOut: string;
|
|
12
|
+
amountIn: bigint;
|
|
13
|
+
minAmountOut: bigint;
|
|
14
|
+
recipient: string;
|
|
15
|
+
deadline: number;
|
|
16
|
+
};
|
|
17
|
+
export declare function generateCalldata(quote: Quote, slippagePercentage: number, deadlineInMinutes: number, tokenIn: string, tokenOut: string, amountIn: bigint, recipient: string): string;
|
|
18
|
+
export declare function transferQuoteWithBalancerPoolIdToAddress(quote: Quote): Quote;
|
|
19
|
+
export declare function hashStringToInt(dexName: string): number;
|
|
20
|
+
export declare function encodeSwapData(swap: SwapStep): string;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.prepareSwapParams = prepareSwapParams;
|
|
4
|
+
exports.generateCalldata = generateCalldata;
|
|
5
|
+
exports.transferQuoteWithBalancerPoolIdToAddress = transferQuoteWithBalancerPoolIdToAddress;
|
|
6
|
+
exports.hashStringToInt = hashStringToInt;
|
|
7
|
+
exports.encodeSwapData = encodeSwapData;
|
|
8
|
+
const RateXAbi_1 = require("../contracts/abi/RateXAbi");
|
|
9
|
+
const ethers_1 = require("ethers");
|
|
10
|
+
function prepareSwapParams(quote, slippagePercentage, deadlineInMinutes, tokenIn, tokenOut, amountIn, recipient) {
|
|
11
|
+
const quoteWithAddressPoolId = transferQuoteWithBalancerPoolIdToAddress(quote);
|
|
12
|
+
const HUNDRED = BigInt(100);
|
|
13
|
+
const slippageBigInt = BigInt(slippagePercentage) * HUNDRED;
|
|
14
|
+
const minAmountOut = (quoteWithAddressPoolId.quote * (HUNDRED - slippageBigInt)) / HUNDRED;
|
|
15
|
+
const adjustedQuote = quoteWithAddressPoolId.routes.map((route) => ({
|
|
16
|
+
swaps: route.swaps.map((swap) => ({
|
|
17
|
+
data: encodeSwapData(swap), // Encode swap data
|
|
18
|
+
dexId: hashStringToInt(swap.dexId), // Convert dexId to uint32
|
|
19
|
+
})),
|
|
20
|
+
amountIn: route.amountIn,
|
|
21
|
+
}));
|
|
22
|
+
const deadline = Math.floor(Date.now() / 1000) + 60 * deadlineInMinutes;
|
|
23
|
+
// Return the parameters as a plain object
|
|
24
|
+
return {
|
|
25
|
+
adjustedQuote,
|
|
26
|
+
tokenIn,
|
|
27
|
+
tokenOut,
|
|
28
|
+
amountIn,
|
|
29
|
+
minAmountOut,
|
|
30
|
+
recipient,
|
|
31
|
+
deadline,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function generateCalldata(quote, slippagePercentage, deadlineInMinutes, tokenIn, tokenOut, amountIn, recipient) {
|
|
35
|
+
const params = prepareSwapParams(quote, slippagePercentage, deadlineInMinutes, tokenIn, tokenOut, amountIn, recipient);
|
|
36
|
+
const abiCoder = new ethers_1.ethers.AbiCoder();
|
|
37
|
+
const calldata = new ethers_1.ethers.Interface(RateXAbi_1.RateXAbi).encodeFunctionData("swap", [
|
|
38
|
+
params.adjustedQuote,
|
|
39
|
+
params.tokenIn,
|
|
40
|
+
params.tokenOut,
|
|
41
|
+
params.amountIn,
|
|
42
|
+
params.minAmountOut,
|
|
43
|
+
params.recipient,
|
|
44
|
+
params.deadline,
|
|
45
|
+
]);
|
|
46
|
+
// Return swap calldata
|
|
47
|
+
return calldata;
|
|
48
|
+
}
|
|
49
|
+
function transferQuoteWithBalancerPoolIdToAddress(quote) {
|
|
50
|
+
quote.routes.forEach((route) => route.swaps.forEach((swap) => {
|
|
51
|
+
if (swap.poolId.length === 66) {
|
|
52
|
+
swap.poolId = swap.poolId.slice(0, 42); // Convert to address format
|
|
53
|
+
}
|
|
54
|
+
}));
|
|
55
|
+
return quote;
|
|
56
|
+
}
|
|
57
|
+
function hashStringToInt(dexName) {
|
|
58
|
+
const hash = (0, ethers_1.keccak256)((0, ethers_1.toUtf8Bytes)(dexName));
|
|
59
|
+
return parseInt(hash.slice(2, 10), 16);
|
|
60
|
+
}
|
|
61
|
+
function encodeSwapData(swap) {
|
|
62
|
+
const abiCoder = new ethers_1.ethers.AbiCoder();
|
|
63
|
+
if (swap.dexId === "BALANCER_V2" ||
|
|
64
|
+
swap.dexId === "UNI_V3") {
|
|
65
|
+
// For DEXes like Balancer, Curve, UniswapV3: encode poolId, tokenIn, and tokenOut
|
|
66
|
+
return abiCoder.encode(["address", "address", "address"], [swap.poolId, swap.tokenIn, swap.tokenOut]);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
// For other DEXes: encode only tokenIn and tokenOut
|
|
70
|
+
return abiCoder.encode(["address", "address"], [swap.tokenIn, swap.tokenOut]);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
Binary file
|
package/index.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import Web3 from "web3"
|
|
2
|
+
import { findRoute } from "./routing/main"
|
|
3
|
+
import { fetchPoolsData } from "./swap/graph_communication"
|
|
4
|
+
import { Pool, Quote} from "./utils/types/types";
|
|
5
|
+
import { generateCalldata, prepareSwapParams } from "./utils/utils";
|
|
6
|
+
|
|
7
|
+
export interface RateXConfig {
|
|
8
|
+
rpcUrl: string;
|
|
9
|
+
chainId: number;
|
|
10
|
+
dexes?: Array<Dexes>;
|
|
11
|
+
graphApiKey: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export enum Dexes {
|
|
15
|
+
UNISWAP_V2 = "UniswapV2",
|
|
16
|
+
UNISWAP_V3 = "UniswapV3",
|
|
17
|
+
SUSHISWAP_V2 = "Sushiswap",
|
|
18
|
+
BALANCER = "Balancer",
|
|
19
|
+
CAMELOT = "Camelot",
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class RateX {
|
|
23
|
+
rpcProvider: Web3
|
|
24
|
+
chainId: number
|
|
25
|
+
graphApiKey: string
|
|
26
|
+
dexes: Array<Dexes>
|
|
27
|
+
|
|
28
|
+
constructor(config: RateXConfig) {
|
|
29
|
+
this.rpcProvider = new Web3(new Web3.providers.HttpProvider(config.rpcUrl))
|
|
30
|
+
this.chainId = config.chainId
|
|
31
|
+
this.graphApiKey = config.graphApiKey
|
|
32
|
+
this.dexes = config.dexes || [Dexes.SUSHISWAP_V2, Dexes.UNISWAP_V2, Dexes.UNISWAP_V3, Dexes.BALANCER, Dexes.CAMELOT]
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
async getQuote(tokenIn: string, tokenOut: string, amountIn: bigint): Promise<Quote> {
|
|
36
|
+
const pools: Pool[] = await fetchPoolsData(tokenIn, tokenOut, 5, 5, this.chainId, this.rpcProvider, this.graphApiKey, this.dexes)
|
|
37
|
+
const route = await findRoute(tokenIn, tokenOut, amountIn, pools, this.chainId)
|
|
38
|
+
return route
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async getSwapCalldata(tokenIn: string, tokenOut: string, amountIn: bigint, slippagePercentage: number, recipient: string, deadlineInMinutes: number): Promise<string> {
|
|
42
|
+
let quote = await this.getQuote(tokenIn, tokenOut, amountIn);
|
|
43
|
+
return generateCalldata(quote, slippagePercentage, deadlineInMinutes, tokenIn, tokenOut, amountIn, recipient);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async getSwapParameters(tokenIn: string, tokenOut: string, amountIn: bigint, slippagePercentage: number, recipient: string, deadlineInMinutes: number): Promise<ReturnType<typeof prepareSwapParams>> {
|
|
47
|
+
let quote = await this.getQuote(tokenIn, tokenOut, amountIn);
|
|
48
|
+
return prepareSwapParams(quote, slippagePercentage, deadlineInMinutes, tokenIn, tokenOut, amountIn, recipient);
|
|
49
|
+
}
|
|
50
|
+
}
|