impermax-sdk 2.1.81 → 2.1.83

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.
@@ -19,6 +19,7 @@ export default class OnchainAccountNftlpUniswapV3 extends OnchainAccountNftlp {
19
19
  [keys in number]: Promise<UniswapV3Position>;
20
20
  };
21
21
  };
22
+ getNftlp: () => any;
22
23
  protected initializePositionData(tokenId: number): Promise<PositionData>;
23
24
  protected getPositionData(tokenId: number): Promise<PositionData>;
24
25
  getFee(tokenId: number): Promise<number>;
@@ -28,7 +29,7 @@ export default class OnchainAccountNftlpUniswapV3 extends OnchainAccountNftlp {
28
29
  getPriceB(tokenId: number): Promise<number>;
29
30
  getLiquidity(tokenId: number): Promise<number>;
30
31
  createPositionObject(tokenId: number, lockStateChange?: boolean): Promise<UniswapV3Position>;
31
- createNewPositionObject(fee: number, tickLower: number, tickUpper: number, lockStateChange?: boolean): Promise<UniswapV3Position>;
32
+ createNewPositionObject(fee: number, priceA: number, priceB: number, lockStateChange?: boolean): Promise<UniswapV3Position>;
32
33
  getPositionObject(tokenId: number): Promise<UniswapV3Position>;
33
34
  }
34
35
  export {};
@@ -18,6 +18,7 @@ class OnchainAccountNftlpUniswapV3 extends onchainAccountNftlp_1.default {
18
18
  constructor() {
19
19
  super(...arguments);
20
20
  this.cache = {};
21
+ this.getNftlp = () => this.getLendingPool().getLendingPool().getNftlp();
21
22
  }
22
23
  initializePositionData(tokenId) {
23
24
  return __awaiter(this, void 0, void 0, function* () {
@@ -51,12 +52,12 @@ class OnchainAccountNftlpUniswapV3 extends onchainAccountNftlp_1.default {
51
52
  }
52
53
  getPriceA(tokenId) {
53
54
  return __awaiter(this, void 0, void 0, function* () {
54
- return Math.pow(1.0001, yield this.getTickLower(tokenId));
55
+ return this.getNftlp().tickToPrice(yield this.getTickLower(tokenId));
55
56
  });
56
57
  }
57
58
  getPriceB(tokenId) {
58
59
  return __awaiter(this, void 0, void 0, function* () {
59
- return Math.pow(1.0001, yield this.getTickUpper(tokenId));
60
+ return this.getNftlp().tickToPrice(yield this.getTickUpper(tokenId));
60
61
  });
61
62
  }
62
63
  getLiquidity(tokenId) {
@@ -69,9 +70,9 @@ class OnchainAccountNftlpUniswapV3 extends onchainAccountNftlp_1.default {
69
70
  return new uniswapV3Position_1.default(yield this.getLiquidity(tokenId), yield this.getLendingPool().getBorrowableA().getBorrowed(tokenId), yield this.getLendingPool().getBorrowableB().getBorrowed(tokenId), yield this.getNftlp().getMarketPrice(), yield this.getNftlp().getOraclePrice(), yield this.getPriceA(tokenId), yield this.getPriceB(tokenId), yield this.getLendingPool().getSafetyMargin(), yield this.getLendingPool().getLiquidationPenalty(), lockStateChange);
70
71
  });
71
72
  }
72
- createNewPositionObject(fee, tickLower, tickUpper, lockStateChange = false) {
73
+ createNewPositionObject(fee, priceA, priceB, lockStateChange = false) {
73
74
  return __awaiter(this, void 0, void 0, function* () {
74
- return new uniswapV3Position_1.default(0, 0, 0, yield this.getNftlp().getMarketPrice(), yield this.getNftlp().getOraclePrice(), Math.pow(1.0001, tickLower), Math.pow(1.0001, tickUpper), yield this.getLendingPool().getSafetyMargin(), yield this.getLendingPool().getLiquidationPenalty(), lockStateChange);
75
+ return new uniswapV3Position_1.default(0, 0, 0, yield this.getNftlp().getMarketPrice(), yield this.getNftlp().getOraclePrice(), priceA, priceB, yield this.getLendingPool().getSafetyMargin(), yield this.getLendingPool().getLiquidationPenalty(), lockStateChange);
75
76
  });
76
77
  }
77
78
  getPositionObject(tokenId) {
@@ -20,4 +20,6 @@ export default class OnchainNftlpUniswapV3 extends OnchainNftlp {
20
20
  getUniswapV3Pool(fee: number): Promise<any>;
21
21
  protected initializeMarketPrice(): Promise<number>;
22
22
  protected initializeOraclePrice(): Promise<number>;
23
+ tickToPrice(tick: number): Promise<number>;
24
+ priceToTick(price: number): Promise<number>;
23
25
  }
@@ -78,5 +78,22 @@ class OnchainNftlpUniswapV3 extends onchainNftlp_1.default {
78
78
  return Math.pow((nftlp.methods.oraclePriceSqrtX96().call() / Math.pow(2, 96)), 2);
79
79
  });
80
80
  }
81
+ tickToPrice(tick) {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ const decimalA = yield this.getLendingPool().getBorrowableA().getDecimals();
84
+ const decimalB = yield this.getLendingPool().getBorrowableB().getDecimals();
85
+ const netDecimal = decimalA - decimalB;
86
+ return Math.pow(1.0001, tick) * Math.pow(10, netDecimal);
87
+ });
88
+ }
89
+ priceToTick(price) {
90
+ return __awaiter(this, void 0, void 0, function* () {
91
+ const decimalA = yield this.getLendingPool().getBorrowableA().getDecimals();
92
+ const decimalB = yield this.getLendingPool().getBorrowableB().getDecimals();
93
+ const netDecimal = decimalA - decimalB;
94
+ // TODO round to tick precision
95
+ return Math.log(price / Math.pow(10, netDecimal)) / Math.log(1.0001);
96
+ });
97
+ }
81
98
  }
