@paraswap/dex-lib 3.8.30 → 3.8.32

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 (237) hide show
  1. package/build/abi/AaveV1_lending_pool.json +27 -0
  2. package/build/abi/Aave_GSM.json +751 -0
  3. package/build/abi/aave-gsm/Aave_GSM.json +751 -0
  4. package/build/abi/aave-gsm/IFeeStrategy.json +78 -0
  5. package/build/abi/inception/inception-ineth-pool.json +1062 -0
  6. package/build/abi/inception/inception-ineth.json +609 -0
  7. package/build/abi/inception/inception-ratio-feed.json +93 -0
  8. package/build/abi/inception/inception-vault.json +991 -0
  9. package/build/abi/platypus/asset.json +490 -0
  10. package/build/abi/platypus/avax-pool.json +820 -0
  11. package/build/abi/platypus/oracle.json +119 -0
  12. package/build/abi/platypus/pool.json +677 -0
  13. package/build/abi/smardex/all/smardex-router.json +648 -0
  14. package/build/abi/smardex/layer-1/smardex-factory.json +242 -0
  15. package/build/abi/smardex/layer-1/smardex-pool.json +506 -0
  16. package/build/abi/smardex/layer-2/smardex-factory.json +185 -0
  17. package/build/abi/smardex/layer-2/smardex-pool.json +578 -0
  18. package/build/abi/stkGHO.json +1131 -0
  19. package/build/abi/swaap-v1/pool.json +1346 -0
  20. package/build/abi/usual-bond/usd0pp.abi.json +867 -0
  21. package/build/dex/aave-gsm/aave-gsm-pool.d.ts +28 -0
  22. package/build/dex/aave-gsm/aave-gsm-pool.js +117 -0
  23. package/build/dex/aave-gsm/aave-gsm-pool.js.map +1 -0
  24. package/build/dex/aave-gsm/aave-gsm.d.ts +43 -0
  25. package/build/dex/aave-gsm/aave-gsm.js +306 -0
  26. package/build/dex/aave-gsm/aave-gsm.js.map +1 -0
  27. package/build/dex/aave-gsm/config.d.ts +3 -0
  28. package/build/dex/aave-gsm/config.js +17 -0
  29. package/build/dex/aave-gsm/config.js.map +1 -0
  30. package/build/dex/aave-gsm/types.d.ts +18 -0
  31. package/build/dex/aave-gsm/types.js +3 -0
  32. package/build/dex/aave-gsm/types.js.map +1 -0
  33. package/build/dex/aave-v1/aave-v1.d.ts +34 -0
  34. package/build/dex/aave-v1/aave-v1.js +180 -0
  35. package/build/dex/aave-v1/aave-v1.js.map +1 -0
  36. package/build/dex/aave-v1/config.d.ts +11 -0
  37. package/build/dex/aave-v1/config.js +20 -0
  38. package/build/dex/aave-v1/config.js.map +1 -0
  39. package/build/dex/aave-v1/tokens-mainnet.json +134 -0
  40. package/build/dex/aave-v1/tokens.d.ts +3 -0
  41. package/build/dex/aave-v1/tokens.js +50 -0
  42. package/build/dex/aave-v1/tokens.js.map +1 -0
  43. package/build/dex/aave-v1/types.d.ts +12 -0
  44. package/build/dex/aave-v1/types.js +3 -0
  45. package/build/dex/aave-v1/types.js.map +1 -0
  46. package/build/dex/idle-dao/idle-dao-pool.d.ts +56 -0
  47. package/build/dex/idle-dao/idle-dao-pool.js +176 -0
  48. package/build/dex/idle-dao/idle-dao-pool.js.map +1 -0
  49. package/build/dex/idle-dao/idle-dao-pooling-pool.d.ts +16 -0
  50. package/build/dex/idle-dao/idle-dao-pooling-pool.js +57 -0
  51. package/build/dex/idle-dao/idle-dao-pooling-pool.js.map +1 -0
  52. package/build/dex/idle-dao/scripts.d.ts +9 -0
  53. package/build/dex/idle-dao/scripts.js +552 -0
  54. package/build/dex/idle-dao/scripts.js.map +1 -0
  55. package/build/dex/inception/config.d.ts +5 -0
  56. package/build/dex/inception/config.js +112 -0
  57. package/build/dex/inception/config.js.map +1 -0
  58. package/build/dex/inception/inception-event-pool.d.ts +25 -0
  59. package/build/dex/inception/inception-event-pool.js +57 -0
  60. package/build/dex/inception/inception-event-pool.js.map +1 -0
  61. package/build/dex/inception/inception-native.d.ts +43 -0
  62. package/build/dex/inception/inception-native.js +131 -0
  63. package/build/dex/inception/inception-native.js.map +1 -0
  64. package/build/dex/inception/inception-pool.d.ts +18 -0
  65. package/build/dex/inception/inception-pool.js +32 -0
  66. package/build/dex/inception/inception-pool.js.map +1 -0
  67. package/build/dex/inception/inception-price-feed.d.ts +19 -0
  68. package/build/dex/inception/inception-price-feed.js +51 -0
  69. package/build/dex/inception/inception-price-feed.js.map +1 -0
  70. package/build/dex/inception/inception.d.ts +44 -0
  71. package/build/dex/inception/inception.js +177 -0
  72. package/build/dex/inception/inception.js.map +1 -0
  73. package/build/dex/inception/tokens.d.ts +9 -0
  74. package/build/dex/inception/tokens.js +28 -0
  75. package/build/dex/inception/tokens.js.map +1 -0
  76. package/build/dex/inception/types.d.ts +22 -0
  77. package/build/dex/inception/types.js +3 -0
  78. package/build/dex/inception/types.js.map +1 -0
  79. package/build/dex/inception/utils.d.ts +7 -0
  80. package/build/dex/inception/utils.js +58 -0
  81. package/build/dex/inception/utils.js.map +1 -0
  82. package/build/dex/index.js +4 -0
  83. package/build/dex/index.js.map +1 -1
  84. package/build/dex/platypus/asset.d.ts +14 -0
  85. package/build/dex/platypus/asset.js +97 -0
  86. package/build/dex/platypus/asset.js.map +1 -0
  87. package/build/dex/platypus/config.d.ts +11 -0
  88. package/build/dex/platypus/config.js +69 -0
  89. package/build/dex/platypus/config.js.map +1 -0
  90. package/build/dex/platypus/platypus.d.ts +63 -0
  91. package/build/dex/platypus/platypus.js +473 -0
  92. package/build/dex/platypus/platypus.js.map +1 -0
  93. package/build/dex/platypus/pool-avax.d.ts +8 -0
  94. package/build/dex/platypus/pool-avax.js +54 -0
  95. package/build/dex/platypus/pool-avax.js.map +1 -0
  96. package/build/dex/platypus/pool-base.d.ts +19 -0
  97. package/build/dex/platypus/pool-base.js +86 -0
  98. package/build/dex/platypus/pool-base.js.map +1 -0
  99. package/build/dex/platypus/pool-pure.d.ts +8 -0
  100. package/build/dex/platypus/pool-pure.js +37 -0
  101. package/build/dex/platypus/pool-pure.js.map +1 -0
  102. package/build/dex/platypus/pool-subscriber.d.ts +14 -0
  103. package/build/dex/platypus/pool-subscriber.js +167 -0
  104. package/build/dex/platypus/pool-subscriber.js.map +1 -0
  105. package/build/dex/platypus/pool.d.ts +8 -0
  106. package/build/dex/platypus/pool.js +58 -0
  107. package/build/dex/platypus/pool.js.map +1 -0
  108. package/build/dex/platypus/types.d.ts +92 -0
  109. package/build/dex/platypus/types.js +10 -0
  110. package/build/dex/platypus/types.js.map +1 -0
  111. package/build/dex/quickswap/spiritswap-v3.d.ts +6 -0
  112. package/build/dex/quickswap/spiritswap-v3.js +19 -0
  113. package/build/dex/quickswap/spiritswap-v3.js.map +1 -0
  114. package/build/dex/smardex/config.d.ts +4 -0
  115. package/build/dex/smardex/config.js +64 -0
  116. package/build/dex/smardex/config.js.map +1 -0
  117. package/build/dex/smardex/constants.d.ts +18 -0
  118. package/build/dex/smardex/constants.js +33 -0
  119. package/build/dex/smardex/constants.js.map +1 -0
  120. package/build/dex/smardex/sdk/constants.d.ts +6 -0
  121. package/build/dex/smardex/sdk/constants.js +13 -0
  122. package/build/dex/smardex/sdk/constants.js.map +1 -0
  123. package/build/dex/smardex/sdk/core.d.ts +113 -0
  124. package/build/dex/smardex/sdk/core.js +499 -0
  125. package/build/dex/smardex/sdk/core.js.map +1 -0
  126. package/build/dex/smardex/sdk/errors.d.ts +15 -0
  127. package/build/dex/smardex/sdk/errors.js +22 -0
  128. package/build/dex/smardex/sdk/errors.js.map +1 -0
  129. package/build/dex/smardex/sdk/types.d.ts +62 -0
  130. package/build/dex/smardex/sdk/types.js +3 -0
  131. package/build/dex/smardex/sdk/types.js.map +1 -0
  132. package/build/dex/smardex/sdk/utils.d.ts +44 -0
  133. package/build/dex/smardex/sdk/utils.js +133 -0
  134. package/build/dex/smardex/sdk/utils.js.map +1 -0
  135. package/build/dex/smardex/smardex-event-pool.d.ts +28 -0
  136. package/build/dex/smardex/smardex-event-pool.js +119 -0
  137. package/build/dex/smardex/smardex-event-pool.js.map +1 -0
  138. package/build/dex/smardex/smardex.d.ts +65 -0
  139. package/build/dex/smardex/smardex.js +519 -0
  140. package/build/dex/smardex/smardex.js.map +1 -0
  141. package/build/dex/smardex/types.d.ts +55 -0
  142. package/build/dex/smardex/types.js +3 -0
  143. package/build/dex/smardex/types.js.map +1 -0
  144. package/build/dex/solidly/forks-override/cone.d.ts +22 -0
  145. package/build/dex/solidly/forks-override/cone.js +53 -0
  146. package/build/dex/solidly/forks-override/cone.js.map +1 -0
  147. package/build/dex/solidly/forks-override/solisnek.d.ts +8 -0
  148. package/build/dex/solidly/forks-override/solisnek.js +15 -0
  149. package/build/dex/solidly/forks-override/solisnek.js.map +1 -0
  150. package/build/dex/stkgho/config.d.ts +3 -0
  151. package/build/dex/stkgho/config.js +14 -0
  152. package/build/dex/stkgho/config.js.map +1 -0
  153. package/build/dex/stkgho/stkgho-pool.d.ts +41 -0
  154. package/build/dex/stkgho/stkgho-pool.js +81 -0
  155. package/build/dex/stkgho/stkgho-pool.js.map +1 -0
  156. package/build/dex/stkgho/stkgho.d.ts +41 -0
  157. package/build/dex/stkgho/stkgho.js +163 -0
  158. package/build/dex/stkgho/stkgho.js.map +1 -0
  159. package/build/dex/stkgho/types.d.ts +11 -0
  160. package/build/dex/stkgho/types.js +3 -0
  161. package/build/dex/stkgho/types.js.map +1 -0
  162. package/build/dex/swaap-v1/config.d.ts +17 -0
  163. package/build/dex/swaap-v1/config.js +56 -0
  164. package/build/dex/swaap-v1/config.js.map +1 -0
  165. package/build/dex/swaap-v1/constants.d.ts +6 -0
  166. package/build/dex/swaap-v1/constants.js +17 -0
  167. package/build/dex/swaap-v1/constants.js.map +1 -0
  168. package/build/dex/swaap-v1/libraries/ChainlinkUtils.d.ts +25 -0
  169. package/build/dex/swaap-v1/libraries/ChainlinkUtils.js +62 -0
  170. package/build/dex/swaap-v1/libraries/ChainlinkUtils.js.map +1 -0
  171. package/build/dex/swaap-v1/libraries/Const.d.ts +18 -0
  172. package/build/dex/swaap-v1/libraries/Const.js +26 -0
  173. package/build/dex/swaap-v1/libraries/Const.js.map +1 -0
  174. package/build/dex/swaap-v1/libraries/GeometricBrownianMotion.d.ts +77 -0
  175. package/build/dex/swaap-v1/libraries/GeometricBrownianMotion.js +316 -0
  176. package/build/dex/swaap-v1/libraries/GeometricBrownianMotion.js.map +1 -0
  177. package/build/dex/swaap-v1/libraries/LogExpMath.d.ts +75 -0
  178. package/build/dex/swaap-v1/libraries/LogExpMath.js +418 -0
  179. package/build/dex/swaap-v1/libraries/LogExpMath.js.map +1 -0
  180. package/build/dex/swaap-v1/libraries/MathMMM.d.ts +185 -0
  181. package/build/dex/swaap-v1/libraries/MathMMM.js +453 -0
  182. package/build/dex/swaap-v1/libraries/MathMMM.js.map +1 -0
  183. package/build/dex/swaap-v1/libraries/Num.d.ts +31 -0
  184. package/build/dex/swaap-v1/libraries/Num.js +167 -0
  185. package/build/dex/swaap-v1/libraries/Num.js.map +1 -0
  186. package/build/dex/swaap-v1/libraries/PoolQuotations.d.ts +23 -0
  187. package/build/dex/swaap-v1/libraries/PoolQuotations.js +131 -0
  188. package/build/dex/swaap-v1/libraries/PoolQuotations.js.map +1 -0
  189. package/build/dex/swaap-v1/libraries/Struct.d.ts +56 -0
  190. package/build/dex/swaap-v1/libraries/Struct.js +3 -0
  191. package/build/dex/swaap-v1/libraries/Struct.js.map +1 -0
  192. package/build/dex/swaap-v1/swaap-v1-pool.d.ts +45 -0
  193. package/build/dex/swaap-v1/swaap-v1-pool.js +656 -0
  194. package/build/dex/swaap-v1/swaap-v1-pool.js.map +1 -0
  195. package/build/dex/swaap-v1/swaap-v1.d.ts +62 -0
  196. package/build/dex/swaap-v1/swaap-v1.js +379 -0
  197. package/build/dex/swaap-v1/swaap-v1.js.map +1 -0
  198. package/build/dex/swaap-v1/types.d.ts +106 -0
  199. package/build/dex/swaap-v1/types.js +9 -0
  200. package/build/dex/swaap-v1/types.js.map +1 -0
  201. package/build/dex/usual-bond/config.d.ts +3 -0
  202. package/build/dex/usual-bond/config.js +13 -0
  203. package/build/dex/usual-bond/config.js.map +1 -0
  204. package/build/dex/usual-bond/types.d.ts +7 -0
  205. package/build/dex/usual-bond/types.js +3 -0
  206. package/build/dex/usual-bond/types.js.map +1 -0
  207. package/build/dex/usual-bond/usual-bond.d.ts +35 -0
  208. package/build/dex/usual-bond/usual-bond.js +162 -0
  209. package/build/dex/usual-bond/usual-bond.js.map +1 -0
  210. package/build/dex/zerox/index.d.ts +1 -1
  211. package/build/dex/zerox/index.js.map +1 -1
  212. package/package.json +2 -3
  213. package/src/abi/aave-gsm/Aave_GSM.json +751 -0
  214. package/src/abi/aave-gsm/IFeeStrategy.json +78 -0
  215. package/src/abi/stkGHO.json +1131 -0
  216. package/src/dex/aave-gsm/aave-gsm-e2e.test.ts +109 -0
  217. package/src/dex/aave-gsm/aave-gsm-events.test.ts +79 -0
  218. package/src/dex/aave-gsm/aave-gsm-integration.test.ts +522 -0
  219. package/src/dex/aave-gsm/aave-gsm-pool.ts +178 -0
  220. package/src/dex/aave-gsm/aave-gsm.ts +410 -0
  221. package/src/dex/aave-gsm/config.ts +16 -0
  222. package/src/dex/aave-gsm/types.ts +23 -0
  223. package/src/dex/index.ts +4 -0
  224. package/src/dex/stkgho/config.ts +14 -0
  225. package/src/dex/stkgho/stkgho-e2e.test.ts +77 -0
  226. package/src/dex/stkgho/stkgho-events.test.ts +79 -0
  227. package/src/dex/stkgho/stkgho-integration.test.ts +226 -0
  228. package/src/dex/stkgho/stkgho-pool.ts +111 -0
  229. package/src/dex/stkgho/stkgho.ts +221 -0
  230. package/src/dex/stkgho/types.ts +14 -0
  231. package/tests/constants-e2e.ts +4 -0
  232. package/.idea/codeStyles/Project.xml +0 -59
  233. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  234. package/.idea/inspectionProfiles/Project_Default.xml +0 -6
  235. package/.idea/modules.xml +0 -8
  236. package/.idea/paraswap-dex-lib.iml +0 -12
  237. package/.idea/vcs.xml +0 -6
