@paraswap/dex-lib 4.0.1 → 4.0.3-Cables.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 (211) hide show
  1. package/.idea/aws.xml +17 -0
  2. package/.idea/codeStyles/Project.xml +19 -0
  3. package/.idea/codeStyles/codeStyleConfig.xml +5 -0
  4. package/.idea/misc.xml +6 -0
  5. package/.idea/modules.xml +8 -0
  6. package/.idea/paraswap-dex-lib.iml +9 -0
  7. package/.idea/prettier.xml +7 -0
  8. package/.idea/vcs.xml +6 -0
  9. package/build/abi/integral/factory.json +333 -0
  10. package/build/abi/integral/oracle.json +501 -0
  11. package/build/abi/{inception/inception-ineth-pool.json → integral/pool.json} +522 -543
  12. package/build/abi/{wombat/pool.json → integral/relayer.json} +720 -473
  13. package/build/dex/aave-v1/aave-v1.d.ts +1 -2
  14. package/build/dex/aave-v1/aave-v1.js +0 -23
  15. package/build/dex/aave-v1/aave-v1.js.map +1 -1
  16. package/build/dex/balancer-v3/balancer-v3.d.ts +3 -0
  17. package/build/dex/balancer-v3/balancer-v3.js +10 -1
  18. package/build/dex/balancer-v3/balancer-v3.js.map +1 -1
  19. package/build/dex/bebop/bebop.js +1 -1
  20. package/build/dex/bebop/bebop.js.map +1 -1
  21. package/build/dex/cables/cables.d.ts +1 -0
  22. package/build/dex/cables/cables.js +145 -88
  23. package/build/dex/cables/cables.js.map +1 -1
  24. package/build/dex/cables/constants.js +1 -1
  25. package/build/dex/cables/constants.js.map +1 -1
  26. package/build/dex/cables/rate-fetcher.d.ts +1 -0
  27. package/build/dex/cables/rate-fetcher.js +30 -10
  28. package/build/dex/cables/rate-fetcher.js.map +1 -1
  29. package/build/dex/cables/types.d.ts +16 -15
  30. package/build/dex/cables/types.js +2 -5
  31. package/build/dex/cables/types.js.map +1 -1
  32. package/build/dex/dexalot/dexalot.js.map +1 -1
  33. package/build/dex/fluid-dex/fluid-dex-generate-pool.d.ts +49 -0
  34. package/build/dex/fluid-dex/fluid-dex-generate-pool.js +104 -0
  35. package/build/dex/fluid-dex/fluid-dex-generate-pool.js.map +1 -0
  36. package/build/dex/index.js +2 -0
  37. package/build/dex/index.js.map +1 -1
  38. package/build/dex/integral/config.d.ts +4 -0
  39. package/build/dex/integral/config.js +20 -0
  40. package/build/dex/integral/config.js.map +1 -0
  41. package/build/dex/integral/context.d.ts +40 -0
  42. package/build/dex/integral/context.js +158 -0
  43. package/build/dex/integral/context.js.map +1 -0
  44. package/build/dex/integral/integral-factory.d.ts +24 -0
  45. package/build/dex/integral/integral-factory.js +95 -0
  46. package/build/dex/integral/integral-factory.js.map +1 -0
  47. package/build/dex/integral/integral-pool.d.ts +50 -0
  48. package/build/dex/integral/integral-pool.js +149 -0
  49. package/build/dex/integral/integral-pool.js.map +1 -0
  50. package/build/dex/integral/integral-pricing.d.ts +18 -0
  51. package/build/dex/integral/integral-pricing.js +114 -0
  52. package/build/dex/integral/integral-pricing.js.map +1 -0
  53. package/build/dex/integral/integral-relayer.d.ts +42 -0
  54. package/build/dex/integral/integral-relayer.js +192 -0
  55. package/build/dex/integral/integral-relayer.js.map +1 -0
  56. package/build/dex/integral/integral-token.d.ts +27 -0
  57. package/build/dex/integral/integral-token.js +59 -0
  58. package/build/dex/integral/integral-token.js.map +1 -0
  59. package/build/dex/integral/integral.d.ts +41 -0
  60. package/build/dex/integral/integral.js +376 -0
  61. package/build/dex/integral/integral.js.map +1 -0
  62. package/build/dex/integral/types.d.ts +85 -0
  63. package/build/dex/integral/types.js +9 -0
  64. package/build/dex/integral/types.js.map +1 -0
  65. package/build/dex/integral/utils-e2e.d.ts +9 -0
  66. package/build/dex/integral/utils-e2e.js +131 -0
  67. package/build/dex/integral/utils-e2e.js.map +1 -0
  68. package/build/dex/integral/utils.d.ts +17 -0
  69. package/build/dex/integral/utils.js +94 -0
  70. package/build/dex/integral/utils.js.map +1 -0
  71. package/build/dex/jarvis.d.ts +1 -2
  72. package/build/dex/jarvis.js +0 -58
  73. package/build/dex/jarvis.js.map +1 -1
  74. package/build/dex/platypus/platypus.d.ts +1 -2
  75. package/build/dex/platypus/platypus.js +0 -45
  76. package/build/dex/platypus/platypus.js.map +1 -1
  77. package/build/dex/stable-pool.js +8 -1
  78. package/build/dex/stable-pool.js.map +1 -1
  79. package/build/dex/swaap-v1/swaap-v1.d.ts +1 -2
  80. package/build/dex/swaap-v1/swaap-v1.js +0 -14
  81. package/build/dex/swaap-v1/swaap-v1.js.map +1 -1
  82. package/build/dex/trader-joe-v2.1/optimizer.d.ts +2 -0
  83. package/build/dex/trader-joe-v2.1/optimizer.js +47 -0
  84. package/build/dex/trader-joe-v2.1/optimizer.js.map +1 -0
  85. package/build/dex/zerox/index.d.ts +26 -4
  86. package/build/dex/zerox/index.js +42 -50
  87. package/build/dex/zerox/index.js.map +1 -1
  88. package/build/dex/zerox/types.d.ts +0 -29
  89. package/build/dex/zerox/types.js +1 -8
  90. package/build/dex/zerox/types.js.map +1 -1
  91. package/build/executor/Executor01BytecodeBuilder.js +11 -6
  92. package/build/executor/Executor01BytecodeBuilder.js.map +1 -1
  93. package/build/executor/Executor02BytecodeBuilder.js +11 -6
  94. package/build/executor/Executor02BytecodeBuilder.js.map +1 -1
  95. package/build/executor/Executor03BytecodeBuilder.js +11 -6
  96. package/build/executor/Executor03BytecodeBuilder.js.map +1 -1
  97. package/build/types.d.ts +1 -0
  98. package/package.json +1 -1
  99. package/src/abi/cables/CablesMainnetRFQ.json +1083 -0
  100. package/src/dex/balancer-v3/balancer-v3.ts +14 -1
  101. package/src/dex/bebop/bebop.ts +1 -1
  102. package/src/dex/cables/cables-e2e.test.ts +207 -0
  103. package/src/dex/cables/cables-integration.test.ts +391 -0
  104. package/src/dex/cables/cables.ts +1006 -0
  105. package/src/dex/cables/config.ts +13 -0
  106. package/src/dex/cables/constants.ts +35 -0
  107. package/src/dex/cables/rate-fetcher.ts +219 -0
  108. package/src/dex/cables/types.ts +130 -0
  109. package/src/dex/cables/validators.test.ts +151 -0
  110. package/src/dex/cables/validators.ts +61 -0
  111. package/src/dex/dexalot/dexalot.ts +1 -0
  112. package/src/dex/index.ts +2 -0
  113. package/src/executor/Executor01BytecodeBuilder.ts +15 -11
  114. package/src/executor/Executor02BytecodeBuilder.ts +13 -9
  115. package/src/executor/Executor03BytecodeBuilder.ts +16 -9
  116. package/src/types.ts +1 -0
  117. package/tests/constants-e2e.ts +2 -2
  118. package/.vscode/launch.json +0 -53
  119. package/.vscode/settings.json +0 -2
  120. package/.vscode/tasks.json +0 -15
  121. package/build/abi/inception/inception-ratio-feed.json +0 -329
  122. package/build/abi/inception/inception-vault.json +0 -991
  123. package/build/abi/uniswap-v3/AlienBaseV3Router.abi.json +0 -52
  124. package/build/abi/usual-m-smart-m/usualM.abi.json +0 -483
  125. package/build/abi/usual-m-usd0/usualCollateralDao.abi.json +0 -681
  126. package/build/dex/aave-v2/tokens-avalanche.json +0 -44
  127. package/build/dex/aave-v2/tokens-mainnet.json +0 -188
  128. package/build/dex/aave-v2/tokens-polygon.json +0 -44
  129. package/build/dex/balancer-v3/abi/balancerBatchRouter.d.ts +0 -729
  130. package/build/dex/balancer-v3/abi/balancerBatchRouter.js +0 -732
  131. package/build/dex/balancer-v3/abi/balancerBatchRouter.js.map +0 -1
  132. package/build/dex/balancer-v3/abi/balancerRouter.d.ts +0 -1406
  133. package/build/dex/balancer-v3/abi/balancerRouter.js +0 -1175
  134. package/build/dex/balancer-v3/abi/balancerRouter.js.map +0 -1
  135. package/build/dex/balancer-v3/abi/vaultExtension.V3.d.ts +0 -2081
  136. package/build/dex/balancer-v3/abi/vaultExtension.V3.js +0 -1851
  137. package/build/dex/balancer-v3/abi/vaultExtension.V3.js.map +0 -1
  138. package/build/dex/balancer-v3/balancer-v3-sepolia-pools.d.ts +0 -34
  139. package/build/dex/balancer-v3/balancer-v3-sepolia-pools.js +0 -406
  140. package/build/dex/balancer-v3/balancer-v3-sepolia-pools.js.map +0 -1
  141. package/build/dex/inception/config.d.ts +0 -4
  142. package/build/dex/inception/config.js +0 -109
  143. package/build/dex/inception/config.js.map +0 -1
  144. package/build/dex/inception/inception-event-pool.d.ts +0 -16
  145. package/build/dex/inception/inception-event-pool.js +0 -49
  146. package/build/dex/inception/inception-event-pool.js.map +0 -1
  147. package/build/dex/inception/inception.d.ts +0 -43
  148. package/build/dex/inception/inception.js +0 -180
  149. package/build/dex/inception/inception.js.map +0 -1
  150. package/build/dex/inception/tokens.d.ts +0 -9
  151. package/build/dex/inception/tokens.js +0 -28
  152. package/build/dex/inception/tokens.js.map +0 -1
  153. package/build/dex/inception/types.d.ts +0 -22
  154. package/build/dex/inception/types.js +0 -3
  155. package/build/dex/inception/types.js.map +0 -1
  156. package/build/dex/inception/utils.d.ts +0 -7
  157. package/build/dex/inception/utils.js +0 -58
  158. package/build/dex/inception/utils.js.map +0 -1
  159. package/build/dex/maker-psm/scripts/gem.abi.json +0 -136
  160. package/build/dex/maker-psm/scripts/validate-state.d.ts +0 -0
  161. package/build/dex/maker-psm/scripts/validate-state.js +0 -185
  162. package/build/dex/maker-psm/scripts/validate-state.js.map +0 -1
  163. package/build/dex/maker-psm/scripts/vat.abi.json +0 -243
  164. package/build/dex/solidly-v3/scripts/check-event-pool-event.d.ts +0 -1
  165. package/build/dex/solidly-v3/scripts/check-event-pool-event.js +0 -53
  166. package/build/dex/solidly-v3/scripts/check-event-pool-event.js.map +0 -1
  167. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.d.ts +0 -17
  168. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js +0 -64
  169. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js.map +0 -1
  170. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.d.ts +0 -17
  171. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js +0 -79
  172. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js.map +0 -1
  173. package/build/dex/uniswap-v3/uniswap-v3-pricing.d.ts +0 -4
  174. package/build/dex/uniswap-v3/uniswap-v3-pricing.js +0 -43
  175. package/build/dex/uniswap-v3/uniswap-v3-pricing.js.map +0 -1
  176. package/build/dex/uniswap-v3/uniswap-v3-worker.d.ts +0 -1
  177. package/build/dex/uniswap-v3/uniswap-v3-worker.js +0 -10
  178. package/build/dex/uniswap-v3/uniswap-v3-worker.js.map +0 -1
  179. package/build/dex/usual/types.d.ts +0 -13
  180. package/build/dex/usual/types.js +0 -3
  181. package/build/dex/usual/types.js.map +0 -1
  182. package/build/dex/usual/usual-bond.d.ts +0 -18
  183. package/build/dex/usual/usual-bond.js +0 -52
  184. package/build/dex/usual/usual-bond.js.map +0 -1
  185. package/build/dex/usual/usual-m-smart-m.d.ts +0 -17
  186. package/build/dex/usual/usual-m-smart-m.js +0 -50
  187. package/build/dex/usual/usual-m-smart-m.js.map +0 -1
  188. package/build/dex/usual/usual-m-usd0.d.ts +0 -17
  189. package/build/dex/usual/usual-m-usd0.js +0 -51
  190. package/build/dex/usual/usual-m-usd0.js.map +0 -1
  191. package/build/dex/usual/usual.d.ts +0 -27
  192. package/build/dex/usual/usual.js +0 -125
  193. package/build/dex/usual/usual.js.map +0 -1
  194. package/build/dex/usual-m-smart-m/config.d.ts +0 -3
  195. package/build/dex/usual-m-smart-m/config.js +0 -13
  196. package/build/dex/usual-m-smart-m/config.js.map +0 -1
  197. package/build/dex/usual-m-smart-m/types.d.ts +0 -7
  198. package/build/dex/usual-m-smart-m/types.js +0 -3
  199. package/build/dex/usual-m-smart-m/types.js.map +0 -1
  200. package/build/dex/usual-m-smart-m/usual-m-smart-m.d.ts +0 -35
  201. package/build/dex/usual-m-smart-m/usual-m-smart-m.js +0 -161
  202. package/build/dex/usual-m-smart-m/usual-m-smart-m.js.map +0 -1
  203. package/build/dex/usual-m-usd0/config.d.ts +0 -3
  204. package/build/dex/usual-m-usd0/config.js +0 -14
  205. package/build/dex/usual-m-usd0/config.js.map +0 -1
  206. package/build/dex/usual-m-usd0/types.d.ts +0 -8
  207. package/build/dex/usual-m-usd0/types.js +0 -3
  208. package/build/dex/usual-m-usd0/types.js.map +0 -1
  209. package/build/dex/usual-m-usd0/usual-m-usd0.d.ts +0 -35
  210. package/build/dex/usual-m-usd0/usual-m-usd0.js +0 -159
  211. package/build/dex/usual-m-usd0/usual-m-usd0.js.map +0 -1
