@kamino-finance/klend-sdk 7.2.2 → 7.2.4

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.
@@ -155,6 +155,7 @@ class KaminoVaultClient {
155
155
  adminTokenAccount,
156
156
  };
157
157
  const initVaultIx = (0, instructions_1.initVault)(initVaultAccounts, undefined, this._kaminoVaultProgramId);
158
+ const createVaultFarm = await this.createVaultFarm(vaultConfig.admin, vaultState.address, sharesMint);
158
159
  // create and set up the vault lookup table
159
160
  const [createLUTIx, lut] = await (0, lookupTable_1.initLookupTableIx)(vaultConfig.admin, slot);
160
161
  const accountsToBeInserted = [
@@ -169,6 +170,8 @@ class KaminoVaultClient {
169
170
  token_1.TOKEN_PROGRAM_ADDRESS,
170
171
  this._kaminoLendProgramId,
171
172
  sysvars_1.SYSVAR_INSTRUCTIONS_ADDRESS,
173
+ createVaultFarm.farm.address,
174
+ farm_utils_1.FARMS_GLOBAL_CONFIG_MAINNET,
172
175
  ];
173
176
  const insertIntoLUTIxs = await (0, lookupTable_1.insertIntoLookupTableIxs)(this.getConnection(), vaultConfig.admin, lut, accountsToBeInserted, []);
174
177
  const setLUTIx = this.updateUninitialisedVaultConfigIx(vaultConfig.admin, vaultState.address, new types_1.VaultConfigField.LookupTable(), lut.toString());
@@ -185,6 +188,7 @@ class KaminoVaultClient {
185
188
  const setNameIx = this.updateUninitialisedVaultConfigIx(vaultConfig.admin, vaultState.address, new types_1.VaultConfigField.Name(), vaultConfig.name);
186
189
  ixs.push(setNameIx);
187
190
  }
191
+ const setFarmIx = this.updateUninitialisedVaultConfigIx(vaultConfig.admin, vaultState.address, new types_1.VaultConfigField.Farm(), createVaultFarm.farm.address);
188
192
  const metadataIx = await this.getSetSharesMetadataIx(this.getConnection(), vaultConfig.admin, vaultState.address, sharesMint, baseVaultAuthority, vaultConfig.vaultTokenSymbol, vaultConfig.vaultTokenName);
189
193
  return {
190
194
  vault: vaultState,
@@ -196,9 +200,30 @@ class KaminoVaultClient {
196
200
  populateLUTIxs: insertIntoLUTIxs,
197
201
  cleanupIxs,
198
202
  initSharesMetadataIx: metadataIx,
203
+ createVaultFarm,
204
+ setFarmToVaultIx: setFarmIx,
199
205
  },
200
206
  };
201
207
  }
208
+ /**
209
+ * This method creates a farm for a vault
210
+ * @param signer - the signer of the transaction
211
+ * @param vaultSharesMint - the mint of the vault shares
212
+ * @param vaultAddress - the address of the vault (it doesn't need to be already initialized)
213
+ * @returns a struct with the farm, the setup farm ixs and the update farm ixs
214
+ */
215
+ async createVaultFarm(signer, vaultAddress, vaultSharesMint) {
216
+ const farmsSDK = new farms_sdk_1.Farms(this._rpc);
217
+ const farm = await (0, kit_1.generateKeyPairSigner)();
218
+ const ixs = await farmsSDK.createFarmIxs(signer, farm, farm_utils_1.FARMS_GLOBAL_CONFIG_MAINNET, vaultSharesMint);
219
+ const updatePendingFarmAdminIx = await farmsSDK.updateFarmConfigIx(signer, farm.address, lib_1.DEFAULT_PUBLIC_KEY, new dist_1.FarmConfigOption.UpdatePendingFarmAdmin(), farm_utils_1.FARMS_ADMIN_MAINNET, undefined, undefined, true);
220
+ const updateFarmVaultIdIx = await farmsSDK.updateFarmConfigIx(signer, farm.address, lib_1.DEFAULT_PUBLIC_KEY, new dist_1.FarmConfigOption.UpdateVaultId(), vaultAddress, undefined, undefined, true);
221
+ return {
222
+ farm,
223
+ setupFarmIxs: ixs,
224
+ updateFarmIxs: [updatePendingFarmAdminIx, updateFarmVaultIdIx],
225
+ };
226
+ }
202
227
  /**
203
228
  * This method creates an instruction to set the shares metadata for a vault
204
229
  * @param rpc
@@ -412,9 +437,11 @@ class KaminoVaultClient {
412
437
  * @param mode the field to update (based on VaultConfigFieldKind enum)
413
438
  * @param value the value to update the field with
414
439
  * @param [vaultAdminAuthority] the signer of the transaction. Optional. If not provided the admin of the vault will be used. It should be used when changing the admin of the vault if we want to build or batch multiple ixs in the same tx
440
+ * @param [lutIxsSigner] the signer of the transaction to be used for the lookup table instructions. Optional. If not provided the admin of the vault will be used. It should be used when changing the admin of the vault if we want to build or batch multiple ixs in the same tx
441
+ * @param [skipLutUpdate] if true, the lookup table instructions will not be included in the returned instructions
415
442
  * @returns a struct that contains the instruction to update the field and an optional list of instructions to update the lookup table
416
443
  */