@@ -0,0 +1,178 @@
1
+ import { Interface } from '@ethersproject/abi';
2
+ import { DeepReadonly } from 'ts-essentials';
3
+ import { Log, Logger } from '../../types';
4
+ import { catchParseLogError } from '../../utils';
5
+ import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
6
+ import { IDexHelper } from '../../dex-helper/idex-helper';
7
+ import { PoolState } from './types';
8
+ import GSM_ABI from '../../abi/aave-gsm/Aave_GSM.json';
9
+ import FEE_STRATEGY_ABI from '../../abi/aave-gsm/IFeeStrategy.json';
10
+ import {
11
+ addressDecode,
12
+ booleanDecode,
13
+ uint256ToBigInt,
14
+ } from '../../lib/decoders';
15
+
16
+ export class AaveGsmEventPool extends StatefulEventSubscriber<PoolState> {
17
+ handlers: {
18
+ [event: string]: (
19
+ event: any,
20
+ state: DeepReadonly<PoolState>,
21
+ log: Readonly<Log>,
22
+ ) => Promise<DeepReadonly<PoolState> | null>;
23
+ } = {};
24
+
25
+ logDecoder: (log: Log) => any;
26
+
27
+ addressesSubscribed: string[];
28
+
29
+ constructor(
30
+ readonly gsm: string,
31
+ readonly parentName: string,
32
+ protected network: number,
33
+ protected dexHelper: IDexHelper,
34
+ logger: Logger,
35
+ protected aaveGsmIface = new Interface(GSM_ABI),
36
+ protected feeStrategyIface = new Interface(FEE_STRATEGY_ABI),
37
+ protected PERCENT_FACTOR = 10_000n,
38
+ ) {
39
+ super(parentName, `${parentName}_${gsm}`, dexHelper, logger);
40
+
41
+ this.logDecoder = (log: Log) => this.aaveGsmIface.parseLog(log);
42
+ this.addressesSubscribed = [gsm];
43
+
44
+ // Add handlers
45
+ this.handlers['FeeStrategyUpdated'] =
46
+ this.handleFeeStrategyUpdated.bind(this);
47
+
48
+ this.handlers['SwapFreeze'] = this.handleSwapFreeze.bind(this);
49
+ this.handlers['Seized'] = this.handleSeized.bind(this);
50
+ }
51
+
52
+ getIdentifier(): string {
53
+ return `${this.parentName}_${this.gsm}`;
54
+ }
55
+
56
+ protected async processLog(
57
+ state: DeepReadonly<PoolState>,
58
+ log: Readonly<Log>,
59
+ ): Promise<DeepReadonly<PoolState> | null> {
60
+ try {
61
+ const event = this.logDecoder(log);
62
+ if (event.name in this.handlers) {
63
+ return await this.handlers[event.name](event, state, log);
64
+ }
65
+ } catch (e) {
66
+ catchParseLogError(e, this.logger);
67
+ }
68
+
69
+ return null;
70
+ }
71
+
72
+ async getOnChainState(
73
+ address: string,
74
+ blockNumber: number | string = 'latest',
75
+ ): Promise<PoolState> {
76
+ const callData = [
77
+ {
78
+ target: address,
79
+ callData: this.feeStrategyIface.encodeFunctionData('getBuyFee', [
80
+ this.PERCENT_FACTOR,
81
+ ]),
82
+ decodeFunction: uint256ToBigInt,
83
+ },
84
+ {
85
+ target: address,
86
+ callData: this.feeStrategyIface.encodeFunctionData('getSellFee', [
87
+ this.PERCENT_FACTOR,
88
+ ]),
89
+ decodeFunction: uint256ToBigInt,
90
+ },
91
+ {
92
+ target: this.gsm,
93
+ callData: this.aaveGsmIface.encodeFunctionData(
94
+ 'getAvailableLiquidity',
95
+ [],
96
+ ),
97
+ decodeFunction: uint256ToBigInt,
98
+ },
99
+ {
100
+ target: this.gsm,
101
+ callData: this.aaveGsmIface.encodeFunctionData('getIsFrozen', []),
102
+ decodeFunction: booleanDecode,
103
+ },
104
+ {
105
+ target: this.gsm,
106
+ callData: this.aaveGsmIface.encodeFunctionData('getIsSeized', []),
107
+ decodeFunction: booleanDecode,
108
+ },
109
+ ];
110
+
111
+ const results = await this.dexHelper.multiWrapper.tryAggregate<
112
+ bigint | boolean
113
+ >(true, callData, blockNumber);
114
+
115
+ return {
116
+ buyFee: results[0].returnData as bigint,
117
+ sellFee: results[1].returnData as bigint,
118
+ underlyingLiquidity: results[2].returnData as bigint,
119
+ isFrozen: results[3].returnData as boolean,
120
+ isSeized: results[4].returnData as boolean,
121
+ };
122
+ }
123
+
124
+ async generateState(
125
+ blockNumber: number | string = 'latest',
126
+ ): Promise<DeepReadonly<PoolState>> {
127
+ const callData = [
128
+ {
129
+ target: this.gsm,
130
+ callData: this.aaveGsmIface.encodeFunctionData('getFeeStrategy', []),
131
+ decodeFunction: addressDecode,
132
+ },
133
+ ];
134
+
135
+ const feeStrategy = await this.dexHelper.multiWrapper.tryAggregate<string>(
136
+ true,
137
+ callData,
138
+ blockNumber,
139
+ );
140
+
141
+ const result = await this.getOnChainState(
142
+ feeStrategy[0].returnData,
143
+ blockNumber,
144
+ );
145
+
146
+ return result;
147
+ }
148
+
149
+ async handleFeeStrategyUpdated(
150
+ event: any,
151
+ state: DeepReadonly<PoolState>,
152
+ log: Readonly<Log>,
153
+ ): Promise<DeepReadonly<PoolState> | null> {
154
+ return await this.getOnChainState(event.args.newFeeStrategy);
155
+ }
156
+
157
+ async handleSwapFreeze(
158
+ event: any,
159
+ state: DeepReadonly<PoolState>,
160
+ log: Readonly<Log>,
161
+ ): Promise<DeepReadonly<PoolState> | null> {
162
+ return {
163
+ ...state,
164
+ isFrozen: event.args.enabled,
165
+ };
166
+ }
167
+
168
+ async handleSeized(
169
+ event: any,
170
+ state: DeepReadonly<PoolState>,
171
+ log: Readonly<Log>,
172
+ ): Promise<DeepReadonly<PoolState> | null> {
173
+ return {
174
+ ...state,
175
+ isSeized: true,
176
+ };
177
+ }
178
+ }
@@ -0,0 +1,410 @@
1
+ import { AsyncOrSync } from 'ts-essentials';
2
+ import {
3
+ Token,
4
+ Address,
5
+ ExchangePrices,
6
+ PoolPrices,
7
+ AdapterExchangeParam,
8
+ PoolLiquidity,
9
+ Logger,
10
+ NumberAsString,
11
+ DexExchangeParam,
12
+ } from '../../types';
13
+ import { SwapSide, Network } from '../../constants';
14
+ import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
15
+ import { getDexKeysWithNetwork } from '../../utils';
16
+ import { IDex } from '../../dex/idex';
17
+ import { IDexHelper } from '../../dex-helper/idex-helper';
18
+ import { AaveGsmData, DexParams, PoolState } from './types';
19
+ import { SimpleExchange } from '../simple-exchange';
20
+ import { AaveGsmConfig } from './config';
21
+
22
+ import GSM_ABI from '../../abi/aave-gsm/Aave_GSM.json';
23
+ import { Interface } from '@ethersproject/abi';
24
+ import { formatUnits, parseUnits } from 'ethers/lib/utils';
25
+ import { AaveGsmEventPool } from './aave-gsm-pool';
26
+ import { MMath } from '../maverick-v1/maverick-math/maverick-basic-math';
27
+ import {
28
+ RETURN_AMOUNT_POS_0,
29
+ RETURN_AMOUNT_POS_32,
30
+ } from '../../executor/constants';
31
+
32
+ export class AaveGsm extends SimpleExchange implements IDex<AaveGsmData> {
33
+ static readonly gsmInterface = new Interface(GSM_ABI);
34
+ protected eventPools: Record<string, AaveGsmEventPool>;
35
+
36
+ protected config: DexParams;
37
+
38
+ readonly hasConstantPriceLargeAmounts = false;
39
+ readonly needWrapNative = false;
40
+
41
+ readonly isFeeOnTransferSupported = false;
42
+
43
+ public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
44
+ getDexKeysWithNetwork(AaveGsmConfig);
45
+
46
+ logger: Logger;
47
+
48
+ constructor(
49
+ readonly network: Network,
50
+ readonly dexKey: string,
51
+ readonly dexHelper: IDexHelper,
52
+ ) {
53
+ super(dexHelper, dexKey);
54
+ const config = AaveGsmConfig[dexKey][network];
55
+ this.config = {
56
+ GSM_USDT: config.GSM_USDT.toLowerCase(),
57
+ GSM_USDC: config.GSM_USDC.toLowerCase(),
58
+ USDT: config.USDT.toLowerCase(),
59
+ USDC: config.USDC.toLowerCase(),
60
+ GHO: config.GHO.toLowerCase(),
61
+ };
62
+
63
+ this.logger = dexHelper.getLogger(dexKey);
64
+
65
+ this.eventPools = {
66
+ [this.config.GSM_USDT]: new AaveGsmEventPool(
67
+ this.config.GSM_USDT,
68
+ this.dexKey,
69
+ this.network,
70
+ this.dexHelper,
71
+ this.logger,
72
+ ),
73
+ [this.config.GSM_USDC]: new AaveGsmEventPool(
74
+ this.config.GSM_USDC,
75
+ this.dexKey,
76
+ this.network,
77
+ this.dexHelper,
78
+ this.logger,
79
+ ),
80
+ };
81
+ }
82
+
83
+ async initializePoolPricing(pool: AaveGsmEventPool, blockNumber: number) {
84
+ const state = await pool.generateState(blockNumber);
85
+
86
+ pool.initialize(blockNumber, {
87
+ state,
88
+ });
89
+ }
90
+
91
+ async initializePricing(blockNumber: number) {
92
+ await this.initializePoolPricing(
93
+ this.eventPools[this.config.GSM_USDT],
94
+ blockNumber,
95
+ );
96
+ await this.initializePoolPricing(
97
+ this.eventPools[this.config.GSM_USDC],
98
+ blockNumber,
99
+ );
100
+ }
101
+
102
+ getAdapters(side: SwapSide): { name: string; index: number }[] | null {
103
+ return null;
104
+ }
105
+
106
+ async getPoolIdentifiers(
107
+ srcToken: Token,
108
+ destToken: Token,
109
+ side: SwapSide,
110
+ blockNumber: number,
111
+ ): Promise<string[]> {
112
+ const srcTokenAddress = srcToken.address.toLowerCase();
113
+ const destTokenAddress = destToken.address.toLowerCase();
114
+
115
+ if (
116
+ (srcTokenAddress === this.config.GHO &&
117
+ destTokenAddress === this.config.USDT) ||
118
+ (srcTokenAddress === this.config.USDT &&
119
+ destTokenAddress === this.config.GHO)
120
+ ) {
121
+ return [`${this.dexKey}_${this.config.GSM_USDT}`];
122
+ } else if (
123
+ (srcTokenAddress === this.config.GHO &&
124
+ destTokenAddress === this.config.USDC) ||
125
+ (srcTokenAddress === this.config.USDC &&
126
+ destTokenAddress === this.config.GHO)
127
+ ) {
128
+ return [`${this.dexKey}_${this.config.GSM_USDC}`];
129
+ } else {
130
+ return [];
131
+ }
132
+ }
133
+
134
+ async updatePoolState(): Promise<void> {
135
+ const blockNumber = await this.dexHelper.web3Provider.eth.getBlockNumber();
136
+ await this.initializePricing(blockNumber);
137
+ }
138
+
139
+ async getPoolState(gsm: string, blockNumber?: number): Promise<PoolState> {
140
+ if (!blockNumber) {
141
+ return this.eventPools[gsm].getStaleState()!;
142
+ }
143
+ const eventState = this.eventPools[gsm].getState(blockNumber);
144
+ if (eventState) return eventState;
145
+ const onChainState = await this.eventPools[gsm].generateState(blockNumber);
146
+ this.eventPools[gsm].setState(onChainState, blockNumber);
147
+ return onChainState;
148
+ }
149
+
150
+ getGhoAmountForBuyAsset(assetAmount: bigint, state: PoolState) {
151
+ if (assetAmount === 0n) {
152
+ return 0n;
153
+ }
154
+
155
+ const grossAmount = assetAmount * 1_000_000_000_000n; // 18 - 6 = 12 (decimals)
156
+ const fee = MMath.mulDiv(grossAmount, state.buyFee, 10_000n, true);
157
+
158
+ return grossAmount + fee;
159
+ }
160
+
161
+ getAssetAmountForBuyAsset(ghoAmount: bigint, state: PoolState) {
162
+ const grossAmount = (ghoAmount * 10_000n) / (10_000n + state.buyFee);
163
+
164
+ return grossAmount / 1_000_000_000_000n; // 18 - 6 = 12 (decimals)
165
+ }
166
+
167
+ getGhoAmountForSellAsset(assetAmount: bigint, state: PoolState) {
168
+ if (assetAmount === 0n) {
169
+ return 0n;
170
+ }
171
+
172
+ const grossAmount = assetAmount * 1_000_000_000_000n; // 18 - 6 = 12 (decimals)
173
+ const fee = MMath.mulDiv(grossAmount, state.sellFee, 10_000n, true);
174
+
175
+ return grossAmount - fee;
176
+ }
177
+
178
+ getAssetAmountForSellAsset(ghoAmount: bigint, state: PoolState) {
179
+ const grossAmount = MMath.mulDiv(
180
+ ghoAmount,
181
+ 10_000n,
182
+ 10_000n - state.sellFee,
183
+ true,
184
+ );
185
+
186
+ return MMath.mulDiv(grossAmount, 1n, 1_000_000_000_000n, true); // 18 - 6 = 12 (decimals)
187
+ }
188
+
189
+ async getPricesVolume(
190
+ srcToken: Token,
191
+ destToken: Token,
192
+ amounts: bigint[],
193
+ side: SwapSide,
194
+ blockNumber: number,
195
+ limitPools?: string[],
196
+ ): Promise<null | ExchangePrices<AaveGsmData>> {
197
+ const srcTokenAddress = srcToken.address.toLowerCase();
198
+ const destTokenAddress = destToken.address.toLowerCase();
199
+
200
+ if (
201
+ !(
202
+ (srcTokenAddress === this.config.GHO &&
203
+ (destTokenAddress === this.config.USDC ||
204
+ destTokenAddress === this.config.USDT)) ||
205
+ (destTokenAddress === this.config.GHO &&
206
+ (srcTokenAddress === this.config.USDC ||
207
+ srcTokenAddress === this.config.USDT))
208
+ )
209
+ ) {
210
+ return null;
211
+ }
212
+
213
+ let endpoint: Function;
214
+ let target: string;
215
+ let gas: number;
216
+
217
+ if (
218
+ srcTokenAddress === this.config.USDT ||
219
+ destTokenAddress === this.config.USDT
220
+ ) {
221
+ target = this.config.GSM_USDT;
222
+ } else {
223
+ target = this.config.GSM_USDC;
224
+ }
225
+
226
+ if (srcTokenAddress === this.config.GHO && side === SwapSide.BUY) {
227
+ endpoint = this.getGhoAmountForBuyAsset;
228
+ gas = 80_000;
229
+ } else if (srcTokenAddress === this.config.GHO && side === SwapSide.SELL) {
230
+ endpoint = this.getAssetAmountForBuyAsset;
231
+ gas = 80_000;
232
+ } else if (destTokenAddress === this.config.GHO && side === SwapSide.SELL) {
233
+ endpoint = this.getGhoAmountForSellAsset;
234
+ gas = 70_000;
235
+ } else {
236
+ endpoint = this.getAssetAmountForSellAsset;
237
+ gas = 70_000;
238
+ }
239
+
240
+ const unit = parseUnits(
241
+ '1',
242
+ side === SwapSide.SELL ? srcToken.decimals : destToken.decimals,
243
+ ).toBigInt();
244
+
245
+ const poolState = await this.getPoolState(target, blockNumber);
246
+
247
+ if (poolState.isFrozen || poolState.isSeized) {
248
+ return null;
249
+ }
250
+
251
+ return [
252
+ {
253
+ unit: endpoint(unit, poolState),
254
+ prices: amounts.map(amount => endpoint(amount, poolState)),
255
+ data: {
256
+ exchange: this.dexKey,
257
+ },
258
+ poolAddresses: [target],
259
+ exchange: this.dexKey,
260
+ gasCost: gas,
261
+ poolIdentifier: `${this.dexKey}_${target}`,
262
+ },
263
+ ];
264
+ }
265
+
266
+ getCalldataGasCost(poolPrices: PoolPrices<AaveGsmData>): number | number[] {
267
+ return CALLDATA_GAS_COST.DEX_NO_PAYLOAD;
268
+ }
269
+
270
+ getAdapterParam(
271
+ srcToken: string,
272
+ destToken: string,
273
+ srcAmount: string,
274
+ destAmount: string,
275
+ data: AaveGsmData,
276
+ side: SwapSide,
277
+ ): AdapterExchangeParam {
278
+ const { exchange } = data;
279
+
280
+ const payload = '';
281
+
282
+ return {
283
+ targetExchange: exchange,
284
+ payload,
285
+ networkFee: '0',
286
+ };
287
+ }
288
+
289
+ async getDexParam(
290
+ srcToken: Address,
291
+ destToken: Address,
292
+ srcAmount: NumberAsString,
293
+ destAmount: NumberAsString,
294
+ recipient: Address,
295
+ data: AaveGsmData,
296
+ side: SwapSide,
297
+ ): Promise<DexExchangeParam> {
298
+ const isSrcGho = srcToken.toLowerCase() === this.config.GHO;
299
+ const swapFunction = isSrcGho ? 'buyAsset' : 'sellAsset';
300
+ const ghoAmount = isSrcGho ? srcAmount : destAmount;
301
+ let assetAmount = isSrcGho ? destAmount : srcAmount;
302
+ const targetExchange =
303
+ srcToken.toLowerCase() === this.config.USDT ||
304
+ destToken.toLowerCase() === this.config.USDT
305
+ ? this.config.GSM_USDT
306
+ : this.config.GSM_USDC;
307
+
308
+ if (BigInt(assetAmount) === 1n) {
309
+ const poolState = await this.getPoolState(targetExchange);
310
+ assetAmount = isSrcGho
311
+ ? this.getAssetAmountForBuyAsset(
312
+ BigInt(ghoAmount),
313
+ poolState,
314
+ ).toString()
315
+ : this.getAssetAmountForSellAsset(
316
+ BigInt(ghoAmount),
317
+ poolState,
318
+ ).toString();
319
+ }
320
+
321
+ const exchangeData = AaveGsm.gsmInterface.encodeFunctionData(swapFunction, [
322
+ assetAmount,
323
+ recipient,
324
+ ]);
325
+
326
+ return {
327
+ needWrapNative: this.needWrapNative,
328
+ dexFuncHasRecipient: true,
329
+ exchangeData,
330
+ targetExchange,
331
+ returnAmountPos:
332
+ side === SwapSide.SELL
333
+ ? isSrcGho
334
+ ? RETURN_AMOUNT_POS_0
335
+ : RETURN_AMOUNT_POS_32
336
+ : undefined,
337
+ };
338
+ }
339
+
340
+ async getTopPoolsForToken(
341
+ tokenAddress: Address,
342
+ limit: number,
343
+ ): Promise<PoolLiquidity[]> {
344
+ tokenAddress = tokenAddress.toLowerCase();
345
+
346
+ if (tokenAddress === this.config.GHO) {
347
+ const usdtState = this.eventPools[this.config.GSM_USDT].getStaleState();
348
+ const usdcState = this.eventPools[this.config.GSM_USDC].getStaleState();
349
+
350
+ return [
351
+ {
352
+ exchange: this.dexKey,
353
+ address: this.config.GSM_USDT,
354
+ connectorTokens: [
355
+ {
356
+ decimals: 6,
357
+ address: this.config.USDT,
358
+ },
359
+ ],
360
+ liquidityUSD: usdtState
361
+ ? +formatUnits(usdtState.underlyingLiquidity, 6)
362
+ : 1000000000,
363
+ },
364
+ {
365
+ exchange: this.dexKey,
366
+ address: this.config.GSM_USDC,
367
+ connectorTokens: [
368
+ {
369
+ decimals: 6,
370
+ address: this.config.USDC,
371
+ },
372
+ ],
373
+ liquidityUSD: usdcState
374
+ ? +formatUnits(usdcState.underlyingLiquidity, 6)
375
+ : 1000000000,
376
+ },
377
+ ];
378
+ } else if (tokenAddress === this.config.USDC) {
379
+ return [
380
+ {
381
+ exchange: this.dexKey,
382
+ address: this.config.GSM_USDC,
383
+ connectorTokens: [
384
+ {
385
+ decimals: 18,
386
+ address: this.config.GHO,
387
+ },
388
+ ],
389
+ liquidityUSD: 1000000000, // Just returning a big number so this DEX will be preferred
390
+ },
391
+ ];
392
+ } else if (tokenAddress === this.config.USDT) {
393
+ return [
394
+ {
395
+ exchange: this.dexKey,
396
+ address: this.config.GSM_USDT,
397
+ connectorTokens: [
398
+ {
399
+ decimals: 18,
400
+ address: this.config.GHO,
401
+ },
402
+ ],
403
+ liquidityUSD: 1000000000, // Just returning a big number so this DEX will be preferred
404
+ },
405
+ ];
406
+ } else {
407
+ return [];
408
+ }
409
+ }
410
+ }
@@ -0,0 +1,16 @@
1
+ import { DexParams } from './types';
2
+ import { DexConfigMap } from '../../types';
3
+ import { Network } from '../../constants';
4
+ import { AaveV3Ethereum, MiscEthereum } from '@bgd-labs/aave-address-book';
5
+
6
+ export const AaveGsmConfig: DexConfigMap<DexParams> = {
7
+ AaveGsm: {
8
+ [Network.MAINNET]: {
9
+ GSM_USDT: MiscEthereum.GSM_USDT,
10
+ GSM_USDC: MiscEthereum.GSM_USDC,
11
+ USDT: AaveV3Ethereum.ASSETS.USDT.UNDERLYING,
12
+ USDC: AaveV3Ethereum.ASSETS.USDC.UNDERLYING,
13
+ GHO: AaveV3Ethereum.ASSETS.GHO.UNDERLYING,
14
+ },
15
+ },
16
+ };
@@ -0,0 +1,23 @@
1
+ import { Address } from '../../types';
2
+
3
+ export type PoolState = {
4
+ buyFee: bigint;
5
+ sellFee: bigint;
6
+ underlyingLiquidity: bigint;
7
+ isFrozen: boolean;
8
+ isSeized: boolean;
9
+ };
10
+
11
+ export type AaveGsmData = {
12
+ // returned by the API that can be used for
13
+ // tx building. The data structure should be minimal.
14
+ exchange: Address;
15
+ };
16
+
17
+ export type DexParams = {
18
+ GSM_USDT: string;
19
+ GSM_USDC: string;
20
+ USDT: string;
21
+ USDC: string;
22
+ GHO: string;
23
+ };
package/src/dex/index.ts CHANGED
@@ -86,7 +86,9 @@ import { AaveV3Stata } from './aave-v3-stata/aave-v3-stata';
86
86
  import { OSwap } from './oswap/oswap';
