@paraswap/dex-lib 3.8.24 → 3.8.26

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 (222) hide show
  1. package/.vscode/launch.json +16 -0
  2. package/.vscode/settings.json +3 -0
  3. package/build/abi/Uncompressor.json +250 -0
  4. package/build/abi/integral/factory.json +333 -0
  5. package/build/abi/integral/oracle.json +501 -0
  6. package/build/abi/{inception/inception-ineth-pool.json → integral/pool.json} +522 -543
  7. package/build/abi/integral/relayer.json +1536 -0
  8. package/build/dex/aave-v1/aave-v1.d.ts +1 -2
  9. package/build/dex/aave-v1/aave-v1.js +0 -22
  10. package/build/dex/aave-v1/aave-v1.js.map +1 -1
  11. package/build/dex/bProtocol.d.ts +18 -0
  12. package/build/dex/bProtocol.js +39 -0
  13. package/build/dex/bProtocol.js.map +1 -0
  14. package/build/dex/bancor.d.ts +26 -0
  15. package/build/dex/bancor.js +58 -0
  16. package/build/dex/bancor.js.map +1 -0
  17. package/build/dex/compound.d.ts +18 -0
  18. package/build/dex/compound.js +46 -0
  19. package/build/dex/compound.js.map +1 -0
  20. package/build/dex/curve-v2.d.ts +58 -0
  21. package/build/dex/curve-v2.js +180 -0
  22. package/build/dex/curve-v2.js.map +1 -0
  23. package/build/dex/dodo-v1.d.ts +33 -0
  24. package/build/dex/dodo-v1.js +63 -0
  25. package/build/dex/dodo-v1.js.map +1 -0
  26. package/build/dex/etherfi/etherfi.d.ts +26 -0
  27. package/build/dex/etherfi/etherfi.js +96 -0
  28. package/build/dex/etherfi/etherfi.js.map +1 -0
  29. package/build/dex/generic-rfq/rate-fetcher.js +1 -1
  30. package/build/dex/generic-rfq/rate-fetcher.js.map +1 -1
  31. package/build/dex/index.js +2 -0
  32. package/build/dex/index.js.map +1 -1
  33. package/build/dex/integral/config.d.ts +4 -0
  34. package/build/dex/integral/config.js +20 -0
  35. package/build/dex/integral/config.js.map +1 -0
  36. package/build/dex/integral/context.d.ts +40 -0
  37. package/build/dex/integral/context.js +158 -0
  38. package/build/dex/integral/context.js.map +1 -0
  39. package/build/dex/integral/helpers.d.ts +17 -0
  40. package/build/dex/integral/helpers.js +157 -0
  41. package/build/dex/integral/helpers.js.map +1 -0
  42. package/build/dex/integral/integral-factory.d.ts +24 -0
  43. package/build/dex/integral/integral-factory.js +95 -0
  44. package/build/dex/integral/integral-factory.js.map +1 -0
  45. package/build/dex/integral/integral-pool.d.ts +50 -0
  46. package/build/dex/integral/integral-pool.js +149 -0
  47. package/build/dex/integral/integral-pool.js.map +1 -0
  48. package/build/dex/integral/integral-pricing.d.ts +18 -0
  49. package/build/dex/integral/integral-pricing.js +114 -0
  50. package/build/dex/integral/integral-pricing.js.map +1 -0
  51. package/build/dex/integral/integral-relayer.d.ts +42 -0
  52. package/build/dex/integral/integral-relayer.js +192 -0
  53. package/build/dex/integral/integral-relayer.js.map +1 -0
  54. package/build/dex/integral/integral-token.d.ts +27 -0
  55. package/build/dex/integral/integral-token.js +59 -0
  56. package/build/dex/integral/integral-token.js.map +1 -0
  57. package/build/dex/{inception/inception-native.d.ts → integral/integral.d.ts} +19 -21
  58. package/build/dex/integral/integral.js +376 -0
  59. package/build/dex/integral/integral.js.map +1 -0
  60. package/build/dex/integral/types.d.ts +85 -0
  61. package/build/dex/integral/types.js +9 -0
  62. package/build/dex/integral/types.js.map +1 -0
  63. package/build/dex/integral/utils-e2e.d.ts +9 -0
  64. package/build/dex/integral/utils-e2e.js +131 -0
  65. package/build/dex/integral/utils-e2e.js.map +1 -0
  66. package/build/dex/integral/utils.d.ts +17 -0
  67. package/build/dex/integral/utils.js +94 -0
  68. package/build/dex/integral/utils.js.map +1 -0
  69. package/build/dex/lido.d.ts +19 -0
  70. package/build/dex/lido.js +42 -0
  71. package/build/dex/lido.js.map +1 -0
  72. package/build/dex/onebit.d.ts +25 -0
  73. package/build/dex/onebit.js +42 -0
  74. package/build/dex/onebit.js.map +1 -0
  75. package/build/dex/oswap/oswap-pool.js +8 -8
  76. package/build/dex/oswap/oswap-pool.js.map +1 -1
  77. package/build/dex/oswap/oswap.d.ts +3 -3
  78. package/build/dex/oswap/oswap.js +21 -12
  79. package/build/dex/oswap/oswap.js.map +1 -1
  80. package/build/dex/oswap/types.d.ts +4 -4
  81. package/build/dex/platypus/platypus.d.ts +1 -2
  82. package/build/dex/platypus/platypus.js +0 -31
  83. package/build/dex/platypus/platypus.js.map +1 -1
  84. package/build/dex/sdai/config.d.ts +11 -0
  85. package/build/dex/sdai/config.js +21 -0
  86. package/build/dex/sdai/config.js.map +1 -0
  87. package/build/dex/sdai/constants.d.ts +2 -0
  88. package/build/dex/sdai/constants.js +6 -0
  89. package/build/dex/sdai/constants.js.map +1 -0
  90. package/build/dex/sdai/scripts/validate-state.d.ts +1 -0
  91. package/build/dex/sdai/scripts/validate-state.js +102 -0
  92. package/build/dex/sdai/scripts/validate-state.js.map +1 -0
  93. package/build/dex/sdai/sdai-pool.d.ts +16 -0
  94. package/build/dex/sdai/sdai-pool.js +85 -0
  95. package/build/dex/sdai/sdai-pool.js.map +1 -0
  96. package/build/dex/sdai/sdai.d.ts +51 -0
  97. package/build/dex/sdai/sdai.js +172 -0
  98. package/build/dex/sdai/sdai.js.map +1 -0
  99. package/build/dex/sdai/types.d.ts +21 -0
  100. package/build/dex/sdai/types.js +11 -0
  101. package/build/dex/sdai/types.js.map +1 -0
  102. package/build/dex/sdai/utils.d.ts +4 -0
  103. package/build/dex/sdai/utils.js +39 -0
  104. package/build/dex/sdai/utils.js.map +1 -0
  105. package/build/dex/smoothy.d.ts +26 -0
  106. package/build/dex/smoothy.js +48 -0
  107. package/build/dex/smoothy.js.map +1 -0
  108. package/build/dex/stable-pool.d.ts +28 -0
  109. package/build/dex/stable-pool.js +62 -0
  110. package/build/dex/stable-pool.js.map +1 -0
  111. package/build/dex/swaap-v1/swaap-v1.d.ts +1 -2
  112. package/build/dex/swaap-v1/swaap-v1.js +0 -10
  113. package/build/dex/swaap-v1/swaap-v1.js.map +1 -1
  114. package/build/dex/trader-joe-v2.1/optimizer.d.ts +2 -0
  115. package/build/dex/trader-joe-v2.1/optimizer.js +44 -0
  116. package/build/dex/trader-joe-v2.1/optimizer.js.map +1 -0
  117. package/build/dex/trader-joe-v2.1.d.ts +43 -0
  118. package/build/dex/trader-joe-v2.1.js +79 -0
  119. package/build/dex/trader-joe-v2.1.js.map +1 -0
  120. package/build/executor/compressor/Compressor.d.ts +23 -0
  121. package/build/executor/compressor/Compressor.js +144 -0
  122. package/build/executor/compressor/Compressor.js.map +1 -0
  123. package/build/executor/compressor/compress.d.ts +17 -0
  124. package/build/executor/compressor/compress.js +187 -0
  125. package/build/executor/compressor/compress.js.map +1 -0
  126. package/build/executor/compressor/compress_functions/addressSubstitution.d.ts +6 -0
  127. package/build/executor/compressor/compress_functions/addressSubstitution.js +29 -0
  128. package/build/executor/compressor/compress_functions/addressSubstitution.js.map +1 -0
  129. package/build/executor/compressor/compress_functions/addressSubstitutionFromObject.d.ts +10 -0
  130. package/build/executor/compressor/compress_functions/addressSubstitutionFromObject.js +40 -0
  131. package/build/executor/compressor/compress_functions/addressSubstitutionFromObject.js.map +1 -0
  132. package/build/executor/compressor/compress_functions/byteSubstitution.d.ts +11 -0
  133. package/build/executor/compressor/compress_functions/byteSubstitution.js +64 -0
  134. package/build/executor/compressor/compress_functions/byteSubstitution.js.map +1 -0
  135. package/build/executor/compressor/compress_functions/ffSubstitution.d.ts +7 -0
  136. package/build/executor/compressor/compress_functions/ffSubstitution.js +54 -0
  137. package/build/executor/compressor/compress_functions/ffSubstitution.js.map +1 -0
  138. package/build/executor/compressor/compress_functions/findLongestAndSubstitute.d.ts +8 -0
  139. package/build/executor/compressor/compress_functions/findLongestAndSubstitute.js +67 -0
  140. package/build/executor/compressor/compress_functions/findLongestAndSubstitute.js.map +1 -0
  141. package/build/executor/compressor/compress_functions/findLongestAndSubstitute_old.d.ts +8 -0
  142. package/build/executor/compressor/compress_functions/findLongestAndSubstitute_old.js +61 -0
  143. package/build/executor/compressor/compress_functions/findLongestAndSubstitute_old.js.map +1 -0
  144. package/build/executor/compressor/compress_functions/reverseAddressSubstitution.d.ts +6 -0
  145. package/build/executor/compressor/compress_functions/reverseAddressSubstitution.js +37 -0
  146. package/build/executor/compressor/compress_functions/reverseAddressSubstitution.js.map +1 -0
  147. package/build/executor/compressor/compress_functions/searchUnusedBytes.d.ts +9 -0
  148. package/build/executor/compressor/compress_functions/searchUnusedBytes.js +33 -0
  149. package/build/executor/compressor/compress_functions/searchUnusedBytes.js.map +1 -0
  150. package/build/executor/compressor/fetchSaved.d.ts +0 -0
  151. package/build/executor/compressor/fetchSaved.js +17 -0
  152. package/build/executor/compressor/fetchSaved.js.map +1 -0
  153. package/build/executor/compressor/utils/computeCostL2.d.ts +2 -0
  154. package/build/executor/compressor/utils/computeCostL2.js +14 -0
  155. package/build/executor/compressor/utils/computeCostL2.js.map +1 -0
  156. package/build/executor/compressor/utils/findLongestDuplicatedString.d.ts +2 -0
  157. package/build/executor/compressor/utils/findLongestDuplicatedString.js +30 -0
  158. package/build/executor/compressor/utils/findLongestDuplicatedString.js.map +1 -0
  159. package/build/executor/compressor/utils/getAllIndexes.d.ts +2 -0
  160. package/build/executor/compressor/utils/getAllIndexes.js +12 -0
  161. package/build/executor/compressor/utils/getAllIndexes.js.map +1 -0
  162. package/build/executor/compressor/utils/getByteForAddress.d.ts +2 -0
  163. package/build/executor/compressor/utils/getByteForAddress.js +14 -0
  164. package/build/executor/compressor/utils/getByteForAddress.js.map +1 -0
  165. package/build/executor/compressor/utils/intTo2Bytes.d.ts +2 -0
  166. package/build/executor/compressor/utils/intTo2Bytes.js +14 -0
  167. package/build/executor/compressor/utils/intTo2Bytes.js.map +1 -0
  168. package/build/executor/compressor/utils/intTo3Bytes.d.ts +2 -0
  169. package/build/executor/compressor/utils/intTo3Bytes.js +18 -0
  170. package/build/executor/compressor/utils/intTo3Bytes.js.map +1 -0
  171. package/build/executor/compressor/utils/intToByte.d.ts +2 -0
  172. package/build/executor/compressor/utils/intToByte.js +10 -0
  173. package/build/executor/compressor/utils/intToByte.js.map +1 -0
  174. package/package.json +1 -1
  175. package/src/abi/oswap/oswap.abi.json +452 -0
  176. package/src/dex/generic-rfq/rate-fetcher.ts +6 -5
  177. package/src/dex/index.ts +2 -0
  178. package/src/dex/oswap/config.ts +32 -0
  179. package/src/dex/oswap/oswap-e2e.test.ts +91 -0
  180. package/src/dex/oswap/oswap-events.test.ts +88 -0
  181. package/src/dex/oswap/oswap-integration.test.ts +291 -0
  182. package/src/dex/oswap/oswap-pool.ts +190 -0
  183. package/src/dex/oswap/oswap.ts +414 -0
  184. package/src/dex/oswap/types.ts +29 -0
  185. package/build/abi/inception/inception-ineth.json +0 -609
  186. package/build/abi/inception/inception-ratio-feed.json +0 -93
  187. package/build/abi/inception/inception-vault.json +0 -991
  188. package/build/dex/idle-dao/idle-dao-pool.d.ts +0 -56
  189. package/build/dex/idle-dao/idle-dao-pool.js +0 -176
  190. package/build/dex/idle-dao/idle-dao-pool.js.map +0 -1
  191. package/build/dex/idle-dao/idle-dao-pooling-pool.d.ts +0 -16
  192. package/build/dex/idle-dao/idle-dao-pooling-pool.js +0 -57
  193. package/build/dex/idle-dao/idle-dao-pooling-pool.js.map +0 -1
  194. package/build/dex/idle-dao/scripts.d.ts +0 -9
  195. package/build/dex/idle-dao/scripts.js +0 -552
  196. package/build/dex/idle-dao/scripts.js.map +0 -1
  197. package/build/dex/inception/config.d.ts +0 -5
  198. package/build/dex/inception/config.js +0 -112
  199. package/build/dex/inception/config.js.map +0 -1
  200. package/build/dex/inception/inception-event-pool.d.ts +0 -23
  201. package/build/dex/inception/inception-event-pool.js +0 -34
  202. package/build/dex/inception/inception-event-pool.js.map +0 -1
  203. package/build/dex/inception/inception-native.js +0 -131
  204. package/build/dex/inception/inception-native.js.map +0 -1
  205. package/build/dex/inception/inception-pool.d.ts +0 -18
  206. package/build/dex/inception/inception-pool.js +0 -32
  207. package/build/dex/inception/inception-pool.js.map +0 -1
  208. package/build/dex/inception/inception-price-feed.d.ts +0 -19
  209. package/build/dex/inception/inception-price-feed.js +0 -51
  210. package/build/dex/inception/inception-price-feed.js.map +0 -1
  211. package/build/dex/inception/inception.d.ts +0 -44
  212. package/build/dex/inception/inception.js +0 -162
  213. package/build/dex/inception/inception.js.map +0 -1
  214. package/build/dex/inception/tokens.d.ts +0 -9
  215. package/build/dex/inception/tokens.js +0 -28
  216. package/build/dex/inception/tokens.js.map +0 -1
  217. package/build/dex/inception/types.d.ts +0 -22
  218. package/build/dex/inception/types.js +0 -3
  219. package/build/dex/inception/types.js.map +0 -1
  220. package/build/dex/inception/utils.d.ts +0 -7
  221. package/build/dex/inception/utils.js +0 -58
  222. package/build/dex/inception/utils.js.map +0 -1
