impermax-sdk 2.1.545 → 2.1.546

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.
@@ -3,6 +3,7 @@ import { Address, Borrowable } from "../../config/types";
3
3
  import OffchainPoolToken from "../offchainPoolToken";
4
4
  import OffchainLendingPool from "./offchainLendingPool";
5
5
  import { BorrowableEntity } from "../../utils/borrowable/borrowableEntity";
6
+ import { OffchainLendingVault } from "../vault";
6
7
  export default class OffchainBorrowable extends OffchainPoolToken {
7
8
  protected readonly lendingPool: OffchainLendingPool;
8
9
  protected readonly borrowable: Borrowable;
@@ -44,4 +45,10 @@ export default class OffchainBorrowable extends OffchainPoolToken {
44
45
  getTokenPriceAccurate(): Promise<number>;
45
46
  private getAccrueFactor;
46
47
  getCurrentExchangeRate(): Promise<number>;
48
+ getMaxFlashAllocate(): Promise<{
49
+ vault: OffchainLendingVault;
50
+ amount: number;
51
+ } | null>;
52
+ getAvailableToBorrow(): Promise<number>;
53
+ getAvailableToBorrowUSD(): Promise<number>;
47
54
  }
@@ -285,5 +285,35 @@ class OffchainBorrowable extends offchainPoolToken_1.default {
285
285
  const accrueFactor = await this.getAccrueFactor();
286
286
  return lastExchangeRate * accrueFactor;
287
287
  }
288
+ async getMaxFlashAllocate() {
289
+ const vaults = await this.getOffchain().getVaultsLinkedToBorrowable(this);
290
+ let bestAmount = 0;
291
+ let bestVault = null;
292
+ for (const vault of vaults) {
293
+ const amount = await vault.getMaxFlashAllocate(await this.getPoolTokenAddress());
294
+ if (amount > bestAmount) {
295
+ bestVault = vault;
296
+ bestAmount = amount;
297
+ }
298
+ }
299
+ if (!bestVault)
300
+ return null;
301
+ return {
302
+ vault: bestVault,
303
+ amount: bestAmount
304
+ };
305
+ }
306
+ async getAvailableToBorrow() {
307
+ const totalBalance = await this.getTotalBalance();
308
+ const maxFlashAllocate = await this.getMaxFlashAllocate();
309
+ if (maxFlashAllocate)
310
+ return totalBalance + maxFlashAllocate.amount;
311
+ return totalBalance;
312
+ }
313
+ async getAvailableToBorrowUSD() {
314
+ const availableToBorrow = await this.getAvailableToBorrow();
315
+ const tokenPrice = await this.getTokenPriceFast();
316
+ return availableToBorrow * tokenPrice;
317
+ }
288
318
  }
289
319
  exports.default = OffchainBorrowable;
@@ -1,20 +1,21 @@
1
1
  import * as initializer from './initializer';
2
- import OffchainLendingPool from './lendingPool';
2
+ import OffchainLendingPool, { OffchainBorrowable } from './lendingPool';
3
3
  import OffchainSolidexHelper from './offchainSolidexHelper';
4
4
  import OffchainAccount from './account';
5
- import { Address, AddressIndex, Factory, FactoryIndex, LendingPoolIndex, Networks, WhitelistState, Extension } from '../config/types';
6
- import { LendingPoolData, VaultData, VaultPosition, TvlData, UserData, XIbexData, NftlpData, PoolChart, NewlyEarnedFees, NftlpStats, XIbexUserData, NewlyEarnedReward, NetworkTokenData } from './offchainTypes';
5
+ import { Address, AddressIndex, Extension, Factory, FactoryIndex, LendingPoolIndex, Networks, WhitelistState } from '../config/types';
6
+ import { LendingPoolData, NetworkTokenData, NewlyEarnedFees, NewlyEarnedReward, NftlpData, NftlpStats, PoolChart, TvlData, UserData, VaultData, VaultPosition, XIbexData, XIbexUserData } from './offchainTypes';
7
7
  import OffchainVault from './vault/offchainVault';
8
8
  import OffchainConfigManager from './configManager';
9
9
  import OffchainConfigManagerV3 from './configManager/v3';