87
87
  import { ConcentratorArusd } from './concentrator-arusd/concentrator-arusd';
88
88
  import { FxProtocolRusd } from './fx-protocol-rusd/fx-protocol-rusd';
89
+ import { AaveGsm } from './aave-gsm/aave-gsm';
89
90
  import { LitePsm } from './lite-psm/lite-psm';
91
+ import { StkGHO } from './stkgho/stkgho';
90
92
 
91
93
  const LegacyDexes = [
92
94
  CurveV2,
@@ -168,7 +170,9 @@ const Dexes = [
168
170
  OSwap,
169
171
  ConcentratorArusd,
170
172
  FxProtocolRusd,
173
+ AaveGsm,
171
174
  LitePsm,
175
+ StkGHO,
172
176
  ];
173
177
 
174
178
  export type LegacyDexConstructor = new (dexHelper: IDexHelper) => IDexTxBuilder<
@@ -0,0 +1,14 @@
1
+ import { DexParams } from './types';
2
+ import { DexConfigMap } from '../../types';
3
+ import { Network } from '../../constants';
4
+
5
+ import { MiscEthereum } from '@bgd-labs/aave-address-book';
6
+
7
+ export const StkGHOConfig: DexConfigMap<DexParams> = {
8
+ StkGHO: {
9
+ [Network.MAINNET]: {
10
+ stkGHO: '0x1a88Df1cFe15Af22B3c4c783D4e6F7F9e0C1885d',
11
+ GHO: MiscEthereum.GHO_TOKEN,
12
+ },
13
+ },
14
+ };