82
99
  exports.default = OnchainNftlpUniswapV3;
@@ -38,7 +38,9 @@ class OnchainInteractionsNftlpUniswapV3 extends onchainInteractionsNftlp_1.defau
38
38
  borrowBDelta < 0)
39
39
  throw new Error("Unexpected negative inputs");
40
40
  const actionsGetter = this.getActionsGetter();
41
- const result = yield this._getUpdatePositionActions(-1, yield this.getAccountNftlp().createNewPositionObject(fee, tickLower, tickUpper), depositADelta, depositBDelta, borrowADelta, borrowBDelta);
41
+ const priceA = yield this.getNftlp().tickToPrice(tickLower);
42
+ const priceB = yield this.getNftlp().tickToPrice(tickUpper);
43
+ const result = yield this._getUpdatePositionActions(-1, yield this.getAccountNftlp().createNewPositionObject(fee, priceA, priceB), depositADelta, depositBDelta, borrowADelta, borrowBDelta);
42
44
  // TODO check fee and ticks encoding
43
45
  result.actions.unshift(yield actionsGetter.methods.getMintUniV3EmptyAction(fee.toString(), tickLower.toString(), tickUpper.toString()).call());
44
46
  return result;
@@ -58,14 +58,23 @@ class UniswapV3Position {
58
58
  // amountX and amountY are expected to have the same sign
59
59
  getOptimalLiquidityAtMarketPrice(amountX, amountY) {
60
60
  let liquidity;
61
+ console.log("initial", amountX, amountY);
61
62
  if (amountX != 0 && amountY != 0) {
62
63
  const sampleX = this.getRealX(this.marketPrice);
63
64
  const sampleY = this.getRealY(this.marketPrice);
64
- if (Math.abs(amountX) / sampleX > Math.abs(amountY) / sampleY)
65
- amountX = amountY / sampleY * sampleX;
66
- else
67
- amountY = amountX / sampleX * sampleY;
65
+ console.log("sample", sampleX, sampleY);
66
+ if (sampleX == 0)
67
+ amountX = 0;
68
+ else if (sampleX == 0)
69
+ amountY = 0;
70
+ else {
71
+ if (Math.abs(amountX) / sampleX > Math.abs(amountY) / sampleY)
72
+ amountX = amountY / sampleY * sampleX;
73
+ else
74
+ amountY = amountX / sampleX * sampleY;
75
+ }
68
76
  }
77
+ console.log("final", amountX, amountY);
69
78
  liquidity = this.getLiquidity(this.marketPrice, amountX, amountY);
70
79
  return { liquidity, amountX, amountY };
71
80
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "impermax-sdk",
3
- "version": "2.1.81",
3
+ "version": "2.1.83",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",