@@ -0,0 +1,13 @@
1
+ import { Network } from '../../constants';
2
+ import { DexConfigMap } from '../../types';
3
+
4
+ export const CablesConfig: DexConfigMap<{ mainnetRFQAddress: string }> = {
5
+ Cables: {
6
+ [Network.AVALANCHE]: {
7
+ mainnetRFQAddress: '0xfA12DCB2e1FD72bD92E8255Db6A781b2c76adC20',
8
+ },
9
+ [Network.ARBITRUM]: {
10
+ mainnetRFQAddress: '0xfA12DCB2e1FD72bD92E8255Db6A781b2c76adC20',
11
+ },
12
+ },
13
+ };
@@ -0,0 +1,35 @@
1
+ import BigNumber from 'bignumber.js';
2
+
3
+ /**
4
+ * Cables
5
+ */
6
+ export const CABLES_API_URL =
7
+ 'https://cables-evm-rfq-service.cryptosrvc.com/v1';
8
+
9
+ export const CABLES_PRICES_CACHES_TTL_S = 10;
10
+ export const CABLES_API_PRICES_POLLING_INTERVAL_MS = 2000; // 2 sec
11
+
12
+ export const CABLES_PAIRS_CACHES_TTL_S = 12;
13
+ export const CABLES_API_PAIRS_POLLING_INTERVAL_MS = 10000; // 10 sec
14
+
15
+ export const CABLES_BLACKLIST_CACHES_TTL_S = 60;
16
+ export const CABLES_API_BLACKLIST_POLLING_INTERVAL_MS = 30000; // 30 sec
17
+
18
+ export const CABLES_TOKENS_CACHES_TTL_S = 60;
19
+ export const CABLES_API_TOKENS_POLLING_INTERVAL_MS = 30000; // 30 sec
20
+
21
+ export const CABLES_FIRM_QUOTE_TIMEOUT_MS = 2000;
22
+
23
+ export const CABLES_BLACKLIST_CACHE_KEY = 'cablesBlacklistCacheKey';
24
+
25
+ export const CABLES_RESTRICTED_CACHE_KEY = 'restricted';
26
+
27
+ export const CABLES_ERRORS_CACHE_KEY = 'errors';
28
+
29
+ export const CABLES_RESTRICT_CHECK_INTERVAL_MS = 1000 * 60 * 3; // 3 min
30
+
31
+ export const CABLES_RESTRICT_COUNT_THRESHOLD = 3;
32
+
33
+ export const CABLES_RESTRICT_TTL_S = 10 * 60; // 10 min
34
+
35
+ export const CABLES_GAS_COST = 120_000;
@@ -0,0 +1,219 @@
1
+ import { Network } from '../../constants';
2
+ import { IDexHelper } from '../../dex-helper';
3
+ import { Fetcher } from '../../lib/fetcher/fetcher';
4
+ import { validateAndCast } from '../../lib/validators';
5
+ import { Logger, Token } from '../../types';
6
+ import { PairData } from '../cables/types';
7
+ import {
8
+ CablesBlacklistResponse,
9
+ CablesPairsResponse,
10
+ CablesPricesResponse,
11
+ CablesRateFetcherConfig,
12
+ CablesTokensResponse,
13
+ } from './types';
14
+ import {
15
+ blacklistResponseValidator,
16
+ pairsResponseValidator,
17
+ pricesResponseValidator,
18
+ tokensResponseValidator,
19
+ } from './validators';
20
+
21
+ export class CablesRateFetcher {
22
+ public tokensFetcher: Fetcher<CablesTokensResponse>;
23
+ public tokensCacheKey: string;
24
+ public tokensCacheTTL: number;
25
+
26
+ public pairsFetcher: Fetcher<CablesPairsResponse>;
27
+ public pairsCacheKey: string;
28
+ public pairsCacheTTL: number;
29
+
30
+ public pricesFetcher: Fetcher<CablesPricesResponse>;
31
+ public pricesCacheKey: string;
32
+ public pricesCacheTTL: number;
33
+
34
+ public blacklistFetcher: Fetcher<CablesBlacklistResponse>;
35
+ public blacklistCacheKey: string;
36
+ public blacklistCacheTTL: number;
37
+
38
+ tokensHandleResponseCallback: () => Promise<void>;
39
+
40
+ constructor(
41
+ private dexHelper: IDexHelper,
42
+ private dexKey: string,
43
+ private network: Network,
44
+ private logger: Logger,
45
+ config: CablesRateFetcherConfig,
46
+ ) {
47
+ this.tokensCacheKey = config.rateConfig.tokensCacheKey;
48
+ this.tokensCacheTTL = config.rateConfig.tokensCacheTTLSecs;
49
+
50
+ this.pairsCacheKey = config.rateConfig.pairsCacheKey;
51
+ this.pairsCacheTTL = config.rateConfig.pairsCacheTTLSecs;
52
+
53
+ this.pricesCacheKey = config.rateConfig.pricesCacheKey;
54
+ this.pricesCacheTTL = config.rateConfig.pricesCacheTTLSecs;
55
+
56
+ this.blacklistCacheKey = config.rateConfig.blacklistCacheKey;
57
+ this.blacklistCacheTTL = config.rateConfig.blacklistCacheTTLSecs;
58
+
59
+ this.tokensHandleResponseCallback =
60
+ config.rateConfig.tokensHandleResponseCallback;
61
+
62
+ this.pairsFetcher = new Fetcher<CablesPairsResponse>(
63
+ dexHelper.httpRequest,
64
+ {
65
+ info: {
66
+ requestOptions: config.rateConfig.pairsReqParams,
67
+ caster: (data: unknown) => {
68
+ return validateAndCast<CablesPairsResponse>(
69
+ data,
70
+ pairsResponseValidator,
71
+ );
72
+ },
73
+ },
74
+ handler: this.handlePairsResponse.bind(this),
75
+ },
76
+ config.rateConfig.pairsIntervalMs,
77
+ logger,
78
+ );
79
+
80
+ this.pricesFetcher = new Fetcher<CablesPricesResponse>(
81
+ dexHelper.httpRequest,
82
+ {
83
+ info: {
84
+ requestOptions: config.rateConfig.pricesReqParams,
85
+ caster: (data: unknown) => {
86
+ return validateAndCast<CablesPricesResponse>(
87
+ data,
88
+ pricesResponseValidator,
89
+ );
90
+ },
91
+ },
92
+ handler: this.handlePricesResponse.bind(this),
93
+ },
94
+ config.rateConfig.pricesIntervalMs,
95
+ logger,
96
+ );
97
+
98
+ this.blacklistFetcher = new Fetcher<CablesBlacklistResponse>(
99
+ dexHelper.httpRequest,
100
+ {
101
+ info: {
102
+ requestOptions: config.rateConfig.blacklistReqParams,
103
+ caster: (data: unknown) => {
104
+ return validateAndCast<CablesBlacklistResponse>(
105
+ data,
106
+ blacklistResponseValidator,
107
+ );
108
+ },
109
+ },
110
+ handler: this.handleBlacklistResponse.bind(this),
111
+ },
112
+ config.rateConfig.blacklistIntervalMs,
113
+ logger,
114
+ );
115
+
116
+ this.tokensFetcher = new Fetcher<CablesTokensResponse>(
117
+ dexHelper.httpRequest,
118
+ {
119
+ info: {
120
+ requestOptions: config.rateConfig.tokensReqParams,
121
+ caster: (data: unknown) => {
122
+ return validateAndCast<CablesTokensResponse>(
123
+ data,
124
+ tokensResponseValidator,
125
+ );
126
+ },
127
+ },
128
+ handler: this.handleTokensResponse.bind(this),
129
+ },
130
+ config.rateConfig.tokensIntervalMs,
131
+ logger,
132
+ );
133
+ }
134
+
135
+ /**
136
+ * Utils
137
+ */
138
+ start() {
139
+ this.pairsFetcher.startPolling();
140
+ this.pricesFetcher.startPolling();
141
+ this.blacklistFetcher.startPolling();
142
+ this.tokensFetcher.startPolling();
143
+ }
144
+ stop() {
145
+ this.pairsFetcher.stopPolling();
146
+ this.pricesFetcher.stopPolling();
147
+ this.blacklistFetcher.stopPolling();
148
+ this.tokensFetcher.stopPolling();
149
+ }
150
+
151
+ private handlePairsResponse(res: CablesPairsResponse): void {
152
+ const networkId = String(this.network);
153
+ const pairs = res.pairs[networkId];
154
+
155
+ let normalized_pairs: { [token: string]: PairData } = {};
156
+ Object.keys(pairs).forEach(key => {
157
+ normalized_pairs[key.toLowerCase()] = pairs[key];
158
+ });
159
+
160
+ this.dexHelper.cache.setex(
161
+ this.dexKey,
162
+ this.network,
163
+ this.pairsCacheKey,
164
+ this.pairsCacheTTL,
165
+ JSON.stringify(normalized_pairs),
166
+ );
167
+ }
168
+
169
+ private handlePricesResponse(res: CablesPricesResponse): void {
170
+ const networkId = String(this.network);
171
+ const prices = res.prices[networkId];
172
+
173
+ this.dexHelper.cache.setex(
174
+ this.dexKey,
175
+ this.network,
176
+ this.pricesCacheKey,
177
+ this.pricesCacheTTL,
178
+ JSON.stringify(prices),
179
+ );
180
+ }
181
+
182
+ private handleBlacklistResponse(res: CablesBlacklistResponse): void {
183
+ const { blacklist } = res;
184
+ this.dexHelper.cache.setex(
185
+ this.dexKey,
186
+ this.network,
187
+ this.blacklistCacheKey,
188
+ this.blacklistCacheTTL,
189
+ JSON.stringify(blacklist.map(item => item.toLowerCase())),
190
+ );
191
+ }
192
+
193
+ // Convert addresses to lowercase
194
+ private normalizeAddressesToLowerCase = (
195
+ jsonData: Record<string, { address: string }>,
196
+ ) => {
197
+ Object.keys(jsonData).forEach(key => {
198
+ jsonData[key].address = jsonData[key].address.toLowerCase();
199
+ });
200
+ return jsonData;
201
+ };
202
+
203
+ private async handleTokensResponse(res: CablesTokensResponse): Promise<void> {
204
+ const networkId = String(this.network);
205
+ const tokens = res.tokens[networkId];
206
+
207
+ const normalizedTokens = this.normalizeAddressesToLowerCase(tokens);
208
+
209
+ this.dexHelper.cache.setex(
210
+ this.dexKey,
211
+ this.network,
212
+ this.tokensCacheKey,
213
+ this.tokensCacheTTL,
214
+ JSON.stringify(normalizedTokens),
215
+ );
216
+
217
+ await this.tokensHandleResponseCallback();
218
+ }
219
+ }
@@ -0,0 +1,130 @@
1
+ import { RequestHeaders } from '../../dex-helper';
2
+ import { Token } from '../../types';
3
+ import { Method } from '../../dex-helper/irequest-wrapper';
4
+ import { AugustusRFQOrderData } from '../augustus-rfq';
5
+
6
+ export type CablesRFQResponse = {
7
+ order: AugustusRFQOrderData;
8
+ signature: string;
9
+ };
10
+
11
+ export type CablesData = {
12
+ quoteData?: AugustusRFQOrderData;
13
+ };
14
+
15
+ export enum OrderbookSide {
16
+ bids = 'bids',
17
+ asks = 'asks',
18
+ }
19
+
20
+ /**
21
+ * Utils
22
+ */
23
+ export type CablesAPIParameters = {
24
+ url: string;
25
+ method: Method;
26
+ };
27
+ export class CablesRfqError extends Error {}
28
+
29
+ /**
30
+ * Types
31
+ */
32
+ export type PairData = {
33
+ base: string;
34
+ quote: string;
35
+ liquidityUSD: number;
36
+ };
37
+
38
+ export type PriceAndAmount = [string, string];
39
+
40
+ export type PriceData = {
41
+ bids: PriceAndAmount[];
42
+ asks: PriceAndAmount[];
43
+ };
44
+
45
+ export type PriceDataMap = {
46
+ [network: string]: {
47
+ [pair: string]: PriceData;
48
+ };
49
+ };
50
+
51
+ export type TokenDataMap = {
52
+ [network: string]: {
53
+ [token: string]: Token;
54
+ };
55
+ };
56
+
57
+ export type PairsDataMap = {
58
+ [network: string]: {
59
+ [token: string]: PairData;
60
+ };
61
+ };
62
+
63
+ /**
64
+ * Responses
65
+ */
66
+ export type CablesPricesResponse = {
67
+ prices: PriceDataMap;
68
+ };
69
+ export type CablesBlacklistResponse = {
70
+ blacklist: string[];
71
+ };
72
+ export type CablesTokensResponse = {
73
+ tokens: TokenDataMap;
74
+ };
75
+ export type CablesPairsResponse = {
76
+ pairs: PairsDataMap;
77
+ };
78
+
79
+ /**
80
+ * Rate Fetcher
81
+ */
82
+ export type CablesRateFetcherConfig = {
83
+ rateConfig: {
84
+ pairsReqParams: {
85
+ url: string;
86
+ headers?: RequestHeaders;
87
+ params?: any;
88
+ };
89
+ pricesReqParams: {
90
+ url: string;
91
+ headers?: RequestHeaders;
92
+ params?: any;
93
+ };
94
+ blacklistReqParams: {
95
+ url: string;
96
+ headers?: RequestHeaders;
97
+ params?: any;
98
+ };
99
+ tokensReqParams: {
100
+ url: string;
101
+ headers?: RequestHeaders;
102
+ params?: any;
103
+ };
104
+ pairsIntervalMs: number;
105
+ pricesIntervalMs: number;
106
+ blacklistIntervalMs: number;
107
+ tokensIntervalMs: number;
108
+
109
+ pairsCacheKey: string;
110
+ pricesCacheKey: string;
111
+ blacklistCacheKey: string;
112
+ tokensCacheKey: string;
113
+
114
+ blacklistCacheTTLSecs: number;
115
+ pairsCacheTTLSecs: number;
116
+ pricesCacheTTLSecs: number;
117
+ tokensCacheTTLSecs: number;
118
+
119
+ tokensHandleResponseCallback: () => Promise<void>;
120
+ };
121
+ };
122
+
123
+ export type RestrictData = {
124
+ count: number;
125
+ addedDatetimeMs: number;
126
+ } | null;
127
+
128
+ export class SlippageError extends Error {
129
+ isSlippageError = true;
130
+ }
@@ -0,0 +1,151 @@
1
+ import Joi from 'joi';
2
+ import {
3
+ pairsResponseValidator,
4
+ pricesResponseValidator,
5
+ tokensResponseValidator,
6
+ blacklistResponseValidator,
7
+ } from './validators'; //
8
+
9
+ describe('Validation Schemas', () => {
10
+ describe('pairsResponseValidator', () => {
11
+ it('should validate correct pairs response', () => {
12
+ const validData = {
13
+ pairs: { '43114': { 'USDC/USDT': { base: 'USDC', quote: 'USDT' } } },
14
+ };
15
+ const { error } = pairsResponseValidator.validate(validData);
16
+ expect(error).toBeUndefined();
17
+ });
18
+
19
+ it('should invalidate incorrect pairs response', () => {
20
+ const invalidData = { pairs: { '43114': 'USDC/USDT' } };
21
+ const { error } = pairsResponseValidator.validate(invalidData);
22
+ expect(error).toBeDefined();
23
+ });
24
+ });
25
+
26
+ describe('pricesResponseValidator', () => {
27
+ it('should validate correct prices response', () => {
28
+ const validData = {
29
+ prices: {
30
+ '43114': {
31
+ 'USDC/USDT': {
32
+ bids: [
33
+ ['0.9996', '244305.9'],
34
+ ['0.9995', '236021.6'],
35
+ ],
36
+ asks: [
37
+ ['0.9996', '244305.9'],
38
+ ['0.9995', '236021.6'],
39
+ ],
40
+ },
41
+ },
42
+ },
43
+ };
44
+ const { error } = pricesResponseValidator.validate(validData);
45
+ expect(error).toBeUndefined();
46
+ });
47
+
48
+ it('should invalidate incorrect prices response', () => {
49
+ const invalidData = {
50
+ prices: {
51
+ chain1: {
52
+ bids: [
53
+ ['1000'], // invalid entry length
54
+ ],
55
+ asks: [['1010', '1']],
56
+ },
57
+ },
58
+ };
59
+ const { error } = pricesResponseValidator.validate(invalidData);
60
+ expect(error).toBeDefined();
61
+ });
62
+ });
63
+
64
+ describe('tokensResponseValidator', () => {
65
+ it('should validate correct tokens response', () => {
66
+ const validData = {
67
+ tokens: {
68
+ '43114': {
69
+ AVAX: {
70
+ symbol: 'AVAX',
71
+ decimals: 18,
72
+ name: 'AVAX',
73
+ address: '0x0000000000000000000000000000000000000000',
74
+ },
75
+ WAVAX: {
76
+ symbol: 'WAVAX',
77
+ decimals: 18,
78
+ name: 'WAVAX',
79
+ address: '0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7',
80
+ },
81
+ 'WETH.e': {
82
+ symbol: 'WETH.e',
83
+ decimals: 18,
84
+ name: 'WETH.e',
85
+ address: '0x49D5c2BdFfac6CE2BFdB6640F4F80f226bc10bAB',
86
+ },
87
+ USDT: {
88
+ symbol: 'USDT',
89
+ decimals: 6,
90
+ name: 'USDT',
91
+ address: '0x9702230A8Ea53601f5cD2dc00fDBc13d4dF4A8c7',
92
+ },
93
+ USDC: {
94
+ symbol: 'USDC',
95
+ decimals: 6,
96
+ name: 'USDC',
97
+ address: '0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E',
98
+ },
99
+ 'USDC.e': {
100
+ symbol: 'USDC.e',
101
+ decimals: 6,
102
+ name: 'USDC.e',
103
+ address: '0xA7D7079b0FEaD91F3e65f86E8915Cb59c1a4C664',
104
+ },
105
+ },
106
+ },
107
+ };
108
+ const { error } = tokensResponseValidator.validate(validData);
109
+ expect(error).toBeUndefined();
110
+ });
111
+
112
+ it('should invalidate incorrect tokens response', () => {
113
+ const invalidData = {
114
+ tokens: {
115
+ chain1: {
116
+ ETH: {
117
+ symbol: '', // invalid value
118
+ name: 'Ethereum',
119
+ description: 'A popular cryptocurrency',
120
+ address: '0x...',
121
+ decimals: 18,
122
+ type: 'ERC20',
123
+ },
124
+ },
125
+ },
126
+ };
127
+ const { error } = tokensResponseValidator.validate(invalidData);
128
+ expect(error).toBeDefined();
129
+ });
130
+ });
131
+
132
+ describe('blacklistResponseValidator', () => {
133
+ it('should validate correct blacklist response', () => {
134
+ const validData = {
135
+ blacklist: ['0xAddress1', '0xAddress2'],
136
+ };
137
+ const { error } = blacklistResponseValidator.validate(validData);
138
+ expect(error).toBeUndefined();
139
+ });
140
+
141
+ it('should invalidate incorrect blacklist response', () => {
142
+ const invalidData = {
143
+ blacklist: [
144
+ '', // invalid value
145
+ ],
146
+ };
147
+ const { error } = blacklistResponseValidator.validate(invalidData);
148
+ expect(error).toBeDefined();
149
+ });
150
+ });
151
+ });
@@ -0,0 +1,61 @@
1
+ import joi from 'joi';
2
+
3
+ const pairValidator = joi.object({
4
+ base: joi.string().min(1),
5
+ quote: joi.string().min(1),
6
+ liquidityUSD: joi.number().min(0),
7
+ baseAddress: joi.string().min(1),
8
+ quoteAddress: joi.string().min(1),
9
+ baseDecimals: joi.number().min(0),
10
+ quoteDecimals: joi.number().min(0),
11
+ });
12
+
13
+ const pairMap = joi.object().pattern(
14
+ joi.string(), // Pair name ETH/USDT
15
+ pairValidator,
16
+ );
17
+
18
+ export const pairsResponseValidator = joi.object({
19
+ pairs: joi.object().pattern(
20
+ joi.string(), // chain id
21
+ pairMap,
22
+ ),
23
+ });
24
+
25
+ const orderbookEntry = joi.array().items(joi.string().min(1)).length(2);
26
+
27
+ const orderbookValidator = joi.object({
28
+ bids: joi.array().items(orderbookEntry),
29
+ asks: joi.array().items(orderbookEntry),
30
+ });
31
+
32
+ const chainDataSchema = joi.object().pattern(
33
+ joi.string(), // pair name USDC/USDT
34
+ orderbookValidator,
35
+ );
36
+
37
+ export const pricesResponseValidator = joi.object({
38
+ prices: joi.object().pattern(
39
+ joi.string(), // chain id
40
+ chainDataSchema,
41
+ ),
42
+ });
43
+
44
+ const tokenValidator = joi.object({
45
+ symbol: joi.string().min(1),
46
+ name: joi.string().min(1),
47
+ description: joi.string().min(1),
48
+ address: joi.string().min(1),
49
+ decimals: joi.number().min(0),
50
+ type: joi.string().min(1),
51
+ });
52
+
53
+ const chainTokens = joi.object().pattern(joi.string(), tokenValidator);
54
+
55
+ export const tokensResponseValidator = joi.object({
56
+ tokens: joi.object().pattern(joi.string(), chainTokens),
57
+ });
58
+
59
+ export const blacklistResponseValidator = joi.object({
60
+ blacklist: joi.array().items(joi.string().min(1)),
61
+ });
@@ -593,6 +593,7 @@ export class Dexalot extends SimpleExchange implements IDex<DexalotData> {
593
593
  .multipliedBy(10000)
594
594
  .toFixed(0)
595
595
  : options.slippageFactor.minus(1).multipliedBy(10000).toFixed(0);
596
+
596
597
  const rfqParams = {
597
598
  makerAsset: ethers.utils.getAddress(makerToken.address),
598
599
  takerAsset: ethers.utils.getAddress(takerToken.address),
package/src/dex/index.ts CHANGED
@@ -96,6 +96,7 @@ import { StkGHO } from './stkgho/stkgho';
96
96
  import { BalancerV3 } from './balancer-v3/balancer-v3';
97
97
  import { balancerV3Merge } from './balancer-v3/optimizer';
98
98
  import { SkyConverter } from './sky-converter/sky-converter';
99
+ import { Cables } from './cables/cables';
99
100
  import { Stader } from './stader/stader';
100
101
 
101
102
  const LegacyDexes = [
@@ -187,6 +188,7 @@ const Dexes = [
187
188
  UsualBond,
188
189
  StkGHO,
189
190
  SkyConverter,
191
+ Cables,
190
192
  FluidDex,
191
193
  ];
192
194
 
@@ -379,17 +379,21 @@ export class Executor01BytecodeBuilder extends ExecutorBytecodeBuilder<
379
379
 
380
380
  let fromAmountPos = 0;
381
381
  if (insertFromAmount) {
382
- const fromAmount = ethers.utils.defaultAbiCoder.encode(
383
- ['uint256'],
384
- [swap.swapExchanges[swapExchangeIndex].srcAmount],
385
- );
386
-
387
- const fromAmountIndex = exchangeData
388
- .replace('0x', '')
389
- .indexOf(fromAmount.replace('0x', ''));
390
-
391
- fromAmountPos =
392
- (fromAmountIndex !== -1 ? fromAmountIndex : exchangeData.length) / 2;
382
+ if (exchangeParam.insertFromAmountPos) {
383
+ fromAmountPos = exchangeParam.insertFromAmountPos;
384
+ } else {
385
+ const fromAmount = ethers.utils.defaultAbiCoder.encode(
386
+ ['uint256'],
387
+ [swap.swapExchanges[swapExchangeIndex].srcAmount],
388
+ );
389
+
390
+ const fromAmountIndex = exchangeData
391
+ .replace('0x', '')
392
+ .indexOf(fromAmount.replace('0x', ''));
393
+
394
+ fromAmountPos =
395
+ (fromAmountIndex !== -1 ? fromAmountIndex : exchangeData.length) / 2;
396
+ }
393
397
  }
394
398
 
395
399
  return this.buildCallData(
@@ -335,16 +335,20 @@ export class Executor02BytecodeBuilder extends ExecutorBytecodeBuilder<
335
335
 
336
336
  let fromAmountPos = 0;
337
337
  if (insertFromAmount) {
338
- const fromAmount = ethers.utils.defaultAbiCoder.encode(
339
- ['uint256'],
340
- [swapExchange.srcAmount],
341
- );
342
- const fromAmountIndex = exchangeData
343
- .replace('0x', '')
344
- .indexOf(fromAmount.replace('0x', ''));
338
+ if (exchangeParam.insertFromAmountPos) {
339
+ fromAmountPos = exchangeParam.insertFromAmountPos;
340
+ } else {
341
+ const fromAmount = ethers.utils.defaultAbiCoder.encode(
342
+ ['uint256'],
343
+ [swapExchange.srcAmount],
344
+ );
345
+ const fromAmountIndex = exchangeData
346
+ .replace('0x', '')
347
+ .indexOf(fromAmount.replace('0x', ''));
345
348
 
346
- fromAmountPos =
347
- (fromAmountIndex !== -1 ? fromAmountIndex : exchangeData.length) / 2;
349
+ fromAmountPos =
350
+ (fromAmountIndex !== -1 ? fromAmountIndex : exchangeData.length) / 2;
351
+ }
348
352
  }
349
353
 
350
354
  return this.buildCallData(