@paraswap/dex-lib 3.8.14 → 3.8.16-oswap

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 (219) hide show
  1. package/build/abi/concentrator/arUSD.json +568 -0
  2. package/build/abi/concentrator/arUSD5115.json +1746 -0
  3. package/build/abi/fx-protocol/FxMarket.json +1284 -0
  4. package/build/abi/{wombat/pool.json → fx-protocol/FxUSD.json} +541 -603
  5. package/build/abi/fx-protocol/weETHOralce.json +676 -0
  6. package/build/abi/inception/inception-ineth-pool.json +1062 -0
  7. package/build/abi/inception/inception-ineth.json +609 -0
  8. package/build/abi/inception/inception-ratio-feed.json +93 -0
  9. package/build/abi/inception/inception-vault.json +991 -0
  10. package/build/abi/infusion/InfusionFactory.json +147 -0
  11. package/build/abi/infusion/InfusionPair.json +658 -0
  12. package/build/abi/infusion/InfusionRouter.json +442 -0
  13. package/build/abi/oswap/oswap.abi.json +452 -0
  14. package/build/dex/aave-v1/aave-v1.js +0 -1
  15. package/build/dex/aave-v1/aave-v1.js.map +1 -1
  16. package/build/dex/angle-transmuter/angle-transmuter.js +1 -1
  17. package/build/dex/angle-transmuter/angle-transmuter.js.map +1 -1
  18. package/build/dex/concentrator-arusd/concentrator-arusd-event.d.ts +17 -0
  19. package/build/dex/concentrator-arusd/concentrator-arusd-event.js +66 -0
  20. package/build/dex/concentrator-arusd/concentrator-arusd-event.js.map +1 -0
  21. package/build/dex/concentrator-arusd/concentrator-arusd.d.ts +41 -0
  22. package/build/dex/concentrator-arusd/concentrator-arusd.js +188 -0
  23. package/build/dex/concentrator-arusd/concentrator-arusd.js.map +1 -0
  24. package/build/dex/concentrator-arusd/config.d.ts +3 -0
  25. package/build/dex/concentrator-arusd/config.js +14 -0
  26. package/build/dex/concentrator-arusd/config.js.map +1 -0
  27. package/build/dex/concentrator-arusd/types.d.ts +14 -0
  28. package/build/dex/concentrator-arusd/types.js +3 -0
  29. package/build/dex/concentrator-arusd/types.js.map +1 -0
  30. package/build/dex/concentrator-arusd/utils.d.ts +4 -0
  31. package/build/dex/concentrator-arusd/utils.js +27 -0
  32. package/build/dex/concentrator-arusd/utils.js.map +1 -0
  33. package/build/dex/fx-protocol-rusd/config.d.ts +3 -0
  34. package/build/dex/fx-protocol-rusd/config.js +15 -0
  35. package/build/dex/fx-protocol-rusd/config.js.map +1 -0
  36. package/build/dex/fx-protocol-rusd/fx-protocol-rusd-event.d.ts +18 -0
  37. package/build/dex/fx-protocol-rusd/fx-protocol-rusd-event.js +71 -0
  38. package/build/dex/fx-protocol-rusd/fx-protocol-rusd-event.js.map +1 -0
  39. package/build/dex/fx-protocol-rusd/fx-protocol-rusd.d.ts +39 -0
  40. package/build/dex/fx-protocol-rusd/fx-protocol-rusd.js +201 -0
  41. package/build/dex/fx-protocol-rusd/fx-protocol-rusd.js.map +1 -0
  42. package/build/dex/fx-protocol-rusd/types.d.ts +13 -0
  43. package/build/dex/fx-protocol-rusd/types.js +3 -0
  44. package/build/dex/fx-protocol-rusd/types.js.map +1 -0
  45. package/build/dex/fx-protocol-rusd/utils.d.ts +4 -0
  46. package/build/dex/fx-protocol-rusd/utils.js +26 -0
  47. package/build/dex/fx-protocol-rusd/utils.js.map +1 -0
  48. package/build/dex/idex.d.ts +11 -7
  49. package/build/dex/idle-dao/idle-dao-pool.d.ts +56 -0
  50. package/build/dex/idle-dao/idle-dao-pool.js +176 -0
  51. package/build/dex/idle-dao/idle-dao-pool.js.map +1 -0
  52. package/build/dex/idle-dao/idle-dao-pooling-pool.d.ts +16 -0
  53. package/build/dex/idle-dao/idle-dao-pooling-pool.js +57 -0
  54. package/build/dex/idle-dao/idle-dao-pooling-pool.js.map +1 -0
  55. package/build/dex/idle-dao/scripts.d.ts +9 -0
  56. package/build/dex/idle-dao/scripts.js +552 -0
  57. package/build/dex/idle-dao/scripts.js.map +1 -0
  58. package/build/dex/inception/config.d.ts +5 -0
  59. package/build/dex/inception/config.js +112 -0
  60. package/build/dex/inception/config.js.map +1 -0
  61. package/build/dex/inception/inception-event-pool.d.ts +23 -0
  62. package/build/dex/inception/inception-event-pool.js +34 -0
  63. package/build/dex/inception/inception-event-pool.js.map +1 -0
  64. package/build/dex/inception/inception-native.d.ts +43 -0
  65. package/build/dex/inception/inception-native.js +131 -0
  66. package/build/dex/inception/inception-native.js.map +1 -0
  67. package/build/dex/inception/inception-pool.d.ts +18 -0
  68. package/build/dex/inception/inception-pool.js +32 -0
  69. package/build/dex/inception/inception-pool.js.map +1 -0
  70. package/build/dex/inception/inception-price-feed.d.ts +19 -0
  71. package/build/dex/inception/inception-price-feed.js +51 -0
  72. package/build/dex/inception/inception-price-feed.js.map +1 -0
  73. package/build/dex/inception/inception.d.ts +44 -0
  74. package/build/dex/inception/inception.js +162 -0
  75. package/build/dex/inception/inception.js.map +1 -0
  76. package/build/dex/inception/tokens.d.ts +9 -0
  77. package/build/dex/inception/tokens.js +28 -0
  78. package/build/dex/inception/tokens.js.map +1 -0
  79. package/build/dex/inception/types.d.ts +22 -0
  80. package/build/dex/inception/types.js +3 -0
  81. package/build/dex/inception/types.js.map +1 -0
  82. package/build/dex/inception/utils.d.ts +7 -0
  83. package/build/dex/inception/utils.js +58 -0
  84. package/build/dex/inception/utils.js.map +1 -0
  85. package/build/dex/index.js +6 -0
  86. package/build/dex/index.js.map +1 -1
  87. package/build/dex/infusion/config.d.ts +4 -0
  88. package/build/dex/infusion/config.js +25 -0
  89. package/build/dex/infusion/config.js.map +1 -0
  90. package/build/dex/infusion/infusion-stable-pool.d.ts +4 -0
  91. package/build/dex/infusion/infusion-stable-pool.js +74 -0
  92. package/build/dex/infusion/infusion-stable-pool.js.map +1 -0
  93. package/build/dex/infusion/infusion.d.ts +54 -0
  94. package/build/dex/infusion/infusion.js +510 -0
  95. package/build/dex/infusion/infusion.js.map +1 -0
  96. package/build/dex/infusion/types.d.ts +47 -0
  97. package/build/dex/infusion/types.js +3 -0
  98. package/build/dex/infusion/types.js.map +1 -0
  99. package/build/dex/infusion/utils/isStablePair.d.ts +2 -0
  100. package/build/dex/infusion/utils/isStablePair.js +18 -0
  101. package/build/dex/infusion/utils/isStablePair.js.map +1 -0
  102. package/build/dex/oswap/config.d.ts +5 -0
  103. package/build/dex/oswap/config.js +31 -0
  104. package/build/dex/oswap/config.js.map +1 -0
  105. package/build/dex/oswap/oswap-pool.d.ts +51 -0
  106. package/build/dex/oswap/oswap-pool.js +150 -0
  107. package/build/dex/oswap/oswap-pool.js.map +1 -0
  108. package/build/dex/oswap/oswap.d.ts +46 -0
  109. package/build/dex/oswap/oswap.js +273 -0
  110. package/build/dex/oswap/oswap.js.map +1 -0
  111. package/build/dex/oswap/types.d.ts +20 -0
  112. package/build/dex/oswap/types.js +3 -0
  113. package/build/dex/oswap/types.js.map +1 -0
  114. package/build/dex/platypus/platypus.js +3 -17
  115. package/build/dex/platypus/platypus.js.map +1 -1
  116. package/build/dex/swaap-v1/swaap-v1.js +0 -4
  117. package/build/dex/swaap-v1/swaap-v1.js.map +1 -1
  118. package/build/dex/weth/config.js +3 -0
  119. package/build/dex/weth/config.js.map +1 -1
  120. package/package.json +1 -1
  121. package/src/abi/concentrator/arUSD.json +568 -0
  122. package/src/abi/concentrator/arUSD5115.json +1746 -0
  123. package/src/abi/fx-protocol/FxMarket.json +1284 -0
  124. package/src/abi/fx-protocol/FxUSD.json +1227 -0
  125. package/src/abi/fx-protocol/weETHOralce.json +676 -0
  126. package/src/abi/oswap/oswap.abi.json +452 -0
  127. package/src/dex/angle-transmuter/angle-transmuter.ts +1 -1
  128. package/src/dex/concentrator-arusd/concentrator-arusd-e2e.test.ts +92 -0
  129. package/src/dex/concentrator-arusd/concentrator-arusd-event.ts +100 -0
  130. package/src/dex/concentrator-arusd/concentrator-arusd-integration.test.ts +104 -0
  131. package/src/dex/concentrator-arusd/concentrator-arusd.ts +269 -0
  132. package/src/dex/concentrator-arusd/concetrator-arusd-events.test.ts +97 -0
  133. package/src/dex/concentrator-arusd/config.ts +13 -0
  134. package/src/dex/concentrator-arusd/types.ts +16 -0
  135. package/src/dex/concentrator-arusd/utils.ts +36 -0
  136. package/src/dex/fx-protocol-rusd/config.ts +14 -0
  137. package/src/dex/fx-protocol-rusd/fx-protocol-e2e.test.ts +96 -0
  138. package/src/dex/fx-protocol-rusd/fx-protocol-events.test.ts +101 -0
  139. package/src/dex/fx-protocol-rusd/fx-protocol-integration.test.ts +104 -0
  140. package/src/dex/fx-protocol-rusd/fx-protocol-rusd-event.ts +112 -0
  141. package/src/dex/fx-protocol-rusd/fx-protocol-rusd.ts +282 -0
  142. package/src/dex/fx-protocol-rusd/types.ts +16 -0
  143. package/src/dex/fx-protocol-rusd/utils.ts +47 -0
  144. package/src/dex/idex.ts +48 -41
  145. package/src/dex/index.ts +6 -0
  146. package/src/dex/oswap/config.ts +32 -0
  147. package/src/dex/oswap/oswap-e2e.test.ts +91 -0
  148. package/src/dex/oswap/oswap-events.test.ts +88 -0
  149. package/src/dex/oswap/oswap-integration.test.ts +269 -0
  150. package/src/dex/oswap/oswap-pool.ts +190 -0
  151. package/src/dex/oswap/oswap.ts +392 -0
  152. package/src/dex/oswap/types.ts +29 -0
  153. package/src/dex/weth/config.ts +3 -0
  154. package/src/dex/weth/weth-e2e.test.ts +156 -0
  155. package/src/dex/wsteth/wsteth-integration.test.ts +0 -1
  156. package/tests/constants-e2e.ts +10 -0
  157. package/.vscode/launch.json +0 -53
  158. package/.vscode/settings.json +0 -2
  159. package/.vscode/tasks.json +0 -15
  160. package/build/abi/maverick-v2/MaverickV2Router.json +0 -778
  161. package/build/abi/uniswap-v3/AlienBaseV3Router.abi.json +0 -52
  162. package/build/dex/aave-v2/tokens-avalanche.json +0 -44
  163. package/build/dex/aave-v2/tokens-mainnet.json +0 -188
  164. package/build/dex/aave-v2/tokens-polygon.json +0 -44
  165. package/build/dex/bProtocol.d.ts +0 -18
  166. package/build/dex/bProtocol.js +0 -39
  167. package/build/dex/bProtocol.js.map +0 -1
  168. package/build/dex/bancor.d.ts +0 -26
  169. package/build/dex/bancor.js +0 -58
  170. package/build/dex/bancor.js.map +0 -1
  171. package/build/dex/compound.d.ts +0 -18
  172. package/build/dex/compound.js +0 -46
  173. package/build/dex/compound.js.map +0 -1
  174. package/build/dex/curve-v2.d.ts +0 -58
  175. package/build/dex/curve-v2.js +0 -180
  176. package/build/dex/curve-v2.js.map +0 -1
  177. package/build/dex/dodo-v1.d.ts +0 -33
  178. package/build/dex/dodo-v1.js +0 -63
  179. package/build/dex/dodo-v1.js.map +0 -1
  180. package/build/dex/lido.d.ts +0 -19
  181. package/build/dex/lido.js +0 -42
  182. package/build/dex/lido.js.map +0 -1
  183. package/build/dex/maker-psm/scripts/gem.abi.json +0 -136
  184. package/build/dex/maker-psm/scripts/validate-state.d.ts +0 -0
  185. package/build/dex/maker-psm/scripts/validate-state.js +0 -185
  186. package/build/dex/maker-psm/scripts/validate-state.js.map +0 -1
  187. package/build/dex/maker-psm/scripts/vat.abi.json +0 -243
  188. package/build/dex/onebit.d.ts +0 -25
  189. package/build/dex/onebit.js +0 -42
  190. package/build/dex/onebit.js.map +0 -1
  191. package/build/dex/smoothy.d.ts +0 -26
  192. package/build/dex/smoothy.js +0 -48
  193. package/build/dex/smoothy.js.map +0 -1
  194. package/build/dex/solidly-v3/scripts/check-event-pool-event.d.ts +0 -1
  195. package/build/dex/solidly-v3/scripts/check-event-pool-event.js +0 -53
  196. package/build/dex/solidly-v3/scripts/check-event-pool-event.js.map +0 -1
  197. package/build/dex/stable-pool.d.ts +0 -28
  198. package/build/dex/stable-pool.js +0 -55
  199. package/build/dex/stable-pool.js.map +0 -1
  200. package/build/dex/trader-joe-v2.1/base.d.ts +0 -18
  201. package/build/dex/trader-joe-v2.1/base.js +0 -99
  202. package/build/dex/trader-joe-v2.1/base.js.map +0 -1
  203. package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.d.ts +0 -7
  204. package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.js +0 -20
  205. package/build/dex/trader-joe-v2.1/trader-joe-v2.1-new.js.map +0 -1
  206. package/build/dex/trader-joe-v2.1/trader-joe-v2.2.d.ts +0 -7
  207. package/build/dex/trader-joe-v2.1/trader-joe-v2.2.js +0 -20
  208. package/build/dex/trader-joe-v2.1/trader-joe-v2.2.js.map +0 -1
  209. package/build/dex/trader-joe-v2.1.d.ts +0 -43
  210. package/build/dex/trader-joe-v2.1.js +0 -79
  211. package/build/dex/trader-joe-v2.1.js.map +0 -1
  212. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.d.ts +0 -17
  213. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js +0 -64
  214. package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js.map +0 -1
  215. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.d.ts +0 -17
  216. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js +0 -79
  217. package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js.map +0 -1
  218. package/config.ts +0 -1946
  219. package/src/dex/dexs.txt +0 -58