417
- async updateVaultConfigIxs(vault, mode, value, vaultAdminAuthority) {
444
+ async updateVaultConfigIxs(vault, mode, value, vaultAdminAuthority, lutIxsSigner, skipLutUpdate = false) {
418
445
  const vaultState = await vault.getState();
419
446
  const admin = parseVaultAdmin(vaultState, vaultAdminAuthority);
420
447
  const updateVaultConfigAccs = {
@@ -449,22 +476,25 @@ class KaminoVaultClient {
449
476
  let updateVaultConfigIx = (0, instructions_1.updateVaultConfig)(updateVaultConfigArgs, updateVaultConfigAccs, undefined, this._kaminoVaultProgramId);
450
477
  updateVaultConfigIx = this.appendRemainingAccountsForVaultReserves(updateVaultConfigIx, vaultReserves, vaultReservesState);
451
478
  const updateLUTIxs = [];
452
- if (mode.kind === new types_1.VaultConfigField.PendingVaultAdmin().kind) {
453
- const newPubkey = (0, kit_1.address)(value);
454
- const insertIntoLutIxs = await (0, lookupTable_1.insertIntoLookupTableIxs)(this.getConnection(), admin, vaultState.vaultLookupTable, [newPubkey]);
455
- updateLUTIxs.push(...insertIntoLutIxs);
456
- }
457
- else if (mode.kind === new types_1.VaultConfigField.Farm().kind) {
458
- const keysToAddToLUT = [(0, kit_1.address)(value)];
459
- // if the farm already exist we want to read its state to add it to the LUT
460
- try {
461
- const farmState = await dist_1.FarmState.fetch(this.getConnection(), keysToAddToLUT[0]);
462
- keysToAddToLUT.push(farmState.farmVault, farmState.farmVaultsAuthority, farmState.token.mint, farmState.scopePrices, farmState.globalConfig);
463
- const insertIntoLutIxs = await (0, lookupTable_1.insertIntoLookupTableIxs)(this.getConnection(), admin, vaultState.vaultLookupTable, keysToAddToLUT);
479
+ if (!skipLutUpdate) {
480
+ const lutIxsSignerAccount = lutIxsSigner ? lutIxsSigner : admin;
481
+ if (mode.kind === new types_1.VaultConfigField.PendingVaultAdmin().kind) {
482
+ const newPubkey = (0, kit_1.address)(value);
483
+ const insertIntoLutIxs = await (0, lookupTable_1.insertIntoLookupTableIxs)(this.getConnection(), lutIxsSignerAccount, vaultState.vaultLookupTable, [newPubkey]);
464
484
  updateLUTIxs.push(...insertIntoLutIxs);
465
485
  }
466
- catch (error) {
467
- console.log(`Error fetching farm ${keysToAddToLUT[0].toString()} state`, error);
486
+ else if (mode.kind === new types_1.VaultConfigField.Farm().kind) {
487
+ const keysToAddToLUT = [(0, kit_1.address)(value)];
488
+ // if the farm already exist we want to read its state to add it to the LUT
489
+ try {
490
+ const farmState = await dist_1.FarmState.fetch(this.getConnection(), keysToAddToLUT[0]);
491
+ keysToAddToLUT.push(farmState.farmVault, farmState.farmVaultsAuthority, farmState.token.mint, farmState.scopePrices, farmState.globalConfig);
492
+ const insertIntoLutIxs = await (0, lookupTable_1.insertIntoLookupTableIxs)(this.getConnection(), lutIxsSignerAccount, vaultState.vaultLookupTable, keysToAddToLUT);
493
+ updateLUTIxs.push(...insertIntoLutIxs);
494
+ }
495
+ catch (error) {
496
+ console.log(`Error fetching farm ${keysToAddToLUT[0].toString()} state`, error);
497
+ }
468
498
  }
469
499
  }
470
500
  const updateVaultConfigIxs = {
@@ -478,14 +508,16 @@ class KaminoVaultClient {
478
508
  * @param farm - the farm where the vault shares can be staked
479
509
  * @param [errorOnOverride] - if true, the function will throw an error if the vault already has a farm. If false, it will override the farm
480
510
  * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
481
- *
511
+ * @param [lutIxsSigner] - the signer of the transaction to be used for the lookup table instructions. Optional. If not provided the admin of the vault will be used. It should be used when changing the admin of the vault if we want to build or batch multiple ixs in the same tx
512
+ * @param [skipLutUpdate] - if true, the lookup table instructions will not be included in the returned instructions
513
+ * @returns - a struct that contains the instruction to update the farm and an optional list of instructions to update the lookup table
482
514
  */
483
- async setVaultFarmIxs(vault, farm, errorOnOverride = true, vaultAdminAuthority) {
515
+ async setVaultFarmIxs(vault, farm, errorOnOverride = true, vaultAdminAuthority, lutIxsSigner, skipLutUpdate = false) {
484
516
  const vaultHasFarm = await vault.hasFarm();
485
517
  if (vaultHasFarm && errorOnOverride) {
486
518
  throw new Error('Vault already has a farm, if you want to override it set errorOnOverride to false');
487
519
  }
488
- return this.updateVaultConfigIxs(vault, new types_1.VaultConfigField.Farm(), farm, vaultAdminAuthority);
520
+ return this.updateVaultConfigIxs(vault, new types_1.VaultConfigField.Farm(), farm, vaultAdminAuthority, lutIxsSigner, skipLutUpdate);
489
521
  }
490
522
  /**
491
523
  * This method updates the vault config for a vault that
@@ -1242,10 +1274,10 @@ class KaminoVaultClient {
1242
1274
  }
1243
1275
  const initialVaultAllocations = this.getVaultAllocations(vaultState);
1244
1276
  // 2. Compute the allocation
1245
- return this.computeReservesAllocation(holdings.totalAUMIncludingFees.sub(holdings.pendingFees), new decimal_js_1.default(vaultState.unallocatedWeight.toString()), new decimal_js_1.default(vaultState.unallocatedTokensCap.toString()), initialVaultAllocations);
1277
+ return this.computeReservesAllocation(holdings.totalAUMIncludingFees.sub(holdings.pendingFees), new decimal_js_1.default(vaultState.unallocatedWeight.toString()), new decimal_js_1.default(vaultState.unallocatedTokensCap.toString()), initialVaultAllocations, vaultState.tokenMintDecimals.toNumber());
1246
1278
  }
1247
- computeReservesAllocation(vaultAUM, vaultUnallocatedWeight, vaultUnallocatedCap, initialVaultAllocations) {
1248
- return (0, vaultAllocation_1.computeReservesAllocation)(vaultAUM, vaultUnallocatedWeight, vaultUnallocatedCap, initialVaultAllocations);
1279
+ computeReservesAllocation(vaultAUM, vaultUnallocatedWeight, vaultUnallocatedCap, initialVaultAllocations, vaultTokenDecimals) {
1280
+ return (0, vaultAllocation_1.computeReservesAllocation)(vaultAUM, vaultUnallocatedWeight, vaultUnallocatedCap, initialVaultAllocations, vaultTokenDecimals);
1249
1281
  }
1250
1282
  /**
1251
1283
  * This method returns the user shares balance for a given vault
@@ -1314,9 +1346,7 @@ class KaminoVaultClient {
1314
1346
  if (await vault.hasFarm()) {
1315
1347
  const userFarmState = allUserFarmStatesMap.get(state.vaultFarm);
1316
1348
  if (userFarmState) {
1317
- console.log('there is a farm state for vault', vault.address);
1318
1349
  const stakedShares = (0, farm_utils_1.getSharesInFarmUserPosition)(userFarmState, state.sharesMintDecimals.toNumber());
1319
- console.log('staked shares', stakedShares);
1320
1350
  const userSharesBalance = vaultUserShareBalance.get(vault.address);
1321
1351
  if (userSharesBalance) {
1322
1352
  userSharesBalance.stakedShares = stakedShares;