@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.
Files changed (173) hide show
  1. package/.idea/copilot.data.migration.agent.xml +6 -0
  2. package/.idea/copilot.data.migration.ask.xml +6 -0
  3. package/.idea/copilot.data.migration.ask2agent.xml +6 -0
  4. package/.idea/copilot.data.migration.edit.xml +6 -0
  5. package/.idea/modules.xml +8 -0
  6. package/.idea/php.xml +19 -0
  7. package/.idea/ratex-sdk.iml +8 -0
  8. package/.idea/vcs.xml +6 -0
  9. package/LICENSE-MIT +21 -0
  10. package/README.md +209 -0
  11. package/contracts/abi/BalancerHelperAbi.ts +1 -0
  12. package/contracts/abi/CamelotHelperAbi.ts +1 -0
  13. package/contracts/abi/RateXAbi.ts +482 -0
  14. package/contracts/abi/SushiSwapHelperAbi.ts +1 -0
  15. package/contracts/abi/UniswapHelperAbi.ts +1 -0
  16. package/contracts/abi/UniswapV2HelperAbi.ts +1 -0
  17. package/contracts/addresses-arbitrum.ts +8 -0
  18. package/contracts/addresses-mainnet.ts +7 -0
  19. package/contracts/addresses-polkadot.ts +9 -0
  20. package/contracts/addresses-sei.ts +9 -0
  21. package/contracts/rateX/BalancerHelper.ts +13 -0
  22. package/contracts/rateX/CamelotHelper.ts +13 -0
  23. package/contracts/rateX/SushiSwapHelper.ts +13 -0
  24. package/contracts/rateX/UniswapHelper.ts +19 -0
  25. package/contracts/rateX/UniswapV2Helper.ts +19 -0
  26. package/dexes/dexIdsList.ts +9 -0
  27. package/dexes/graph_queries/BalancerV2.ts +150 -0
  28. package/dexes/graph_queries/CamelotV2.ts +202 -0
  29. package/dexes/graph_queries/SushiSwapV2.ts +283 -0
  30. package/dexes/graph_queries/UniswapV2.ts +252 -0
  31. package/dexes/graph_queries/UniswapV3.ts +229 -0
  32. package/dexes/graph_queries/graphQueryFilters.ts +41 -0
  33. package/dexes/graph_queries/x_template.ts +67 -0
  34. package/dexes/pools/Balancer/BalancerState.ts +34 -0
  35. package/dexes/pools/Balancer/BalancerWeightedPool.ts +96 -0
  36. package/dexes/pools/Camelot.ts +164 -0
  37. package/dexes/pools/SushiSwapV2.ts +35 -0
  38. package/dexes/pools/UniswapV2.ts +36 -0
  39. package/dexes/pools/uniswap/UniswapV3.ts +40 -0
  40. package/dexes/pools/uniswap/testUniswapOffchainQuoter.ts +169 -0
  41. package/dexes/pools/uniswap/types.ts +174 -0
  42. package/dexes/pools/uniswap/uniswapOffchainQuoter.ts +173 -0
  43. package/dexes/pools/uniswap/uniswapState.ts +56 -0
  44. package/dexes/pools/uniswap/utils.ts +71 -0
  45. package/dist/contracts/abi/BalancerHelperAbi.d.ts +25 -0
  46. package/dist/contracts/abi/BalancerHelperAbi.js +4 -0
  47. package/dist/contracts/abi/CamelotHelperAbi.d.ts +45 -0
  48. package/dist/contracts/abi/CamelotHelperAbi.js +4 -0
  49. package/dist/contracts/abi/RateXAbi.d.ts +71 -0
  50. package/dist/contracts/abi/RateXAbi.js +485 -0
  51. package/dist/contracts/abi/SushiSwapHelperAbi.d.ts +45 -0
  52. package/dist/contracts/abi/SushiSwapHelperAbi.js +4 -0
  53. package/dist/contracts/abi/UniswapHelperAbi.d.ts +39 -0
  54. package/dist/contracts/abi/UniswapHelperAbi.js +4 -0
  55. package/dist/contracts/abi/UniswapV2HelperAbi.d.ts +45 -0
  56. package/dist/contracts/abi/UniswapV2HelperAbi.js +4 -0
  57. package/dist/contracts/addresses-arbitrum.d.ts +6 -0
  58. package/dist/contracts/addresses-arbitrum.js +10 -0
  59. package/dist/contracts/addresses-mainnet.d.ts +6 -0
  60. package/dist/contracts/addresses-mainnet.js +10 -0
  61. package/dist/contracts/addresses-polkadot.d.ts +6 -0
  62. package/dist/contracts/addresses-polkadot.js +10 -0
  63. package/dist/contracts/addresses-sei.d.ts +6 -0
  64. package/dist/contracts/addresses-sei.js +10 -0
  65. package/dist/contracts/rateX/BalancerHelper.d.ts +26 -0
  66. package/dist/contracts/rateX/BalancerHelper.js +14 -0
  67. package/dist/contracts/rateX/CamelotHelper.d.ts +46 -0
  68. package/dist/contracts/rateX/CamelotHelper.js +14 -0
  69. package/dist/contracts/rateX/SushiSwapHelper.d.ts +46 -0
  70. package/dist/contracts/rateX/SushiSwapHelper.js +14 -0
  71. package/dist/contracts/rateX/UniswapHelper.d.ts +40 -0
  72. package/dist/contracts/rateX/UniswapHelper.js +22 -0
  73. package/dist/contracts/rateX/UniswapV2Helper.d.ts +46 -0
  74. package/dist/contracts/rateX/UniswapV2Helper.js +22 -0
  75. package/dist/dexes/dexIdsList.d.ts +9 -0
  76. package/dist/dexes/dexIdsList.js +12 -0
  77. package/dist/dexes/graph_queries/BalancerV2.d.ts +14 -0
  78. package/dist/dexes/graph_queries/BalancerV2.js +141 -0
  79. package/dist/dexes/graph_queries/CamelotV2.d.ts +14 -0
  80. package/dist/dexes/graph_queries/CamelotV2.js +183 -0
  81. package/dist/dexes/graph_queries/SushiSwapV2.d.ts +14 -0
  82. package/dist/dexes/graph_queries/SushiSwapV2.js +263 -0
  83. package/dist/dexes/graph_queries/UniswapV2.d.ts +14 -0
  84. package/dist/dexes/graph_queries/UniswapV2.js +217 -0
  85. package/dist/dexes/graph_queries/UniswapV3.d.ts +14 -0
  86. package/dist/dexes/graph_queries/UniswapV3.js +198 -0
  87. package/dist/dexes/graph_queries/graphQueryFilters.d.ts +19 -0
  88. package/dist/dexes/graph_queries/graphQueryFilters.js +40 -0
  89. package/dist/dexes/graph_queries/x_template.d.ts +12 -0
  90. package/dist/dexes/graph_queries/x_template.js +57 -0
  91. package/dist/dexes/pools/Balancer/BalancerState.d.ts +6 -0
  92. package/dist/dexes/pools/Balancer/BalancerState.js +32 -0
  93. package/dist/dexes/pools/Balancer/BalancerWeightedPool.d.ts +12 -0
  94. package/dist/dexes/pools/Balancer/BalancerWeightedPool.js +109 -0
  95. package/dist/dexes/pools/Camelot.d.ts +12 -0
  96. package/dist/dexes/pools/Camelot.js +135 -0
  97. package/dist/dexes/pools/SushiSwapV2.d.ts +9 -0
  98. package/dist/dexes/pools/SushiSwapV2.js +34 -0
  99. package/dist/dexes/pools/UniswapV2.d.ts +9 -0
  100. package/dist/dexes/pools/UniswapV2.js +34 -0
  101. package/dist/dexes/pools/uniswap/UniswapV3.d.ts +7 -0
  102. package/dist/dexes/pools/uniswap/UniswapV3.js +36 -0
  103. package/dist/dexes/pools/uniswap/types.d.ts +76 -0
  104. package/dist/dexes/pools/uniswap/types.js +111 -0
  105. package/dist/dexes/pools/uniswap/uniswapOffchainQuoter.d.ts +13 -0
  106. package/dist/dexes/pools/uniswap/uniswapOffchainQuoter.js +121 -0
  107. package/dist/dexes/pools/uniswap/uniswapState.d.ts +14 -0
  108. package/dist/dexes/pools/uniswap/uniswapState.js +51 -0
  109. package/dist/dexes/pools/uniswap/utils.d.ts +3 -0
  110. package/dist/dexes/pools/uniswap/utils.js +41 -0
  111. package/dist/index.d.ts +26 -0
  112. package/dist/index.js +41 -0
  113. package/dist/routes.d.ts +1 -0
  114. package/dist/routes.js +20 -0
  115. package/dist/routing/iterative_spliting/main.d.ts +3 -0
  116. package/dist/routing/iterative_spliting/main.js +104 -0
  117. package/dist/routing/iterative_spliting/multiHopSwap.d.ts +4 -0
  118. package/dist/routing/iterative_spliting/multiHopSwap.js +83 -0
  119. package/dist/routing/main.d.ts +2 -0
  120. package/dist/routing/main.js +22 -0
  121. package/dist/routing/uni_like_algo/algo_config.d.ts +2 -0
  122. package/dist/routing/uni_like_algo/algo_config.js +8 -0
  123. package/dist/routing/uni_like_algo/amount_distribution.d.ts +2 -0
  124. package/dist/routing/uni_like_algo/amount_distribution.js +17 -0
  125. package/dist/routing/uni_like_algo/compute_routes_backtrack.d.ts +3 -0
  126. package/dist/routing/uni_like_algo/compute_routes_backtrack.js +44 -0
  127. package/dist/routing/uni_like_algo/main.d.ts +2 -0
  128. package/dist/routing/uni_like_algo/main.js +49 -0
  129. package/dist/routing/uni_like_algo/routes_quoter.d.ts +21 -0
  130. package/dist/routing/uni_like_algo/routes_quoter.js +53 -0
  131. package/dist/routing/uni_like_algo/swap_finder.d.ts +25 -0
  132. package/dist/routing/uni_like_algo/swap_finder.js +154 -0
  133. package/dist/routing/uni_like_algo/types.d.ts +40 -0
  134. package/dist/routing/uni_like_algo/types.js +12 -0
  135. package/dist/swap/graph_communication.d.ts +5 -0
  136. package/dist/swap/graph_communication.js +187 -0
  137. package/dist/swap/my_local_storage.d.ts +8 -0
  138. package/dist/swap/my_local_storage.js +16 -0
  139. package/dist/utils/addresses.d.ts +24 -0
  140. package/dist/utils/addresses.js +60 -0
  141. package/dist/utils/math/fixed-points.d.ts +14 -0
  142. package/dist/utils/math/fixed-points.js +123 -0
  143. package/dist/utils/math/log-exp.d.ts +5 -0
  144. package/dist/utils/math/log-exp.js +385 -0
  145. package/dist/utils/math/math.d.ts +12 -0
  146. package/dist/utils/math/math.js +50 -0
  147. package/dist/utils/types/types.d.ts +51 -0
  148. package/dist/utils/types/types.js +25 -0
  149. package/dist/utils/utils.d.ts +20 -0
  150. package/dist/utils/utils.js +72 -0
  151. package/images/decenter_logo.png +0 -0
  152. package/index.ts +50 -0
  153. package/package.json +39 -0
  154. package/routes.ts +27 -0
  155. package/routing/iterative_spliting/main.ts +131 -0
  156. package/routing/iterative_spliting/multiHopSwap.ts +98 -0
  157. package/routing/main.ts +22 -0
  158. package/routing/uni_like_algo/algo_config.ts +7 -0
  159. package/routing/uni_like_algo/amount_distribution.ts +16 -0
  160. package/routing/uni_like_algo/compute_routes_backtrack.ts +81 -0
  161. package/routing/uni_like_algo/main.ts +65 -0
  162. package/routing/uni_like_algo/routes_quoter.ts +63 -0
  163. package/routing/uni_like_algo/swap_finder.ts +185 -0
  164. package/routing/uni_like_algo/types.ts +54 -0
  165. package/swap/graph_communication.ts +212 -0
  166. package/swap/my_local_storage.ts +27 -0
  167. package/tsconfig.json +26 -0
  168. package/utils/addresses.ts +64 -0
  169. package/utils/math/fixed-points.ts +88 -0
  170. package/utils/math/log-exp.ts +469 -0
  171. package/utils/math/math.ts +46 -0
  172. package/utils/types/types.ts +100 -0
  173. package/utils/utils.ts +125 -0
