impermax-sdk 2.1.550 → 2.1.551

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.
@@ -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());
@@ -133,9 +133,29 @@ class LendingVaultEntity {
133
133
  }
134
134
  getMaxFlashAllocate(borrowable) {
135
135
  // TODO dynamically calculate this
136
- const amountToExclude = this.positions[borrowable].getInitialAvailableToWithdraw();
137
- return (this.getAvailableLiquidity() - amountToExclude) * 0.99;
136
+ //let amountToExclude = this.positions[borrowable].getInitialAvailableToWithdraw();
137
+ //return (this.getAvailableLiquidity() - amountToExclude) * 0.99;
138
138
  // TODO questa è l'aggiunta, ma devo callare sommando anche initial amount... mmmmh
139
+ let maxFlashAllocate = 0;
140
+ const maxRate = this.positions[borrowable].borrowable.calculateSupplyRateForUtilization(1);
141
+ for (let i = 0; i < this.positions.length; i++) {
142
+ if (i === borrowable)
143
+ continue;
144
+ const b = this.positions[i].borrowable;
145
+ const maxWithdrawable = this.positions[i].getInitialAvailableToWithdraw();
146
+ const targetUtilizationRate = b.calculateUtilizationForRate(maxRate);
147
+ if (targetUtilizationRate > 1) {
148
+ maxFlashAllocate += maxWithdrawable;
149
+ continue;
150
+ }
151
+ const targetSupply = b.initialTotalBorrows / targetUtilizationRate;
152
+ // notice: supply delta CAN be negative
153
+ // in this case we have to consider that the vault will have to deposit
154
+ // liquidity in this pool instead of withdrawing it
155
+ const supplyDelta = b.totalSupply - targetSupply;
156
+ maxFlashAllocate += Math.min(maxWithdrawable, supplyDelta);
157
+ }
158
+ return Math.max(maxFlashAllocate * 0.99, 0);
139
159
  }
140
160
  }
141
161
  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.551",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",