@paraswap/dex-lib 2.28.7 → 2.28.9-ramses-v2

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 (149) hide show
  1. package/.idea/aws.xml +17 -0
  2. package/.idea/codeStyles/Project.xml +19 -0
  3. package/.idea/codeStyles/codeStyleConfig.xml +5 -0
  4. package/.idea/misc.xml +6 -0
  5. package/.idea/modules.xml +8 -0
  6. package/.idea/paraswap-dex-lib.iml +9 -0
  7. package/.idea/vcs.xml +6 -0
  8. package/build/abi/RamsesV2StateMulticall.abi.json +891 -0
  9. package/build/abi/algebra/AlgebraPool.abi.json +727 -0
  10. package/build/abi/curve-v1/StableSwapFRXETH.json +889 -0
  11. package/build/abi/curve-v1/StableSwapWBETH.json +1223 -0
  12. package/build/abi/wBETH.json +1527 -0
  13. package/build/dex/algebra/algebra-pool.d.ts +46 -0
  14. package/build/dex/algebra/algebra-pool.js +336 -0
  15. package/build/dex/algebra/algebra-pool.js.map +1 -0
  16. package/build/dex/balancer-v2/balancer-v2.js +2 -0
  17. package/build/dex/balancer-v2/balancer-v2.js.map +1 -1
  18. package/build/dex/balancer-v2/optimizer.js +51 -38
  19. package/build/dex/balancer-v2/optimizer.js.map +1 -1
  20. package/build/dex/balancer-v2/types.d.ts +4 -0
  21. package/build/dex/balancer-v2/types.js.map +1 -1
  22. package/build/dex/curve-v1/pools/frxETHpool.d.ts +7 -0
  23. package/build/dex/curve-v1/pools/frxETHpool.js +27 -0
  24. package/build/dex/curve-v1/pools/frxETHpool.js.map +1 -0
  25. package/build/dex/curve-v1/pools/wbETHpool.d.ts +29 -0
  26. package/build/dex/curve-v1/pools/wbETHpool.js +408 -0
  27. package/build/dex/curve-v1/pools/wbETHpool.js.map +1 -0
  28. package/build/dex/curve-v1-factory/price-handlers/functions/calc_token_amount.js +26 -1
  29. package/build/dex/curve-v1-factory/price-handlers/functions/calc_token_amount.js.map +1 -1
  30. package/build/dex/index.js +2 -0
  31. package/build/dex/index.js.map +1 -1
  32. package/build/dex/pancakeswap-v3/types.d.ts +14 -0
  33. package/build/dex/{test-dex → pancakeswap-v3}/types.js.map +1 -1
  34. package/build/dex/{test-dex → ramses-v2}/config.d.ts +1 -1
  35. package/build/dex/ramses-v2/config.js +28 -0
  36. package/build/dex/ramses-v2/config.js.map +1 -0
  37. package/build/dex/ramses-v2/constants.d.ts +28 -0
  38. package/build/dex/ramses-v2/constants.js +35 -0
  39. package/build/dex/ramses-v2/constants.js.map +1 -0
  40. package/build/dex/ramses-v2/contract-math/BitMath.d.ts +4 -0
  41. package/build/dex/ramses-v2/contract-math/BitMath.js +93 -0
  42. package/build/dex/ramses-v2/contract-math/BitMath.js.map +1 -0
  43. package/build/dex/ramses-v2/contract-math/FixedPoint128.d.ts +3 -0
  44. package/build/dex/ramses-v2/contract-math/FixedPoint128.js +8 -0
  45. package/build/dex/ramses-v2/contract-math/FixedPoint128.js.map +1 -0
  46. package/build/dex/ramses-v2/contract-math/FixedPoint96.d.ts +4 -0
  47. package/build/dex/ramses-v2/contract-math/FixedPoint96.js +9 -0
  48. package/build/dex/ramses-v2/contract-math/FixedPoint96.js.map +1 -0
  49. package/build/dex/ramses-v2/contract-math/FullMath.d.ts +4 -0
  50. package/build/dex/ramses-v2/contract-math/FullMath.js +19 -0
  51. package/build/dex/ramses-v2/contract-math/FullMath.js.map +1 -0
  52. package/build/dex/ramses-v2/contract-math/LiquidityMath.d.ts +3 -0
  53. package/build/dex/ramses-v2/contract-math/LiquidityMath.js +22 -0
  54. package/build/dex/ramses-v2/contract-math/LiquidityMath.js.map +1 -0
  55. package/build/dex/ramses-v2/contract-math/Oracle.d.ts +10 -0
  56. package/build/dex/ramses-v2/contract-math/Oracle.js +133 -0
  57. package/build/dex/ramses-v2/contract-math/Oracle.js.map +1 -0
  58. package/build/dex/ramses-v2/contract-math/SqrtPriceMath.d.ts +10 -0
  59. package/build/dex/ramses-v2/contract-math/SqrtPriceMath.js +91 -0
  60. package/build/dex/ramses-v2/contract-math/SqrtPriceMath.js.map +1 -0
  61. package/build/dex/ramses-v2/contract-math/SwapMath.d.ts +8 -0
  62. package/build/dex/ramses-v2/contract-math/SwapMath.js +70 -0
  63. package/build/dex/ramses-v2/contract-math/SwapMath.js.map +1 -0
  64. package/build/dex/ramses-v2/contract-math/Tick.d.ts +7 -0
  65. package/build/dex/ramses-v2/contract-math/Tick.js +45 -0
  66. package/build/dex/ramses-v2/contract-math/Tick.js.map +1 -0
  67. package/build/dex/ramses-v2/contract-math/TickBitMap.d.ts +8 -0
  68. package/build/dex/ramses-v2/contract-math/TickBitMap.js +87 -0
  69. package/build/dex/ramses-v2/contract-math/TickBitMap.js.map +1 -0
  70. package/build/dex/ramses-v2/contract-math/TickMath.d.ts +8 -0
  71. package/build/dex/ramses-v2/contract-math/TickMath.js +162 -0
  72. package/build/dex/ramses-v2/contract-math/TickMath.js.map +1 -0
  73. package/build/dex/ramses-v2/contract-math/UnsafeMath.d.ts +3 -0
  74. package/build/dex/ramses-v2/contract-math/UnsafeMath.js +10 -0
  75. package/build/dex/ramses-v2/contract-math/UnsafeMath.js.map +1 -0
  76. package/build/dex/ramses-v2/contract-math/uniswap-v3-math.d.ts +18 -0
  77. package/build/dex/ramses-v2/contract-math/uniswap-v3-math.js +392 -0
  78. package/build/dex/ramses-v2/contract-math/uniswap-v3-math.js.map +1 -0
  79. package/build/dex/ramses-v2/contract-math/utils.d.ts +3 -0
  80. package/build/dex/ramses-v2/contract-math/utils.js +16 -0
  81. package/build/dex/ramses-v2/contract-math/utils.js.map +1 -0
  82. package/build/dex/ramses-v2/ramses-v2-pool.d.ts +44 -0
  83. package/build/dex/ramses-v2/ramses-v2-pool.js +327 -0
  84. package/build/dex/ramses-v2/ramses-v2-pool.js.map +1 -0
  85. package/build/dex/ramses-v2/ramses-v2.d.ts +61 -0
  86. package/build/dex/ramses-v2/ramses-v2.js +690 -0
  87. package/build/dex/ramses-v2/ramses-v2.js.map +1 -0
  88. package/build/dex/ramses-v2/types.d.ts +154 -0
  89. package/build/dex/ramses-v2/types.js +9 -0
  90. package/build/dex/ramses-v2/types.js.map +1 -0
  91. package/build/dex/ramses-v2/utils.d.ts +6 -0
  92. package/build/dex/ramses-v2/utils.js +71 -0
  93. package/build/dex/ramses-v2/utils.js.map +1 -0
  94. package/build/dex/solidly/forks-override/ramses.d.ts +8 -0
  95. package/build/dex/solidly/forks-override/ramses.js +15 -0
  96. package/build/dex/solidly/forks-override/ramses.js.map +1 -0
  97. package/build/dex/uniswap-v3/uniswap-v3.js +1 -1
  98. package/build/dex/uniswap-v3/uniswap-v3.js.map +1 -1
  99. package/build/lib/multi-wrapper.js +5 -3
  100. package/build/lib/multi-wrapper.js.map +1 -1
  101. package/package.json +1 -1
  102. package/src/abi/RamsesV2Pool.abi.json +1554 -0
  103. package/src/abi/RamsesV2StateMulticall.abi.json +891 -0
  104. package/src/dex/balancer-v2/balancer-v2.ts +2 -0
  105. package/src/dex/balancer-v2/optimizer.ts +62 -49
  106. package/src/dex/balancer-v2/types.ts +4 -0
  107. package/src/dex/curve-v1-factory/price-handlers/functions/calc_token_amount.ts +33 -1
  108. package/src/dex/index.ts +2 -0
  109. package/src/dex/ramses-v2/config.ts +31 -0
  110. package/src/dex/ramses-v2/constants.ts +41 -0
  111. package/src/dex/ramses-v2/contract-math/BitMath.ts +90 -0
  112. package/src/dex/ramses-v2/contract-math/FixedPoint128.ts +3 -0
  113. package/src/dex/ramses-v2/contract-math/FixedPoint96.ts +4 -0
  114. package/src/dex/ramses-v2/contract-math/FullMath.ts +30 -0
  115. package/src/dex/ramses-v2/contract-math/LiquidityMath.ts +17 -0
  116. package/src/dex/ramses-v2/contract-math/Oracle.ts +226 -0
  117. package/src/dex/ramses-v2/contract-math/SqrtPriceMath.ts +226 -0
  118. package/src/dex/ramses-v2/contract-math/SwapMath.ts +139 -0
  119. package/src/dex/ramses-v2/contract-math/Tick.ts +82 -0
  120. package/src/dex/ramses-v2/contract-math/TickBitMap.ts +128 -0
  121. package/src/dex/ramses-v2/contract-math/TickMath.ts +211 -0
  122. package/src/dex/ramses-v2/contract-math/UnsafeMath.ts +5 -0
  123. package/src/dex/ramses-v2/contract-math/uniswap-v3-math.ts +666 -0
  124. package/src/dex/ramses-v2/contract-math/utils.ts +11 -0
  125. package/src/dex/ramses-v2/ramses-v2-e2e.test.ts +100 -0
  126. package/src/dex/ramses-v2/ramses-v2-events.test.ts +182 -0
  127. package/src/dex/ramses-v2/ramses-v2-integration.test.ts +462 -0
  128. package/src/dex/ramses-v2/ramses-v2-pool.ts +546 -0
  129. package/src/dex/ramses-v2/ramses-v2.ts +1136 -0
  130. package/src/dex/ramses-v2/types.ts +177 -0
  131. package/src/dex/ramses-v2/utils.ts +90 -0
  132. package/src/dex/uniswap-v3/uniswap-v3-integration.test.ts +26 -3
  133. package/src/dex/uniswap-v3/uniswap-v3.ts +1 -1
  134. package/src/lib/multi-wrapper.ts +4 -4
  135. package/tests/constants-e2e.ts +1 -5
  136. package/tests/utils-e2e.ts +1 -0
  137. package/build/dex/test-dex/config.js +0 -15
  138. package/build/dex/test-dex/config.js.map +0 -1
  139. package/build/dex/test-dex/test-dex-pool.d.ts +0 -39
  140. package/build/dex/test-dex/test-dex-pool.js +0 -63
  141. package/build/dex/test-dex/test-dex-pool.js.map +0 -1
  142. package/build/dex/test-dex/test-dex.d.ts +0 -37
  143. package/build/dex/test-dex/test-dex.js +0 -126
  144. package/build/dex/test-dex/test-dex.js.map +0 -1
  145. package/build/dex/test-dex/types.d.ts +0 -6
  146. package/build/dex/woo-fi-v2/woo-fi-math.d.ts +0 -16
  147. package/build/dex/woo-fi-v2/woo-fi-math.js +0 -137
  148. package/build/dex/woo-fi-v2/woo-fi-math.js.map +0 -1
  149. /package/build/dex/{test-dex → pancakeswap-v3}/types.js +0 -0
