@paraswap/dex-lib 2.47.5 → 2.47.7

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 (139) hide show
  1. package/build/abi/balancer-v2/gyro-e.abi.json +1595 -0
  2. package/build/dex/balancer-v2/balancer-v2.d.ts +6 -3
  3. package/build/dex/balancer-v2/balancer-v2.js +56 -35
  4. package/build/dex/balancer-v2/balancer-v2.js.map +1 -1
  5. package/build/dex/balancer-v2/pools/gyro/Gyro3Pool.d.ts +32 -0
  6. package/build/dex/balancer-v2/pools/gyro/Gyro3Pool.js +149 -0
  7. package/build/dex/balancer-v2/pools/gyro/Gyro3Pool.js.map +1 -0
  8. package/build/dex/balancer-v2/pools/gyro/GyroEPool.d.ts +37 -0
  9. package/build/dex/balancer-v2/pools/gyro/GyroEPool.js +222 -0
  10. package/build/dex/balancer-v2/pools/gyro/GyroEPool.js.map +1 -0
  11. package/build/dex/balancer-v2/pools/gyro/gyro-helpers.d.ts +2 -0
  12. package/build/dex/balancer-v2/pools/gyro/gyro-helpers.js +19 -0
  13. package/build/dex/balancer-v2/pools/gyro/gyro-helpers.js.map +1 -0
  14. package/build/dex/balancer-v2/queryOnChain.d.ts +1 -0
  15. package/build/dex/balancer-v2/queryOnChain.js +40 -0
  16. package/build/dex/balancer-v2/queryOnChain.js.map +1 -0
  17. package/build/dex/balancer-v2/types.d.ts +24 -1
  18. package/build/dex/balancer-v2/types.js +2 -0
  19. package/build/dex/balancer-v2/types.js.map +1 -1
  20. package/build/dex/balancer-v2/utils.d.ts +8 -0
  21. package/build/dex/balancer-v2/utils.js +17 -1
  22. package/build/dex/balancer-v2/utils.js.map +1 -1
  23. package/package.json +2 -1
  24. package/src/abi/balancer-v2/gyro-e.abi.json +1595 -0
  25. package/src/dex/balancer-v2/balancer-v2-e2e.test.ts +94 -0
  26. package/src/dex/balancer-v2/balancer-v2-gyro3.test.ts +268 -0
  27. package/src/dex/balancer-v2/balancer-v2-gyroE.test.ts +401 -0
  28. package/src/dex/balancer-v2/balancer-v2-integration.test.ts +253 -112
  29. package/src/dex/balancer-v2/balancer-v2.ts +107 -67
  30. package/src/dex/balancer-v2/pools/gyro/Gyro3Pool.ts +255 -0
  31. package/src/dex/balancer-v2/pools/gyro/GyroEPool.ts +403 -0
  32. package/src/dex/balancer-v2/pools/gyro/gyro-helpers.ts +15 -0
  33. package/src/dex/balancer-v2/queryOnChain.ts +48 -0
  34. package/src/dex/balancer-v2/types.ts +26 -0
  35. package/src/dex/balancer-v2/utils.ts +16 -0
  36. package/src/dex/synthetix/synthetix-state.ts +2 -2
  37. package/tests/constants-e2e.ts +26 -0
  38. package/build/abi/Redstone.json +0 -739
  39. package/build/abi/angle/stagToken.json +0 -963
  40. package/build/abi/angle-transmuter/Pyth.json +0 -1534
  41. package/build/abi/angle-transmuter/Transmuter.json +0 -1719
  42. package/build/abi/smardex/smardex-factory.json +0 -185
  43. package/build/abi/smardex/smardex-pool.json +0 -578
  44. package/build/abi/smardex/smardex-router.json +0 -648
  45. package/build/abi/trader-joe-v2_1/FactoryABI.json +0 -1208
  46. package/build/abi/trader-joe-v2_1/PairABI.json +0 -1493
  47. package/build/abi/trader-joe-v2_1/RouterABI.json +0 -1489
  48. package/build/abi/trader-joe-v2_1/StateMulticall.json +0 -193
  49. package/build/abi/wombat/pool.json +0 -1289
  50. package/build/dex/angle-staked-stable/angle-staked-stable-pool.d.ts +0 -53
  51. package/build/dex/angle-staked-stable/angle-staked-stable-pool.js +0 -175
  52. package/build/dex/angle-staked-stable/angle-staked-stable-pool.js.map +0 -1
  53. package/build/dex/angle-staked-stable/angle-staked-stable.d.ts +0 -42
  54. package/build/dex/angle-staked-stable/angle-staked-stable.js +0 -232
  55. package/build/dex/angle-staked-stable/angle-staked-stable.js.map +0 -1
  56. package/build/dex/angle-staked-stable/config.d.ts +0 -4
  57. package/build/dex/angle-staked-stable/config.js +0 -26
  58. package/build/dex/angle-staked-stable/config.js.map +0 -1
  59. package/build/dex/angle-staked-stable/types.d.ts +0 -15
  60. package/build/dex/angle-staked-stable/types.js +0 -3
  61. package/build/dex/angle-staked-stable/types.js.map +0 -1
  62. package/build/dex/angle-transmuter/angle-transmuter-pool.d.ts +0 -43
  63. package/build/dex/angle-transmuter/angle-transmuter-pool.js +0 -309
  64. package/build/dex/angle-transmuter/angle-transmuter-pool.js.map +0 -1
  65. package/build/dex/angle-transmuter/angle-transmuter.d.ts +0 -46
  66. package/build/dex/angle-transmuter/angle-transmuter.js +0 -233
  67. package/build/dex/angle-transmuter/angle-transmuter.js.map +0 -1
  68. package/build/dex/angle-transmuter/backedOracle.d.ts +0 -19
  69. package/build/dex/angle-transmuter/backedOracle.js +0 -56
  70. package/build/dex/angle-transmuter/backedOracle.js.map +0 -1
  71. package/build/dex/angle-transmuter/config.d.ts +0 -4
  72. package/build/dex/angle-transmuter/config.js +0 -20
  73. package/build/dex/angle-transmuter/config.js.map +0 -1
  74. package/build/dex/angle-transmuter/constants.d.ts +0 -4
  75. package/build/dex/angle-transmuter/constants.js +0 -8
  76. package/build/dex/angle-transmuter/constants.js.map +0 -1
  77. package/build/dex/angle-transmuter/pyth.d.ts +0 -19
  78. package/build/dex/angle-transmuter/pyth.js +0 -71
  79. package/build/dex/angle-transmuter/pyth.js.map +0 -1
  80. package/build/dex/angle-transmuter/redstone.d.ts +0 -20
  81. package/build/dex/angle-transmuter/redstone.js +0 -73
  82. package/build/dex/angle-transmuter/redstone.js.map +0 -1
  83. package/build/dex/angle-transmuter/transmuter.d.ts +0 -50
  84. package/build/dex/angle-transmuter/transmuter.js +0 -314
  85. package/build/dex/angle-transmuter/transmuter.js.map +0 -1
  86. package/build/dex/angle-transmuter/types.d.ts +0 -144
  87. package/build/dex/angle-transmuter/types.js +0 -31
  88. package/build/dex/angle-transmuter/types.js.map +0 -1
  89. package/build/dex/angle-transmuter/utils.d.ts +0 -9
  90. package/build/dex/angle-transmuter/utils.js +0 -264
  91. package/build/dex/angle-transmuter/utils.js.map +0 -1
  92. package/build/dex/smardex/subgraph-queries.d.ts +0 -1
  93. package/build/dex/smardex/subgraph-queries.js +0 -17
  94. package/build/dex/smardex/subgraph-queries.js.map +0 -1
  95. package/build/dex/smardex/usd-reserves.d.ts +0 -23
  96. package/build/dex/smardex/usd-reserves.js +0 -86
  97. package/build/dex/smardex/usd-reserves.js.map +0 -1
  98. package/build/dex/trader-joe-v2-1-2/config.d.ts +0 -4
  99. package/build/dex/trader-joe-v2-1-2/config.js +0 -47
  100. package/build/dex/trader-joe-v2-1-2/config.js.map +0 -1
  101. package/build/dex/trader-joe-v2-1-2/constants.d.ts +0 -20
  102. package/build/dex/trader-joe-v2-1-2/constants.js +0 -22
  103. package/build/dex/trader-joe-v2-1-2/constants.js.map +0 -1
  104. package/build/dex/trader-joe-v2-1-2/math/index.d.ts +0 -37
  105. package/build/dex/trader-joe-v2-1-2/math/index.js +0 -291
  106. package/build/dex/trader-joe-v2-1-2/math/index.js.map +0 -1
  107. package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2-pool.d.ts +0 -60
  108. package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2-pool.js +0 -248
  109. package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2-pool.js.map +0 -1
  110. package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2.d.ts +0 -53
  111. package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2.js +0 -307
  112. package/build/dex/trader-joe-v2-1-2/trader-joe-v2-1-2.js.map +0 -1
  113. package/build/dex/trader-joe-v2-1-2/types.d.ts +0 -82
  114. package/build/dex/trader-joe-v2-1-2/types.js +0 -9
  115. package/build/dex/trader-joe-v2-1-2/types.js.map +0 -1
  116. package/build/dex/trader-joe-v2_1/config.d.ts +0 -4
  117. package/build/dex/trader-joe-v2_1/config.js +0 -47
  118. package/build/dex/trader-joe-v2_1/config.js.map +0 -1
  119. package/build/dex/trader-joe-v2_1/constants.d.ts +0 -20
  120. package/build/dex/trader-joe-v2_1/constants.js +0 -22
  121. package/build/dex/trader-joe-v2_1/constants.js.map +0 -1
  122. package/build/dex/trader-joe-v2_1/math/index.d.ts +0 -37
  123. package/build/dex/trader-joe-v2_1/math/index.js +0 -291
  124. package/build/dex/trader-joe-v2_1/math/index.js.map +0 -1
  125. package/build/dex/trader-joe-v2_1/trader-joe-v2.1.d.ts +0 -0
  126. package/build/dex/trader-joe-v2_1/trader-joe-v2.1.js +0 -384
  127. package/build/dex/trader-joe-v2_1/trader-joe-v2.1.js.map +0 -1
  128. package/build/dex/trader-joe-v2_1/trader-joe-v2_1-pool.d.ts +0 -60
  129. package/build/dex/trader-joe-v2_1/trader-joe-v2_1-pool.js +0 -247
  130. package/build/dex/trader-joe-v2_1/trader-joe-v2_1-pool.js.map +0 -1
  131. package/build/dex/trader-joe-v2_1/trader-joe-v2_1.d.ts +0 -53
  132. package/build/dex/trader-joe-v2_1/trader-joe-v2_1.js +0 -307
  133. package/build/dex/trader-joe-v2_1/trader-joe-v2_1.js.map +0 -1
  134. package/build/dex/trader-joe-v2_1/types.d.ts +0 -82
  135. package/build/dex/trader-joe-v2_1/types.js +0 -9
  136. package/build/dex/trader-joe-v2_1/types.js.map +0 -1
  137. package/build/dex/wombat/wombat-pool-poller.d.ts +0 -22
  138. package/build/dex/wombat/wombat-pool-poller.js +0 -164
  139. package/build/dex/wombat/wombat-pool-poller.js.map +0 -1