@@ -0,0 +1,414 @@
1
+ import { Interface } from '@ethersproject/abi';
2
+ import { AsyncOrSync } from 'ts-essentials';
3
+ import {
4
+ Token,
5
+ Address,
6
+ ExchangePrices,
7
+ PoolPrices,
8
+ AdapterExchangeParam,
9
+ PoolLiquidity,
10
+ Logger,
11
+ NumberAsString,
12
+ DexExchangeParam,
13
+ TransferFeeParams,
14
+ } from '../../types';
15
+ import {
16
+ SwapSide,
17
+ Network,
18
+ DEST_TOKEN_PARASWAP_TRANSFERS,
19
+ SRC_TOKEN_PARASWAP_TRANSFERS,
20
+ } from '../../constants';
21
+ import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
22
+ import { getDexKeysWithNetwork, getBigIntPow } from '../../utils';
23
+ import { Context, IDex } from '../../dex/idex';
24
+ import { IDexHelper } from '../../dex-helper/idex-helper';
25
+ import { OSwapData, OSwapPool, OSwapPoolState } from './types';
26
+ import {
27
+ SimpleExchange,
28
+ getLocalDeadlineAsFriendlyPlaceholder,
29
+ } from '../simple-exchange';
30
+ import { OSwapConfig, Adapters, OSWAP_GAS_COST } from './config';
31
+ import { OSwapEventPool } from './oswap-pool';
32
+ import OSwapABI from '../../abi/oswap/oswap.abi.json';
33
+ import { extractReturnAmountPosition } from '../../executor/utils';
34
+ import { applyTransferFee } from '../../lib/token-transfer-fee';
35
+
36
+ export class OSwap extends SimpleExchange implements IDex<OSwapData> {
37
+ readonly eventPools: { [id: string]: OSwapEventPool } = {};
38
+
39
+ readonly hasConstantPriceLargeAmounts = false;
40
+
41
+ // This may change in the future, but currently OSwap does not support native ETH.
42
+ readonly needWrapNative = true;
43
+
44
+ readonly isFeeOnTransferSupported = true;
45
+
46
+ public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
47
+ getDexKeysWithNetwork(OSwapConfig);
48
+
49
+ logger: Logger;
50
+
51
+ readonly iOSwap: Interface;
52
+
53
+ readonly pools: [OSwapPool];
54
+
55
+ constructor(
56
+ readonly network: Network,
57
+ readonly dexKey: string,
58
+ readonly dexHelper: IDexHelper,
59
+ protected adapters = Adapters[network] || {},
60
+ ) {
61
+ super(dexHelper, dexKey);
62
+ this.logger = dexHelper.getLogger(dexKey);
63
+ this.iOSwap = new Interface(OSwapABI);
64
+
65
+ this.pools = OSwapConfig[dexKey][network].pools;
66
+
67
+ // Create an OSwapEventPool per pool, to track each pool's state by subscribing to on-chain events.
68
+ for (const pool of this.pools) {
69
+ this.eventPools[pool.id] = new OSwapEventPool(
70
+ dexKey,
71
+ pool,
72
+ network,
73
+ dexHelper,
74
+ this.logger,
75
+ );
76
+ }
77
+ }
78
+
79
+ // Returns the list of contract adapters (name and index)
80
+ // for a buy/sell. Return null if there are no adapters.
81
+ getAdapters(side: SwapSide): { name: string; index: number }[] | null {
82
+ return null;
83
+ }
84
+
85
+ // Returns the pool matching the specified token pair or null if none found.
86
+ // Note: OSwap V1 does not support more than 1 pool per pair.
87
+ getPoolByTokenPair(srcToken: Token, destToken: Token): OSwapPool | null {
88
+ const srcAddress = srcToken.address.toLowerCase();
89
+ const destAddress = destToken.address.toLowerCase();
90
+
91
+ // A pair must have 2 different tokens.
92
+ if (srcAddress === destAddress) return null;
93
+
94
+ for (const pool of this.pools) {
95
+ if (
96
+ (srcAddress === pool.token0 && destAddress === pool.token1) ||
97
+ (srcAddress === pool.token1 && destAddress === pool.token0)
98
+ ) {
99
+ return pool;
100
+ }
101
+ }
102
+ return null;
103
+ }
104
+
105
+ getPoolById(id: string): OSwapPool | null {
106
+ for (const pool of this.pools) {
107
+ if (pool.id === id) return pool;
108
+ }
109
+ return null;
110
+ }
111
+
112
+ // Returns a list of pool using the token.
113
+ getPoolsByTokenAddress(tokenAddress: Address): OSwapPool[] {
114
+ const address = tokenAddress.toLowerCase();
115
+ let pools: OSwapPool[] = [];
116
+ for (const pool of this.pools) {
117
+ if (address === pool.token0 || address === pool.token1) {
118
+ pools.push(pool);
119
+ }
120
+ }
121
+ return pools;
122
+ }
123
+
124
+ // Returns the list of pool identifiers that can be used
125
+ // for a given swap. poolIdentifiers must be unique
126
+ // across DEXes. It is recommended to use
127
+ // ${dexKey}_${poolAddress} as a poolIdentifier
128
+ async getPoolIdentifiers(
129
+ srcToken: Token,
130
+ destToken: Token,
131
+ side: SwapSide,
132
+ blockNumber: number,
133
+ ): Promise<string[]> {
134
+ const pool = this.getPoolByTokenPair(srcToken, destToken);
135
+ return pool ? [pool.id] : [];
136
+ }
137
+
138
+ // Sell: Given "amount" of "from" token, how much of "to" token will be received by the trader.
139
+ // Buy: Given "amount" of "dest" token, how much of "to" token is required from the trader.
140
+ // Note: OSwap traderate is at precision 36.
141
+ calcPrice(
142
+ pool: OSwapPool,
143
+ state: OSwapPoolState,
144
+ from: Token,
145
+ amount: bigint,
146
+ side: SwapSide,
147
+ checkLiquidity = true,
148
+ ): bigint {
149
+ const rate =
150
+ from.address.toLowerCase() === pool.token0
151
+ ? BigInt(state.traderate0)
152
+ : BigInt(state.traderate1);
153
+
154
+ const price =
155
+ side === SwapSide.SELL
156
+ ? (amount * rate) / getBigIntPow(36)
157
+ : (amount * getBigIntPow(36)) / rate;
158
+
159
+ if (
160
+ checkLiquidity &&
161
+ !this.hasEnoughLiquidity(pool, state, from, amount, price, side)
162
+ ) {
163
+ throw new Error('Not enough liquidity');
164
+ }
165
+
166
+ return price;
167
+ }
168
+
169
+ // Returns true if the pool has enough liquidity for the swap. False otherwise.
170
+ hasEnoughLiquidity(
171
+ pool: OSwapPool,
172
+ state: OSwapPoolState,
173
+ from: Token,
174
+ amount: bigint,
175
+ needed: bigint,
176
+ side: SwapSide,
177
+ ): boolean {
178
+ if (side === SwapSide.SELL) {
179
+ return from.address.toLowerCase() === pool.token0
180
+ ? needed <= BigInt(state.balance1)
181
+ : needed <= BigInt(state.balance0);
182
+ }
183
+ // SwapSide.BUY
184
+ return from.address.toLowerCase() === pool.token0
185
+ ? amount <= BigInt(state.balance1)
186
+ : amount <= BigInt(state.balance0);
187
+ }
188
+
189
+ // Returns pool prices for amounts.
190
+ // If limitPools is defined only pools in limitPools
191
+ // should be used. If limitPools is undefined then
192
+ // any pool can be used.
193
+ async getPricesVolume(
194
+ srcToken: Token,
195
+ destToken: Token,
196
+ amounts: bigint[],
197
+ side: SwapSide,
198
+ blockNumber: number,
199
+ limitPools?: string[],
200
+ transferFees: TransferFeeParams = {
201
+ srcFee: 0,
202
+ destFee: 0,
203
+ srcDexFee: 0,
204
+ destDexFee: 0,
205
+ },
206
+ ): Promise<null | ExchangePrices<OSwapData>> {
207
+ try {
208
+ // Get the pool to use.
209
+ const pool = this.getPoolByTokenPair(srcToken, destToken);
210
+ if (!pool) return null;
211
+
212
+ // Make sure the pool meets the optional limitPools filter.
213
+ if (limitPools && !limitPools.includes(pool.id)) return null;
214
+
215
+ const eventPool = this.eventPools[pool.id];
216
+
217
+ if (!eventPool) {
218
+ this.logger.error(`OSwap pool ${pool.id}: No EventPool found.`);
219
+
220
+ return null;
221
+ }
222
+
223
+ const state = await eventPool.getStateOrGenerate(blockNumber);
224
+
225
+ // Calculate the prices
226
+ const unitAmount = getBigIntPow(18);
227
+ const unitPrice = this.calcPrice(
228
+ pool,
229
+ state,
230
+ srcToken,
231
+ unitAmount,
232
+ side,
233
+ false,
234
+ );
235
+ const prices = amounts.map(amount =>
236
+ this.calcPrice(pool, state, srcToken, amount, side),
237
+ );
238
+
239
+ const [unitPriceWithFee, ...pricesWithFee] = applyTransferFee(
240
+ [unitPrice, ...prices],
241
+ side,
242
+ side === SwapSide.SELL ? transferFees.srcFee : transferFees.destFee,
243
+ side === SwapSide.SELL
244
+ ? SRC_TOKEN_PARASWAP_TRANSFERS
245
+ : DEST_TOKEN_PARASWAP_TRANSFERS,
246
+ );
247
+
248
+ return [
249
+ {
250
+ prices: pricesWithFee,
251
+ unit: unitPriceWithFee,
252
+ data: {
253
+ pool: pool.address,
254
+ path: [srcToken.address, destToken.address],
255
+ },
256
+ exchange: this.dexKey,
257
+ poolIdentifier: pool.id,
258
+ gasCost: OSWAP_GAS_COST,
259
+ poolAddresses: [pool.address],
260
+ },
261
+ ];
262
+ } catch (e) {
263
+ this.logger.error(
264
+ `Error_getPricesVolume ${srcToken.address || srcToken.symbol}, ${
265
+ destToken.address || destToken.symbol
266
+ }, ${side}:`,
267
+ e,
268
+ );
269
+
270
+ return null;
271
+ }
272
+ }
273
+
274
+ // Returns estimated gas cost of calldata for this DEX in multiSwap
275
+ getCalldataGasCost(poolPrices: PoolPrices<OSwapData>): number | number[] {
276
+ return (
277
+ CALLDATA_GAS_COST.DEX_OVERHEAD +
278
+ // ParentStruct header
279
+ CALLDATA_GAS_COST.OFFSET_SMALL +
280
+ // ParentStruct -> path[] header
281
+ CALLDATA_GAS_COST.OFFSET_SMALL +
282
+ // ParentStruct -> path length
283
+ CALLDATA_GAS_COST.LENGTH_SMALL +
284
+ // ParentStruct -> path[0]
285
+ CALLDATA_GAS_COST.ADDRESS +
286
+ // ParentStruct -> path[1]
287
+ CALLDATA_GAS_COST.ADDRESS +
288
+ // ParentStruct -> receiver header
289
+ CALLDATA_GAS_COST.OFFSET_SMALL +
290
+ // ParentStruct -> receiver
291
+ CALLDATA_GAS_COST.ADDRESS
292
+ );
293
+ }
294
+
295
+ // Encode params required by the exchange adapter
296
+ // Used for multiSwap, buy & megaSwap
297
+ getAdapterParam(
298
+ srcToken: string,
299
+ destToken: string,
300
+ srcAmount: string,
301
+ destAmount: string,
302
+ data: OSwapData,
303
+ side: SwapSide,
304
+ ): AdapterExchangeParam {
305
+ return {
306
+ targetExchange: data.pool,
307
+ payload: '',
308
+ networkFee: '0',
309
+ };
310
+ }
311
+
312
+ // Encode call data used by simpleSwap like routers
313
+ // Used for simpleSwap & simpleBuy
314
+ getDexParam(
315
+ srcToken: Address,
316
+ destToken: Address,
317
+ srcAmount: NumberAsString,
318
+ destAmount: NumberAsString,
319
+ recipient: Address,
320
+ data: OSwapData,
321
+ side: SwapSide,
322
+ _: Context,
323
+ executorAddress: Address,
324
+ ): DexExchangeParam {
325
+ let method: string;
326
+ let args: any;
327
+ let returnAmountPos: number | undefined = undefined;
328
+
329
+ const deadline = getLocalDeadlineAsFriendlyPlaceholder();
330
+ if (side === SwapSide.SELL) {
331
+ method = 'swapExactTokensForTokens';
332
+ returnAmountPos = extractReturnAmountPosition(
333
+ this.iOSwap,
334
+ method,
335
+ 'amounts',
336
+ 1,
337
+ );
338
+ args = [srcAmount, destAmount, data.path, recipient, deadline];
339
+ } else {
340
+ method = 'swapTokensForExactTokens';
341
+ args = [destAmount, srcAmount, data.path, recipient, deadline];
342
+ }
343
+
344
+ const swapData = this.iOSwap.encodeFunctionData(method, args);
345
+
346
+ return {
347
+ needWrapNative: this.needWrapNative,
348
+ dexFuncHasRecipient: true,
349
+ exchangeData: swapData,
350
+ targetExchange: data.pool,
351
+ returnAmountPos,
352
+ };
353
+ }
354
+
355
+ // This is called once before getTopPoolsForToken is
356
+ // called for multiple tokens. This can be helpful to
357
+ // update common state required for calculating
358
+ // getTopPoolsForToken. It is optional for a DEX
359
+ // to implement this
360
+ async updatePoolState(): Promise<void> {
361
+ return Promise.resolve();
362
+ }
363
+
364
+ // Returns a list of top pools based on liquidity. Max
365
+ // limit number pools should be returned.
366
+ async getTopPoolsForToken(
367
+ tokenAddress: Address,
368
+ limit: number,
369
+ ): Promise<PoolLiquidity[]> {
370
+ // Get the list of pools using the token.
371
+ const pools = this.getPoolsByTokenAddress(tokenAddress);
372
+ if (!pools.length) return [];
373
+
374
+ const results = await Promise.all<PoolLiquidity>(
375
+ pools.map(async pool => {
376
+ // Get the pool's balance and its USD value.
377
+ const eventPool = this.eventPools[pool.id];
378
+ const blockNumber =
379
+ await this.dexHelper.web3Provider.eth.getBlockNumber();
380
+ const state = await eventPool.getStateOrGenerate(blockNumber);
381
+
382
+ const usd0 = await this.dexHelper.getTokenUSDPrice(
383
+ { address: pool.token0, decimals: 18 },
384
+ BigInt(state.balance0),
385
+ );
386
+ const usd1 = await this.dexHelper.getTokenUSDPrice(
387
+ { address: pool.token1, decimals: 18 },
388
+ BigInt(state.balance1),
389
+ );
390
+
391
+ // Get the other token in the pair.
392
+ const pairedToken =
393
+ pool.token0 === tokenAddress.toLowerCase()
394
+ ? { address: pool.token1, decimals: 18 }
395
+ : { address: pool.token0, decimals: 18 };
396
+
397
+ return {
398
+ exchange: this.dexKey,
399
+ address: pool.address,
400
+ connectorTokens: [pairedToken],
401
+ liquidityUSD: usd0 + usd1,
402
+ };
403
+ }),
404
+ );
405
+ return results
406
+ .filter(r => r)
407
+ .sort((a, b) => a.liquidityUSD - b.liquidityUSD)
408
+ .slice(0, limit);
409
+ }
410
+
411
+ // This is optional function in case if your implementation has acquired any resources
412
+ // you need to release for graceful shutdown. For example, it may be any interval timer
413
+ releaseResources(): AsyncOrSync<void> {}
414
+ }
@@ -0,0 +1,29 @@
1
+ import { Address } from '../../types';
2
+
3
+ // OSwapPoolState is the state of the event subscriber. It is the minimum
4
+ // set of parameters required to compute pool prices.
5
+ export type OSwapPoolState = {
6
+ traderate0: string;
7
+ traderate1: string;
8
+ balance0: string;
9
+ balance1: string;
10
+ };
11
+
12
+ // OSwapPoolState is the state of the event subscriber. It is the minimum
13
+ // set of parameters required to compute pool prices.
14
+ export type OSwapData = {
15
+ pool: Address;
16
+ path: Address[];
17
+ };
18
+
19
+ // Each pool has a contract address and token pairs.
20
+ export type OSwapPool = {
21
+ id: string;
22
+ address: Address;
23
+ token0: Address;
24
+ token1: Address;
25
+ };
26
+
27
+ export type DexParams = {
28
+ pools: [OSwapPool];
29
+ };