@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,150 @@
1
+ import { parse } from 'graphql'
2
+ import { gql, request } from 'graphql-request'
3
+ import { TypedDocumentNode } from '@graphql-typed-document-node/core'
4
+ import { dexIds, balancerWeightedPoolTypes } from '../dexIdsList'
5
+ import { Pool, PoolInfo, DEXGraphFunctionality } from '../../utils/types/types'
6
+ import { BalancerState } from '../pools/Balancer/BalancerState'
7
+ import { myLocalStorage } from '../../swap/my_local_storage'
8
+ import Web3 from 'web3'
9
+
10
+ export default class BalancerV2 implements DEXGraphFunctionality {
11
+ endpoint = ``
12
+ dexId = dexIds.BALANCER_V2
13
+ chainId = 1
14
+ myLocalStorage = null
15
+
16
+ static initialize(myLocalStorage: any): DEXGraphFunctionality {
17
+ const object = new BalancerV2();
18
+ object.myLocalStorage = myLocalStorage;
19
+ return object
20
+ }
21
+
22
+ setEndpoint(chainId: number, graphApiKey: string): void {
23
+ if (chainId == 1) {
24
+ this.endpoint = `https://gateway-arbitrum.network.thegraph.com/api/${graphApiKey}/subgraphs/id/C4ayEZP2yTXRAB8vSaTrgN4m9anTe9Mdm2ViyiAuV9TV`
25
+ }
26
+ if (chainId == 42161) {
27
+ this.endpoint = `https://gateway-arbitrum.network.thegraph.com/api/${graphApiKey}/subgraphs/id/itkjv6Vdh22HtNEPQuk5c9M3T7VeGLQtXxcH8rFi1vc`
28
+ }
29
+ this.chainId = chainId
30
+ }
31
+
32
+ async getTopPools(numPools: number): Promise<PoolInfo[]> {
33
+ const poolsInfo: PoolInfo[] = []
34
+ const queryResult = await request(this.endpoint, queryTopPools(numPools))
35
+ queryResult.pools.forEach((pool: any) => {
36
+ try {
37
+ poolsInfo.push(createPoolFromGraph(pool, this.dexId))
38
+ } catch (e) { }
39
+ })
40
+
41
+ return poolsInfo
42
+ }
43
+
44
+ async getPoolsWithTokenPair(tokenA: string, tokenB: string, numPools: number): Promise<PoolInfo[]> {
45
+ const poolsInfo: PoolInfo[] = []
46
+ const queryResult = await request(this.endpoint, queryPoolsWithTokenPair(tokenA, tokenB, numPools))
47
+ queryResult.pools.forEach((pool: any) => {
48
+ try {
49
+ poolsInfo.push(createPoolFromGraph(pool, this.dexId))
50
+ } catch (e) { }
51
+ })
52
+
53
+ return poolsInfo
54
+ }
55
+
56
+ async getPoolsWithToken(token: string, numPools: number): Promise<PoolInfo[]> {
57
+ const poolsInfo: PoolInfo[] = []
58
+ const queryResult = await request(this.endpoint, queryPoolsWithToken(token, numPools))
59
+ queryResult.pools.forEach((pool: any) => {
60
+ try {
61
+ poolsInfo.push(createPoolFromGraph(pool, this.dexId))
62
+ } catch (e) { }
63
+ })
64
+
65
+ return poolsInfo
66
+ }
67
+
68
+ async getAdditionalPoolDataFromSolidity(poolInfos: PoolInfo[], rpcProvider: Web3): Promise<Pool[]> {
69
+ const pools: Pool[] = await BalancerState.getPoolDataFromContract(poolInfos, this.chainId, rpcProvider)
70
+ for (const pool of pools)
71
+ // @ts-ignore
72
+ this.myLocalStorage.setItem(pool.poolId.toLowerCase(), pool)
73
+ return pools
74
+ }
75
+ }
76
+
77
+ function queryTopPools(numPools: number): TypedDocumentNode<any, Record<string, unknown>> {
78
+ return parse(gql`{
79
+ pools(first: ${numPools}, orderBy: totalLiquidity, orderDirection: desc, where: {totalLiquidity_not: "0"}) {
80
+ id
81
+ name
82
+ poolType
83
+ tokens {
84
+ id
85
+ decimals
86
+ name
87
+ }
88
+ }
89
+ }
90
+ `)
91
+ }
92
+
93
+ function queryPoolsWithTokenPair(tokenA: string, tokenB: string, numPools: number): TypedDocumentNode<any, Record<string, unknown>> {
94
+ return parse(gql`{
95
+ pools(first: ${numPools}, orderBy: totalLiquidity, orderDirection: desc, where: {
96
+ and: [
97
+ {tokens_: {address: "${tokenA.toLowerCase()}"}},
98
+ {tokens_: {address: "${tokenB.toLowerCase()}"}},
99
+ {totalLiquidity_not: "0"}
100
+ ],
101
+ }
102
+ ) {
103
+ id
104
+ poolType
105
+ tokens {
106
+ id
107
+ decimals
108
+ name
109
+ }
110
+ }
111
+ }
112
+ `)
113
+ }
114
+
115
+ function queryPoolsWithToken(token: string, numPools: number): TypedDocumentNode<any, Record<string, unknown>> {
116
+ return parse(gql`{
117
+ pools(first: ${numPools}, orderBy: totalLiquidity, orderDirection: desc, where:
118
+ {
119
+ tokens_: {address_contains: "${token.toLowerCase()}"},
120
+ totalLiquidity_not: "0"
121
+ }
122
+ ) {
123
+ id
124
+ poolType
125
+ tokens {
126
+ id
127
+ decimals
128
+ name
129
+ }
130
+ }
131
+ }
132
+ `)
133
+ }
134
+
135
+ function createPoolFromGraph(jsonData: any, dexId: string): PoolInfo {
136
+ const isWeighted = balancerWeightedPoolTypes.includes(jsonData.poolType)
137
+ if (!isWeighted) throw new Error('BALANCER: Pool type not supported')
138
+ const pool: PoolInfo = {
139
+ poolId: jsonData.id,
140
+ dexId: dexId,
141
+ tokens: jsonData.tokens.map((token: any) => {
142
+ return {
143
+ _address: token.id,
144
+ decimals: token.decimals,
145
+ name: token.name,
146
+ }
147
+ }),
148
+ }
149
+ return pool
150
+ }
@@ -0,0 +1,202 @@
1
+ import { parse } from 'graphql'
2
+ import { gql, request } from 'graphql-request'
3
+ import { TypedDocumentNode } from '@graphql-typed-document-node/core'
4
+ import { Pool, PoolInfo, Token, DEXGraphFunctionality } from '../../utils/types/types'
5
+ import { dexIds } from '../dexIdsList'
6
+ import { CamelotPool } from '../pools/Camelot'
7
+ import { CreateCamelotHelperContract } from '../../contracts/rateX/CamelotHelper'
8
+ import Web3 from 'web3'
9
+
10
+ // Camelot is a silly place
11
+
12
+ export default class CamelotV2 implements DEXGraphFunctionality {
13
+ // Camelot is currently not working, ne
14
+ endpoint = ``
15
+ dexId = dexIds.CAMELOT
16
+ chainId = 1
17
+ myLocalStorage = null
18
+
19
+ static initialize(myLocalStorage: any): DEXGraphFunctionality {
20
+ const object = new CamelotV2();
21
+ object.myLocalStorage = myLocalStorage;
22
+ return object
23
+ }
24
+
25
+ setEndpoint(chainId: number, graphApiKey: string): void {
26
+ if (chainId == 1) {
27
+ this.endpoint = `https://gateway-arbitrum.network.thegraph.com/api/${graphApiKey}/subgraphs/id/8zagLSufxk5cVhzkzai3tyABwJh53zxn9tmUYJcJxijG`
28
+ }
29
+ if (chainId == 42161) {
30
+ this.endpoint = `https://gateway-arbitrum.network.thegraph.com/api/${graphApiKey}/subgraphs/id/8zagLSufxk5cVhzkzai3tyABwJh53zxn9tmUYJcJxijG`
31
+ }
32
+ this.chainId = chainId
33
+ }
34
+
35
+ async getTopPools(numPools: number): Promise<PoolInfo[]> {
36
+ const poolsInfo: PoolInfo[] = []
37
+ const queryResult = await request(this.endpoint, queryTopPools(numPools))
38
+ queryResult.pairs.forEach((lp: any) => {
39
+ poolsInfo.push(createPoolFromGraph(lp, this.dexId))
40
+ })
41
+
42
+ return poolsInfo
43
+ }
44
+
45
+ async getPoolsWithTokenPair(token1: string, token2: string, first: number): Promise<PoolInfo[]> {
46
+ const poolsInfo: PoolInfo[] = []
47
+ const queryResult = await request(this.endpoint, queryPoolsWithTokenPair(token1, token2, first))
48
+ queryResult.pairs.forEach((lp: any) => {
49
+ poolsInfo.push(createPoolFromGraph(lp, this.dexId))
50
+ })
51
+
52
+ return poolsInfo
53
+ }
54
+
55
+ async getPoolsWithToken(token: string, numPools: number): Promise<PoolInfo[]> {
56
+ const poolsInfo: PoolInfo[] = []
57
+ const queryResult = await request(this.endpoint, queryPoolsWithToken(token, numPools))
58
+ queryResult.pairs.forEach((lp: any) => {
59
+ poolsInfo.push(createPoolFromGraph(lp, this.dexId))
60
+ })
61
+
62
+ return poolsInfo
63
+ }
64
+
65
+ // call to Solidity for additional data
66
+ async getAdditionalPoolDataFromSolidity(poolInfos: PoolInfo[], rpcProvider: Web3): Promise<Pool[]> {
67
+ const CamelotHelperContract = CreateCamelotHelperContract(this.chainId, rpcProvider)
68
+ //@ts-ignore
69
+ const rawData: any[][] = await CamelotHelperContract.methods.getPoolsData(poolInfos).call()
70
+
71
+ const pools: Pool[] = []
72
+ for (let pool of rawData) {
73
+ const poolId = pool[0]
74
+ const dexId = pool[1]
75
+ const tokensRaw1 = pool[2][0]
76
+ const tokensRaw2 = pool[2][1]
77
+
78
+ const token1: Token = {
79
+ _address: tokensRaw1[0],
80
+ decimals: Number(tokensRaw1[1]),
81
+ }
82
+
83
+ const token2: Token = {
84
+ _address: tokensRaw2[0],
85
+ decimals: Number(tokensRaw2[1]),
86
+ }
87
+
88
+ const reserves = [BigInt(pool[3][0]), BigInt(pool[3][1])]
89
+ const fees = [BigInt(pool[4][0]), BigInt(pool[4][1])]
90
+ const stableSwap = pool[5]
91
+
92
+ // do not include pools with no liquidity
93
+ if (reserves[0] === BigInt(0) || reserves[1] === BigInt(0)) {
94
+ continue
95
+ }
96
+
97
+ pools.push(new CamelotPool(poolId, dexId, [token1, token2], reserves, fees, stableSwap))
98
+ }
99
+ for (const pool of pools)
100
+ // @ts-ignore
101
+ this.myLocalStorage.setItem(pool.poolId.toLowerCase(), pool)
102
+ return pools
103
+ }
104
+ }
105
+
106
+ function queryTopPools(numPools: number): TypedDocumentNode<any, Record<string, unknown>> {
107
+ return parse(gql`
108
+ {
109
+ pairs(first: ${numPools}, orderDirection: desc, orderBy: volumeUSD) {
110
+ id
111
+ token0 {
112
+ id
113
+ name
114
+ decimals
115
+ }
116
+ token1 {
117
+ id
118
+ name
119
+ decimals
120
+ }
121
+ }
122
+ }
123
+ `)
124
+ }
125
+
126
+ function queryPoolsWithTokenPair(tokenA: string, tokenB: string, numPools: number): TypedDocumentNode<any, Record<string, unknown>> {
127
+ return parse(gql`
128
+ {
129
+ pairs(first: ${numPools}, orderDirection: desc, orderBy: volumeUSD, where: {
130
+ or: [
131
+ {and: [
132
+ {token0_: {id: "${tokenA.toLowerCase()}"}},
133
+ {token1_: {id: "${tokenB.toLowerCase()}"}}
134
+ ]},
135
+ {and: [
136
+ {token0_: {id: "${tokenB.toLowerCase()}"}},
137
+ {token1_: {id: "${tokenA.toLowerCase()}"}}
138
+ ]}
139
+ ]
140
+ }) {
141
+ id
142
+ volumeUSD
143
+ token0 {
144
+ id
145
+ name
146
+ decimals
147
+ }
148
+ token1 {
149
+ id
150
+ name
151
+ decimals
152
+ }
153
+ }
154
+ }
155
+ `)
156
+ }
157
+
158
+ function queryPoolsWithToken(token: string, numPools: number): TypedDocumentNode<any, Record<string, unknown>> {
159
+ return parse(gql`
160
+ {
161
+ pairs(first: ${numPools}, orderDirection: desc, orderBy: volumeUSD, where: {
162
+ or: [
163
+ {token0_: {id: "${token.toLowerCase()}"}},
164
+ {token1_: {id: "${token.toLowerCase()}"}}
165
+ ]
166
+ }) {
167
+ id
168
+ volumeUSD
169
+ token0 {
170
+ id
171
+ name
172
+ decimals
173
+ }
174
+ token1 {
175
+ id
176
+ name
177
+ decimals
178
+ }
179
+ }
180
+ }
181
+ `)
182
+ }
183
+
184
+ function createPoolFromGraph(jsonData: any, dexId: string): PoolInfo {
185
+ const pool: PoolInfo = {
186
+ poolId: jsonData.id,
187
+ dexId: dexId,
188
+ tokens: [
189
+ {
190
+ _address: jsonData.token0.id,
191
+ decimals: jsonData.token0.decimals,
192
+ name: jsonData.token0.name,
193
+ },
194
+ {
195
+ _address: jsonData.token1.id,
196
+ decimals: jsonData.token1.decimals,
197
+ name: jsonData.token1.name,
198
+ },
199
+ ],
200
+ }
201
+ return pool
202
+ }
@@ -0,0 +1,283 @@
1
+ import { parse } from 'graphql'
2
+ import { gql, request } from 'graphql-request'
3
+ import { TypedDocumentNode } from '@graphql-typed-document-node/core'
4
+ import { dexIds } from '../dexIdsList'
5
+ import { Pool, PoolInfo, Token, DEXGraphFunctionality } from '../../utils/types/types'
6
+ import { SushiSwapV2Pool } from '../pools/SushiSwapV2'
7
+ import Web3 from 'web3'
8
+ import { CreateSushiSwapHelperContract } from '../../contracts/rateX/SushiSwapHelper'
9
+
10
+
11
+ export default class SushiSwapV2 implements DEXGraphFunctionality {
12
+ endpoint = ""
13
+ dexId = dexIds.SUSHI_V2
14
+ chainId = 1
15
+ myLocalStorage = null
16
+
17
+ static initialize(myLocalStorage: any): DEXGraphFunctionality {
18
+ const object = new SushiSwapV2();
19
+ object.myLocalStorage = myLocalStorage;
20
+ return object
21
+ }
22
+
23
+ setEndpoint(chainId: number, graphApiKey: string): void {
24
+ if (chainId == 1) {
25
+ this.endpoint = `https://gateway-arbitrum.network.thegraph.com/api/${graphApiKey}/subgraphs/id/77jZ9KWeyi3CJ96zkkj5s1CojKPHt6XJKjLFzsDCd8Fd`
26
+ }
27
+ if (chainId == 42161) {
28
+ this.endpoint = `https://gateway-arbitrum.network.thegraph.com/api/${graphApiKey}/subgraphs/id/8yBXBTMfdhsoE5QCf7KnoPmQb7QAWtRzESfYjiCjGEM9`
29
+ }
30
+ this.chainId = chainId
31
+ }
32
+
33
+ async getTopPools(numPools: number): Promise<PoolInfo[]> {
34
+ const poolsInfo: PoolInfo[] = []
35
+ const queryResult = await request(this.endpoint, queryTopPools(numPools, this.chainId))
36
+ if (this.chainId == 1) {
37
+ queryResult.liquidityPools.forEach((pool: any) => {
38
+ poolsInfo.push(createPoolFromGraph(pool, this.dexId, this.chainId))
39
+ })
40
+ } else {
41
+ queryResult.pairs.forEach((pool: any) => {
42
+ poolsInfo.push(createPoolFromGraph(pool, this.dexId, this.chainId))
43
+ })
44
+ }
45
+
46
+ return poolsInfo
47
+ }
48
+
49
+ async getPoolsWithTokenPair(tokenA: string, tokenB: string, numPools: number): Promise<PoolInfo[]> {
50
+ const poolsInfo: PoolInfo[] = []
51
+ const queryResult = await request(this.endpoint, queryPoolsWithTokenPair(tokenA, tokenB, numPools, this.chainId))
52
+ if (this.chainId == 1) {
53
+ queryResult.liquidityPools.forEach((pool: any) => {
54
+ poolsInfo.push(createPoolFromGraph(pool, this.dexId, this.chainId))
55
+ })
56
+ } else {
57
+ queryResult.pairs.forEach((pool: any) => {
58
+ poolsInfo.push(createPoolFromGraph(pool, this.dexId, this.chainId))
59
+ })
60
+ }
61
+
62
+ return poolsInfo
63
+ }
64
+
65
+ async getPoolsWithToken(token: string, numPools: number): Promise<PoolInfo[]> {
66
+ const poolsInfo: PoolInfo[] = []
67
+ const queryResult = await request(this.endpoint, queryPoolsWithToken(token, numPools, this.chainId))
68
+ if (this.chainId == 1) {
69
+ queryResult.liquidityPools.forEach((pool: any) => {
70
+ poolsInfo.push(createPoolFromGraph(pool, this.dexId, this.chainId))
71
+ })
72
+ } else {
73
+ queryResult.pairs.forEach((pool: any) => {
74
+ poolsInfo.push(createPoolFromGraph(pool, this.dexId, this.chainId))
75
+ })
76
+ }
77
+
78
+ return poolsInfo
79
+ }
80
+
81
+ async getAdditionalPoolDataFromSolidity(poolInfos: PoolInfo[], rpcProvider: Web3): Promise<Pool[]> {
82
+ //@ts-ignore
83
+ const SushiSwapHelperContract = CreateSushiSwapHelperContract(this.chainId, rpcProvider)
84
+ const rawData: any[][] = await SushiSwapHelperContract.methods.getPoolsData(poolInfos).call()
85
+
86
+ const pools: Pool[] = []
87
+ for (let pool of rawData) {
88
+ const poolId = pool[0]
89
+ const dexId = pool[1]
90
+ const tokensRaw1 = pool[2][0]
91
+ const tokensRaw2 = pool[2][1]
92
+
93
+ const token1: Token = {
94
+ _address: tokensRaw1[0],
95
+ decimals: Number(tokensRaw1[1]),
96
+ name: tokensRaw1[2],
97
+ }
98
+
99
+ const token2: Token = {
100
+ _address: tokensRaw2[0],
101
+ decimals: Number(tokensRaw2[1]),
102
+ name: tokensRaw2[2],
103
+ }
104
+
105
+ pools.push(new SushiSwapV2Pool(pool[0], pool[1], [token1, token2], pool[3]))
106
+ }
107
+ for (const pool of pools)
108
+ // @ts-ignore
109
+ this.myLocalStorage.setItem(pool.poolId.toLowerCase(), pool)
110
+ return pools
111
+ }
112
+ }
113
+
114
+ function queryTopPools(numPools: number, chainId: number): TypedDocumentNode<any, Record<string, unknown>> {
115
+ if (chainId == 1) {
116
+ return parse(gql`
117
+ {
118
+ liquidityPools(first:${numPools}, orderDirection: desc, orderBy: cumulativeVolumeUSD) {
119
+ id
120
+ cumulativeVolumeUSD
121
+ inputTokens {
122
+ id
123
+ decimals
124
+ name
125
+ }
126
+ }
127
+ }
128
+ `)
129
+ }
130
+ return parse(gql`
131
+ {
132
+ pairs(first: ${numPools}, orderDirection: desc, orderBy: volumeUSD) {
133
+ id
134
+ volumeUSD
135
+ token0 {
136
+ id
137
+ name
138
+ decimals
139
+ }
140
+ token1 {
141
+ id
142
+ name
143
+ decimals
144
+ }
145
+ }
146
+ }
147
+ `)
148
+ }
149
+
150
+ function queryPoolsWithTokenPair(
151
+ tokenA: string,
152
+ tokenB: string,
153
+ numPools: number,
154
+ chainId: number
155
+ ): TypedDocumentNode<any, Record<string, unknown>> {
156
+ if (chainId == 1) {
157
+ return parse(gql`{
158
+ liquidityPools(first: ${numPools}, orderDirection: desc, orderBy: cumulativeVolumeUSD, where: {
159
+ and: [
160
+ {inputTokens_: {id: "${tokenA.toLowerCase()}"}},
161
+ {inputTokens_: {id: "${tokenB.toLowerCase()}"}}
162
+ ]
163
+ }
164
+ ) {
165
+ id
166
+ cumulativeVolumeUSD
167
+ inputTokens {
168
+ id
169
+ decimals
170
+ name
171
+ }
172
+ }
173
+ }`)
174
+ }
175
+
176
+ return parse(gql`
177
+ {
178
+ pairs(first: ${numPools}, orderDirection: desc, orderBy: volumeUSD, where: {
179
+ or: [
180
+ {and: [
181
+ {token0_: {id: "${tokenA.toLowerCase()}"}},
182
+ {token1_: {id: "${tokenB.toLowerCase()}"}}
183
+ ]},
184
+ {and: [
185
+ {token0_: {id: "${tokenB.toLowerCase()}"}},
186
+ {token1_: {id: "${tokenA.toLowerCase()}"}}
187
+ ]}
188
+ ]
189
+ }) {
190
+ id
191
+ volumeUSD
192
+ token0 {
193
+ id
194
+ name
195
+ decimals
196
+ }
197
+ token1 {
198
+ id
199
+ name
200
+ decimals
201
+ }
202
+ }
203
+ }
204
+ `)
205
+ }
206
+
207
+ function queryPoolsWithToken(token: string, numPools: number, chainId: number): TypedDocumentNode<any, Record<string, unknown>> {
208
+ if (chainId == 1) {
209
+ return parse(gql`{
210
+ liquidityPools(first: ${numPools}, orderDirection: desc, orderBy: cumulativeVolumeUSD, where: {
211
+ inputTokens_: { id: "${token.toLowerCase()}" }
212
+ }
213
+ ) {
214
+ id
215
+ cumulativeVolumeUSD
216
+ inputTokens {
217
+ id
218
+ decimals
219
+ name
220
+ }
221
+ }
222
+ }`)
223
+ }
224
+
225
+ return parse(gql`
226
+ {
227
+ pairs(first: ${numPools}, orderDirection: desc, orderBy: volumeUSD, where: {
228
+ or: [
229
+ {token0_: {id: "${token.toLowerCase()}"}},
230
+ {token1_: {id: "${token.toLowerCase()}"}}
231
+ ]
232
+ }) {
233
+ id
234
+ volumeUSD
235
+ token0 {
236
+ id
237
+ name
238
+ decimals
239
+ }
240
+ token1 {
241
+ id
242
+ name
243
+ decimals
244
+ }
245
+ }
246
+ }
247
+ `)
248
+ }
249
+
250
+ function createPoolFromGraph(jsonData: any, dexId: string, chainId: number): PoolInfo {
251
+ let pool: PoolInfo
252
+ if (chainId == 1) {
253
+ pool = {
254
+ poolId: jsonData.id,
255
+ dexId: dexId,
256
+ tokens: jsonData.inputTokens.map((token: any, index: any) => {
257
+ return {
258
+ _address: token.id,
259
+ decimals: token.decimals,
260
+ name: token.name,
261
+ }
262
+ }),
263
+ }
264
+ } else {
265
+ pool = {
266
+ poolId: jsonData.id,
267
+ dexId: dexId,
268
+ tokens: [
269
+ {
270
+ _address: jsonData.token0.id,
271
+ decimals: jsonData.token0.decimals,
272
+ name: jsonData.token0.name,
273
+ },
274
+ {
275
+ _address: jsonData.token1.id,
276
+ decimals: jsonData.token1.decimals,
277
+ name: jsonData.token1.name,
278
+ },
279
+ ],
280
+ }
281
+ }
282
+ return pool
283
+ }