10
10
  import { PairState } from '../config/whitelist';
11
11
  import OffchainStakingModule from './staking/offchainStakingModule';
12
+ import OffchainMultichain from "./offchainMultichain";
13
+ import { OffchainLendingVault } from "./vault";
12
14
  export interface OffchainCfg {
13
15
  network: Networks;
14
16
  chainId: number;
15
17
  whitelistedPairs?: FactoryIndex<Address[]>;
16
18
  }
17
- import OffchainMultichain from "./offchainMultichain";
18
19
  export default class Offchain {
19
20
  readonly offchainMultichain: OffchainMultichain;
20
21
  readonly network: Networks;
@@ -44,6 +45,9 @@ export default class Offchain {
44
45
  protected vaultsUsersData: {
45
46
  [key in Address]?: Promise<AddressIndex<AddressIndex<VaultPosition>>>;
46
47
  };
48
+ protected vaultsLinkToBorrowable: {
49
+ [key in Address]: Promise<Array<OffchainLendingVault>>;
50
+ };
47
51
  protected borrowersList: Promise<OffchainAccount[]> | null;
48
52
  protected stakingModule: OffchainStakingModule;
49
53
  protected xibexUsersData: {
@@ -125,4 +129,7 @@ export default class Offchain {
125
129
  protected initializeNftlpsData: typeof initializer.initializeNftlpsData;
126
130
  getPairsList(factory: Factory, state?: PairState): Address[];
127
131
  getNetworkTokens(): Promise<NetworkTokenData[]>;
132
+ getVaultList(): Promise<OffchainVault[]>;
133
+ private _getVaultsLinkedToBorrowable;
134
+ getVaultsLinkedToBorrowable(borrowable: OffchainBorrowable): Promise<OffchainLendingVault[]>;
128
135
  }
@@ -136,6 +136,7 @@ class Offchain {
136
136
  this.usersData = {};
137
137
  this.vaultsUsersData = {};
138
138
  this.xibexUsersData = {};
139
+ this.vaultsLinkToBorrowable = {};
139
140
  // Staking module is only available on Arbitrum atm,
140
141
  // so we initialize it once in the offchainMultichain.
141
142
  // this.stakingModule = new OffchainStakingModule(this);
@@ -159,6 +160,7 @@ class Offchain {
159
160
  this.userNewlyEarnedFeesData = {};
160
161
  this.userNewlyEarnedRewardData = {};
161
162
  this.xibexUsersData = {};
163
+ this.vaultsLinkToBorrowable = {};
162
164
  this.stakingModule?.cleanCache();
163
165
  for (const address in this.accounts) {
164
166
  await this.accounts[address].cleanCache();
@@ -272,5 +274,29 @@ class Offchain {
272
274
  return [];
273
275
  }
274
276
  }
277
+ async getVaultList() {
278
+ return this.offchainMultichain.getVaultList({ networks: [this.network] });
279
+ }
280
+ async _getVaultsLinkedToBorrowable(borrowable) {
281
+ const result = [];
282
+ const underlying = await borrowable.getUnderlyingAddress();
283
+ const vaults = await this.getVaultList();
284
+ for (const vault of vaults) {
285
+ if (vault.getVaultType() !== types_1.VaultType.LENDING)
286
+ continue;
287
+ if (await vault.getUnderlyingAddress() === underlying) {
288
+ const id = await vault.getBorrowableId(await borrowable.getPoolTokenAddress());
289
+ if (id !== null)
290
+ result.push(vault);
291
+ }
292
+ }
293
+ return result;
294
+ }
295
+ async getVaultsLinkedToBorrowable(borrowable) {
296
+ const borrowableAddress = await borrowable.getPoolTokenAddress();
297
+ if (!this.vaultsLinkToBorrowable[borrowableAddress])
298
+ this.vaultsLinkToBorrowable[borrowableAddress] = this._getVaultsLinkedToBorrowable(borrowable);
299
+ return this.vaultsLinkToBorrowable[borrowableAddress];
300
+ }
275
301
  }
276
302
  exports.default = Offchain;
@@ -12,6 +12,7 @@ export default abstract class OffchainPoolToken {
12
12
  getSymbol(): Promise<string>;
13
13
  getDecimals(): Promise<number>;
14
14
  getExchangeRate(): Promise<number>;
15
+ getPoolTokenAddress(): Promise<Address>;
15
16
  getUnderlyingAddress(): Promise<Address>;
16
17
  getTotalBalance(): Promise<number>;
17
18
  getTotalBalanceUSD(): Promise<number>;
@@ -63,6 +63,10 @@ class OffchainPoolToken {
63
63
  return this.getPoolTokenParamFloat("exchangeRate");
64
64
  }
65
65
  // Underlying Address
66
+ async getPoolTokenAddress() {
67
+ return this.getPoolTokenParam("id");
68
+ }
69
+ // Underlying Address
66
70
  async getUnderlyingAddress() {
67
71
  return this.getUnderlyingParam("id");
68
72
  }
@@ -13,6 +13,9 @@ export declare enum RiskLevel {
13
13
  export default class OffchainLendingVault extends OffchainVault {
14
14
  protected cache: {
15
15
  lendingVaultObject?: Promise<LendingVaultEntity>;
16
+ maxFlashAllocate?: {
17
+ [key in Address]: Promise<number>;
18
+ };
16
19
  };
17
20
  protected vaultBorrowables: Array<VaultBorrowable> | null;
18
21
  constructor(offchain: Offchain, vaultAddress: Address);
@@ -26,4 +29,7 @@ export default class OffchainLendingVault extends OffchainVault {
26
29
  getAccountBorrowables(): Promise<Array<OffchainAccountBorrowable>>;
27
30
  getBorrowables(): Promise<Array<VaultBorrowable>>;
28
31
  getLendingVaultBorrowables(): Promise<Array<VaultBorrowable>>;
32
+ getBorrowableId(borrowableAddress: Address): Promise<number | null>;
33
+ private _getMaxFlashAllocate;
34
+ getMaxFlashAllocate(borrowableAddress: Address): Promise<number>;
29
35
  }
@@ -131,5 +131,28 @@ class OffchainLendingVault extends offchainVault_1.default {
131
131
  this.vaultBorrowables = await this.getBorrowables();
132
132
  return this.vaultBorrowables;
133
133
  }
134
+ async getBorrowableId(borrowableAddress) {
135
+ const borrowables = await this.getLendingVaultBorrowables();
136
+ for (let i = 0; i < borrowables.length; i++) {
137
+ if (borrowables[i].address.toLowerCase() === borrowableAddress.toLowerCase()) {
138
+ return i;
139
+ }
140
+ }
141
+ return null;
142
+ }
143
+ async _getMaxFlashAllocate(borrowableAddress) {
144
+ const id = await this.getBorrowableId(borrowableAddress);
145
+ // TODO return 0 if disabled
146
+ if (id === null)
147
+ return 0;
148
+ return (await this.getLendingVaultObject()).getMaxFlashAllocate(id);
149
+ }
150
+ async getMaxFlashAllocate(borrowableAddress) {
151
+ if (!this.cache.maxFlashAllocate)
152
+ this.cache.maxFlashAllocate = {};
153
+ if (!this.cache.maxFlashAllocate[borrowableAddress])
154
+ this.cache.maxFlashAllocate[borrowableAddress] = this._getMaxFlashAllocate(borrowableAddress);
155
+ return this.cache.maxFlashAllocate[borrowableAddress];
156
+ }
134
157
  }
135
158
  exports.default = OffchainLendingVault;
@@ -27,8 +27,6 @@ class OnchainInteractionsNftlp {
27
27
  * Utilities
28
28
  */
29
29
  encodeActions(actions = []) {
30
- console.log("actions");
31
- console.log(actions);
32
30
  return ethers_1.ethers.utils.defaultAbiCoder.encode(['tuple(uint256 actionType, bytes actionData, bytes nextAction)[]'], [actions]);
33
31
  }
34
32
  }
@@ -70,12 +70,24 @@ class OnchainInteractionsNftlpGenericCL extends onchainInteractionsNftlp_1.defau
70
70
  const isEthB = await borrowableB.isETH();
71
71
  // Vault flash allocate
72
72
  // TODO understand when to flashallocate, and how much
73
- const ethVault = this.getNftlp().getLendingPool().getImpermaxFactory().getOnchain().getLendingVault("0x0988cc53b8ddd625c20e382f1af2f9c385e4f9a3");
73
+ // if we reallocate always reallocate all the available liquidity, in order to make sure we have enough
74
+ /*const ethVault = this.getNftlp().getLendingPool().getImpermaxFactory().getOnchain().getLendingVault("0x0988cc53b8ddd625c20e382f1af2f9c385e4f9a3")
74
75
  const availableLiquidity0 = await ethVault.getAvailableLiquidity() * 0.9;
75
- const usdcVault = this.getNftlp().getLendingPool().getImpermaxFactory().getOnchain().getLendingVault("0xf7408ba0aaf8ca80d4442731415bbe2156da8958");
76
+
77
+ const usdcVault = this.getNftlp().getLendingPool().getImpermaxFactory().getOnchain().getLendingVault("0xf7408ba0aaf8ca80d4442731415bbe2156da8958")
76
78
  const availableLiquidity1 = await usdcVault.getAvailableLiquidity() * 0.9;
77
- actions.push(await actionsGetter.methods.getFlashAllocateAction("0", "0x0988cc53b8ddd625c20e382f1af2f9c385e4f9a3", await borrowableA.toBigNumber(availableLiquidity0)).call());
78
- actions.push(await actionsGetter.methods.getFlashAllocateAction("1", "0xf7408ba0aaf8ca80d4442731415bbe2156da8958", await borrowableB.toBigNumber(availableLiquidity1)).call());
79
+
80
+ actions.push(await actionsGetter.methods.getFlashAllocateAction(
81
+ "0",
82
+ "0x0988cc53b8ddd625c20e382f1af2f9c385e4f9a3",
83
+ await borrowableA.toBigNumber(availableLiquidity0)
84
+ ).call())
85
+
86
+ actions.push(await actionsGetter.methods.getFlashAllocateAction(
87
+ "1",
88
+ "0xf7408ba0aaf8ca80d4442731415bbe2156da8958",
89
+ await borrowableB.toBigNumber(availableLiquidity1)
90
+ ).call())*/
79
91
  // Borrow and mint
80
92
  if ((borrowADelta > 0 && depositADelta > 0) || (borrowBDelta > 0 && depositBDelta > 0)) {
81
93
  // slightly increase borrowDelta in order to avoid amountUser to be dust
@@ -37,10 +37,10 @@ class BorrowableEntity {
37
37
  // TODO implement debt ceiling
38
38
  // TODO implement flash allocate
39
39
  getInitialAvailableToBorrow() {
40
- return this.initialTotalSupply - this.initialTotalBorrows + 1e9;
40
+ return this.initialTotalSupply - this.initialTotalBorrows;
41
41
  }
42
42
  getAvailableToBorrow() {
43
- return this.totalSupply - this.totalBorrows + 1e9;
43
+ return this.totalSupply - this.totalBorrows;
44
44
  }
45
45
  getInitialAvailableToWithdraw() {
46
46
  return this.initialTotalSupply - this.initialTotalBorrows;
@@ -30,4 +30,5 @@ export declare class LendingVaultEntity {
30
30
  supply(amount: number, relative?: boolean): void;
31
31
  withdraw(amount: number, relative?: boolean): void;
32
32
  reallocate(withdrawAmount?: number): void;
33
+ getMaxFlashAllocate(borrowable: number): number;
33
34
  }
@@ -17,7 +17,6 @@ class LendingVaultEntity {
17
17
  else
18
18
  this.state++;
19
19
  }
20
- // TODO recreate reallocate algorithm
21
20
  /**
22
21
  * PUBLIC GETTERS
23
22
  */
@@ -132,5 +131,11 @@ class LendingVaultEntity {
132
131
  this.totalBalance -= amount;
133
132
  }
134
133
  }
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
139
+ }
135
140
  }
136
141
  exports.LendingVaultEntity = LendingVaultEntity;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "impermax-sdk",
3
- "version": "2.1.545",
3
+ "version": "2.1.546",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",