@@ -0,0 +1,101 @@
1
+ import dotenv from 'dotenv';
2
+ dotenv.config();
3
+
4
+ import { FxProtocolRusdEvent } from './fx-protocol-rusd-event';
5
+ import { FxProtocolConfig } from './config';
6
+ import { Network } from '../../constants';
7
+ import { DummyDexHelper } from '../../dex-helper/index';
8
+ import { testEventSubscriber } from '../../../tests/utils-events';
9
+ import FxUSD_ABI from '../../abi/fx-protocol/FxUSD.json';
10
+ import FxMarket_ABI from '../../abi/fx-protocol/FxMarket.json';
11
+ import EthWeETHOralce_ABI from '../../abi/fx-protocol/weETHOralce.json';
12
+
13
+ import _ from 'lodash';
14
+ import { FxProtocolPoolState } from './types';
15
+ import { Interface } from 'ethers/lib/utils';
16
+ import { JsonFragment } from '@ethersproject/abi';
17
+
18
+ /*
19
+ README
20
+ ======
21
+
22
+ This test script adds unit tests for fxProtocol event based
23
+ system. This is done by fetching the state on-chain before the
24
+ event block, manually pushing the block logs to the event-subscriber,
25
+ comparing the local state with on-chain state.
26
+
27
+ Most of the logic for testing is abstracted by `testEventSubscriber`.
28
+ You need to do two things to make the tests work:
29
+
30
+ 1. Fetch the block numbers where certain events were released. You
31
+ can modify the `./scripts/fetch-event-blocknumber.ts` to get the
32
+ block numbers for different events. Make sure to get sufficient
33
+ number of blockNumbers to cover all possible cases for the event
34
+ mutations.
35
+
36
+ 2. Complete the implementation for fetchPoolState function. The
37
+ function should fetch the on-chain state of the event subscriber
38
+ using just the blocknumber.
39
+
40
+ The template tests only include the test for a single event
41
+ subscriber. There can be cases where multiple event subscribers
42
+ exist for a single DEX. In such cases additional tests should be
43
+ added.
44
+
45
+ You can run this individual test script by running:
46
+ `npx jest src/dex/<dex-name>/<dex-name>-events.test.ts`
47
+
48
+ (This comment should be removed from the final implementation)
49
+ */
50
+
51
+ jest.setTimeout(50 * 1000);
52
+ const dexKey = 'FxProtocolRusd';
53
+ const network = Network.MAINNET;
54
+
55
+ async function fetchPoolState(
56
+ fxProtocolPool: FxProtocolRusdEvent,
57
+ blockNumber: number,
58
+ ): Promise<FxProtocolPoolState> {
59
+ return fxProtocolPool.generateState(blockNumber);
60
+ }
61
+
62
+ describe('FxProtocolRusd Event', function () {
63
+ const blockNumbers: { [eventName: string]: number[] } = {
64
+ UpdateRedeemFeeRatioFToken: [19460858, 19465918, 19460857],
65
+ AnswerUpdated: [20482537, 20481328, 20480824, 20480670, 20480669],
66
+ };
67
+
68
+ describe('FxProtocolRusdPool', function () {
69
+ Object.keys(blockNumbers).forEach((event: string) => {
70
+ blockNumbers[event].forEach((blockNumber: number) => {
71
+ it(`Should return the correct state after the ${blockNumber}:${event}`, async function () {
72
+ const dexHelper = new DummyDexHelper(network);
73
+ const logger = dexHelper.getLogger(dexKey);
74
+ const config = FxProtocolConfig[dexKey][network];
75
+
76
+ const fxProtocolPool = new FxProtocolRusdEvent(
77
+ dexKey,
78
+ dexHelper,
79
+ config.rUSDWeETHMarketAddress,
80
+ new Interface(FxMarket_ABI as JsonFragment[]),
81
+ config.weETHOracleAddress,
82
+ new Interface(EthWeETHOralce_ABI as JsonFragment[]),
83
+ logger,
84
+ );
85
+
86
+ await fxProtocolPool.initialize(blockNumber);
87
+
88
+ await testEventSubscriber(
89
+ fxProtocolPool,
90
+ fxProtocolPool.addressesSubscribed,
91
+ (_blockNumber: number) =>
92
+ fetchPoolState(fxProtocolPool, _blockNumber),
93
+ blockNumber,
94
+ `${dexKey}_${fxProtocolPool}`,
95
+ dexHelper.provider,
96
+ );
97
+ });
98
+ });
99
+ });
100
+ });
101
+ });
@@ -0,0 +1,104 @@
1
+ import dotenv from 'dotenv';
2
+ dotenv.config();
3
+
4
+ import { DummyDexHelper } from '../../dex-helper/index';
5
+ import { Network, SwapSide } from '../../constants';
6
+ import { checkPoolPrices, checkPoolsLiquidity } from '../../../tests/utils';
7
+ import { Tokens } from '../../../tests/constants-e2e';
8
+ import { BI_POWS } from '../../bigint-constants';
9
+ import { FxProtocolRusd } from './fx-protocol-rusd';
10
+
11
+ const network = Network.MAINNET;
12
+
13
+ const rUSDSymbol = 'rUSD';
14
+ const rUSDToken = Tokens[network][rUSDSymbol];
15
+
16
+ const weETHSymbol = 'weETH';
17
+ const weETHToken = Tokens[network][weETHSymbol];
18
+
19
+ const amounts = [0n, BI_POWS[18], 2000000000000000000n];
20
+
21
+ const dexKey = 'FxProtocolRusd';
22
+ const dexHelper = new DummyDexHelper(network);
23
+ let blocknumber: number;
24
+ let fxProtocolRusd: FxProtocolRusd;
25
+
26
+ describe('FxProtocolRusd', function () {
27
+ beforeAll(async () => {
28
+ blocknumber = await dexHelper.web3Provider.eth.getBlockNumber();
29
+ fxProtocolRusd = new FxProtocolRusd(network, dexKey, dexHelper);
30
+ if (fxProtocolRusd.initializePricing) {
31
+ await fxProtocolRusd.initializePricing(blocknumber);
32
+ }
33
+ });
34
+
35
+ it('getPoolIdentifiers and getPricesVolume weETH -> rUSD SELL', async function () {
36
+ const pools = await fxProtocolRusd.getPoolIdentifiers(
37
+ weETHToken,
38
+ rUSDToken,
39
+ SwapSide.SELL,
40
+ blocknumber,
41
+ );
42
+ console.log(`${weETHSymbol} <> ${rUSDSymbol} Pool Identifiers: `, pools);
43
+
44
+ expect(pools.length).toBeGreaterThan(0);
45
+
46
+ const poolPrices = await fxProtocolRusd.getPricesVolume(
47
+ weETHToken,
48
+ rUSDToken,
49
+ amounts,
50
+ SwapSide.SELL,
51
+ blocknumber,
52
+ pools,
53
+ );
54
+ console.log(`${weETHSymbol} <> ${rUSDSymbol} Pool Prices: `, poolPrices);
55
+
56
+ expect(poolPrices).not.toBeNull();
57
+ checkPoolPrices(poolPrices!, amounts, SwapSide.SELL, dexKey);
58
+ });
59
+
60
+ it('getPoolIdentifiers and getPricesVolume rUSD -> weETH SELL', async function () {
61
+ const pools = await fxProtocolRusd.getPoolIdentifiers(
62
+ rUSDToken,
63
+ weETHToken,
64
+ SwapSide.SELL,
65
+ blocknumber,
66
+ );
67
+ console.log(`${rUSDSymbol} <> ${weETHSymbol} Pool Identifiers: `, pools);
68
+
69
+ expect(pools.length).toBeGreaterThan(0);
70
+
71
+ const poolPrices = await fxProtocolRusd.getPricesVolume(
72
+ rUSDToken,
73
+ weETHToken,
74
+ amounts,
75
+ SwapSide.SELL,
76
+ blocknumber,
77
+ pools,
78
+ );
79
+ console.log(`${rUSDSymbol} <> ${weETHSymbol} Pool Prices: `, poolPrices);
80
+
81
+ expect(poolPrices).not.toBeNull();
82
+ checkPoolPrices(poolPrices!, amounts, SwapSide.SELL, dexKey);
83
+ });
84
+
85
+ it('weETH getTopPoolsForToken', async function () {
86
+ const poolLiquidity = await fxProtocolRusd.getTopPoolsForToken(
87
+ weETHToken.address,
88
+ 10,
89
+ );
90
+ console.log(`${weETHSymbol} Top Pools:`, poolLiquidity);
91
+
92
+ checkPoolsLiquidity(poolLiquidity, weETHToken.address, dexKey);
93
+ });
94
+
95
+ it('rUSD getTopPoolsForToken', async function () {
96
+ const poolLiquidity = await fxProtocolRusd.getTopPoolsForToken(
97
+ rUSDToken.address,
98
+ 10,
99
+ );
100
+ console.log(`${rUSDSymbol} Top Pools:`, poolLiquidity);
101
+
102
+ checkPoolsLiquidity(poolLiquidity, rUSDToken.address, dexKey);
103
+ });
104
+ });
@@ -0,0 +1,112 @@
1
+ import _ from 'lodash';
2
+ import { defaultAbiCoder, Interface } from '@ethersproject/abi';
3
+ import { IDexHelper } from '../../dex-helper';
4
+ import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
5
+ import { Address, Log, Logger } from '../../types';
6
+ import { AsyncOrSync, DeepReadonly } from 'ts-essentials';
7
+ import { FxProtocolPoolState } from './types';
8
+ import { BI_POWS } from '../../bigint-constants';
9
+ import { getOnChainState } from './utils';
10
+
11
+ const ANSWER_UPDATED_TOPICHASH = `0x0559884fd3a460db3073b7fc896cc77986f16e378210ded43186175bf646fc5f`;
12
+ const UPDATE_REDEEM_FEE_TOPICHASH = `0xb3d49e95905d108ea668ffe62a85ee16bdc4ff9f122a7137964327ea8e0585ff`;
13
+
14
+ export class FxProtocolRusdEvent extends StatefulEventSubscriber<FxProtocolPoolState> {
15
+ constructor(
16
+ parentName: string,
17
+ protected dexHelper: IDexHelper,
18
+ private marketAddress: Address,
19
+ private marketInterface: Interface,
20
+ private weETHOracleAddress: Address,
21
+ private weETHOracleIface: Interface,
22
+ logger: Logger,
23
+ ) {
24
+ super(parentName, 'fxProtocolRusd', dexHelper, logger);
25
+ this.addressesSubscribed = [marketAddress, weETHOracleAddress];
26
+ }
27
+
28
+ protected processLog(
29
+ state: DeepReadonly<FxProtocolPoolState>,
30
+ log: Readonly<Log>,
31
+ ): AsyncOrSync<DeepReadonly<FxProtocolPoolState> | null> {
32
+ if (log.topics[0] === UPDATE_REDEEM_FEE_TOPICHASH) {
33
+ const [defaultFeeRatio] = defaultAbiCoder.decode(
34
+ ['uint256', 'int256'],
35
+ log.data,
36
+ );
37
+
38
+ return {
39
+ ...state,
40
+ redeemFee: BigInt(defaultFeeRatio).toString(),
41
+ };
42
+ }
43
+
44
+ if (log.topics[0] === ANSWER_UPDATED_TOPICHASH) {
45
+ return {
46
+ ...state,
47
+ weETHPrice: BigInt(log.topics[1]).toString(),
48
+ };
49
+ }
50
+
51
+ return null;
52
+ }
53
+
54
+ async generateState(
55
+ blockNumber: number | 'latest' = 'latest',
56
+ ): Promise<DeepReadonly<FxProtocolPoolState>> {
57
+ const state = await getOnChainState(
58
+ this.dexHelper.multiContract,
59
+ this.marketAddress,
60
+ this.marketInterface,
61
+ this.weETHOracleAddress,
62
+ this.weETHOracleIface,
63
+ blockNumber,
64
+ );
65
+ return state;
66
+ }
67
+
68
+ async getPrice(
69
+ blockNumber: number,
70
+ amount: bigint,
71
+ isRedeem: boolean,
72
+ ): Promise<bigint> {
73
+ const state = await this.getOrGenerateState(blockNumber);
74
+ if (!state) throw new Error('Cannot compute price');
75
+
76
+ const { nav, redeemFee, weETHPrice } = state;
77
+ const baseTokenPrice = BigInt(weETHPrice) * BI_POWS[10];
78
+
79
+ if (isRedeem) {
80
+ return BigInt(
81
+ (amount * BigInt(nav) * (BI_POWS[18] - BigInt(redeemFee))) /
82
+ BI_POWS[18] /
83
+ baseTokenPrice,
84
+ );
85
+ } else {
86
+ return BigInt((baseTokenPrice * amount) / BigInt(nav));
87
+ }
88
+ }
89
+
90
+ async getOrGenerateState(
91
+ blockNumber: number,
92
+ ): Promise<DeepReadonly<FxProtocolPoolState> | null> {
93
+ const state = this.getState(blockNumber);
94
+ if (state) {
95
+ return state;
96
+ }
97
+
98
+ this.logger.debug(
99
+ `No state found for ${this.addressesSubscribed[0]}, generating new one`,
100
+ );
101
+ const newState = await this.generateState(blockNumber);
102
+
103
+ if (!newState) {
104
+ this.logger.debug(
105
+ `Could not regenerate state for ${this.addressesSubscribed[0]}`,
106
+ );
107
+ return null;
108
+ }
109
+ this.setState(newState, blockNumber);
110
+ return newState;
111
+ }
112
+ }
@@ -0,0 +1,282 @@
1
+ import {
2
+ Token,
3
+ Address,
4
+ ExchangePrices,
5
+ PoolPrices,
6
+ AdapterExchangeParam,
7
+ Logger,
8
+ NumberAsString,
9
+ DexExchangeParam,
10
+ PoolLiquidity,
11
+ } from '../../types';
12
+ import { SwapSide, Network } from '../../constants';
13
+ import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
14
+ import { getDexKeysWithNetwork } from '../../utils';
15
+ import { IDex } from '../idex';
16
+ import { IDexHelper } from '../../dex-helper/idex-helper';
17
+ import { DexParams, FxProtocolData } from './types';
18
+ import { SimpleExchange } from '../simple-exchange';
19
+ import { FxProtocolConfig } from './config';
20
+ import { Interface, JsonFragment } from '@ethersproject/abi';
21
+ import FxUSD_ABI from '../../abi/fx-protocol/FxUSD.json';
22
+ import FxMarket_ABI from '../../abi/fx-protocol/FxMarket.json';
23
+ import EthWeETHOralce_ABI from '../../abi/fx-protocol/weETHOralce.json';
24
+
25
+ import { extractReturnAmountPosition } from '../../executor/utils';
26
+ import { FxProtocolRusdEvent } from './fx-protocol-rusd-event';
27
+ import { BI_POWS } from '../../bigint-constants';
28
+ import { getOnChainState } from './utils';
29
+
30
+ export class FxProtocolRusd
31
+ extends SimpleExchange
32
+ implements IDex<FxProtocolData>
33
+ {
34
+ protected config: DexParams;
35
+
36
+ readonly hasConstantPriceLargeAmounts = true;
37
+
38
+ readonly needWrapNative = false;
39
+
40
+ readonly isFeeOnTransferSupported = false;
41
+
42
+ public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
43
+ getDexKeysWithNetwork(FxProtocolConfig);
44
+
45
+ fxUSDIface: Interface;
46
+ rUSDMarketIface: Interface;
47
+ weETHOracleIface: Interface;
48
+ fxProtocolRusdPool: FxProtocolRusdEvent;
49
+ logger: Logger;
50
+
51
+ constructor(
52
+ readonly network: Network,
53
+ readonly dexKey: string,
54
+ readonly dexHelper: IDexHelper,
55
+ ) {
56
+ super(dexHelper, dexKey);
57
+ const config = FxProtocolConfig[dexKey][network];
58
+ this.fxUSDIface = new Interface(FxUSD_ABI as JsonFragment[]);
59
+ this.rUSDMarketIface = new Interface(FxMarket_ABI as JsonFragment[]);
60
+ this.weETHOracleIface = new Interface(EthWeETHOralce_ABI as JsonFragment[]);
61
+ this.config = {
62
+ rUSDAddress: config.rUSDAddress.toLowerCase(),
63
+ weETHAddress: config.weETHAddress.toLowerCase(),
64
+ rUSDWeETHMarketAddress: config.rUSDWeETHMarketAddress.toLowerCase(),
65
+ weETHOracleAddress: config.weETHOracleAddress.toLowerCase(),
66
+ };
67
+ this.logger = dexHelper.getLogger(dexKey);
68
+ this.fxProtocolRusdPool = new FxProtocolRusdEvent(
69
+ this.dexKey,
70
+ dexHelper,
71
+ this.config.rUSDWeETHMarketAddress,
72
+ this.rUSDMarketIface,
73
+ this.config.weETHOracleAddress,
74
+ this.weETHOracleIface,
75
+ this.logger,
76
+ );
77
+ }
78
+
79
+ async initializePricing(blockNumber: number) {
80
+ const poolState = await getOnChainState(
81
+ this.dexHelper.multiContract,
82
+ this.config.rUSDWeETHMarketAddress,
83
+ this.rUSDMarketIface,
84
+ this.config.weETHOracleAddress,
85
+ this.weETHOracleIface,
86
+ blockNumber,
87
+ );
88
+
89
+ await this.fxProtocolRusdPool.initialize(blockNumber, {
90
+ state: poolState,
91
+ });
92
+ }
93
+
94
+ getConfig() {
95
+ return this.config;
96
+ }
97
+
98
+ is_weETH(token: string) {
99
+ return token.toLowerCase() === this.config.weETHAddress.toLowerCase();
100
+ }
101
+
102
+ is_rUSD(token: string) {
103
+ return token.toLowerCase() === this.config.rUSDAddress.toLowerCase();
104
+ }
105
+
106
+ is_rUSD_swap_token(srcToken: string, destToken: string) {
107
+ return (
108
+ (this.is_weETH(srcToken) && this.is_rUSD(destToken)) ||
109
+ (this.is_rUSD(srcToken) && this.is_weETH(destToken))
110
+ );
111
+ }
112
+ // Returns the list of contract adapters (name and index)
113
+ // for a buy/sell. Return null if there are no adapters.
114
+ getAdapters() {
115
+ return null;
116
+ }
117
+
118
+ async getPoolIdentifiers(
119
+ srcToken: Token,
120
+ destToken: Token,
121
+ side: SwapSide,
122
+ blockNumber: number,
123
+ ): Promise<string[]> {
124
+ if (!this.is_rUSD_swap_token(srcToken.address, destToken.address)) {
125
+ return [];
126
+ }
127
+ return [`${this.dexKey}_${this.config.rUSDAddress}`];
128
+ }
129
+
130
+ async getPricesVolume(
131
+ srcToken: Token,
132
+ destToken: Token,
133
+ amounts: bigint[],
134
+ side: SwapSide,
135
+ blockNumber: number,
136
+ limitPools?: string[],
137
+ ): Promise<null | ExchangePrices<FxProtocolData>> {
138
+ // note: BUY is not supported
139
+ if (side === SwapSide.BUY) return null;
140
+
141
+ const isRUSDSwapToken = this.is_rUSD_swap_token(
142
+ srcToken.address,
143
+ destToken.address,
144
+ );
145
+ if (!isRUSDSwapToken) {
146
+ return null;
147
+ }
148
+ const pool = this.fxProtocolRusdPool;
149
+ const is_redeem = this.is_weETH(destToken.address);
150
+ const unitIn = BI_POWS[18];
151
+ const unitOut = await pool.getPrice(blockNumber, unitIn, is_redeem);
152
+ const amountsOut = await Promise.all(
153
+ amounts.map(_amountIn =>
154
+ pool.getPrice(blockNumber, _amountIn, is_redeem),
155
+ ),
156
+ );
157
+
158
+ return [
159
+ {
160
+ unit: unitOut,
161
+ prices: amountsOut,
162
+ data: {},
163
+ poolAddresses: [this.config.rUSDAddress],
164
+ exchange: this.dexKey,
165
+ gasCost: 1000000,
166
+ },
167
+ ];
168
+ }
169
+
170
+ // Returns estimated gas cost of calldata for this DEX in multiSwap
171
+ getCalldataGasCost(
172
+ poolPrices: PoolPrices<FxProtocolData>,
173
+ ): number | number[] {
174
+ return CALLDATA_GAS_COST.DEX_NO_PAYLOAD;
175
+ }
176
+
177
+ // Encode params required by the exchange adapter
178
+ // V5: Used for multiSwap, buy & megaSwap
179
+ // V6: Not used, can be left blank
180
+ // Hint: abiCoder.encodeParameter() could be useful
181
+ getAdapterParam(
182
+ srcToken: string,
183
+ destToken: string,
184
+ srcAmount: string,
185
+ destAmount: string,
186
+ data: FxProtocolData,
187
+ side: SwapSide,
188
+ ): AdapterExchangeParam {
189
+ const payload = '0x';
190
+
191
+ return {
192
+ targetExchange: this.config.rUSDAddress,
193
+ payload,
194
+ networkFee: '0',
195
+ };
196
+ }
197
+
198
+ async getDexParam(
199
+ srcToken: Address,
200
+ destToken: Address,
201
+ srcAmount: NumberAsString,
202
+ destAmount: NumberAsString,
203
+ recipient: Address,
204
+ data: FxProtocolData,
205
+ side: SwapSide,
206
+ ): Promise<DexExchangeParam> {
207
+ const is_rUSD_src = this.is_rUSD(srcToken);
208
+ const is_weETH_src = this.is_weETH(srcToken);
209
+ const is_rUSD_dest = this.is_rUSD(destToken);
210
+ const is_weETH_dest = this.is_weETH(destToken);
211
+
212
+ if (is_weETH_src && is_rUSD_dest) {
213
+ const exchangeData = this.fxUSDIface.encodeFunctionData('mint', [
214
+ this.config.weETHAddress,
215
+ srcAmount,
216
+ recipient,
217
+ '0',
218
+ ]);
219
+ return {
220
+ needWrapNative: false,
221
+ dexFuncHasRecipient: true,
222
+ exchangeData,
223
+ targetExchange: this.config.rUSDAddress,
224
+ returnAmountPos: extractReturnAmountPosition(
225
+ this.fxUSDIface,
226
+ 'mint',
227
+ '_amountOut',
228
+ ),
229
+ };
230
+ }
231
+ if (is_rUSD_src && is_weETH_dest) {
232
+ const exchangeData = this.fxUSDIface.encodeFunctionData('redeem', [
233
+ this.config.weETHAddress,
234
+ srcAmount,
235
+ recipient,
236
+ '0',
237
+ ]);
238
+ return {
239
+ needWrapNative: false,
240
+ dexFuncHasRecipient: true,
241
+ exchangeData,
242
+ targetExchange: this.config.rUSDAddress,
243
+ returnAmountPos: extractReturnAmountPosition(
244
+ this.fxUSDIface,
245
+ 'redeem',
246
+ '_amountOut',
247
+ ),
248
+ };
249
+ }
250
+ throw new Error('LOGIC ERROR');
251
+ }
252
+
253
+ // Returns list of top pools based on liquidity. Max
254
+ // limit number pools should be returned.
255
+ async getTopPoolsForToken(
256
+ tokenAddress: Address,
257
+ limit: number,
258
+ ): Promise<PoolLiquidity[]> {
259
+ if (!this.is_rUSD(tokenAddress) && !this.is_weETH(tokenAddress)) return [];
260
+
261
+ return [
262
+ {
263
+ exchange: this.dexKey,
264
+ address: this.config.rUSDAddress,
265
+ connectorTokens: this.is_weETH(tokenAddress)
266
+ ? [
267
+ {
268
+ decimals: 18,
269
+ address: this.config.rUSDAddress,
270
+ },
271
+ ]
272
+ : [
273
+ {
274
+ decimals: 18,
275
+ address: this.config.weETHAddress,
276
+ },
277
+ ],
278
+ liquidityUSD: 1000000000, // Just returning a big number so this DEX will be preferred
279
+ },
280
+ ];
281
+ }
282
+ }
@@ -0,0 +1,16 @@
1
+ import { Address } from '../../types';
2
+
3
+ export type FxProtocolPoolState = {
4
+ nav: string;
5
+ redeemFee: string;
6
+ weETHPrice: string;
7
+ };
8
+
9
+ export type FxProtocolData = {};
10
+
11
+ export type DexParams = {
12
+ rUSDAddress: Address;
13
+ weETHAddress: Address;
14
+ rUSDWeETHMarketAddress: Address;
15
+ weETHOracleAddress: Address;
16
+ };
@@ -0,0 +1,47 @@
1
+ import { Contract } from 'web3-eth-contract';
2
+ import { FxProtocolPoolState } from './types';
3
+ import { Interface } from '@ethersproject/abi';
4
+
5
+ export async function getOnChainState(
6
+ multiContract: Contract,
7
+ marketAddress: string,
8
+ marketInterface: Interface,
9
+ ethWeETHOracleAddress: string,
10
+ ethWeETHOracleIface: Interface,
11
+ blockNumber: number | 'latest',
12
+ ): Promise<FxProtocolPoolState> {
13
+ const data: { returnData: any[] } = await multiContract.methods
14
+ .aggregate([
15
+ {
16
+ target: marketAddress,
17
+ callData: marketInterface.encodeFunctionData(
18
+ 'fTokenRedeemFeeRatio',
19
+ [],
20
+ ),
21
+ },
22
+ {
23
+ target: ethWeETHOracleAddress,
24
+ callData: ethWeETHOracleIface.encodeFunctionData('latestAnswer', []),
25
+ },
26
+ ])
27
+ .call({}, blockNumber);
28
+
29
+ const redeemFee = BigInt(
30
+ marketInterface.decodeFunctionResult(
31
+ 'fTokenRedeemFeeRatio',
32
+ data.returnData[0],
33
+ )[0],
34
+ ).toString();
35
+ const weETHPrice = BigInt(
36
+ ethWeETHOracleIface.decodeFunctionResult(
37
+ 'latestAnswer',
38
+ data.returnData[1],
39
+ )[0],
40
+ ).toString();
41
+
42
+ return {
43
+ nav: '1000000000000000000',
44
+ redeemFee,
45
+ weETHPrice,
46
+ };
47
+ }