@@ -1,55 +1,68 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.balancerV2Merge = void 0;
7
+ const lodash_1 = __importDefault(require("lodash"));
4
8
  const constants_1 = require("../../constants");
5
9
  const config_1 = require("./config");
6
- const MAX_UINT256 = '115792089237316195423570985008687907853269984665640564039457584007913129639935';
7
- const AllBalancerV2Forks = Object.keys(config_1.BalancerConfig);
8
10
  function balancerV2Merge(or) {
9
- const fixSwap = (rawSwap, side) => {
10
- const newBalancers = {};
11
- let optimizedSwap = new Array();
12
- rawSwap.forEach((s) => {
13
- const exchangeKey = s.exchange.toLowerCase();
14
- if (AllBalancerV2Forks.some(d => d.toLowerCase() === exchangeKey)) {
15
- if (!(exchangeKey in newBalancers)) {
16
- newBalancers[exchangeKey] = {
17
- exchange: s.exchange,
18
- srcAmount: '0',
19
- destAmount: '0',
20
- percent: 0,
21
- poolAddresses: [],
22
- data: {
23
- swaps: new Array(),
24
- gasUSD: '0',
25
- },
26
- };
27
- }
28
- newBalancers[exchangeKey].srcAmount = (BigInt(newBalancers[exchangeKey].srcAmount) + BigInt(s.srcAmount)).toString();
29
- newBalancers[exchangeKey].destAmount = (BigInt(newBalancers[exchangeKey].destAmount) + BigInt(s.destAmount)).toString();
30
- newBalancers[exchangeKey].percent += s.percent;
31
- newBalancers[exchangeKey].data.exchangeProxy = s.data.exchangeProxy;
32
- newBalancers[exchangeKey].data.gasUSD = (parseFloat(newBalancers[exchangeKey].data.gasUSD) +
33
- parseFloat(s.data.gasUSD)).toFixed(6);
34
- newBalancers[exchangeKey].data.swaps.push({
35
- poolId: s.data.poolId,
36
- amount: side === constants_1.SwapSide.SELL ? s.srcAmount : s.destAmount,
11
+ const balancerForksList = Object.keys(config_1.BalancerConfig).map(b => b.toLowerCase());
12
+ const fixSwap = (rawRate, side) => {
13
+ let lastExchange = false;
14
+ let optimizedRate = new Array();
15
+ rawRate.forEach((s) => {
16
+ if (s.swapExchanges.length !== 1 ||
17
+ !balancerForksList.includes(s.swapExchanges[0].exchange.toLowerCase())) {
18
+ lastExchange = false;
19
+ optimizedRate.push(s);
20
+ }
21
+ else if (lastExchange &&
22
+ lastExchange.swapExchanges[0].exchange.toLowerCase() ===
23
+ s.swapExchanges[0].exchange.toLowerCase() &&
24
+ lodash_1.default.last(lastExchange.swapExchanges[0].data.swaps).tokenOut.toLowerCase() ===
25
+ s.swapExchanges[0].data.tokenIn.toLowerCase()) {
26
+ const [lastExchangeSwap] = lastExchange.swapExchanges;
27
+ const [currentSwap] = s.swapExchanges;
28
+ lastExchangeSwap.srcAmount = (BigInt(lastExchangeSwap.srcAmount) + BigInt(currentSwap.srcAmount)).toString();
29
+ lastExchangeSwap.destAmount = (BigInt(lastExchangeSwap.destAmount) + BigInt(currentSwap.destAmount)).toString();
30
+ lastExchangeSwap.percent += currentSwap.percent;
31
+ lastExchangeSwap.data.gasUSD = (parseFloat(lastExchangeSwap.data.gasUSD) +
32
+ parseFloat(currentSwap.data.gasUSD)).toFixed(6);
33
+ lastExchangeSwap.data.swaps.push({
34
+ poolId: currentSwap.data.poolId,
35
+ amount: side === constants_1.SwapSide.SELL
36
+ ? currentSwap.srcAmount
37
+ : currentSwap.destAmount,
38
+ tokenIn: currentSwap.data.tokenIn,
39
+ tokenOut: currentSwap.data.tokenOut,
37
40
  });
38
- newBalancers[exchangeKey].poolAddresses.push(s.poolAddresses[0]);
41
+ lastExchangeSwap.poolAddresses.push(currentSwap.poolAddresses[0]);
39
42
  }
40
43
  else {
41
- optimizedSwap.push(s);
44
+ lastExchange = lodash_1.default.cloneDeep(s);
45
+ lastExchange.swapExchanges[0].data = {};
46
+ lastExchange.swapExchanges[0].data.gasUSD =
47
+ s.swapExchanges[0].data.gasUSD;
48
+ lastExchange.swapExchanges[0].data.swaps = [
49
+ {
50
+ poolId: s.swapExchanges[0].data.poolId,
51
+ amount: side === constants_1.SwapSide.SELL
52
+ ? s.swapExchanges[0].srcAmount
53
+ : s.swapExchanges[0].destAmount,
54
+ tokenIn: s.swapExchanges[0].data.tokenIn,
55
+ tokenOut: s.swapExchanges[0].data.tokenOut,
56
+ },
57
+ ];
58
+ optimizedRate.push(lastExchange);
42
59
  }
43
60
  });
44
- optimizedSwap = optimizedSwap.concat(Object.values(newBalancers));
45
- return optimizedSwap;
61
+ return optimizedRate;
46
62
  };
47
63
  or.bestRoute = or.bestRoute.map(r => ({
48
64
  ...r,
49
- swaps: r.swaps.map(s => ({
50
- ...s,
51
- swapExchanges: fixSwap(s.swapExchanges, or.side),
52
- })),
65
+ swaps: fixSwap(r.swaps, or.side),
53
66
  }));
54
67
  return or;
55
68
  }
@@ -1 +1 @@
1
- {"version":3,"file":"optimizer.js","sourceRoot":"","sources":["../../../src/dex/balancer-v2/optimizer.ts"],"names":[],"mappings":";;;AAEA,+CAA2C;AAC3C,qCAA0C;AAE1C,MAAM,WAAW,GACf,gFAAgF,CAAC;AAEnF,MAAM,kBAAkB,GAAG,MAAM,CAAC,IAAI,CAAC,uBAAc,CAAC,CAAC;AAEvD,SAAgB,eAAe,CAAC,EAAmB;IACjD,MAAM,OAAO,GAAG,CACd,OAAmC,EACnC,IAAc,EACc,EAAE;QAC9B,MAAM,YAAY,GAAgD,EAAE,CAAC;QACrE,IAAI,aAAa,GAAG,IAAI,KAAK,EAA4B,CAAC;QAC1D,OAAO,CAAC,OAAO,CAAC,CAAC,CAA2B,EAAE,EAAE;YAC9C,MAAM,WAAW,GAAG,CAAC,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC;YAC7C,IAAI,kBAAkB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,KAAK,WAAW,CAAC,EAAE;gBACjE,IAAI,CAAC,CAAC,WAAW,IAAI,YAAY,CAAC,EAAE;oBAClC,YAAY,CAAC,WAAW,CAAC,GAAG;wBAC1B,QAAQ,EAAE,CAAC,CAAC,QAAQ;wBACpB,SAAS,EAAE,GAAG;wBACd,UAAU,EAAE,GAAG;wBACf,OAAO,EAAE,CAAC;wBACV,aAAa,EAAE,E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CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ;qBAC3C;iBACF,CAAC;gBACF,aAAa,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;aAClC;QACH,CAAC,CAAC,CAAC;QACH,OAAO,aAAa,CAAC;IACvB,CAAC,CAAC;IAEF,EAAE,CAAC,SAAS,GAAG,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QACpC,GAAG,CAAC;QACJ,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC;KACjC,CAAC,CAAC,CAAC;IACJ,OAAO,EAAE,CAAC;AACZ,CAAC;AA5ED,0CA4EC"}
@@ -70,6 +70,8 @@ export interface SubgraphPoolBase {
70
70
  export declare type BalancerSwapV2 = {
71
71
  poolId: string;
72
72
  amount: string;
73
+ tokenIn: string;
74
+ tokenOut: string;
73
75
  };
74
76
  export declare type OptimizedBalancerV2Data = {
75
77
  swaps: BalancerSwapV2[];
@@ -119,6 +121,8 @@ export declare type BalancerV2DirectParam = [
119
121
  ];
120
122
  export declare type BalancerV2Data = {
121
123
  poolId: string;
124
+ tokenIn: string;
125
+ tokenOut: string;
122
126
  };
123
127
  export declare type DexParams = {
124
128
  subgraphURL: string;
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/dex/balancer-v2/types.ts"],"names":[],"mappings":";;;AAGA,mEAAmE;AACnE,IAAY,iBAkBX;AAlBD,WAAY,iBAAiB;IAC3B,0CAAqB,CAAA;IACrB,sCAAiB,CAAA;IACjB,8CAAyB,CAAA;IACzB,sEAAiD,CAAA;IACjD,8CAAyB,CAAA;IACzB,oDAA+B,CAAA;IAC/B,0DAAqC,CAAA;IACrC,sCAAiB,CAAA;IACjB,8CAAyB,CAAA;IACzB,oDAA+B,CAAA;IAC/B,gDAA2B,CAAA;IAC3B,oDAA+B,CAAA;IAC/B,gDAA2B,CAAA;IAC3B,kDAA6B,CAAA;IAC7B,8CAAyB,CAAA;IACzB,8CAAyB,CAAA;IACzB,gDAA2B,CAAA;AAC7B,CAAC,EAlBW,iBAAiB,GAAjB,yBAAiB,KAAjB,yBAAiB,QAkB5B;AAqFD,IAAY,SAGX;AAHD,WAAY,SAAS;IACnB,uDAAW,CAAA;IACX,yDAAY,CAAA;AACd,CAAC,EAHW,SAAS,GAAT,iBAAS,KAAT,iBAAS,QAGpB"}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/dex/balancer-v2/types.ts"],"names":[],"mappings":";;;AAGA,mEAAmE;AACnE,IAAY,iBAkBX;AAlBD,WAAY,iBAAiB;IAC3B,0CAAqB,CAAA;IACrB,sCAAiB,CAAA;IACjB,8CAAyB,CAAA;IACzB,sEAAiD,CAAA;IACjD,8CAAyB,CAAA;IACzB,oDAA+B,CAAA;IAC/B,0DAAqC,CAAA;IACrC,sCAAiB,CAAA;IACjB,8CAAyB,CAAA;IACzB,oDAA+B,CAAA;IAC/B,gDAA2B,CAAA;IAC3B,oDAA+B,CAAA;IAC/B,gDAA2B,CAAA;IAC3B,kDAA6B,CAAA;IAC7B,8CAAyB,CAAA;IACzB,8CAAyB,CAAA;IACzB,gDAA2B,CAAA;AAC7B,CAAC,EAlBW,iBAAiB,GAAjB,yBAAiB,KAAjB,yBAAiB,QAkB5B;AAuFD,IAAY,SAGX;AAHD,WAAY,SAAS;IACnB,uDAAW,CAAA;IACX,yDAAY,CAAA;AACd,CAAC,EAHW,SAAS,GAAT,iBAAS,KAAT,iBAAS,QAGpB"}
@@ -0,0 +1,7 @@
1
+ import { Address } from '../../../types';
2
+ import { IDexHelper } from '../../../dex-helper';
3
+ import { ThreePool } from './3pool';
4
+ export declare const address: Address;
5
+ export declare class FRXETHPool extends ThreePool {
6
+ constructor(parentName: string, dexHelper: IDexHelper);
7
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.FRXETHPool = exports.address = void 0;
7
+ const utils_1 = require("../../../utils");
8
+ const StableSwapFRXETH_json_1 = __importDefault(require("../../../abi/curve-v1/StableSwapFRXETH.json"));
9
+ const _3pool_1 = require("./3pool");
10
+ const pool = 'frxETH';
11
+ exports.address = '0xa1f8a6807c402e4a15ef4eba36528a3fed24e577'.toLowerCase();
12
+ const tokenAddress = '0xf43211935c781d5ca1a41d2041f397b8a7366c7a';
13
+ const N_COINS = 2;
14
+ const PRECISION_MUL = ['1', '1'].map(utils_1.bigNumberify);
15
+ const USE_LENDING = [false, false];
16
+ const COINS = [
17
+ '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
18
+ '0x5e8422345238f34275888049021821e8e08caa1f',
19
+ ];
20
+ const trackCoins = true;
21
+ class FRXETHPool extends _3pool_1.ThreePool {
22
+ constructor(parentName, dexHelper) {
23
+ super(parentName, dexHelper, pool, exports.address, tokenAddress, trackCoins, StableSwapFRXETH_json_1.default, N_COINS, PRECISION_MUL, USE_LENDING, COINS, 'FRXETHPool');
24
+ }
25
+ }
26
+ exports.FRXETHPool = FRXETHPool;
27
+ //# sourceMappingURL=frxETHpool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"frxETHpool.js","sourceRoot":"","sources":["../../../../src/dex/curve-v1/pools/frxETHpool.ts"],"names":[],"mappings":";;;;;;AAEA,0CAA8C;AAC9C,wGAA2E;AAC3E,oCAAoC;AAEpC,MAAM,IAAI,GAAG,QAAQ,CAAC;AACT,QAAA,OAAO,GAClB,4CAA4C,CAAC,WAAW,EAAE,CAAC;AAC7D,MAAM,YAAY,GAAY,4CAA4C,CAAC;AAC3E,MAAM,OAAO,GAAW,CAAC,CAAC;AAC1B,MAAM,aAAa,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,GAAG,CAAC,oBAAY,CAAC,CAAC;AACnD,MAAM,WAAW,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;AACnC,MAAM,KAAK,GAAG;IACZ,4CAA4C;IAC5C,4CAA4C;CAC7C,CAAC;AACF,MAAM,UAAU,GAAG,IAAI,CAAC;AAExB,MAAa,UAAW,SAAQ,kBAAS;IACvC,YAAY,UAAkB,EAAE,SAAqB;QACnD,KAAK,CACH,UAAU,EACV,SAAS,EACT,IAAI,EACJ,eAAO,EACP,YAAY,EACZ,UAAU,EACV,+BAAgB,EAChB,OAAO,EACP,aAAa,EACb,WAAW,EACX,KAAK,EACL,YAAY,CACb,CAAC;IACJ,CAAC;CACF;AAjBD,gCAiBC"}
@@ -0,0 +1,29 @@
1
+ import { Address, Log } from '../../../types';
2
+ import { IDexHelper } from '../../../dex-helper';
3
+ import BigNumber from 'bignumber.js';
4
+ import { CurvePool, PoolState } from './curve-pool';
5
+ import { DeepReadonly } from 'ts-essentials';
6
+ export declare const address: Address;
7
+ export declare class WBETHPool extends CurvePool {
8
+ isAdminBalancesSupported: boolean;
9
+ lastTransferredCoin?: Address;
10
+ constructor(parentName: string, dexHelper: IDexHelper, _pool?: string, _address?: string, _tokenAddress?: string, _trackCoins?: boolean, _abi?: any, _N_COINS?: number, _PRECISION_MUL?: BigNumber[], _USE_LENDING?: boolean[], _COINS?: string[], prefix?: string);
11
+ processLog(state: DeepReadonly<PoolState>, log: Readonly<Log>): DeepReadonly<PoolState> | null;
12
+ handleExchangeRateUpdated(event: any, state: PoolState): PoolState;
13
+ handleCoinTransfer(event: any, state: PoolState, log: Log): PoolState;
14
+ handleRemoveLiquidityOne(event: any, state: PoolState, log: Log): PoolState;
15
+ handleNewFee(event: any, state: PoolState, log: Log): PoolState;
16
+ handleRemoveLiquidity(event: any, state: PoolState, log: Log): PoolState;
17
+ handleRemoveLiquidityImbalances(event: any, state: PoolState, log: Log): PoolState;
18
+ handleAddLiquidity(event: any, state: PoolState, log: Log): PoolState;
19
+ exchange(i: number, j: number, dx: BigNumber, state: PoolState): BigNumber;
20
+ private _balances;
21
+ get_dy(i: number, j: number, dx: BigNumber, state: Readonly<PoolState>): BigNumber;
22
+ _get_dy(i: number, j: number, dx: BigNumber, A: BigNumber, fee: BigNumber, balances: BigNumber[], rates: BigNumber[], usefee?: boolean): BigNumber;
23
+ protected _xp_mem(_rates: BigNumber[], _balances: BigNumber[]): BigNumber[];
24
+ _get_y(i: number, j: number, x: BigNumber, xp: BigNumber[], A: BigNumber): BigNumber;
25
+ remove_liquidity_one_coin(token_amount: BigNumber, token_supply: BigNumber, i: number, state: PoolState): BigNumber;
26
+ private _calc_withdraw_one_coin;
27
+ private get_y_D;
28
+ _get_D(xp: BigNumber[], amp: BigNumber): BigNumber;
29
+ }
@@ -0,0 +1,408 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.WBETHPool = exports.address = void 0;
7
+ const utils_1 = require("../../../utils");
8
+ const StableSwapWBETH_json_1 = __importDefault(require("../../../abi/curve-v1/StableSwapWBETH.json"));
9
+ const wBETH_json_1 = __importDefault(require("../../../abi/wBETH.json"));
10
+ const bignumber_constants_1 = require("../../../bignumber-constants");
11
+ const lodash_1 = __importDefault(require("lodash"));
12
+ const curve_pool_1 = require("./curve-pool");
13
+ const abi_1 = require("@ethersproject/abi");
14
+ const wBETHInterface = new abi_1.Interface(wBETH_json_1.default);
15
+ const pool = 'wbETH';
16
+ exports.address = '0xbfab6fa95e0091ed66058ad493189d2cb29385e6'.toLowerCase();
17
+ const tokenAddress = '0xbfab6fa95e0091ed66058ad493189d2cb29385e6';
18
+ const N_COINS = 2;
19
+ const PRECISION_MUL = ['1', '1'].map(utils_1.bigNumberify);
20
+ const A_PRECISION = (0, utils_1.bigNumberify)(100);
21
+ const USE_LENDING = [false, false];
22
+ const ADMIN_FEE = (0, utils_1.bigNumberify)(5000000000);
23
+ const COINS = [
24
+ '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
25
+ '0xa2e3356610840701bdf5611a53974510ae27e2e1',
26
+ ];
27
+ const trackCoins = true;
28
+ class WBETHPool extends curve_pool_1.CurvePool {
29
+ constructor(parentName, dexHelper, _pool = pool, _address = exports.address, _tokenAddress = tokenAddress, _trackCoins = trackCoins, _abi = StableSwapWBETH_json_1.default, _N_COINS = N_COINS, _PRECISION_MUL = PRECISION_MUL, _USE_LENDING = USE_LENDING, _COINS = COINS, prefix = 'WBETHPool') {
30
+ super(parentName, dexHelper, dexHelper.getLogger(prefix), _pool, _address, _tokenAddress, _trackCoins, _abi, _N_COINS, _PRECISION_MUL, _USE_LENDING, _COINS);
31
+ this.isAdminBalancesSupported = true;
32
+ this.decoder = (log) => {
33
+ if (this.trackCoins &&
34
+ lodash_1.default.findIndex(this.COINS, c => c.toLowerCase() === log.address.toLowerCase()) != -1)
35
+ return wBETHInterface.parseLog(log);
36
+ return this.poolIface.parseLog(log);
37
+ };
38
+ // Add pool specific handlers or overloaded handlers
39
+ this.handlers['RemoveLiquidityOne'] =
40
+ this.handleRemoveLiquidityOne.bind(this);
41
+ this.handlers['CommitNewFee'] = this.handleNewFee.bind(this);
42
+ this.handlers['Transfer'] = this.handleCoinTransfer.bind(this);
43
+ this.handlers['ExchangeRateUpdated'] = this.handleExchangeRateUpdated.bind(this);
44
+ }
45
+ processLog(state, log) {
46
+ let _state = {
47
+ A: (0, utils_1.bigNumberify)(state.A),
48
+ fee: (0, utils_1.bigNumberify)(state.fee),
49
+ admin_fee: (0, utils_1.bigNumberify)(state.admin_fee),
50
+ supply: (0, utils_1.bigNumberify)(state.supply),
51
+ balances: state.balances.map(utils_1.bigNumberify),
52
+ eth_balance: (0, utils_1.bigNumberify)(state.eth_balance),
53
+ token_balance: (0, utils_1.bigNumberify)(state.token_balance),
54
+ stored_rates: state.stored_rates.map(utils_1.bigNumberify),
55
+ admin_balances: state.admin_balances.map(utils_1.bigNumberify),
56
+ };
57
+ try {
58
+ const event = this.decoder(log);
59
+ if (event.name in this.handlers) {
60
+ return this.handlers[event.name](event, _state, log);
61
+ }
62
+ return _state;
63
+ }
64
+ catch (e) {
65
+ (0, utils_1.catchParseLogError)(e, this.logger);
66
+ }
67
+ return _state;
68
+ }
69
+ handleExchangeRateUpdated(event, state) {
70
+ this.logger.info(`CurveV1: wbETH pool handle exchange rate updated state before: ${JSON.stringify(state)}`);
71
+ this.logger.info(`CurveV1: wbETH pool handle exchange rate updated event args: ${JSON.stringify(event.args)}`);
72
+ this.logger.info(`CurveV1: wbETH pool handle exchange rate updated newExchangeRate: ${event.args.newExchangeRate}`);
73
+ this.logger.info(`CurveV1: wbETH pool handle exchange rate updated newExchangeRate big number: ${(0, utils_1.bigNumberify)((0, utils_1.stringify)(event.args.newExchangeRate))}`);
74
+ const newExchangeRateUpdated = (0, utils_1.bigNumberify)((0, utils_1.stringify)(event.args.newExchangeRate));
75
+ this.logger.info(`CurveV1: wbETH pool handle exchange rate updated: ${newExchangeRateUpdated}`);
76
+ state.stored_rates[1] = newExchangeRateUpdated;
77
+ this.logger.info(`CurveV1: wbETH pool handle exchange rate updated state after: ${JSON.stringify(state)}`);
78
+ return state;
79
+ }
80
+ handleCoinTransfer(event, state, log) {
81
+ this.logger.info(`CurveV1: wbETH pool handle coin transfer before lastTransferredCoin: ${this.lastTransferredCoin}`);
82
+ const from = event.args.from;
83
+ const coin = log.address;
84
+ this.logger.info(`CurveV1: wbETH pool handle coin transfer coin: ${coin}`);
85
+ if (from.toLowerCase() === this.address.toLowerCase())
86
+ this.lastTransferredCoin = coin.toLowerCase();
87
+ this.logger.info(`CurveV1: wbETH pool handle coin transfer after lastTransferredCoin: ${this.lastTransferredCoin}`);
88
+ return state;
89
+ }
90
+ handleRemoveLiquidityOne(event, state, log) {
91
+ this.logger.info(`CurveV1: wbETH pool handle remove liquidity one before state: ${JSON.stringify(state)}`);
92
+ const _token_amount = (0, utils_1.bigNumberify)((0, utils_1.stringify)(event.args.token_amount));
93
+ const token_supply = (0, utils_1.bigNumberify)((0, utils_1.stringify)(event.args.token_supply));
94
+ const i = lodash_1.default.findIndex(this.COINS, c => c.toLowerCase() === this.lastTransferredCoin?.toLowerCase());
95
+ if (i == -1) {
96
+ this.logger.error(`Error: expected coin to have a transfer event before RemoveLiquidityOne event`);
97
+ return state;
98
+ }
99
+ this.remove_liquidity_one_coin(_token_amount, token_supply, i, state);
100
+ this.logger.info(`CurveV1: wbETH pool handle remove liquidity one after state: ${JSON.stringify(state)}`);
101
+ return state;
102
+ }
103
+ handleNewFee(event, state, log) {
104
+ this.logger.info(`CurveV1: wbETH pool handle new fee before state: ${JSON.stringify(state)}`);
105
+ const fee = (0, utils_1.bigNumberify)((0, utils_1.stringify)(event.args.new_fee));
106
+ state.fee = fee;
107
+ this.logger.info(`CurveV1: wbETH pool handle new fee after state: ${JSON.stringify(state)}`);
108
+ return state;
109
+ }
110
+ handleRemoveLiquidity(event, state, log) {
111
+ this.logger.info(`CurveV1: wbETH pool handle remove liquidity before state: ${JSON.stringify(state)}`);
112
+ const token_supply = (0, utils_1.bigNumberify)((0, utils_1.stringify)(event.args.token_supply));
113
+ const amounts = event.args.token_amounts
114
+ .map(utils_1.stringify)
115
+ .map(utils_1.bigNumberify);
116
+ for (let i = 0; i < this.N_COINS; i++) {
117
+ if (i === 0) {
118
+ state.eth_balance = state.eth_balance?.minus(amounts[i]);
119
+ }
120
+ else {
121
+ state.token_balance = state.token_balance.minus(amounts[i]);
122
+ }
123
+ }
124
+ state.supply = token_supply;
125
+ this.logger.info(`CurveV1: wbETH pool handle remove liquidity after state: ${JSON.stringify(state)}`);
126
+ return state;
127
+ }
128
+ handleRemoveLiquidityImbalances(event, state, log) {
129
+ this.logger.info(`CurveV1: wbETH pool handle remove liquidity imbalances before state: ${JSON.stringify(state)}`);
130
+ const amounts = event.args.token_amounts.map(utils_1.stringify).map(utils_1.bigNumberify);
131
+ const { A, stored_rates: rates, fee, supply: total_supply } = state;
132
+ const amp = A.times(A_PRECISION);
133
+ const old_balances = this._balances(state);
134
+ const D0 = this.get_D_mem(rates, old_balances, amp);
135
+ let new_balances = lodash_1.default.clone(old_balances);
136
+ for (let i = 0; i < this.N_COINS; i++) {
137
+ new_balances[i] = new_balances[i].minus(amounts[i]);
138
+ }
139
+ const D1 = this.get_D_mem(rates, new_balances, amp);
140
+ const fees = new Array(this.N_COINS);
141
+ const base_fee = fee.times(this.N_COINS).idiv(4 * (this.N_COINS - 1));
142
+ for (let i = 0; i < this.N_COINS; i++) {
143
+ const ideal_balance = D1.times(old_balances[i]).idiv(D0);
144
+ let difference = bignumber_constants_1.BN_0;
145
+ const new_balance = new_balances[i];
146
+ if (ideal_balance.gt(new_balance)) {
147
+ difference = ideal_balance.minus(new_balances[i]);
148
+ }
149
+ else {
150
+ difference = new_balances[i].minus(ideal_balance);
151
+ }
152
+ fees[i] = base_fee.times(difference).idiv(this.FEE_DENOMINATOR);
153
+ state.admin_balances[i] = state.admin_balances[i].plus(fees[i].times(ADMIN_FEE).idiv(this.FEE_DENOMINATOR));
154
+ new_balances[i] = new_balances[i].minus(fees[i]);
155
+ }
156
+ new_balances = this._xp_mem(rates, new_balances);
157
+ const D2 = this.get_D(new_balances, amp);
158
+ const burn_amount = D0.minus(D2).times(total_supply).idiv(D0).plus(1);
159
+ state.supply = state.supply.minus(burn_amount);
160
+ if (!amounts[0].eq(bignumber_constants_1.BN_0)) {
161
+ state.eth_balance = state.eth_balance.minus(amounts[0]);
162
+ }
163
+ if (!amounts[1].eq(bignumber_constants_1.BN_0)) {
164
+ state.token_balance = state.token_balance.minus(amounts[1]);
165
+ }
166
+ this.logger.info(`CurveV1: wbETH pool handle remove liquidity imbalances after state: ${JSON.stringify(state)}`);
167
+ return state;
168
+ }
169
+ handleAddLiquidity(event, state, log) {
170
+ this.logger.info(`CurveV1: wbETH pool handle add liquidity before state: ${JSON.stringify(state)}`);
171
+ const amounts = event.args.token_amounts.map(utils_1.stringify).map(utils_1.bigNumberify);
172
+ const fees = event.args.fees.map(utils_1.stringify).map(utils_1.bigNumberify);
173
+ const total_supply = (0, utils_1.bigNumberify)((0, utils_1.stringify)(event.args.token_supply));
174
+ for (let i = 0; i < this.N_COINS; i++) {
175
+ state.admin_balances[i] = state.admin_balances[i].plus(fees[i].times(ADMIN_FEE).idiv(this.FEE_DENOMINATOR));
176
+ }
177
+ if (amounts[0].gt(0)) {
178
+ state.eth_balance = state.eth_balance.plus(amounts[0]);
179
+ }
180
+ if (amounts[1].gt(0)) {
181
+ state.token_balance = state.token_balance.plus(amounts[1]);
182
+ }
183
+ state.supply = total_supply;
184
+ this.logger.info(`CurveV1: wbETH pool handle add liquidity after state: ${JSON.stringify(state)}`);
185
+ return state;
186
+ }
187
+ exchange(i, j, dx, state) {
188
+ this.logger.info(`CurveV1: wbETH pool exchange before state: ${JSON.stringify(state)}`);
189
+ const rates = state.stored_rates;
190
+ let msgValue;
191
+ if (i === 0) {
192
+ msgValue = (0, utils_1.bigNumberify)(dx);
193
+ }
194
+ else {
195
+ msgValue = bignumber_constants_1.BN_0;
196
+ }
197
+ this.logger.info(`CurveV1: wbETH pool exchange msg value: ${msgValue}`);
198
+ const old_balances = this._balances(state, msgValue);
199
+ const xp = this._xp_mem(rates, old_balances);
200
+ const x = xp[i].plus(dx.times(rates[i]).idiv(this.PRECISION));
201
+ const y = this._get_y(i, j, x, xp, state.A);
202
+ let dy = xp[j].minus(y).minus(1);
203
+ const dy_fee = dy.times(state.fee).idiv(this.FEE_DENOMINATOR);
204
+ dy = dy.minus(dy_fee).times(this.PRECISION).idiv(rates[j]);
205
+ xp[i] = x;
206
+ xp[j] = y;
207
+ const dy_admin_fee = dy_fee.times(ADMIN_FEE).idiv(this.FEE_DENOMINATOR).times(this.PRECISION).idiv(rates[j]);
208
+ if (!dy_admin_fee.eq(bignumber_constants_1.BN_0)) {
209
+ state.admin_balances[j] = state.admin_balances[j].plus(dy_admin_fee);
210
+ }
211
+ if (i === 0) {
212
+ state.eth_balance = state.eth_balance.plus(dx);
213
+ state.token_balance = state.token_balance.minus(dy);
214
+ }
215
+ else {
216
+ state.token_balance = state.token_balance.plus(dx);
217
+ state.eth_balance = state.eth_balance?.minus(dy);
218
+ }
219
+ this.logger.info(`CurveV1: wbETH pool exchange after state: ${JSON.stringify(state)}`);
220
+ return dy;
221
+ }
222
+ _balances(state, value = bignumber_constants_1.BN_0) {
223
+ const { admin_balances, eth_balance, token_balance } = state;
224
+ state.balances = [
225
+ eth_balance.minus(admin_balances[0]).minus(value),
226
+ token_balance.minus(admin_balances[1]),
227
+ ];
228
+ return state.balances;
229
+ }
230
+ get_dy(i, j, dx, state) {
231
+ const { stored_rates: rates } = state;
232
+ this.logger.info(`CurveV1: wbETH pool get_dy state: ${JSON.stringify(state)}`);
233
+ return this._get_dy(i, j, dx, (0, utils_1.bigNumberify)(state.A), (0, utils_1.bigNumberify)(state.fee), this._balances(state), rates);
234
+ }
235
+ _get_dy(i, j, dx, A, fee, balances, rates, usefee = true) {
236
+ const xp = this._xp_mem(rates, balances);
237
+ const x = xp[i].plus(dx.times(rates[i]).idiv(this.PRECISION));
238
+ const y = this._get_y(i, j, x, xp, A);
239
+ const dy = xp[j].minus(y).minus(bignumber_constants_1.BN_1);
240
+ const _fee = fee.times(dy).idiv(this.FEE_DENOMINATOR);
241
+ return dy.minus(_fee).times(this.PRECISION).idiv(rates[j]);
242
+ }
243
+ _xp_mem(_rates, _balances) {
244
+ const result = new Array(N_COINS).fill(0n);
245
+ for (const i of lodash_1.default.range(Number(N_COINS))) {
246
+ result[i] = _rates[i].times(_balances[i]).idiv(this.PRECISION);
247
+ }
248
+ return result;
249
+ }
250
+ _get_y(i, j, x, xp, A) {
251
+ let S_ = bignumber_constants_1.BN_0;
252
+ let _x = bignumber_constants_1.BN_0;
253
+ const amp = A.times(A_PRECISION);
254
+ const D = this._get_D(xp, amp);
255
+ let c = (0, utils_1.bigNumberify)(D);
256
+ let y_prev = bignumber_constants_1.BN_0;
257
+ const Ann = amp.times(this.N_COINS);
258
+ for (const _i of lodash_1.default.range(N_COINS)) {
259
+ if (_i === i) {
260
+ _x = (0, utils_1.bigNumberify)(x);
261
+ }
262
+ else if (_i !== j) {
263
+ _x = (0, utils_1.bigNumberify)(xp[_i]);
264
+ }
265
+ else {
266
+ continue;
267
+ }
268
+ S_ = S_.plus(_x);
269
+ c = c.times(D).idiv(_x.times(this.N_COINS)); // c = c * D / (_x * N_COINS)
270
+ }
271
+ // c = c * D * A_PRECISION / (Ann * N_COINS)
272
+ c = c.times(D).times(A_PRECISION).idiv(Ann.times(this.N_COINS));
273
+ // b: uint256 = S_ + D * A_PRECISION / Ann # - D
274
+ const b = S_.plus(D.times(A_PRECISION).idiv(Ann));
275
+ let y = (0, utils_1.bigNumberify)(D);
276
+ for (const _i of lodash_1.default.range(255)) {
277
+ y_prev = (0, utils_1.bigNumberify)(y);
278
+ y = y.times(y).plus(c).idiv(bignumber_constants_1.BN_2.times(y).plus(b).minus(D)); // y = (y*y + c) / (2 * y + b - D)
279
+ // Equality with the precision of 1
280
+ if (y > y_prev) {
281
+ if (y.minus(y_prev).lte(1)) {
282
+ return y;
283
+ }
284
+ }
285
+ else {
286
+ if (y_prev.minus(y).lte(1)) {
287
+ return y;
288
+ }
289
+ }
290
+ }
291
+ throw Error('Error when calculate y');
292
+ }
293
+ remove_liquidity_one_coin(token_amount, token_supply, i, state) {
294
+ let { dy, dy_fee } = this._calc_withdraw_one_coin(token_amount, i, state);
295
+ state.admin_balances[i] = state.admin_balances[i].plus(dy_fee.times(ADMIN_FEE).idiv(this.FEE_DENOMINATOR));
296
+ state.supply = token_supply;
297
+ if (i === 0) {
298
+ state.eth_balance = state.eth_balance.minus(dy);
299
+ }
300
+ else {
301
+ state.token_balance = state.token_balance.minus(dy);
302
+ }
303
+ return dy;
304
+ }
305
+ _calc_withdraw_one_coin(_token_amount, i, state) {
306
+ const { A, fee, supply, stored_rates: rates } = state;
307
+ const amp = A.times(A_PRECISION);
308
+ const xp = this._xp_mem(rates, this._balances(state));
309
+ const D0 = this._get_D(xp, amp);
310
+ const total_supply = (0, utils_1.bigNumberify)(supply);
311
+ const D1 = D0.minus(_token_amount.idiv(total_supply));
312
+ const new_y = this.get_y_D(amp, i, xp, D1);
313
+ const base_fee = fee.times(this.N_COINS).idiv(4 * (this.N_COINS - 1));
314
+ let xp_reduced = new Array(N_COINS).fill(0n);
315
+ for (const j of lodash_1.default.range(N_COINS)) {
316
+ let dx_expected = bignumber_constants_1.BN_0;
317
+ const xp_j = (0, utils_1.bigNumberify)(xp[j]);
318
+ if (j === i) {
319
+ dx_expected = xp_j.times(D1).idiv(D0).minus(new_y);
320
+ }
321
+ else {
322
+ dx_expected = xp_j.minus(xp_j.times(D1).idiv(D0));
323
+ }
324
+ xp_reduced[j] = xp_j.minus(base_fee.times(dx_expected).idiv(this.FEE_DENOMINATOR));
325
+ }
326
+ let dy = xp_reduced[i].minus(this.get_y_D(amp, i, xp_reduced, D1));
327
+ const dy_0 = xp[i].minus(new_y).times(this.PRECISION).idiv(rates[i]);
328
+ dy = dy.minus(1).times(this.PRECISION).idiv(rates[i]);
329
+ return { dy, dy_fee: dy_0.minus(dy) };
330
+ }
331
+ get_y_D(A_, i, xp, D) {
332
+ let c = D;
333
+ let S_ = bignumber_constants_1.BN_0;
334
+ const Ann = A_.times(this.N_COINS);
335
+ let _x = bignumber_constants_1.BN_0;
336
+ for (let _i = 0; _i < this.N_COINS; _i++) {
337
+ if (_i !== i) {
338
+ _x = xp[_i];
339
+ }
340
+ else {
341
+ continue;
342
+ }
343
+ S_ = S_.plus(_x);
344
+ c = c.times(D).idiv(_x.times(this.N_COINS));
345
+ }
346
+ c = c.times(D).idiv(Ann.times(this.N_COINS));
347
+ const b = S_.plus(D.idiv(Ann));
348
+ let y_prev = bignumber_constants_1.BN_0;
349
+ let y = D;
350
+ for (let _i = 0; _i < 255; _i++) {
351
+ y_prev = y;
352
+ y = y.times(y).plus(c).idiv(y.times(2).plus(b).minus(D));
353
+ // Equality with the precision of 1
354
+ if (y.gt(y_prev)) {
355
+ if (y.minus(y_prev).lte(1)) {
356
+ break;
357
+ }
358
+ }
359
+ else {
360
+ if (y_prev.minus(y).lte(1)) {
361
+ break;
362
+ }
363
+ }
364
+ }
365
+ return y;
366
+ }
367
+ _get_D(xp, amp) {
368
+ let S = bignumber_constants_1.BN_0;
369
+ for (const _x of xp) {
370
+ S = S.plus(_x);
371
+ }
372
+ if (S.eq(bignumber_constants_1.BN_0)) {
373
+ return bignumber_constants_1.BN_0;
374
+ }
375
+ let D = (0, utils_1.bigNumberify)(S);
376
+ const Ann = amp.times(this.N_COINS);
377
+ for (const _i of lodash_1.default.range(255)) {
378
+ let D_P = D.times(D)
379
+ .idiv(xp[0])
380
+ .times(D)
381
+ .idiv(xp[1])
382
+ .idiv(this.N_COINS ** 2); // D_P: uint256 = D * D / _xp[0] * D / _xp[1] / (N_COINS)**2
383
+ let Dprev = (0, utils_1.bigNumberify)(D);
384
+ const dividend = Ann.times(S)
385
+ .idiv(A_PRECISION)
386
+ .plus(D_P.times(this.N_COINS))
387
+ .times(D);
388
+ const divisor = Ann.minus(A_PRECISION)
389
+ .times(D)
390
+ .idiv(A_PRECISION)
391
+ .plus(D_P.times(this.N_COINS + 1));
392
+ D = dividend.idiv(divisor); // D = (Ann * S / A_PRECISION + D_P * N_COINS) * D / ((Ann - A_PRECISION) * D / A_PRECISION + (N_COINS + 1) * D_P)
393
+ if (D.gt(Dprev)) {
394
+ if (D.minus(Dprev).lte(1)) {
395
+ return D;
396
+ }
397
+ }
398
+ else {
399
+ if (Dprev.minus(D).lte(1)) {
400
+ return D;
401
+ }
402
+ }
403
+ }
404
+ throw Error('Error when calcucate D');
405
+ }
406
+ }
407
+ exports.WBETHPool = WBETHPool;
408
+ //# sourceMappingURL=wbETHpool.js.map