@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
package/utils/utils.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { Quote, SwapStep } from "./types/types";
|
|
2
|
+
import { RateXAbi } from "../contracts/abi/RateXAbi";
|
|
3
|
+
import { keccak256, toUtf8Bytes, ethers } from "ethers";
|
|
4
|
+
|
|
5
|
+
export function prepareSwapParams(
|
|
6
|
+
quote: Quote,
|
|
7
|
+
slippagePercentage: number,
|
|
8
|
+
deadlineInMinutes: number,
|
|
9
|
+
tokenIn: string,
|
|
10
|
+
tokenOut: string,
|
|
11
|
+
amountIn: bigint,
|
|
12
|
+
recipient: string
|
|
13
|
+
): {
|
|
14
|
+
adjustedQuote: {
|
|
15
|
+
swaps: {
|
|
16
|
+
data: string;
|
|
17
|
+
dexId: number;
|
|
18
|
+
}[];
|
|
19
|
+
amountIn: bigint;
|
|
20
|
+
}[];
|
|
21
|
+
tokenIn: string;
|
|
22
|
+
tokenOut: string;
|
|
23
|
+
amountIn: bigint;
|
|
24
|
+
minAmountOut: bigint;
|
|
25
|
+
recipient: string;
|
|
26
|
+
deadline: number;
|
|
27
|
+
} {
|
|
28
|
+
const quoteWithAddressPoolId =
|
|
29
|
+
transferQuoteWithBalancerPoolIdToAddress(quote);
|
|
30
|
+
|
|
31
|
+
const HUNDRED = BigInt(100);
|
|
32
|
+
const slippageBigInt = BigInt(slippagePercentage) * HUNDRED;
|
|
33
|
+
const minAmountOut =
|
|
34
|
+
(quoteWithAddressPoolId.quote * (HUNDRED - slippageBigInt)) / HUNDRED;
|
|
35
|
+
|
|
36
|
+
const adjustedQuote = quoteWithAddressPoolId.routes.map((route) => ({
|
|
37
|
+
swaps: route.swaps.map((swap) => ({
|
|
38
|
+
data: encodeSwapData(swap), // Encode swap data
|
|
39
|
+
dexId: hashStringToInt(swap.dexId), // Convert dexId to uint32
|
|
40
|
+
})),
|
|
41
|
+
amountIn: route.amountIn,
|
|
42
|
+
}));
|
|
43
|
+
|
|
44
|
+
const deadline = Math.floor(Date.now() / 1000) + 60 * deadlineInMinutes;
|
|
45
|
+
|
|
46
|
+
// Return the parameters as a plain object
|
|
47
|
+
return {
|
|
48
|
+
adjustedQuote,
|
|
49
|
+
tokenIn,
|
|
50
|
+
tokenOut,
|
|
51
|
+
amountIn,
|
|
52
|
+
minAmountOut,
|
|
53
|
+
recipient,
|
|
54
|
+
deadline,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function generateCalldata(
|
|
59
|
+
quote: Quote,
|
|
60
|
+
slippagePercentage: number,
|
|
61
|
+
deadlineInMinutes: number,
|
|
62
|
+
tokenIn: string,
|
|
63
|
+
tokenOut: string,
|
|
64
|
+
amountIn: bigint,
|
|
65
|
+
recipient: string
|
|
66
|
+
): string {
|
|
67
|
+
const params = prepareSwapParams(
|
|
68
|
+
quote,
|
|
69
|
+
slippagePercentage,
|
|
70
|
+
deadlineInMinutes,
|
|
71
|
+
tokenIn,
|
|
72
|
+
tokenOut,
|
|
73
|
+
amountIn,
|
|
74
|
+
recipient
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const abiCoder = new ethers.AbiCoder();
|
|
78
|
+
const calldata = new ethers.Interface(RateXAbi).encodeFunctionData("swap", [
|
|
79
|
+
params.adjustedQuote,
|
|
80
|
+
params.tokenIn,
|
|
81
|
+
params.tokenOut,
|
|
82
|
+
params.amountIn,
|
|
83
|
+
params.minAmountOut,
|
|
84
|
+
params.recipient,
|
|
85
|
+
params.deadline,
|
|
86
|
+
]);
|
|
87
|
+
// Return swap calldata
|
|
88
|
+
return calldata;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function transferQuoteWithBalancerPoolIdToAddress(quote: Quote): Quote {
|
|
92
|
+
quote.routes.forEach((route) =>
|
|
93
|
+
route.swaps.forEach((swap) => {
|
|
94
|
+
if (swap.poolId.length === 66) {
|
|
95
|
+
swap.poolId = swap.poolId.slice(0, 42); // Convert to address format
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
);
|
|
99
|
+
return quote;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function hashStringToInt(dexName: string): number {
|
|
103
|
+
const hash = keccak256(toUtf8Bytes(dexName));
|
|
104
|
+
return parseInt(hash.slice(2, 10), 16);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function encodeSwapData(swap: SwapStep): string {
|
|
108
|
+
const abiCoder = new ethers.AbiCoder();
|
|
109
|
+
if (
|
|
110
|
+
swap.dexId === "BALANCER_V2" ||
|
|
111
|
+
swap.dexId === "UNI_V3"
|
|
112
|
+
) {
|
|
113
|
+
// For DEXes like Balancer, Curve, UniswapV3: encode poolId, tokenIn, and tokenOut
|
|
114
|
+
return abiCoder.encode(
|
|
115
|
+
["address", "address", "address"],
|
|
116
|
+
[swap.poolId, swap.tokenIn, swap.tokenOut]
|
|
117
|
+
);
|
|
118
|
+
} else {
|
|
119
|
+
// For other DEXes: encode only tokenIn and tokenOut
|
|
120
|
+
return abiCoder.encode(
|
|
121
|
+
["address", "address"],
|
|
122
|
+
[swap.tokenIn, swap.tokenOut]
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
}
|