impermax-sdk 1.1.80 → 1.1.82

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.
@@ -98,11 +98,11 @@ class OnchainAccountBorrowable extends onchainAccountPoolToken_1.default {
98
98
  break;
99
99
  borrowAmountPrev = borrowAmount;
100
100
  const changeCollateral = 0;
101
- const changeBorrowedA = this.getPoolTokenType() == types_1.PoolTokenType.BorrowableA ? borrowAmount * this.getUiMargin() : 0;
102
- const changeBorrowedB = this.getPoolTokenType() == types_1.PoolTokenType.BorrowableB ? borrowAmount * this.getUiMargin() : 0;
101
+ const changeBorrowedA = this.getPoolTokenType() == types_1.PoolTokenType.BorrowableA ? borrowAmount : 0;
102
+ const changeBorrowedB = this.getPoolTokenType() == types_1.PoolTokenType.BorrowableB ? borrowAmount : 0;
103
103
  const changes = { changeCollateral, changeBorrowedA, changeBorrowedB };
104
104
  const [liqPriceA, liqPriceB] = yield this.lendingPool.getLiquidationPrices(changes);
105
- if (liqPriceA > twapPrice || liqPriceB < twapPrice)
105
+ if (liqPriceA * this.getUiMargin() > twapPrice || liqPriceB / this.getUiMargin() < twapPrice)
106
106
  borrowAmount -= jump;
107
107
  else {
108
108
  bestAmount = borrowAmount;
@@ -79,24 +79,21 @@ class OnchainAccountCollateral extends onchainAccountPoolToken_1.default {
79
79
  const collateralAmount = yield this.getDeposited();
80
80
  const twapPrice = yield this.lendingPool.getLendingPool().getTWAPPrice();
81
81
  const [liqPriceA, liqPriceB] = yield this.lendingPool.getLiquidationPrices(onchainTypes_1.NO_CHANGES);
82
- console.log("collateralAmount", collateralAmount);
83
- console.log("liquidationpricenochange", liqPriceA, twapPrice, liqPriceB);
84
82
  // start search
85
83
  let withdrawAmount = collateralAmount;
86
84
  let withdrawAmountPrev = 0;
87
85
  let bestAmount = 0;
88
86
  for (let i = 0; i < 1000; i++) {
89
87
  const jump = Math.abs(withdrawAmount - withdrawAmountPrev) / 2;
90
- if (jump / collateralAmount < 0.001)
88
+ if (jump / collateralAmount < 0.000001)
91
89
  break;
92
90
  withdrawAmountPrev = withdrawAmount;
93
- const changeCollateral = -1 * Math.min(withdrawAmount * this.getUiMargin(), collateralAmount);
91
+ const changeCollateral = -1 * Math.min(withdrawAmount, collateralAmount);
94
92
  const changeBorrowedA = 0;
95
93
  const changeBorrowedB = 0;
96
94
  const changes = { changeCollateral, changeBorrowedA, changeBorrowedB };
97
95
  const [liqPriceA, liqPriceB] = yield this.lendingPool.getLiquidationPrices(changes);
98
- console.log("getMaxWithdrawable", changes, liqPriceA, twapPrice, liqPriceB, bestAmount, withdrawAmount);
99
- if (liqPriceA > twapPrice || liqPriceB < twapPrice)
96
+ if (liqPriceA * this.getUiMargin() > twapPrice || liqPriceB / this.getUiMargin() < twapPrice)
100
97
  withdrawAmount -= jump;
101
98
  else {
102
99
  bestAmount = withdrawAmount;
@@ -274,7 +274,7 @@ class OnchainAccountLendingPool {
274
274
  }
275
275
  const changes = { changeCollateral, changeBorrowedA, changeBorrowedB };
276
276
  const [liqPriceA, liqPriceB] = yield this.getLiquidationPrices(changes);
277
- if (liqPriceA > twapPrice || liqPriceB < twapPrice)
277
+ if (liqPriceA * this.getUiMargin() > twapPrice || liqPriceB / this.getUiMargin() < twapPrice)
278
278
  addLev -= jump;
279
279
  else {
280
280
  bestChanges = changes;
@@ -293,6 +293,7 @@ class OnchainAccountLendingPool {
293
293
  const adjCollateralValue = valueCollateral / Math.sqrt(slippage);
294
294
  const collateralValueA = valueCollateral * reservesWeight0;
295
295
  const collateralValueB = valueCollateral * reservesWeight1;
296
+ console.log("getMaxDeleverage", valueCollateral, valueA, valueB, reservesWeight0, reservesWeight1, adjCollateralValue, collateralValueA, collateralValueB);
296
297
  if (collateralValueA >= valueA && collateralValueB >= valueB) {
297
298
  return this.getCollateral().getDeposited();
298
299
  }
@@ -16,6 +16,8 @@ exports.uniswapV2_getPriceFromReserves = uniswapV2_getPriceFromReserves;
16
16
  * - 1 meaning it is liquidatable
17
17
  */
18
18
  function uniswapV2_getLiquidatableXPriceDecrease(x0, y0, debtX, debtY) {
19
+ if ((x0 == 0 || y0 == 0) && (debtX > 0 || debtY > 0))
20
+ return 1;
19
21
  const k = x0 * y0;
20
22
  const x = k / debtY * (1 + Math.sqrt(1 - debtY * debtX / k));
21
23
  if (!x)
@@ -74,6 +76,8 @@ function solidlyStable_yOut(xIn, x0, y0) {
74
76
  }
75
77
  exports.solidlyStable_yOut = solidlyStable_yOut;
76
78
  function solidlyStable_getEquityValue(x, y, debtX, debtY) {
79
+ if ((x == 0 || y == 0) && (debtX > 0 || debtY > 0))
80
+ return -Infinity;
77
81
  const price = solidlyStable_getPriceFromReserves(x, y);
78
82
  const lpValue = x * price + y;
79
83
  const debtValue = debtX * price + debtY;
@@ -84,6 +88,10 @@ exports.solidlyStable_getEquityValue = solidlyStable_getEquityValue;
84
88
  * Find xIn input amount that would bring the equity to negative
85
89
  */
86
90
  function solidlyStable_getXInMargin(x0, y0, debtX, debtY) {
91
+ if (debtX == 0 && debtY == 0)
92
+ return Infinity;
93
+ if (x0 == 0 || y0 == 0)
94
+ return 0;
87
95
  if (solidlyStable_getEquityValue(x0, y0, debtX, debtY) < 0)
88
96
  return 0;
89
97
  const STARTING_POINT = x0 * 1e9;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "impermax-sdk",
3
- "version": "1.1.80",
3
+ "version": "1.1.82",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",