impermax-sdk 2.1.550 → 2.1.552

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.
@@ -48,6 +48,7 @@ export default class OffchainBorrowable extends OffchainPoolToken {
48
48
  getMaxFlashAllocate(): Promise<{
49
49
  vault: OffchainLendingVault;
50
50
  amount: number;
51
+ availableToWithdraw: number;
51
52
  } | null>;
52
53
  getAvailableToBorrow(): Promise<number>;
53
54
  getAvailableToBorrowUSD(): Promise<number>;
@@ -291,18 +291,22 @@ class OffchainBorrowable extends offchainPoolToken_1.default {
291
291
  const vaults = await this.getOffchain().getVaultsLinkedToBorrowable(this);
292
292
  let bestAmount = 0;
293
293
  let bestVault = null;
294
+ let availableToWithdraw = 0;
295
+ const poolTokenAddress = await this.getPoolTokenAddress();
294
296
  for (const vault of vaults) {
295
- const amount = await vault.getMaxFlashAllocate(await this.getPoolTokenAddress());
297
+ const amount = await vault.getMaxFlashAllocate(poolTokenAddress);
296
298
  if (amount > bestAmount) {
297
299
  bestVault = vault;
298
300
  bestAmount = amount;
301
+ availableToWithdraw = await vault.getAvailableToWithdraw(poolTokenAddress);
299
302
  }
300
303
  }
301
304
  if (bestVault === null)
302
305
  return null;
303
306
  return {
304
307
  vault: bestVault,
305
- amount: bestAmount
308
+ amount: bestAmount,
309
+ availableToWithdraw
306
310
  };
307
311
  }
308
312
  async getAvailableToBorrow() {
@@ -16,6 +16,9 @@ export default class OffchainLendingVault extends OffchainVault {
16
16
  maxFlashAllocate?: {
17
17
  [key in Address]: Promise<number>;
18
18
  };
19
+ availableToWithdraw?: {
20
+ [key in Address]: Promise<number>;
21
+ };
19
22
  };
20
23
  protected vaultBorrowables: Array<VaultBorrowable> | null;
21
24
  constructor(offchain: Offchain, vaultAddress: Address);
@@ -32,4 +35,6 @@ export default class OffchainLendingVault extends OffchainVault {
32
35
  getBorrowableId(borrowableAddress: Address): Promise<number | null>;
33
36
  private _getMaxFlashAllocate;
34
37
  getMaxFlashAllocate(borrowableAddress: Address): Promise<number>;
38
+ private _getAvailableToWithdraw;
39
+ getAvailableToWithdraw(borrowableAddress: Address): Promise<number>;
35
40
  }
@@ -154,5 +154,18 @@ class OffchainLendingVault extends offchainVault_1.default {
154
154
  this.cache.maxFlashAllocate[borrowableAddress] = this._getMaxFlashAllocate(borrowableAddress);
155
155
  return this.cache.maxFlashAllocate[borrowableAddress];
156
156
  }
157
+ async _getAvailableToWithdraw(borrowableAddress) {
158
+ const id = await this.getBorrowableId(borrowableAddress);
159
+ if (id === null)
160
+ return 0;
161
+ return (await this.getLendingVaultObject()).positions[id].getInitialAvailableToWithdraw();
162
+ }
163
+ async getAvailableToWithdraw(borrowableAddress) {
164
+ if (!this.cache.availableToWithdraw)
165
+ this.cache.availableToWithdraw = {};
166
+ if (!this.cache.availableToWithdraw[borrowableAddress])
167
+ this.cache.availableToWithdraw[borrowableAddress] = this._getAvailableToWithdraw(borrowableAddress);
168
+ return this.cache.availableToWithdraw[borrowableAddress];
169
+ }
157
170
  }
158
171
  exports.default = OffchainLendingVault;
@@ -73,14 +73,14 @@ class OnchainInteractionsNftlpGenericCL extends onchainInteractionsNftlp_1.defau
73
73
  const maxFlashAllocateA = await offchainBorrowableA.getMaxFlashAllocate();
74
74
  if (maxFlashAllocateA) {
75
75
  if (borrowADelta > await offchainBorrowableA.getTotalBalance()) {
76
- actions.push(await actionsGetter.methods.getFlashAllocateAction("0", maxFlashAllocateA.vault.getVaultAddress(), await borrowableA.toBigNumber(maxFlashAllocateA.amount)).call());
76
+ actions.push(await actionsGetter.methods.getFlashAllocateAction("0", maxFlashAllocateA.vault.getVaultAddress(), await borrowableA.toBigNumber(maxFlashAllocateA.amount + maxFlashAllocateA.availableToWithdraw)).call());
77
77
  }
78
78
  }
79
79
  const offchainBorrowableB = await borrowableB.getOffchainBorrowable();
80
80
  const maxFlashAllocateB = await offchainBorrowableB.getMaxFlashAllocate();
81
81
  if (maxFlashAllocateB) {
82
82
  if (borrowBDelta > await offchainBorrowableB.getTotalBalance()) {
83
- actions.push(await actionsGetter.methods.getFlashAllocateAction("1", maxFlashAllocateB.vault.getVaultAddress(), await borrowableB.toBigNumber(maxFlashAllocateB.amount)).call());
83
+ actions.push(await actionsGetter.methods.getFlashAllocateAction("1", maxFlashAllocateB.vault.getVaultAddress(), await borrowableB.toBigNumber(maxFlashAllocateB.amount + maxFlashAllocateB.availableToWithdraw)).call());
84
84
  }
85
85
  }
86
86
  // Borrow and mint
@@ -31,6 +31,8 @@ export declare class BorrowableEntity {
31
31
  getAvailableToBorrow(): number;
32
32
  getInitialAvailableToWithdraw(): number;
33
33
  getAvailableToWithdraw(): number;
34
+ calculateBorrowRateForUtilization(utilizationRate: number): number;
35
+ calculateSupplyRateForUtilization(utilizationRate: number): number;
34
36
  getBorrowRate(): number;
35
37
  getSupplyRate(): number;
36
38
  getBorrowAPR(): number;
@@ -49,8 +49,7 @@ class BorrowableEntity {
49
49
  getAvailableToWithdraw() {
50
50
  return Math.max(this.totalSupply - this.totalBorrows, 0);
51
51
  }
52
- getBorrowRate() {
53
- const utilizationRate = this.getUtilizationRate();
52
+ calculateBorrowRateForUtilization(utilizationRate) {
54
53
  if (utilizationRate <= this.kinkUtilizationRate) {
55
54
  return this.kinkBorrowRate * utilizationRate / this.kinkUtilizationRate;
56
55
  }
@@ -59,8 +58,15 @@ class BorrowableEntity {
59
58
  return this.kinkBorrowRate * ((this.kinkMultiplier - 1) * overUtil + 1);
60
59
  }
61
60
  }
61
+ calculateSupplyRateForUtilization(utilizationRate) {
62
+ const borrowRate = this.calculateBorrowRateForUtilization(utilizationRate);
63
+ return borrowRate * utilizationRate * (1 - this.reserveFactor);
64
+ }
65
+ getBorrowRate() {
66
+ return this.calculateBorrowRateForUtilization(this.getUtilizationRate());
67
+ }
62
68
  getSupplyRate() {
63
- return this.getBorrowRate() * this.getUtilizationRate() * (1 - this.reserveFactor);
69
+ return this.calculateSupplyRateForUtilization(this.getUtilizationRate());
64
70
  }
65
71
  getBorrowAPR() {
66
72
  return (0, index_1.toAPR)(this.getBorrowRate());
@@ -132,10 +132,26 @@ class LendingVaultEntity {
132
132
  }
133
133
  }
134
134
  getMaxFlashAllocate(borrowable) {
135
- // TODO dynamically calculate this
136
- const amountToExclude = this.positions[borrowable].getInitialAvailableToWithdraw();
137
- return (this.getAvailableLiquidity() - amountToExclude) * 0.99;
138
- // TODO questa è l'aggiunta, ma devo callare sommando anche initial amount... mmmmh
135
+ let maxFlashAllocate = 0;
136
+ const maxRate = this.positions[borrowable].borrowable.calculateSupplyRateForUtilization(1);
137
+ for (let i = 0; i < this.positions.length; i++) {
138
+ if (i === borrowable)
139
+ continue;
140
+ const b = this.positions[i].borrowable;
141
+ const maxWithdrawable = this.positions[i].getInitialAvailableToWithdraw();
142
+ const targetUtilizationRate = b.calculateUtilizationForRate(maxRate);
143
+ if (targetUtilizationRate > 1) {
144
+ maxFlashAllocate += maxWithdrawable;
145
+ continue;
146
+ }
147
+ const targetSupply = b.initialTotalBorrows / targetUtilizationRate;
148
+ // notice: supply delta CAN be negative
149
+ // in this case we have to consider that the vault will have to deposit
150
+ // liquidity in this pool instead of withdrawing it
151
+ const supplyDelta = b.totalSupply - targetSupply;
152
+ maxFlashAllocate += Math.min(maxWithdrawable, supplyDelta);
153
+ }
154
+ return Math.max(maxFlashAllocate * 0.99, 0);
139
155
  }
140
156
  }
141
157
  exports.LendingVaultEntity = LendingVaultEntity;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "impermax-sdk",
3
- "version": "2.1.550",
3
+ "version": "2.1.552",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",