@@ -5,15 +5,15 @@ import {
5
5
  AdapterExchangeParam,
6
6
  Address,
7
7
  ExchangePrices,
8
+ ExchangeTxInfo,
8
9
  Log,
9
10
  Logger,
10
- TxInfo,
11
- PreprocessTransactionOptions,
12
- ExchangeTxInfo,
13
11
  PoolLiquidity,
14
12
  PoolPrices,
13
+ PreprocessTransactionOptions,
15
14
  SimpleExchangeParam,
16
15
  Token,
16
+ TxInfo,
17
17
  } from '../../types';
18
18
  import {
19
19
  ETHER_ADDRESS,
@@ -29,12 +29,14 @@ import { StablePool } from './pools/stable/StablePool';
29
29
  import { WeightedPool } from './pools/weighted/WeightedPool';
30
30
  import { PhantomStablePool } from './pools/phantom-stable/PhantomStablePool';
31
31
  import { LinearPool } from './pools/linear/LinearPool';
32
+ import { Gyro3Pool } from './pools/gyro/Gyro3Pool';
33
+ import { GyroEPool } from './pools/gyro/GyroEPool';
32
34
  import VaultABI from '../../abi/balancer-v2/vault.json';
33
35
  import DirectSwapABI from '../../abi/DirectSwap.json';
34
36
  import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
35
37
  import {
36
- getDexKeysWithNetwork,
37
38
  getBigIntPow,
39
+ getDexKeysWithNetwork,
38
40
  uuidToBytes16,
39
41
  } from '../../utils';
40
42
  import { IDex } from '../../dex/idex';
@@ -44,12 +46,12 @@ import {
44
46
  BalancerPoolTypes,
45
47
  BalancerSwap,
46
48
  BalancerV2Data,
49
+ BalancerV2DirectParam,
47
50
  OptimizedBalancerV2Data,
48
51
  PoolState,
49
52
  PoolStateCache,
50
53
  PoolStateMap,
51
54
  SubgraphPoolAddressDictionary,
52
- BalancerV2DirectParam,
53
55
  SubgraphPoolBase,
54
56
  SwapTypes,
55
57
  } from './types';
@@ -57,7 +59,7 @@ import {
57
59
  getLocalDeadlineAsFriendlyPlaceholder,
58
60
  SimpleExchange,
59
61
  } from '../simple-exchange';
60
- import { BalancerConfig, Adapters } from './config';
62
+ import { Adapters, BalancerConfig } from './config';
61
63
  import {
62
64
  getAllPoolsUsedInPaths,
63
65
  isSameAddress,
@@ -91,6 +93,8 @@ const enabledPoolTypes = [
91
93
  BalancerPoolTypes.SiloLinear,
92
94
  BalancerPoolTypes.TetuLinear,
93
95
  BalancerPoolTypes.YearnLinear,
96
+ BalancerPoolTypes.GyroE,
97
+ BalancerPoolTypes.Gyro3,
94
98
  ];
95
99
 
96
100
  const disabledPoolIds = [
@@ -186,12 +190,15 @@ const fetchAllPools = `query ($count: Int) {
186
190
  id
187
191
  address
188
192
  poolType
193
+ poolTypeVersion
189
194
  tokens (orderBy: index) {
190
195
  address
191
196
  decimals
192
197
  }
193
198
  mainIndex
194
199
  wrappedIndex
200
+
201
+ root3Alpha
195
202
  }
196
203
  }`;
197
204
  // skipping low liquidity composableStablePool (0xbd482ffb3e6e50dc1c437557c3bea2b68f3683ee0000000000000000000003c6) with oracle issues. Experimental.
@@ -224,12 +231,23 @@ export class BalancerV2EventPool extends StatefulEventSubscriber<PoolStateMap> {
224
231
  } = {};
225
232
 
226
233
  pools: {
227
- [type: string]: WeightedPool | StablePool | LinearPool | PhantomStablePool;
234
+ [type: string]:
235
+ | WeightedPool
236
+ | StablePool
237
+ | LinearPool
238
+ | PhantomStablePool
239
+ | Gyro3Pool
240
+ | GyroEPool;
228
241
  };
229
242
 
230
243
  public allPools: SubgraphPoolBase[] = [];
231
244
  vaultDecoder: (log: Log) => any;
232
245
 
246
+ buySupportedPoolTypes: Set<BalancerPoolTypes> = new Set([
247
+ BalancerPoolTypes.Weighted,
248
+ BalancerPoolTypes.GyroE,
249
+ ]);
250
+
233
251
  eventSupportedPoolTypes: BalancerPoolTypes[] = [
234
252
  BalancerPoolTypes.Stable,
235
253
  BalancerPoolTypes.Weighted,
@@ -287,6 +305,8 @@ export class BalancerV2EventPool extends StatefulEventSubscriber<PoolStateMap> {
287
305
  true,
288
306
  );
289
307
  const linearPool = new LinearPool(this.vaultAddress, this.vaultInterface);
308
+ const gyro3Pool = new Gyro3Pool(this.vaultAddress, this.vaultInterface);
309
+ const gyroEPool = new GyroEPool(this.vaultAddress, this.vaultInterface);
290
310
 
291
311
  this.pools = {};
292
312
  this.pools[BalancerPoolTypes.Weighted] = weightedPool;
@@ -308,6 +328,10 @@ export class BalancerV2EventPool extends StatefulEventSubscriber<PoolStateMap> {
308
328
  this.pools[BalancerPoolTypes.BeefyLinear] = linearPool;
309
329
  // Beets uses "Linear" generically for all linear pool types
310
330
  this.pools[BalancerPoolTypes.Linear] = linearPool;
331
+
332
+ this.pools[BalancerPoolTypes.Gyro3] = gyro3Pool;
333
+ this.pools[BalancerPoolTypes.GyroE] = gyroEPool;
334
+
311
335
  this.vaultDecoder = (log: Log) => this.vaultInterface.parseLog(log);
312
336
  this.addressesSubscribed = [vaultAddress];
313
337
 
@@ -366,6 +390,7 @@ export class BalancerV2EventPool extends StatefulEventSubscriber<PoolStateMap> {
366
390
  this.logger.info(
367
391
  `Fetching ${this.parentName}_${this.network} Pools from subgraph`,
368
392
  );
393
+
369
394
  const variables = {
370
395
  count: MAX_POOL_CNT,
371
396
  };
@@ -401,6 +426,7 @@ export class BalancerV2EventPool extends StatefulEventSubscriber<PoolStateMap> {
401
426
  this.logger.info(
402
427
  `Got ${allPools.length} ${this.parentName}_${this.network} pools from subgraph`,
403
428
  );
429
+
404
430
  return allPools;
405
431
  }
406
432
 
@@ -464,8 +490,8 @@ export class BalancerV2EventPool extends StatefulEventSubscriber<PoolStateMap> {
464
490
  }
465
491
 
466
492
  if (
467
- subgraphPool.poolType !== BalancerPoolTypes.Weighted &&
468
- side === SwapSide.BUY
493
+ side === SwapSide.BUY &&
494
+ !this.buySupportedPoolTypes.has(subgraphPool.poolType)
469
495
  ) {
470
496
  return null;
471
497
  }
@@ -802,6 +828,7 @@ export class BalancerV2
802
828
  }
803
829
 
804
830
  const allPools = this.getPoolsWithTokenPair(_from, _to);
831
+
805
832
  const allowedPools = limitPools
806
833
  ? allPools.filter(({ address }) =>
807
834
  limitPools.includes(`${this.dexKey}_${address.toLowerCase()}`),
@@ -853,72 +880,85 @@ export class BalancerV2
853
880
 
854
881
  const poolPrices = allowedPools
855
882
  .map((pool: SubgraphPoolBase) => {
856
- const poolAddress = pool.address.toLowerCase();
857
-
858
- const path = poolGetPathForTokenInOut(
859
- _from.address,
860
- _to.address,
861
- pool,
862
- this.poolAddressMap,
863
- side,
864
- );
865
-
866
- let pathAmounts = amounts;
867
- let resOut: { unit: bigint; prices: bigint[] } | null = null;
868
-
869
- for (let i = 0; i < path.length; i++) {
870
- const poolAddress = path[i].pool.address.toLowerCase();
871
- const poolState = (eventPoolStates[poolAddress] ||
872
- nonEventPoolStates[poolAddress]) as PoolState | undefined;
873
- if (!poolState) {
874
- this.logger.error(`Unable to find the poolState ${poolAddress}`);
875
- return null;
876
- }
877
-
878
- const unitVolume = getBigIntPow(
879
- (side === SwapSide.SELL ? path[i].tokenIn : path[i].tokenOut)
880
- .decimals,
881
- );
882
-
883
- const res = this.eventPools.getPricesPool(
884
- path[i].tokenIn,
885
- path[i].tokenOut,
886
- path[i].pool,
887
- poolState,
888
- pathAmounts,
889
- unitVolume,
883
+ try {
884
+ const poolAddress = pool.address.toLowerCase();
885
+
886
+ const path = poolGetPathForTokenInOut(
887
+ _from.address,
888
+ _to.address,
889
+ pool,
890
+ this.poolAddressMap,
890
891
  side,
891
892
  );
892
893
 
893
- if (!res) {
894
- return null;
894
+ let pathAmounts = amounts;
895
+ let resOut: { unit: bigint; prices: bigint[] } | null = null;
896
+
897
+ for (let i = 0; i < path.length; i++) {
898
+ const poolAddress = path[i].pool.address.toLowerCase();
899
+ const poolState = (eventPoolStates[poolAddress] ||
900
+ nonEventPoolStates[poolAddress]) as PoolState | undefined;
901
+ if (!poolState) {
902
+ this.logger.error(
903
+ `Unable to find the poolState ${poolAddress}`,
904
+ );
905
+ return null;
906
+ }
907
+
908
+ const unitVolume = getBigIntPow(
909
+ (side === SwapSide.SELL ? path[i].tokenIn : path[i].tokenOut)
910
+ .decimals,
911
+ );
912
+
913
+ const res = this.eventPools.getPricesPool(
914
+ path[i].tokenIn,
915
+ path[i].tokenOut,
916
+ path[i].pool,
917
+ poolState,
918
+ pathAmounts,
919
+ unitVolume,
920
+ side,
921
+ );
922
+
923
+ if (!res) {
924
+ return null;
925
+ }
926
+
927
+ pathAmounts = res.prices;
928
+
929
+ if (i === path.length - 1) {
930
+ resOut = res;
931
+ }
895
932
  }
896
933
 
897
- pathAmounts = res.prices;
898
-
899
- if (i === path.length - 1) {
900
- resOut = res;
934
+ if (!resOut) {
935
+ return null;
901
936
  }
902
- }
903
937
 
904
- if (!resOut) {
905
- return null;
906
- }
938
+ return {
939
+ unit: resOut.unit,
940
+ prices: resOut.prices,
941
+ data: {
942
+ poolId: pool.id,
943
+ },
944
+ poolAddresses: [poolAddress],
945
+ exchange: this.dexKey,
946
+ gasCost:
947
+ STABLE_GAS_COST + VARIABLE_GAS_COST_PER_CYCLE * path.length,
948
+ poolIdentifier: `${this.dexKey}_${poolAddress}`,
949
+ };
950
+
951
+ // TODO: re-check what should be the current block time stamp
952
+ } catch (e) {
953
+ this.logger.warn(
954
+ `Error_getPrices ${from.symbol || from.address}, ${
955
+ to.symbol || to.address
956
+ }, ${side}, ${pool.address}:`,
957
+ e,
958
+ );
907
959
 
908
- return {
909
- unit: resOut.unit,
910
- prices: resOut.prices,
911
- data: {
912
- poolId: pool.id,
913
- },
914
- poolAddresses: [poolAddress],
915
- exchange: this.dexKey,
916
- gasCost:
917
- STABLE_GAS_COST + VARIABLE_GAS_COST_PER_CYCLE * path.length,
918
- poolIdentifier: `${this.dexKey}_${poolAddress}`,
919
- };
920
-
921
- // TODO: re-check what should be the current block time stamp
960
+ return;
961
+ }
922
962
  })
923
963
  .filter(p => !!p);
924
964
  return poolPrices as ExchangePrices<BalancerV2Data>;
@@ -0,0 +1,255 @@
1
+ import { Interface } from '@ethersproject/abi';
2
+ import { BigNumber } from '@ethersproject/bignumber';
3
+
4
+ import { BasePool } from '../balancer-v2-pool';
5
+ import { SwapSide } from '../../../../constants';
6
+ import {
7
+ decodeThrowError,
8
+ getTokenScalingFactor,
9
+ safeParseFixed,
10
+ } from '../../utils';
11
+ import { callData, SubgraphPoolBase, PoolState, TokenState } from '../../types';
12
+ import { MathSol } from '../../balancer-v2-math';
13
+ import { Gyro3Maths } from '@balancer-labs/sor';
14
+ import { _addFee, _reduceFee } from './gyro-helpers';
15
+
16
+ // Swap Limit factor
17
+ const SWAP_LIMIT_FACTOR = BigInt('999999000000000000');
18
+
19
+ // All values should be normalised to 18 decimals. e.g. 1USDC = 1e18 not 1e6
20
+ export type Gyro3PoolPairData = {
21
+ balances: BigNumber[];
22
+ indexIn: number;
23
+ indexOut: number;
24
+ swapFee: bigint;
25
+ balanceTertiary: BigNumber; // Balance of the unchanged asset
26
+ decimalsTertiary: number; // Decimals of the unchanged asset
27
+ scalingFactors: bigint[];
28
+ root3Alpha: BigNumber;
29
+ };
30
+
31
+ export class Gyro3Pool extends BasePool {
32
+ vaultAddress: string;
33
+ vaultInterface: Interface;
34
+ poolInterface = new Interface([
35
+ 'function getSwapFeePercentage() view returns (uint256)',
36
+ ]);
37
+
38
+ constructor(vaultAddress: string, vaultInterface: Interface) {
39
+ super();
40
+ this.vaultAddress = vaultAddress;
41
+ this.vaultInterface = vaultInterface;
42
+ }
43
+
44
+ parsePoolPairData(
45
+ pool: SubgraphPoolBase,
46
+ poolState: PoolState,
47
+ tokenIn: string,
48
+ tokenOut: string,
49
+ ): Gyro3PoolPairData {
50
+ // All values should be normalised to 18 decimals. e.g. 1USDC = 1e18 not 1e6
51
+ let indexIn = 0,
52
+ indexOut = 0,
53
+ indexTertiary = 0;
54
+ const scalingFactors: bigint[] = [];
55
+ const balances = pool.tokens.map((t, i) => {
56
+ if (t.address.toLowerCase() === tokenIn.toLowerCase()) {
57
+ indexIn = i;
58
+ } else if (t.address.toLowerCase() === tokenOut.toLowerCase()) {
59
+ indexOut = i;
60
+ } else {
61
+ indexTertiary = i;
62
+ }
63
+
64
+ const scalingFactor = getTokenScalingFactor(t.decimals);
65
+ scalingFactors.push(scalingFactor);
66
+
67
+ return BigNumber.from(
68
+ this._upscale(
69
+ BigInt(poolState.tokens[t.address.toLowerCase()].balance),
70
+ scalingFactor,
71
+ ),
72
+ );
73
+ });
74
+
75
+ const balanceTertiary = balances[indexTertiary];
76
+ const decimalsTertiary = pool.tokens[indexTertiary].decimals;
77
+
78
+ const poolPairData: Gyro3PoolPairData = {
79
+ balances,
80
+ indexIn,
81
+ indexOut,
82
+ scalingFactors,
83
+ swapFee: poolState.swapFee,
84
+ balanceTertiary,
85
+ decimalsTertiary,
86
+ root3Alpha: safeParseFixed(pool.root3Alpha, 18),
87
+ };
88
+
89
+ return poolPairData;
90
+ }
91
+
92
+ getSwapMaxAmount(poolPairData: Gyro3PoolPairData, side: SwapSide): bigint {
93
+ if (side === SwapSide.SELL) {
94
+ // Approximation - similar to traditional stable pools
95
+ return this._downscaleDown(
96
+ MathSol.mulDownFixed(
97
+ poolPairData.balances[poolPairData.indexOut].toBigInt(),
98
+ SWAP_LIMIT_FACTOR,
99
+ ),
100
+ poolPairData.scalingFactors[poolPairData.indexIn],
101
+ );
102
+ } else {
103
+ return this._downscaleDown(
104
+ MathSol.mulDownFixed(
105
+ poolPairData.balances[poolPairData.indexIn].toBigInt(),
106
+ SWAP_LIMIT_FACTOR,
107
+ ),
108
+ poolPairData.scalingFactors[poolPairData.indexOut],
109
+ );
110
+ }
111
+ }
112
+
113
+ /*
114
+ Helper function to construct onchain multicall data for Pool.
115
+ */
116
+ getOnChainCalls(pool: SubgraphPoolBase): callData[] {
117
+ const poolCallData: callData[] = [
118
+ {
119
+ target: this.vaultAddress,
120
+ callData: this.vaultInterface.encodeFunctionData('getPoolTokens', [
121
+ pool.id,
122
+ ]),
123
+ },
124
+ {
125
+ target: pool.address,
126
+ callData: this.poolInterface.encodeFunctionData('getSwapFeePercentage'),
127
+ },
128
+ ];
129
+ return poolCallData;
130
+ }
131
+
132
+ /*
133
+ Helper function to decode multicall data for Pool.
134
+ data must contain returnData
135
+ startIndex is where to start in returnData. Allows this decode function to be called along with other pool types.
136
+ */
137
+ decodeOnChainCalls(
138
+ pool: SubgraphPoolBase,
139
+ data: { success: boolean; returnData: any }[],
140
+ startIndex: number,
141
+ ): [{ [address: string]: PoolState }, number] {
142
+ const pools = {} as { [address: string]: PoolState };
143
+
144
+ const poolTokens = decodeThrowError(
145
+ this.vaultInterface,
146
+ 'getPoolTokens',
147
+ data[startIndex++],
148
+ pool.address,
149
+ );
150
+ const swapFee = decodeThrowError(
151
+ this.poolInterface,
152
+ 'getSwapFeePercentage',
153
+ data[startIndex++],
154
+ pool.address,
155
+ )[0];
156
+
157
+ const poolState: PoolState = {
158
+ swapFee: BigInt(swapFee.toString()),
159
+ orderedTokens: poolTokens.tokens,
160
+ tokens: poolTokens.tokens.reduce(
161
+ (ptAcc: { [address: string]: TokenState }, pt: string, j: number) => {
162
+ const tokenState: TokenState = {
163
+ balance: BigInt(poolTokens.balances[j].toString()),
164
+ };
165
+ ptAcc[pt.toLowerCase()] = tokenState;
166
+ return ptAcc;
167
+ },
168
+ {},
169
+ ),
170
+ };
171
+ pools[pool.address] = poolState;
172
+ return [pools, startIndex];
173
+ }
174
+
175
+ onSell(amounts: bigint[], poolPairData: Gyro3PoolPairData): bigint[] {
176
+ try {
177
+ const invariant = Gyro3Maths._calculateInvariant(
178
+ poolPairData.balances,
179
+ poolPairData.root3Alpha,
180
+ );
181
+
182
+ const virtualOffsetInOut = MathSol.mulDownFixed(
183
+ invariant.toBigInt(),
184
+ poolPairData.root3Alpha.toBigInt(),
185
+ );
186
+
187
+ const tokenAmountsInWithFee = amounts.map(a => {
188
+ const withFee = _reduceFee(a, poolPairData.swapFee);
189
+ return this._upscale(
190
+ withFee,
191
+ poolPairData.scalingFactors[poolPairData.indexIn],
192
+ );
193
+ });
194
+
195
+ return tokenAmountsInWithFee.map(amount => {
196
+ try {
197
+ const amountOut = Gyro3Maths._calcOutGivenIn(
198
+ poolPairData.balances[poolPairData.indexIn],
199
+ poolPairData.balances[poolPairData.indexOut],
200
+ BigNumber.from(amount),
201
+ BigNumber.from(virtualOffsetInOut),
202
+ ).toBigInt();
203
+
204
+ return this._downscaleDown(
205
+ amountOut,
206
+ poolPairData.scalingFactors[poolPairData.indexOut],
207
+ );
208
+ } catch (error) {
209
+ return BigInt(0);
210
+ }
211
+ });
212
+ } catch (error) {
213
+ return [];
214
+ }
215
+ }
216
+
217
+ onBuy(amounts: bigint[], poolPairData: Gyro3PoolPairData): bigint[] {
218
+ try {
219
+ const invariant = Gyro3Maths._calculateInvariant(
220
+ poolPairData.balances,
221
+ poolPairData.root3Alpha,
222
+ );
223
+
224
+ const virtualOffsetInOut = MathSol.mulDownFixed(
225
+ invariant.toBigInt(),
226
+ poolPairData.root3Alpha.toBigInt(),
227
+ );
228
+
229
+ const tokenAmountsUpscaled = amounts.map(a =>
230
+ this._upscale(a, poolPairData.scalingFactors[poolPairData.indexOut]),
231
+ );
232
+
233
+ return tokenAmountsUpscaled.map(amount => {
234
+ try {
235
+ const amountIn = Gyro3Maths._calcInGivenOut(
236
+ poolPairData.balances[poolPairData.indexIn],
237
+ poolPairData.balances[poolPairData.indexOut],
238
+ BigNumber.from(amount),
239
+ BigNumber.from(virtualOffsetInOut),
240
+ ).toBigInt();
241
+
242
+ const downScaledAmount = this._downscaleUp(
243
+ amountIn,
244
+ poolPairData.scalingFactors[poolPairData.indexIn],
245
+ );
246
+ return _addFee(downScaledAmount, poolPairData.swapFee);
247
+ } catch (error) {
248
+ return BigInt(0);
249
+ }
250
+ });
251
+ } catch (error) {
252
+ return [];
253
+ }
254
+ }
255
+ }