@paraswap/dex-lib 3.7.2 → 3.7.3-idle

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 (251) hide show
  1. package/build/abi/Redstone.json +739 -0
  2. package/build/abi/aavev3stata/Factory.json +128 -0
  3. package/build/abi/aavev3stata/Token.json +735 -0
  4. package/build/abi/angle-transmuter/MorphoOracle.json +193 -0
  5. package/build/abi/angle-transmuter/MorphoVault.json +2506 -0
  6. package/build/abi/angle-transmuter/Pyth.json +1534 -0
  7. package/build/abi/angle-transmuter/Transmuter.json +1711 -0
  8. package/build/abi/angle-transmuter/TransmuterSidechain.json +1798 -0
  9. package/build/abi/idle-dao/idle-cdo-factory.json +38 -0
  10. package/build/abi/idle-dao/idle-cdo.json +1245 -0
  11. package/build/config.js +2 -0
  12. package/build/config.js.map +1 -1
  13. package/build/dex/aave-v1/aave-v1.d.ts +2 -1
  14. package/build/dex/aave-v1/aave-v1.js +22 -0
  15. package/build/dex/aave-v1/aave-v1.js.map +1 -1
  16. package/build/dex/aave-v3-stata/aave-v3-stata.d.ts +50 -0
  17. package/build/dex/aave-v3-stata/aave-v3-stata.js +385 -0
  18. package/build/dex/aave-v3-stata/aave-v3-stata.js.map +1 -0
  19. package/build/dex/aave-v3-stata/config.d.ts +4 -0
  20. package/build/dex/aave-v3-stata/config.js +50 -0
  21. package/build/dex/aave-v3-stata/config.js.map +1 -0
  22. package/build/dex/aave-v3-stata/tokens.d.ts +10 -0
  23. package/build/dex/aave-v3-stata/tokens.js +43 -0
  24. package/build/dex/aave-v3-stata/tokens.js.map +1 -0
  25. package/build/dex/aave-v3-stata/types.d.ts +46 -0
  26. package/build/dex/aave-v3-stata/types.js +23 -0
  27. package/build/dex/aave-v3-stata/types.js.map +1 -0
  28. package/build/dex/aave-v3-stata/utils.d.ts +4 -0
  29. package/build/dex/aave-v3-stata/utils.js +67 -0
  30. package/build/dex/aave-v3-stata/utils.js.map +1 -0
  31. package/build/dex/angle-transmuter/angle-transmuter-pool.d.ts +67 -0
  32. package/build/dex/angle-transmuter/angle-transmuter-pool.js +448 -0
  33. package/build/dex/angle-transmuter/angle-transmuter-pool.js.map +1 -0
  34. package/build/dex/angle-transmuter/angle-transmuter.d.ts +59 -0
  35. package/build/dex/angle-transmuter/angle-transmuter.js +264 -0
  36. package/build/dex/angle-transmuter/angle-transmuter.js.map +1 -0
  37. package/build/dex/angle-transmuter/backedOracle.d.ts +19 -0
  38. package/build/dex/angle-transmuter/backedOracle.js +56 -0
  39. package/build/dex/angle-transmuter/backedOracle.js.map +1 -0
  40. package/build/dex/angle-transmuter/config.d.ts +4 -0
  41. package/build/dex/angle-transmuter/config.js +50 -0
  42. package/build/dex/angle-transmuter/config.js.map +1 -0
  43. package/build/dex/angle-transmuter/constants.d.ts +9 -0
  44. package/build/dex/angle-transmuter/constants.js +136 -0
  45. package/build/dex/angle-transmuter/constants.js.map +1 -0
  46. package/build/dex/angle-transmuter/morphoOracle.d.ts +42 -0
  47. package/build/dex/angle-transmuter/morphoOracle.js +179 -0
  48. package/build/dex/angle-transmuter/morphoOracle.js.map +1 -0
  49. package/build/dex/angle-transmuter/morphoVault.d.ts +20 -0
  50. package/build/dex/angle-transmuter/morphoVault.js +84 -0
  51. package/build/dex/angle-transmuter/morphoVault.js.map +1 -0
  52. package/build/dex/angle-transmuter/pyth.d.ts +19 -0
  53. package/build/dex/angle-transmuter/pyth.js +70 -0
  54. package/build/dex/angle-transmuter/pyth.js.map +1 -0
  55. package/build/dex/angle-transmuter/redstone.d.ts +20 -0
  56. package/build/dex/angle-transmuter/redstone.js +64 -0
  57. package/build/dex/angle-transmuter/redstone.js.map +1 -0
  58. package/build/dex/angle-transmuter/transmuter.d.ts +55 -0
  59. package/build/dex/angle-transmuter/transmuter.js +398 -0
  60. package/build/dex/angle-transmuter/transmuter.js.map +1 -0
  61. package/build/dex/angle-transmuter/types.d.ts +189 -0
  62. package/build/dex/angle-transmuter/types.js +33 -0
  63. package/build/dex/angle-transmuter/types.js.map +1 -0
  64. package/build/dex/angle-transmuter/utils.d.ts +9 -0
  65. package/build/dex/angle-transmuter/utils.js +257 -0
  66. package/build/dex/angle-transmuter/utils.js.map +1 -0
  67. package/build/dex/idle-dao/config.d.ts +6 -0
  68. package/build/dex/idle-dao/config.js +307 -0
  69. package/build/dex/idle-dao/config.js.map +1 -0
  70. package/build/dex/idle-dao/idle-dao-pool.d.ts +56 -0
  71. package/build/dex/idle-dao/idle-dao-pool.js +176 -0
  72. package/build/dex/idle-dao/idle-dao-pool.js.map +1 -0
  73. package/build/dex/idle-dao/idle-dao-pooling-pool.d.ts +16 -0
  74. package/build/dex/idle-dao/idle-dao-pooling-pool.js +57 -0
  75. package/build/dex/idle-dao/idle-dao-pooling-pool.js.map +1 -0
  76. package/build/dex/idle-dao/idle-dao.d.ts +41 -0
  77. package/build/dex/idle-dao/idle-dao.js +238 -0
  78. package/build/dex/idle-dao/idle-dao.js.map +1 -0
  79. package/build/dex/idle-dao/scripts.d.ts +9 -0
  80. package/build/dex/idle-dao/scripts.js +552 -0
  81. package/build/dex/idle-dao/scripts.js.map +1 -0
  82. package/build/dex/idle-dao/tokens.d.ts +9 -0
  83. package/build/dex/idle-dao/tokens.js +65 -0
  84. package/build/dex/idle-dao/tokens.js.map +1 -0
  85. package/build/dex/idle-dao/types.d.ts +35 -0
  86. package/build/dex/idle-dao/types.js +11 -0
  87. package/build/dex/idle-dao/types.js.map +1 -0
  88. package/build/dex/idle-dao/utils.d.ts +8 -0
  89. package/build/dex/idle-dao/utils.js +148 -0
  90. package/build/dex/idle-dao/utils.js.map +1 -0
  91. package/build/dex/index.js +4 -0
  92. package/build/dex/index.js.map +1 -1
  93. package/build/dex/platypus/platypus.d.ts +2 -1
  94. package/build/dex/platypus/platypus.js +31 -0
  95. package/build/dex/platypus/platypus.js.map +1 -1
  96. package/build/dex/swaap-v1/swaap-v1.d.ts +2 -1
  97. package/build/dex/swaap-v1/swaap-v1.js +10 -0
  98. package/build/dex/swaap-v1/swaap-v1.js.map +1 -1
  99. package/build/lib/chainlink.d.ts +2 -0
  100. package/build/lib/chainlink.js +9 -0
  101. package/build/lib/chainlink.js.map +1 -1
  102. package/build/types.d.ts +1 -0
  103. package/build/utils.d.ts +1 -1
  104. package/package.json +1 -1
  105. package/src/abi/Redstone.json +739 -0
  106. package/src/abi/angle-transmuter/ChainlinkAccessControlledOffchainAggregator.json +1246 -0
  107. package/src/abi/angle-transmuter/ChainlinkEACAggregatorProxy.json +509 -0
  108. package/src/abi/angle-transmuter/MorphoOracle.json +193 -0
  109. package/src/abi/angle-transmuter/MorphoVault.json +2506 -0
  110. package/src/abi/angle-transmuter/Pyth.json +1534 -0
  111. package/src/abi/angle-transmuter/Transmuter.json +1711 -0
  112. package/src/abi/angle-transmuter/TransmuterSidechain.json +1798 -0
  113. package/src/abi/idle-dao/idle-cdo-factory.json +38 -0
  114. package/src/abi/idle-dao/idle-cdo.json +1245 -0
  115. package/src/config.ts +3 -0
  116. package/src/dex/angle-transmuter/angle-transmuter-e2e.test.ts +156 -0
  117. package/src/dex/angle-transmuter/angle-transmuter-events.test.ts +365 -0
  118. package/src/dex/angle-transmuter/angle-transmuter-integration.test.ts +390 -0
  119. package/src/dex/angle-transmuter/angle-transmuter-pool.ts +895 -0
  120. package/src/dex/angle-transmuter/angle-transmuter.ts +408 -0
  121. package/src/dex/angle-transmuter/backedOracle.ts +91 -0
  122. package/src/dex/angle-transmuter/config.ts +50 -0
  123. package/src/dex/angle-transmuter/constants.ts +138 -0
  124. package/src/dex/angle-transmuter/morphoOracle.ts +335 -0
  125. package/src/dex/angle-transmuter/morphoVault.ts +134 -0
  126. package/src/dex/angle-transmuter/pyth.ts +105 -0
  127. package/src/dex/angle-transmuter/redstone.ts +105 -0
  128. package/src/dex/angle-transmuter/transmuter.ts +619 -0
  129. package/src/dex/angle-transmuter/types.ts +203 -0
  130. package/src/dex/angle-transmuter/utils.ts +358 -0
  131. package/src/dex/idle-dao/config.ts +309 -0
  132. package/src/dex/idle-dao/idle-dao-e2e.test.ts +112 -0
  133. package/src/dex/idle-dao/idle-dao-integration.test.ts +155 -0
  134. package/src/dex/idle-dao/idle-dao.ts +363 -0
  135. package/src/dex/idle-dao/scripts.ts +553 -0
  136. package/src/dex/idle-dao/tokens.ts +90 -0
  137. package/src/dex/idle-dao/types.ts +43 -0
  138. package/src/dex/idle-dao/utils.ts +215 -0
  139. package/src/dex/index.ts +4 -0
  140. package/src/lib/chainlink.ts +15 -0
  141. package/src/types.ts +1 -0
  142. package/tests/constants-e2e.ts +117 -6
  143. package/build/abi/Uncompressor.json +0 -250
  144. package/build/dex/bProtocol.d.ts +0 -18
  145. package/build/dex/bProtocol.js +0 -39
  146. package/build/dex/bProtocol.js.map +0 -1
  147. package/build/dex/bancor.d.ts +0 -26
  148. package/build/dex/bancor.js +0 -58
  149. package/build/dex/bancor.js.map +0 -1
  150. package/build/dex/compound.d.ts +0 -18
  151. package/build/dex/compound.js +0 -46
  152. package/build/dex/compound.js.map +0 -1
  153. package/build/dex/curve-v2.d.ts +0 -58
  154. package/build/dex/curve-v2.js +0 -180
  155. package/build/dex/curve-v2.js.map +0 -1
  156. package/build/dex/dodo-v1.d.ts +0 -33
  157. package/build/dex/dodo-v1.js +0 -63
  158. package/build/dex/dodo-v1.js.map +0 -1
  159. package/build/dex/etherfi/etherfi.d.ts +0 -26
  160. package/build/dex/etherfi/etherfi.js +0 -96
  161. package/build/dex/etherfi/etherfi.js.map +0 -1
  162. package/build/dex/lido.d.ts +0 -19
  163. package/build/dex/lido.js +0 -42
  164. package/build/dex/lido.js.map +0 -1
  165. package/build/dex/onebit.d.ts +0 -25
  166. package/build/dex/onebit.js +0 -42
  167. package/build/dex/onebit.js.map +0 -1
  168. package/build/dex/sdai/config.d.ts +0 -11
  169. package/build/dex/sdai/config.js +0 -21
  170. package/build/dex/sdai/config.js.map +0 -1
  171. package/build/dex/sdai/constants.d.ts +0 -2
  172. package/build/dex/sdai/constants.js +0 -6
  173. package/build/dex/sdai/constants.js.map +0 -1
  174. package/build/dex/sdai/scripts/validate-state.d.ts +0 -1
  175. package/build/dex/sdai/scripts/validate-state.js +0 -102
  176. package/build/dex/sdai/scripts/validate-state.js.map +0 -1
  177. package/build/dex/sdai/sdai-pool.d.ts +0 -16
  178. package/build/dex/sdai/sdai-pool.js +0 -85
  179. package/build/dex/sdai/sdai-pool.js.map +0 -1
  180. package/build/dex/sdai/sdai.d.ts +0 -51
  181. package/build/dex/sdai/sdai.js +0 -172
  182. package/build/dex/sdai/sdai.js.map +0 -1
  183. package/build/dex/sdai/types.d.ts +0 -21
  184. package/build/dex/sdai/types.js +0 -11
  185. package/build/dex/sdai/types.js.map +0 -1
  186. package/build/dex/sdai/utils.d.ts +0 -4
  187. package/build/dex/sdai/utils.js +0 -39
  188. package/build/dex/sdai/utils.js.map +0 -1
  189. package/build/dex/smoothy.d.ts +0 -26
  190. package/build/dex/smoothy.js +0 -48
  191. package/build/dex/smoothy.js.map +0 -1
  192. package/build/dex/stable-pool.d.ts +0 -28
  193. package/build/dex/stable-pool.js +0 -62
  194. package/build/dex/stable-pool.js.map +0 -1
  195. package/build/dex/trader-joe-v2.1.d.ts +0 -43
  196. package/build/dex/trader-joe-v2.1.js +0 -79
  197. package/build/dex/trader-joe-v2.1.js.map +0 -1
  198. package/build/executor/compressor/Compressor.d.ts +0 -23
  199. package/build/executor/compressor/Compressor.js +0 -144
  200. package/build/executor/compressor/Compressor.js.map +0 -1
  201. package/build/executor/compressor/compress.d.ts +0 -17
  202. package/build/executor/compressor/compress.js +0 -187
  203. package/build/executor/compressor/compress.js.map +0 -1
  204. package/build/executor/compressor/compress_functions/addressSubstitution.d.ts +0 -6
  205. package/build/executor/compressor/compress_functions/addressSubstitution.js +0 -29
  206. package/build/executor/compressor/compress_functions/addressSubstitution.js.map +0 -1
  207. package/build/executor/compressor/compress_functions/addressSubstitutionFromObject.d.ts +0 -10
  208. package/build/executor/compressor/compress_functions/addressSubstitutionFromObject.js +0 -40
  209. package/build/executor/compressor/compress_functions/addressSubstitutionFromObject.js.map +0 -1
  210. package/build/executor/compressor/compress_functions/byteSubstitution.d.ts +0 -11
  211. package/build/executor/compressor/compress_functions/byteSubstitution.js +0 -64
  212. package/build/executor/compressor/compress_functions/byteSubstitution.js.map +0 -1
  213. package/build/executor/compressor/compress_functions/ffSubstitution.d.ts +0 -7
  214. package/build/executor/compressor/compress_functions/ffSubstitution.js +0 -54
  215. package/build/executor/compressor/compress_functions/ffSubstitution.js.map +0 -1
  216. package/build/executor/compressor/compress_functions/findLongestAndSubstitute.d.ts +0 -8
  217. package/build/executor/compressor/compress_functions/findLongestAndSubstitute.js +0 -67
  218. package/build/executor/compressor/compress_functions/findLongestAndSubstitute.js.map +0 -1
  219. package/build/executor/compressor/compress_functions/findLongestAndSubstitute_old.d.ts +0 -8
  220. package/build/executor/compressor/compress_functions/findLongestAndSubstitute_old.js +0 -61
  221. package/build/executor/compressor/compress_functions/findLongestAndSubstitute_old.js.map +0 -1
  222. package/build/executor/compressor/compress_functions/reverseAddressSubstitution.d.ts +0 -6
  223. package/build/executor/compressor/compress_functions/reverseAddressSubstitution.js +0 -37
  224. package/build/executor/compressor/compress_functions/reverseAddressSubstitution.js.map +0 -1
  225. package/build/executor/compressor/compress_functions/searchUnusedBytes.d.ts +0 -9
  226. package/build/executor/compressor/compress_functions/searchUnusedBytes.js +0 -33
  227. package/build/executor/compressor/compress_functions/searchUnusedBytes.js.map +0 -1
  228. package/build/executor/compressor/fetchSaved.d.ts +0 -0
  229. package/build/executor/compressor/fetchSaved.js +0 -17
  230. package/build/executor/compressor/fetchSaved.js.map +0 -1
  231. package/build/executor/compressor/utils/computeCostL2.d.ts +0 -2
  232. package/build/executor/compressor/utils/computeCostL2.js +0 -14
  233. package/build/executor/compressor/utils/computeCostL2.js.map +0 -1
  234. package/build/executor/compressor/utils/findLongestDuplicatedString.d.ts +0 -2
  235. package/build/executor/compressor/utils/findLongestDuplicatedString.js +0 -30
  236. package/build/executor/compressor/utils/findLongestDuplicatedString.js.map +0 -1
  237. package/build/executor/compressor/utils/getAllIndexes.d.ts +0 -2
  238. package/build/executor/compressor/utils/getAllIndexes.js +0 -12
  239. package/build/executor/compressor/utils/getAllIndexes.js.map +0 -1
  240. package/build/executor/compressor/utils/getByteForAddress.d.ts +0 -2
  241. package/build/executor/compressor/utils/getByteForAddress.js +0 -14
  242. package/build/executor/compressor/utils/getByteForAddress.js.map +0 -1
  243. package/build/executor/compressor/utils/intTo2Bytes.d.ts +0 -2
  244. package/build/executor/compressor/utils/intTo2Bytes.js +0 -14
  245. package/build/executor/compressor/utils/intTo2Bytes.js.map +0 -1
  246. package/build/executor/compressor/utils/intTo3Bytes.d.ts +0 -2
  247. package/build/executor/compressor/utils/intTo3Bytes.js +0 -18
  248. package/build/executor/compressor/utils/intTo3Bytes.js.map +0 -1
  249. package/build/executor/compressor/utils/intToByte.d.ts +0 -2
  250. package/build/executor/compressor/utils/intToByte.js +0 -10
  251. package/build/executor/compressor/utils/intToByte.js.map +0 -1