@@ -0,0 +1,183 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const graphql_1 = require("graphql");
4
+ const graphql_request_1 = require("graphql-request");
5
+ const dexIdsList_1 = require("../dexIdsList");
6
+ const Camelot_1 = require("../pools/Camelot");
7
+ const CamelotHelper_1 = require("../../contracts/rateX/CamelotHelper");
8
+ // Camelot is a silly place
9
+ class CamelotV2 {
10
+ constructor() {
11
+ // Camelot is currently not working, ne
12
+ this.endpoint = ``;
13
+ this.dexId = dexIdsList_1.dexIds.CAMELOT;
14
+ this.chainId = 1;
15
+ this.myLocalStorage = null;
16
+ }
17
+ static initialize(myLocalStorage) {
18
+ const object = new CamelotV2();
19
+ object.myLocalStorage = myLocalStorage;
20
+ return object;
21
+ }
22
+ setEndpoint(chainId, graphApiKey) {
23
+ if (chainId == 1) {
24
+ this.endpoint = `https://gateway-arbitrum.network.thegraph.com/api/${graphApiKey}/subgraphs/id/8zagLSufxk5cVhzkzai3tyABwJh53zxn9tmUYJcJxijG`;
25
+ }
26
+ if (chainId == 42161) {
27
+ this.endpoint = `https://gateway-arbitrum.network.thegraph.com/api/${graphApiKey}/subgraphs/id/8zagLSufxk5cVhzkzai3tyABwJh53zxn9tmUYJcJxijG`;
28
+ }
29
+ this.chainId = chainId;
30
+ }
31
+ async getTopPools(numPools) {
32
+ const poolsInfo = [];
33
+ const queryResult = await (0, graphql_request_1.request)(this.endpoint, queryTopPools(numPools));
34
+ queryResult.pairs.forEach((lp) => {
35
+ poolsInfo.push(createPoolFromGraph(lp, this.dexId));
36
+ });
37
+ return poolsInfo;
38
+ }
39
+ async getPoolsWithTokenPair(token1, token2, first) {
40
+ const poolsInfo = [];
41
+ const queryResult = await (0, graphql_request_1.request)(this.endpoint, queryPoolsWithTokenPair(token1, token2, first));
42
+ queryResult.pairs.forEach((lp) => {
43
+ poolsInfo.push(createPoolFromGraph(lp, this.dexId));
44
+ });
45
+ return poolsInfo;
46
+ }
47
+ async getPoolsWithToken(token, numPools) {
48
+ const poolsInfo = [];
49
+ const queryResult = await (0, graphql_request_1.request)(this.endpoint, queryPoolsWithToken(token, numPools));
50
+ queryResult.pairs.forEach((lp) => {
51
+ poolsInfo.push(createPoolFromGraph(lp, this.dexId));
52
+ });
53
+ return poolsInfo;
54
+ }
55
+ // call to Solidity for additional data
56
+ async getAdditionalPoolDataFromSolidity(poolInfos, rpcProvider) {
57
+ const CamelotHelperContract = (0, CamelotHelper_1.CreateCamelotHelperContract)(this.chainId, rpcProvider);
58
+ //@ts-ignore
59
+ const rawData = await CamelotHelperContract.methods.getPoolsData(poolInfos).call();
60
+ const pools = [];
61
+ for (let pool of rawData) {
62
+ const poolId = pool[0];
63
+ const dexId = pool[1];
64
+ const tokensRaw1 = pool[2][0];
65
+ const tokensRaw2 = pool[2][1];
66
+ const token1 = {
67
+ _address: tokensRaw1[0],
68
+ decimals: Number(tokensRaw1[1]),
69
+ };
70
+ const token2 = {
71
+ _address: tokensRaw2[0],
72
+ decimals: Number(tokensRaw2[1]),
73
+ };
74
+ const reserves = [BigInt(pool[3][0]), BigInt(pool[3][1])];
75
+ const fees = [BigInt(pool[4][0]), BigInt(pool[4][1])];
76
+ const stableSwap = pool[5];
77
+ // do not include pools with no liquidity
78
+ if (reserves[0] === BigInt(0) || reserves[1] === BigInt(0)) {
79
+ continue;
80
+ }
81
+ pools.push(new Camelot_1.CamelotPool(poolId, dexId, [token1, token2], reserves, fees, stableSwap));
82
+ }
83
+ for (const pool of pools)
84
+ // @ts-ignore
85
+ this.myLocalStorage.setItem(pool.poolId.toLowerCase(), pool);
86
+ return pools;
87
+ }
88
+ }
89
+ exports.default = CamelotV2;
90
+ function queryTopPools(numPools) {
91
+ return (0, graphql_1.parse)((0, graphql_request_1.gql) `
92
+ {
93
+ pairs(first: ${numPools}, orderDirection: desc, orderBy: volumeUSD) {
94
+ id
95
+ token0 {
96
+ id
97
+ name
98
+ decimals
99
+ }
100
+ token1 {
101
+ id
102
+ name
103
+ decimals
104
+ }
105
+ }
106
+ }
107
+ `);
108
+ }
109
+ function queryPoolsWithTokenPair(tokenA, tokenB, numPools) {
110
+ return (0, graphql_1.parse)((0, graphql_request_1.gql) `
111
+ {
112
+ pairs(first: ${numPools}, orderDirection: desc, orderBy: volumeUSD, where: {
113
+ or: [
114
+ {and: [
115
+ {token0_: {id: "${tokenA.toLowerCase()}"}},
116
+ {token1_: {id: "${tokenB.toLowerCase()}"}}
117
+ ]},
118
+ {and: [
119
+ {token0_: {id: "${tokenB.toLowerCase()}"}},
120
+ {token1_: {id: "${tokenA.toLowerCase()}"}}
121
+ ]}
122
+ ]
123
+ }) {
124
+ id
125
+ volumeUSD
126
+ token0 {
127
+ id
128
+ name
129
+ decimals
130
+ }
131
+ token1 {
132
+ id
133
+ name
134
+ decimals
135
+ }
136
+ }
137
+ }
138
+ `);
139
+ }
140
+ function queryPoolsWithToken(token, numPools) {
141
+ return (0, graphql_1.parse)((0, graphql_request_1.gql) `
142
+ {
143
+ pairs(first: ${numPools}, orderDirection: desc, orderBy: volumeUSD, where: {
144
+ or: [
145
+ {token0_: {id: "${token.toLowerCase()}"}},
146
+ {token1_: {id: "${token.toLowerCase()}"}}
147
+ ]
148
+ }) {
149
+ id
150
+ volumeUSD
151
+ token0 {
152
+ id
153
+ name
154
+ decimals
155
+ }
156
+ token1 {
157
+ id
158
+ name
159
+ decimals
160
+ }
161
+ }
162
+ }
163
+ `);
164
+ }
165
+ function createPoolFromGraph(jsonData, dexId) {
166
+ const pool = {
167
+ poolId: jsonData.id,
168
+ dexId: dexId,
169
+ tokens: [
170
+ {
171
+ _address: jsonData.token0.id,
172
+ decimals: jsonData.token0.decimals,
173
+ name: jsonData.token0.name,
174
+ },
175
+ {
176
+ _address: jsonData.token1.id,
177
+ decimals: jsonData.token1.decimals,
178
+ name: jsonData.token1.name,
179
+ },
180
+ ],
181
+ };
182
+ return pool;
183
+ }
@@ -0,0 +1,14 @@
1
+ import { Pool, PoolInfo, DEXGraphFunctionality } from '../../utils/types/types';
2
+ import Web3 from 'web3';
3
+ export default class SushiSwapV2 implements DEXGraphFunctionality {
4
+ endpoint: string;
5
+ dexId: string;
6
+ chainId: number;
7
+ myLocalStorage: null;
8
+ static initialize(myLocalStorage: any): DEXGraphFunctionality;
9
+ setEndpoint(chainId: number, graphApiKey: string): void;
10
+ getTopPools(numPools: number): Promise<PoolInfo[]>;
11
+ getPoolsWithTokenPair(tokenA: string, tokenB: string, numPools: number): Promise<PoolInfo[]>;
12
+ getPoolsWithToken(token: string, numPools: number): Promise<PoolInfo[]>;
13
+ getAdditionalPoolDataFromSolidity(poolInfos: PoolInfo[], rpcProvider: Web3): Promise<Pool[]>;
14
+ }
@@ -0,0 +1,263 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const graphql_1 = require("graphql");
4
+ const graphql_request_1 = require("graphql-request");
5
+ const dexIdsList_1 = require("../dexIdsList");
6
+ const SushiSwapV2_1 = require("../pools/SushiSwapV2");
7
+ const SushiSwapHelper_1 = require("../../contracts/rateX/SushiSwapHelper");
8
+ class SushiSwapV2 {
9
+ constructor() {
10
+ this.endpoint = "";
11
+ this.dexId = dexIdsList_1.dexIds.SUSHI_V2;
12
+ this.chainId = 1;
13
+ this.myLocalStorage = null;
14
+ }
15
+ static initialize(myLocalStorage) {
16
+ const object = new SushiSwapV2();
17
+ object.myLocalStorage = myLocalStorage;
18
+ return object;
19
+ }
20
+ setEndpoint(chainId, graphApiKey) {
21
+ if (chainId == 1) {
22
+ this.endpoint = `https://gateway-arbitrum.network.thegraph.com/api/${graphApiKey}/subgraphs/id/77jZ9KWeyi3CJ96zkkj5s1CojKPHt6XJKjLFzsDCd8Fd`;
23
+ }
24
+ if (chainId == 42161) {
25
+ this.endpoint = `https://gateway-arbitrum.network.thegraph.com/api/${graphApiKey}/subgraphs/id/8yBXBTMfdhsoE5QCf7KnoPmQb7QAWtRzESfYjiCjGEM9`;
26
+ }
27
+ this.chainId = chainId;
28
+ }
29
+ async getTopPools(numPools) {
30
+ const poolsInfo = [];
31
+ const queryResult = await (0, graphql_request_1.request)(this.endpoint, queryTopPools(numPools, this.chainId));
32
+ if (this.chainId == 1) {
33
+ queryResult.liquidityPools.forEach((pool) => {
34
+ poolsInfo.push(createPoolFromGraph(pool, this.dexId, this.chainId));
35
+ });
36
+ }
37
+ else {
38
+ queryResult.pairs.forEach((pool) => {
39
+ poolsInfo.push(createPoolFromGraph(pool, this.dexId, this.chainId));
40
+ });
41
+ }
42
+ return poolsInfo;
43
+ }
44
+ async getPoolsWithTokenPair(tokenA, tokenB, numPools) {
45
+ const poolsInfo = [];
46
+ const queryResult = await (0, graphql_request_1.request)(this.endpoint, queryPoolsWithTokenPair(tokenA, tokenB, numPools, this.chainId));
47
+ if (this.chainId == 1) {
48
+ queryResult.liquidityPools.forEach((pool) => {
49
+ poolsInfo.push(createPoolFromGraph(pool, this.dexId, this.chainId));
50
+ });
51
+ }
52
+ else {
53
+ queryResult.pairs.forEach((pool) => {
54
+ poolsInfo.push(createPoolFromGraph(pool, this.dexId, this.chainId));
55
+ });
56
+ }
57
+ return poolsInfo;
58
+ }
59
+ async getPoolsWithToken(token, numPools) {
60
+ const poolsInfo = [];
61
+ const queryResult = await (0, graphql_request_1.request)(this.endpoint, queryPoolsWithToken(token, numPools, this.chainId));
62
+ if (this.chainId == 1) {
63
+ queryResult.liquidityPools.forEach((pool) => {
64
+ poolsInfo.push(createPoolFromGraph(pool, this.dexId, this.chainId));
65
+ });
66
+ }
67
+ else {
68
+ queryResult.pairs.forEach((pool) => {
69
+ poolsInfo.push(createPoolFromGraph(pool, this.dexId, this.chainId));
70
+ });
71
+ }
72
+ return poolsInfo;
73
+ }
74
+ async getAdditionalPoolDataFromSolidity(poolInfos, rpcProvider) {
75
+ //@ts-ignore
76
+ const SushiSwapHelperContract = (0, SushiSwapHelper_1.CreateSushiSwapHelperContract)(this.chainId, rpcProvider);
77
+ const rawData = await SushiSwapHelperContract.methods.getPoolsData(poolInfos).call();
78
+ const pools = [];
79
+ for (let pool of rawData) {
80
+ const poolId = pool[0];
81
+ const dexId = pool[1];
82
+ const tokensRaw1 = pool[2][0];
83
+ const tokensRaw2 = pool[2][1];
84
+ const token1 = {
85
+ _address: tokensRaw1[0],
86
+ decimals: Number(tokensRaw1[1]),
87
+ name: tokensRaw1[2],
88
+ };
89
+ const token2 = {
90
+ _address: tokensRaw2[0],
91
+ decimals: Number(tokensRaw2[1]),
92
+ name: tokensRaw2[2],
93
+ };
94
+ pools.push(new SushiSwapV2_1.SushiSwapV2Pool(pool[0], pool[1], [token1, token2], pool[3]));
95
+ }
96
+ for (const pool of pools)
97
+ // @ts-ignore
98
+ this.myLocalStorage.setItem(pool.poolId.toLowerCase(), pool);
99
+ return pools;
100
+ }
101
+ }
102
+ exports.default = SushiSwapV2;
103
+ function queryTopPools(numPools, chainId) {
104
+ if (chainId == 1) {
105
+ return (0, graphql_1.parse)((0, graphql_request_1.gql) `
106
+ {
107
+ liquidityPools(first:${numPools}, orderDirection: desc, orderBy: cumulativeVolumeUSD) {
108
+ id
109
+ cumulativeVolumeUSD
110
+ inputTokens {
111
+ id
112
+ decimals
113
+ name
114
+ }
115
+ }
116
+ }
117
+ `);
118
+ }
119
+ return (0, graphql_1.parse)((0, graphql_request_1.gql) `
120
+ {
121
+ pairs(first: ${numPools}, orderDirection: desc, orderBy: volumeUSD) {
122
+ id
123
+ volumeUSD
124
+ token0 {
125
+ id
126
+ name
127
+ decimals
128
+ }
129
+ token1 {
130
+ id
131
+ name
132
+ decimals
133
+ }
134
+ }
135
+ }
136
+ `);
137
+ }
138
+ function queryPoolsWithTokenPair(tokenA, tokenB, numPools, chainId) {
139
+ if (chainId == 1) {
140
+ return (0, graphql_1.parse)((0, graphql_request_1.gql) `{
141
+ liquidityPools(first: ${numPools}, orderDirection: desc, orderBy: cumulativeVolumeUSD, where: {
142
+ and: [
143
+ {inputTokens_: {id: "${tokenA.toLowerCase()}"}},
144
+ {inputTokens_: {id: "${tokenB.toLowerCase()}"}}
145
+ ]
146
+ }
147
+ ) {
148
+ id
149
+ cumulativeVolumeUSD
150
+ inputTokens {
151
+ id
152
+ decimals
153
+ name
154
+ }
155
+ }
156
+ }`);
157
+ }
158
+ return (0, graphql_1.parse)((0, graphql_request_1.gql) `
159
+ {
160
+ pairs(first: ${numPools}, orderDirection: desc, orderBy: volumeUSD, where: {
161
+ or: [
162
+ {and: [
163
+ {token0_: {id: "${tokenA.toLowerCase()}"}},
164
+ {token1_: {id: "${tokenB.toLowerCase()}"}}
165
+ ]},
166
+ {and: [
167
+ {token0_: {id: "${tokenB.toLowerCase()}"}},
168
+ {token1_: {id: "${tokenA.toLowerCase()}"}}
169
+ ]}
170
+ ]
171
+ }) {
172
+ id
173
+ volumeUSD
174
+ token0 {
175
+ id
176
+ name
177
+ decimals
178
+ }
179
+ token1 {
180
+ id
181
+ name
182
+ decimals
183
+ }
184
+ }
185
+ }
186
+ `);
187
+ }
188
+ function queryPoolsWithToken(token, numPools, chainId) {
189
+ if (chainId == 1) {
190
+ return (0, graphql_1.parse)((0, graphql_request_1.gql) `{
191
+ liquidityPools(first: ${numPools}, orderDirection: desc, orderBy: cumulativeVolumeUSD, where: {
192
+ inputTokens_: { id: "${token.toLowerCase()}" }
193
+ }
194
+ ) {
195
+ id
196
+ cumulativeVolumeUSD
197
+ inputTokens {
198
+ id
199
+ decimals
200
+ name
201
+ }
202
+ }
203
+ }`);
204
+ }
205
+ return (0, graphql_1.parse)((0, graphql_request_1.gql) `
206
+ {
207
+ pairs(first: ${numPools}, orderDirection: desc, orderBy: volumeUSD, where: {
208
+ or: [
209
+ {token0_: {id: "${token.toLowerCase()}"}},
210
+ {token1_: {id: "${token.toLowerCase()}"}}
211
+ ]
212
+ }) {
213
+ id
214
+ volumeUSD
215
+ token0 {
216
+ id
217
+ name
218
+ decimals
219
+ }
220
+ token1 {
221
+ id
222
+ name
223
+ decimals
224
+ }
225
+ }
226
+ }
227
+ `);
228
+ }
229
+ function createPoolFromGraph(jsonData, dexId, chainId) {
230
+ let pool;
231
+ if (chainId == 1) {
232
+ pool = {
233
+ poolId: jsonData.id,
234
+ dexId: dexId,
235
+ tokens: jsonData.inputTokens.map((token, index) => {
236
+ return {
237
+ _address: token.id,
238
+ decimals: token.decimals,
239
+ name: token.name,
240
+ };
241
+ }),
242
+ };
243
+ }
244
+ else {
245
+ pool = {
246
+ poolId: jsonData.id,
247
+ dexId: dexId,
248
+ tokens: [
249
+ {
250
+ _address: jsonData.token0.id,
251
+ decimals: jsonData.token0.decimals,
252
+ name: jsonData.token0.name,
253
+ },
254
+ {
255
+ _address: jsonData.token1.id,
256
+ decimals: jsonData.token1.decimals,
257
+ name: jsonData.token1.name,
258
+ },
259
+ ],
260
+ };
261
+ }
262
+ return pool;
263
+ }
@@ -0,0 +1,14 @@
1
+ import { Pool, PoolInfo, DEXGraphFunctionality } from '../../utils/types/types';
2
+ import Web3 from 'web3';
3
+ export default class UniswapV2 implements DEXGraphFunctionality {
4
+ endpoint: string;
5
+ chainId: number;
6
+ dexId: string;
7
+ myLocalStorage: null;
8
+ static initialize(myLocalStorage: any): DEXGraphFunctionality;
9
+ setEndpoint(chainId: number, graphApiKey: string): void;
10
+ getTopPools(numPools: number): Promise<PoolInfo[]>;
11
+ getPoolsWithTokenPair(tokenA: string, tokenB: string, numPools: number): Promise<PoolInfo[]>;
12
+ getPoolsWithToken(token: string, numPools: number): Promise<PoolInfo[]>;
13
+ getAdditionalPoolDataFromSolidity(poolInfos: PoolInfo[], rpcProvider: Web3): Promise<Pool[]>;
14
+ }
@@ -0,0 +1,217 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const graphql_1 = require("graphql");
4
+ const graphql_request_1 = require("graphql-request");
5
+ const dexIdsList_1 = require("../dexIdsList");
6
+ const UniswapV2_1 = require("../pools/UniswapV2");
7
+ const UniswapV2Helper_1 = require("../../contracts/rateX/UniswapV2Helper");
8
+ const graphQueryFilters_1 = require("./graphQueryFilters");
9
+ class UniswapV2 {
10
+ constructor() {
11
+ this.endpoint = ``;
12
+ this.chainId = 1;
13
+ this.dexId = dexIdsList_1.dexIds.UNI_V2;
14
+ this.myLocalStorage = null;
15
+ }
16
+ static initialize(myLocalStorage) {
17
+ const object = new UniswapV2();
18
+ object.myLocalStorage = myLocalStorage;
19
+ return object;
20
+ }
21
+ // @reminder add uniswapv2 real endpoint for arbitrum
22
+ setEndpoint(chainId, graphApiKey) {
23
+ if (chainId == 1329) {
24
+ this.endpoint = `https://indexer.us.hyperindex.xyz/32230f9/converter`;
25
+ }
26
+ if (chainId == 1) {
27
+ this.endpoint = `https://gateway-arbitrum.network.thegraph.com/api/${graphApiKey}/subgraphs/id/EYCKATKGBKLWvSfwvBjzfCBmGwYNdVkduYXVivCsLRFu`;
28
+ }
29
+ if (chainId == 42161) {
30
+ this.endpoint = `https://gateway-arbitrum.network.thegraph.com/api/${graphApiKey}/subgraphs/id/CStW6CSQbHoXsgKuVCrk3uShGA4JX3CAzzv2x9zaGf8w`;
31
+ }
32
+ if (chainId == 420420417) {
33
+ this.endpoint = `https://saphyre-service-dev.saphyre.xyz/api/graphql/v2`;
34
+ }
35
+ this.chainId = chainId;
36
+ }
37
+ async getTopPools(numPools) {
38
+ const poolsInfo = [];
39
+ const { resultKey } = (0, graphQueryFilters_1.v2LiquidityCollection)(this.chainId);
40
+ const queryResult = await (0, graphql_request_1.request)(this.endpoint, queryTopPools(numPools, this.chainId));
41
+ queryResult[resultKey].forEach((pool) => {
42
+ poolsInfo.push(createPoolFromGraph(pool, this.dexId));
43
+ });
44
+ return poolsInfo;
45
+ }
46
+ async getPoolsWithTokenPair(tokenA, tokenB, numPools) {
47
+ const a = tokenA.toLowerCase();
48
+ const b = tokenB.toLowerCase();
49
+ const { resultKey } = (0, graphQueryFilters_1.v2LiquidityCollection)(this.chainId);
50
+ const [resAb, resBa] = await Promise.all([
51
+ (0, graphql_request_1.request)(this.endpoint, queryPairsWithTokenPairDirected(a, b, numPools, this.chainId)),
52
+ (0, graphql_request_1.request)(this.endpoint, queryPairsWithTokenPairDirected(b, a, numPools, this.chainId)),
53
+ ]);
54
+ return mergePairsByVolume([resAb[resultKey], resBa[resultKey]], numPools, this.dexId);
55
+ }
56
+ async getPoolsWithToken(token, numPools) {
57
+ const t = token.toLowerCase();
58
+ const { resultKey } = (0, graphQueryFilters_1.v2LiquidityCollection)(this.chainId);
59
+ const [res0, res1] = await Promise.all([
60
+ (0, graphql_request_1.request)(this.endpoint, queryPairsWithTokenOnSide0(t, numPools, this.chainId)),
61
+ (0, graphql_request_1.request)(this.endpoint, queryPairsWithTokenOnSide1(t, numPools, this.chainId)),
62
+ ]);
63
+ return mergePairsByVolume([res0[resultKey], res1[resultKey]], numPools, this.dexId);
64
+ }
65
+ async getAdditionalPoolDataFromSolidity(poolInfos, rpcProvider) {
66
+ //@ts-ignore
67
+ const UniswapV2HelperContract = (0, UniswapV2Helper_1.CreateUniswapV2HelperContract)(this.chainId, rpcProvider);
68
+ const rawData = await UniswapV2HelperContract.methods.getPoolsData(poolInfos).call();
69
+ const pools = [];
70
+ for (let pool of rawData) {
71
+ const poolId = pool[0];
72
+ const dexId = pool[1];
73
+ const tokensRaw1 = pool[2][0];
74
+ const tokensRaw2 = pool[2][1];
75
+ const token1 = {
76
+ _address: tokensRaw1[0],
77
+ decimals: Number(tokensRaw1[1]),
78
+ name: tokensRaw1[2],
79
+ };
80
+ const token2 = {
81
+ _address: tokensRaw2[0],
82
+ decimals: Number(tokensRaw2[1]),
83
+ name: tokensRaw2[2],
84
+ };
85
+ pools.push(new UniswapV2_1.UniswapV2Pool(pool[0], pool[1], [token1, token2], pool[3]));
86
+ }
87
+ for (const pool of pools)
88
+ // @ts-ignore
89
+ this.myLocalStorage.setItem(pool.poolId.toLowerCase(), pool);
90
+ return pools;
91
+ }
92
+ }
93
+ exports.default = UniswapV2;
94
+ function queryTopPools(numPools, chainId) {
95
+ const { field } = (0, graphQueryFilters_1.v2LiquidityCollection)(chainId);
96
+ return (0, graphql_1.parse)((0, graphql_request_1.gql) `
97
+ {
98
+ ${field}(first : ${numPools}, orderDirection : desc, orderBy : volumeUSD) {
99
+ id,
100
+ volumeUSD,
101
+ token0 {
102
+ id,
103
+ name,
104
+ decimals,
105
+ },
106
+ token1 {
107
+ id,
108
+ name,
109
+ decimals,
110
+ },
111
+ },
112
+ }
113
+ `);
114
+ }
115
+ function queryPairsWithTokenPairDirected(token0Id, token1Id, numPools, chainId) {
116
+ const { field } = (0, graphQueryFilters_1.v2LiquidityCollection)(chainId);
117
+ return (0, graphql_1.parse)((0, graphql_request_1.gql) `
118
+ {
119
+ ${field}(first : ${numPools}, orderDirection : desc, orderBy : volumeUSD, where : {
120
+ ${(0, graphQueryFilters_1.whereDirectedTokenPair)(token0Id, token1Id, chainId)},
121
+ }) {
122
+ id,
123
+ volumeUSD,
124
+ token0 {
125
+ id,
126
+ name,
127
+ decimals,
128
+ },
129
+ token1 {
130
+ id,
131
+ name,
132
+ decimals,
133
+ },
134
+ },
135
+ }
136
+ `);
137
+ }
138
+ function queryPairsWithTokenOnSide0(token, numPools, chainId) {
139
+ const { field } = (0, graphQueryFilters_1.v2LiquidityCollection)(chainId);
140
+ return (0, graphql_1.parse)((0, graphql_request_1.gql) `
141
+ {
142
+ ${field}(first : ${numPools}, orderDirection : desc, orderBy : volumeUSD, where : {
143
+ ${(0, graphQueryFilters_1.whereTokenOnSide)('token0', token, chainId)},
144
+ }) {
145
+ id,
146
+ volumeUSD,
147
+ token0 {
148
+ id,
149
+ name,
150
+ decimals,
151
+ },
152
+ token1 {
153
+ id,
154
+ name,
155
+ decimals,
156
+ },
157
+ },
158
+ }
159
+ `);
160
+ }
161
+ function queryPairsWithTokenOnSide1(token, numPools, chainId) {
162
+ const { field } = (0, graphQueryFilters_1.v2LiquidityCollection)(chainId);
163
+ return (0, graphql_1.parse)((0, graphql_request_1.gql) `
164
+ {
165
+ ${field}(first : ${numPools}, orderDirection : desc, orderBy : volumeUSD, where : {
166
+ ${(0, graphQueryFilters_1.whereTokenOnSide)('token1', token, chainId)},
167
+ }) {
168
+ id,
169
+ volumeUSD,
170
+ token0 {
171
+ id,
172
+ name,
173
+ decimals,
174
+ },
175
+ token1 {
176
+ id,
177
+ name,
178
+ decimals,
179
+ },
180
+ },
181
+ }
182
+ `);
183
+ }
184
+ function mergePairsByVolume(pairLists, numPools, dexId) {
185
+ const byId = new Map();
186
+ for (const list of pairLists) {
187
+ for (const pair of list) {
188
+ byId.set(String(pair.id).toLowerCase(), pair);
189
+ }
190
+ }
191
+ const merged = [...byId.values()];
192
+ merged.sort((a, b) => {
193
+ const va = parseFloat(a.volumeUSD) || 0;
194
+ const vb = parseFloat(b.volumeUSD) || 0;
195
+ return vb - va;
196
+ });
197
+ return merged.slice(0, numPools).map((p) => createPoolFromGraph(p, dexId));
198
+ }
199
+ function createPoolFromGraph(jsonData, dexId) {
200
+ const pool = {
201
+ poolId: jsonData.id,
202
+ dexId: dexId,
203
+ tokens: [
204
+ {
205
+ _address: jsonData.token0.id,
206
+ decimals: Number(jsonData.token0.decimals),
207
+ name: jsonData.token0.name,
208
+ },
209
+ {
210
+ _address: jsonData.token1.id,
211
+ decimals: Number(jsonData.token1.decimals),
212
+ name: jsonData.token1.name,
213
+ },
214
+ ],
215
+ };
216
+ return pool;
217
+ }
@@ -0,0 +1,14 @@
1
+ import { Pool, PoolInfo, DEXGraphFunctionality } from '../../utils/types/types';
2
+ import Web3 from 'web3';
3
+ export default class UniswapV3 implements DEXGraphFunctionality {
4
+ endpoint: string;
5
+ chainId: number;
6
+ dexId: string;
7
+ myLocalStorage: null;
8
+ static initialize(myLocalStorage: any): DEXGraphFunctionality;
9
+ setEndpoint(chainId: number, graphApiKey: string): void;
10
+ getTopPools(numPools: number): Promise<PoolInfo[]>;
11
+ getPoolsWithTokenPair(tokenA: string, tokenB: string, numPools: number): Promise<PoolInfo[]>;
12
+ getPoolsWithToken(token: string, numPools: number): Promise<PoolInfo[]>;
13
+ getAdditionalPoolDataFromSolidity(poolInfos: PoolInfo[], rpcProvider: Web3): Promise<Pool[]>;
14
+ }