@@ -0,0 +1,363 @@
1
+ import { assert } from 'ts-essentials';
2
+ import { Interface } from '@ethersproject/abi';
3
+ import {
4
+ Token,
5
+ Address,
6
+ ExchangePrices,
7
+ PoolPrices,
8
+ AdapterExchangeParam,
9
+ PoolLiquidity,
10
+ Logger,
11
+ NumberAsString,
12
+ DexExchangeParam,
13
+ } from '../../types';
14
+ import { SwapSide, Network, NULL_ADDRESS } from '../../constants';
15
+ import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
16
+ import { getDexKeysWithNetwork, getBigIntPow } from '../../utils';
17
+ import { Context, IDex } from '../../dex/idex';
18
+ import { IDexHelper } from '../../dex-helper/idex-helper';
19
+ import { IdleDaoData, Param, PoolFunctions, IdleToken } from './types';
20
+ import { SimpleExchange } from '../simple-exchange';
21
+ import { Config, Adapters } from './config';
22
+ import { fetchTokenList_api } from './utils';
23
+ import {
24
+ getIdleTokenIfIdleDaoPair,
25
+ setTokensOnNetwork,
26
+ getPoolsByTokenAddress,
27
+ getTokenFromIdleToken,
28
+ } from './tokens';
29
+ import FACTORY_ABI from '../../abi/idle-dao/idle-cdo-factory.json';
30
+ import CDO_ABI from '../../abi/idle-dao/idle-cdo.json';
31
+
32
+ export const TOKEN_LIST_CACHE_KEY = 'token-list';
33
+ const TOKEN_LIST_TTL_SECONDS = 24 * 60 * 60;
34
+ const TOKEN_LIST_LOCAL_TTL_SECONDS = 3 * 60 * 60;
35
+
36
+ export class IdleDao extends SimpleExchange implements IDex<IdleDaoData> {
37
+ readonly hasConstantPriceLargeAmounts = false;
38
+ readonly needWrapNative = true;
39
+
40
+ readonly isFeeOnTransferSupported = false;
41
+
42
+ public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
43
+ getDexKeysWithNetwork(Config);
44
+
45
+ logger: Logger;
46
+
47
+ private cdo: Interface;
48
+ private factory: Interface;
49
+
50
+ private tokenList: IdleToken[] = [];
51
+ private idleDaoAuthToken: string;
52
+
53
+ constructor(
54
+ readonly network: Network,
55
+ readonly dexKey: string,
56
+ readonly dexHelper: IDexHelper,
57
+ protected config = Config[dexKey][network],
58
+ protected adapters = Adapters[network] || {},
59
+ ) {
60
+ super(dexHelper, dexKey);
61
+ this.logger = dexHelper.getLogger(dexKey);
62
+ this.cdo = new Interface(CDO_ABI);
63
+ this.factory = new Interface(FACTORY_ABI);
64
+
65
+ const idleDaoAuthToken = dexHelper.config.data.idleDaoAuthToken;
66
+ assert(
67
+ idleDaoAuthToken !== undefined,
68
+ 'idleDaoAuthToken auth token is not specified with env variable',
69
+ );
70
+
71
+ this.idleDaoAuthToken = idleDaoAuthToken;
72
+ }
73
+
74
+ async getTokensList(blockNumber: number): Promise<Record<string, IdleToken>> {
75
+ if (!this.tokenList) {
76
+ this.tokenList = await fetchTokenList_api(
77
+ this.network,
78
+ this.dexHelper,
79
+ blockNumber,
80
+ this.cdo,
81
+ this.erc20Interface,
82
+ this.dexHelper.multiWrapper,
83
+ this.idleDaoAuthToken,
84
+ );
85
+ }
86
+
87
+ return this.tokenList.reduce(
88
+ (acc: Record<string, IdleToken>, idleToken: IdleToken) => {
89
+ return {
90
+ ...acc,
91
+ [idleToken.idleAddress]: idleToken,
92
+ };
93
+ },
94
+ {},
95
+ );
96
+ }
97
+
98
+ async initializePricing(blockNumber: number) {
99
+ let cachedTokenList = await this.dexHelper.cache.getAndCacheLocally(
100
+ this.dexKey,
101
+ this.network,
102
+ TOKEN_LIST_CACHE_KEY,
103
+ TOKEN_LIST_LOCAL_TTL_SECONDS,
104
+ );
105
+
106
+ if (cachedTokenList !== null) {
107
+ const tokens: IdleToken[] = JSON.parse(cachedTokenList);
108
+ setTokensOnNetwork(this.network, tokens, this.dexHelper);
109
+
110
+ this.tokenList = tokens;
111
+ return;
112
+ }
113
+
114
+ this.tokenList = await fetchTokenList_api(
115
+ this.network,
116
+ this.dexHelper,
117
+ blockNumber,
118
+ this.cdo,
119
+ this.erc20Interface,
120
+ this.dexHelper.multiWrapper,
121
+ this.idleDaoAuthToken,
122
+ );
123
+
124
+ await this.dexHelper.cache.setex(
125
+ this.dexKey,
126
+ this.network,
127
+ TOKEN_LIST_CACHE_KEY,
128
+ TOKEN_LIST_TTL_SECONDS,
129
+ JSON.stringify(this.tokenList),
130
+ );
131
+
132
+ setTokensOnNetwork(this.network, this.tokenList, this.dexHelper);
133
+ }
134
+
135
+ getAdapters(side: SwapSide): { name: string; index: number }[] | null {
136
+ return this.adapters[side] ? this.adapters[side] : null;
137
+ }
138
+
139
+ private _getPoolIdentifier(srcToken: Token, destToken: Token): string {
140
+ return (
141
+ this.dexKey +
142
+ [srcToken.address.toLowerCase(), destToken.address.toLowerCase()]
143
+ .sort((a, b) => (a > b ? 1 : -1))
144
+ .join('_')
145
+ );
146
+ }
147
+
148
+ async getPoolIdentifiers(
149
+ srcToken: Token,
150
+ destToken: Token,
151
+ ): Promise<string[]> {
152
+ const idleToken = getIdleTokenIfIdleDaoPair(
153
+ this.network,
154
+ this.dexHelper.config.wrapETH(srcToken),
155
+ this.dexHelper.config.wrapETH(destToken),
156
+ );
157
+
158
+ if (idleToken === null) return [];
159
+
160
+ return [this._getPoolIdentifier(srcToken, destToken)];
161
+ }
162
+
163
+ async getPricesVolume(
164
+ srcToken: Token,
165
+ destToken: Token,
166
+ amounts: bigint[],
167
+ side: SwapSide,
168
+ blockNumber: number,
169
+ limitPools?: string[],
170
+ ): Promise<null | ExchangePrices<IdleDaoData>> {
171
+ try {
172
+ const _src = this.dexHelper.config.wrapETH(srcToken);
173
+ const _dst = this.dexHelper.config.wrapETH(destToken);
174
+
175
+ // Look for idleToken
176
+ const idleToken = getIdleTokenIfIdleDaoPair(this.network, _src, _dst);
177
+ if (!idleToken) {
178
+ return null;
179
+ }
180
+
181
+ const fromIdleToken =
182
+ idleToken.idleAddress.toLowerCase() == _src.address.toLowerCase();
183
+
184
+ const tokenPrice = await this.getTokenPrice(idleToken, blockNumber);
185
+
186
+ if (!tokenPrice) {
187
+ return null;
188
+ }
189
+
190
+ const unitVolume = getBigIntPow(
191
+ (side === SwapSide.SELL ? srcToken : destToken).decimals,
192
+ );
193
+
194
+ const prices = [unitVolume, ...amounts].map((amount: bigint) => {
195
+ let output = 0;
196
+ if (side === SwapSide.SELL) {
197
+ // SELL idleToken (amount = 1000000000000000000 AA_idle_cpPOR-USDC, output = 1000000000000000000*tokenPrice/1e18)
198
+ if (fromIdleToken) {
199
+ output = Math.round(
200
+ (parseFloat('' + tokenPrice) * parseFloat('' + amount)) /
201
+ parseFloat(`1e${idleToken.idleDecimals}`),
202
+ );
203
+ // SELL underlyingToken (amount = 1000000 USDC), output = 1000000/tokenPrice*1e18
204
+ } else {
205
+ output = Math.round(
206
+ (parseFloat('' + amount) / parseFloat('' + tokenPrice)) *
207
+ parseFloat(`1e${idleToken.idleDecimals}`),
208
+ );
209
+ }
210
+ } else {
211
+ // BUY idleToken (amount = 1000000 USDC, output = 1000000/tokenPrice*1e18)
212
+ if (fromIdleToken) {
213
+ output = Math.round(
214
+ (parseFloat('' + amount) / parseFloat('' + tokenPrice)) *
215
+ parseFloat(`1e${idleToken.idleDecimals}`),
216
+ );
217
+ // BUY underlyingToken (amount = 1000000000000000000 AA_idle_cpPOR-USDC, output = 1000000000000000000*tokenPrice/1e18)
218
+ } else {
219
+ output = Math.round(
220
+ (parseFloat('' + tokenPrice) * parseFloat('' + amount)) /
221
+ parseFloat(`1e${idleToken.idleDecimals}`),
222
+ );
223
+ }
224
+ }
225
+ return BigInt(output);
226
+ });
227
+
228
+ return [
229
+ {
230
+ unit: prices[0],
231
+ prices: prices.slice(1),
232
+ gasCost: this.config.lendingGasCost,
233
+ exchange: this.dexKey,
234
+ data: {
235
+ idleToken: {
236
+ cdoAddress: idleToken.cdoAddress,
237
+ tokenType: idleToken.tokenType,
238
+ },
239
+ fromIdleToken,
240
+ },
241
+ poolAddresses: [fromIdleToken ? srcToken.address : destToken.address],
242
+ },
243
+ ];
244
+ } catch (e) {
245
+ if (blockNumber === 0)
246
+ this.logger.error(
247
+ `Error_getPricesVolume: Aurelius block manager not yet instantiated`,
248
+ );
249
+ this.logger.error(`Error_getPrices:`, e);
250
+ return null;
251
+ }
252
+ }
253
+
254
+ // Cannot be implemented fully event-based.
255
+ // Rate changes almost every block.
256
+ // rpc-based pricing is only done for limited amount of tokens that are supported
257
+ async getTokenPrice(
258
+ idleToken: IdleToken,
259
+ blockNumber: number,
260
+ ): Promise<bigint | null> {
261
+ if (!idleToken.cdoContract) {
262
+ return null;
263
+ }
264
+
265
+ const tranchePrice = await idleToken.cdoContract.methods['virtualPrice'](
266
+ idleToken.idleAddress,
267
+ ).call({}, blockNumber);
268
+
269
+ return BigInt(tranchePrice);
270
+ }
271
+
272
+ // Returns estimated gas cost of calldata for this DEX in multiSwap
273
+ getCalldataGasCost(poolPrices: PoolPrices<IdleDaoData>): number | number[] {
274
+ // TODO: update if there is any payload in getAdapterParam
275
+ return CALLDATA_GAS_COST.DEX_NO_PAYLOAD;
276
+ }
277
+
278
+ // Encode params required by the exchange adapter
279
+ // Used for multiSwap, buy & megaSwap
280
+ // Hint: abiCoder.encodeParameter() could be useful
281
+ getAdapterParam(
282
+ srcToken: string,
283
+ destToken: string,
284
+ srcAmount: string,
285
+ destAmount: string,
286
+ data: IdleDaoData,
287
+ side: SwapSide,
288
+ ): AdapterExchangeParam {
289
+ return {
290
+ // target exchange is not used by the contract
291
+ targetExchange: NULL_ADDRESS,
292
+ payload: '0x',
293
+ networkFee: '0',
294
+ };
295
+ }
296
+
297
+ getDexParam(
298
+ srcToken: Address,
299
+ destToken: Address,
300
+ srcAmount: NumberAsString,
301
+ destAmount: NumberAsString,
302
+ recipient: Address,
303
+ data: IdleDaoData,
304
+ side: SwapSide,
305
+ _: Context,
306
+ executorAddress: Address,
307
+ ): DexExchangeParam {
308
+ const [Interface, swapCallee, swapFunction, swapFunctionParams] = ((): [
309
+ Interface,
310
+ Address,
311
+ PoolFunctions,
312
+ Param,
313
+ ] => {
314
+ if (data.fromIdleToken) {
315
+ return [
316
+ this.cdo,
317
+ data.idleToken.cdoAddress,
318
+ PoolFunctions[`withdraw${data.idleToken.tokenType}`],
319
+ [srcAmount],
320
+ ];
321
+ }
322
+
323
+ return [
324
+ this.cdo,
325
+ data.idleToken.cdoAddress,
326
+ PoolFunctions[`deposit${data.idleToken.tokenType}`],
327
+ [srcAmount, recipient],
328
+ ];
329
+ })();
330
+
331
+ const exchangeData = Interface.encodeFunctionData(
332
+ swapFunction,
333
+ swapFunctionParams,
334
+ );
335
+
336
+ return {
337
+ needWrapNative: this.needWrapNative,
338
+ dexFuncHasRecipient: false,
339
+ exchangeData,
340
+ targetExchange: swapCallee,
341
+ returnAmountPos: undefined,
342
+ };
343
+ }
344
+
345
+ async getTopPoolsForToken(
346
+ tokenAddress: Address,
347
+ limit: number,
348
+ ): Promise<PoolLiquidity[]> {
349
+ const idleTokens: IdleToken[] = getPoolsByTokenAddress(tokenAddress);
350
+
351
+ return idleTokens
352
+ .map((idleToken: IdleToken) => ({
353
+ // TODO: Re-check?
354
+ // liquidity is infinite, tokens are minted when swapping for idle tokens
355
+ liquidityUSD: 1e12,
356
+ exchange: this.dexKey,
357
+ // address: idleToken.idleAddress,
358
+ address: idleToken.cdoAddress,
359
+ connectorTokens: [getTokenFromIdleToken(idleToken)],
360
+ }))
361
+ .slice(0, limit);
362
+ }
363
+ }