import BN from 'bn.js';
import {
  Account,
  AccountRole,
  Address,
  address,
  Base58EncodedBytes,
  fetchEncodedAccount,
  generateKeyPairSigner,
  getAddressEncoder,
  getBase58Decoder,
  GetProgramAccountsDatasizeFilter,
  GetProgramAccountsMemcmpFilter,
  getProgramDerivedAddress,
  AccountMeta,
  Instruction,
  lamports,
  ProgramDerivedAddress,
  Rpc,
  Slot,
  SolanaRpcApi,
  TransactionSigner,
  AccountInfoWithPubkey,
  AccountInfoBase,
  AccountInfoWithJsonData,
  Option,
  some,
  none,
} from '@solana/kit';
import {
  AllOracleAccounts,
  CdnResources,
  CdnResourcesResponse,
  DEFAULT_PUBLIC_KEY,
  DEFAULT_RECENT_SLOT_DURATION_MS,
  getAssociatedTokenAddress,
  getTokenBalanceFromAccountInfoLamports,
  getTokenOracleData,
  getTransferWsolIxs,
  KaminoMarket,
  KaminoReserve,
  KVaultGlobalConfig,
  lamportsToDecimal,
  Reserve,
  WRAPPED_SOL_MINT,
} from '../lib';
import {
  addUpdateWhitelistedReserve,
  AddUpdateWhitelistedReserveAccounts,
  AddUpdateWhitelistedReserveArgs,
  buy,
  BuyAccounts,
  BuyArgs,
  deposit,
  DepositAccounts,
  DepositArgs,
  giveUpPendingFees,
  GiveUpPendingFeesAccounts,
  GiveUpPendingFeesArgs,
  initKVaultGlobalConfig,
  initVault,
  InitVaultAccounts,
  invest,
  InvestAccounts,
  removeAllocation,
  RemoveAllocationAccounts,
  sell,
  SellAccounts,
  SellArgs,
  updateAdmin,
  UpdateAdminAccounts,
  updateKVaultGlobalConfig,
  UpdateKVaultGlobalConfigAccounts,
  UpdateKVaultGlobalConfigArgs,
  updateReserveAllocation,
  UpdateReserveAllocationAccounts,
  UpdateReserveAllocationArgs,
  updateVaultConfig,
  UpdateVaultConfigAccounts,
  UpdateVaultConfigArgs,
  withdraw,
  WithdrawAccounts,
  WithdrawArgs,
  withdrawFromAvailable,
  WithdrawFromAvailableAccounts,
  WithdrawFromAvailableArgs,
  withdrawPendingFees,
  WithdrawPendingFeesAccounts,
} from '../@codegen/kvault/instructions';
import {
  UpdateGlobalConfigMode,
  UpdateKVaultGlobalConfigModeKind,
  UpdateReserveWhitelistModeKind,
  VaultConfigField,
  VaultConfigFieldKind,
} from '../@codegen/kvault/types';
import { ReserveWhitelistEntry, VaultState } from '../@codegen/kvault/accounts';
import Decimal from 'decimal.js';
import { bpsToPct, decodeVaultName, numberToLamportsDecimal, parseTokenSymbol, pubkeyHashMapToJson } from './utils';
import { PROGRAM_ID } from '../@codegen/klend/programId';
import { ReserveWithAddress } from './reserve';
import { Fraction } from './fraction';
import {
  CDN_ENDPOINT,
  createAtasIdempotent,
  createWsolAtaIfMissing,
  getAllStandardTokenProgramTokenAccounts,
  getKVaultSharesMetadataPda,
  getTokenAccountAmount,
  getTokenAccountMint,
  lendingMarketAuthPda,
  parseBooleanFlag,
  programDataPda,
  SECONDS_PER_YEAR,
  U64_MAX,
  VAULT_INITIAL_DEPOSIT,
} from '../utils';
import { getAccountOwner, getProgramAccounts } from '../utils';
import {
  AcceptVaultOwnershipIxs,
  AllDepositAccounts,
  AllWithdrawAccounts,
  APYs,
  CreateVaultFarm,
  DepositIxs,
  DisinvestAllReservesIxs,
  InitVaultIxs,
  ReserveAllocationOverview,
  SyncVaultLUTIxs,
  UpdateReserveAllocationIxs,
  UpdateVaultConfigIxs,
  UserSharesForVault,
  VaultComputedAllocation,
  VaultReleaseCheckResult,
  WithdrawAndBlockReserveIxs,
  WithdrawIxs,
} from './vault_types';
import { batchFetch, collToLamportsDecimal, ZERO } from '@kamino-finance/kliquidity-sdk';
import { FullBPSDecimal } from '@kamino-finance/kliquidity-sdk/dist/utils/CreationParameters';
import {
  FarmConfigOption,
  FarmIncentives,
  FarmState,
  getFarmIncentivesWithExistentState,
  getUserStatePDA,
  scaleDownWads,
} from '@kamino-finance/farms-sdk/dist';
import { getAccountsInLut, initLookupTableIx, insertIntoLookupTableIxs } from '../utils';
import {
  FARMS_ADMIN_MAINNET,
  FARMS_GLOBAL_CONFIG_DEVNET,
  FARMS_GLOBAL_CONFIG_MAINNET,
  getFarmStakeIxs,
  getFarmUnstakeAndWithdrawIxs,
  getSharesInFarmUserPosition,
  getUserPendingRewardsInFarm,
  getUserSharesInTokensStakedInFarm,
} from './farm_utils';
import { getCreateAccountInstruction, SYSTEM_PROGRAM_ADDRESS } from '@solana-program/system';
import { getInitializeKVaultSharesMetadataIx, getUpdateSharesMetadataIx, resolveMetadata } from '../utils/metadata';
import { decodeReserveWhitelistEntry, decodeVaultState } from '../utils/vault';
import { fetchMaybeToken, findAssociatedTokenPda, getCloseAccountInstruction } from '@solana-program/token-2022';
import { TOKEN_PROGRAM_ADDRESS } from '@solana-program/token';
import { SYSVAR_INSTRUCTIONS_ADDRESS, SYSVAR_RENT_ADDRESS } from '@solana/sysvars';
import { noopSigner } from '../utils/signer';
import { Farms, UserState } from '@kamino-finance/farms-sdk';
import { computeReservesAllocation } from '../utils/vaultAllocation';
import { getReserveFarmRewardsAPY } from '../utils/farmUtils';
import { fetchKaminoCdnData } from '../utils/readCdnData';
import { walletIsSquadsMultisig } from '../utils/multisig';
import { RiskManagerInfo } from '../models/cdn';
import {
  updateGlobalConfigAdmin,
  UpdateGlobalConfigAdminAccounts,
} from '../@codegen/kvault/instructions/updateGlobalConfigAdmin';

export const kaminoVaultId = address('KvauGMspG5k6rtzrqqn7WNn3oZdyKqLKwK2XWQ8FLjd');
export const kaminoVaultStagingId = address('stKvQfwRsQiKnLtMNVLHKS3exFJmZFsgfzBPWHECUYK');

const TOKEN_VAULT_SEED = 'token_vault';
const CTOKEN_VAULT_SEED = 'ctoken_vault';
const BASE_VAULT_AUTHORITY_SEED = 'authority';
const SHARES_SEED = 'shares';
const EVENT_AUTHORITY_SEED = '__event_authority';
export const METADATA_SEED = 'metadata';
const GLOBAL_CONFIG_STATE_SEED = 'global_config';
const WHITELISTED_RESERVES_SEED = 'whitelisted_reserves';

export const METADATA_PROGRAM_ID: Address = address('metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s');

export const INITIAL_DEPOSIT_LAMPORTS = 1000;

export const DEFAULT_CU_PER_TX = 1_400_000;

const addressEncoder = getAddressEncoder();
const base58Decoder = getBase58Decoder();

/**
 * KaminoVaultClient is a class that provides a high-level interface to interact with the Kamino Vault program.
 */
export class KaminoVaultClient {
  private readonly _rpc: Rpc<SolanaRpcApi>;
  private readonly _kaminoVaultProgramId: Address;
  private readonly _kaminoLendProgramId: Address;
  private readonly _farmsProgramId?: Address;
  recentSlotDurationMs: number;

  // CDN cache
  private _cdnResources?: CdnResources;
  private _cdnResourcesPromise?: Promise<CdnResources | undefined>;

  constructor(
    rpc: Rpc<SolanaRpcApi>,
    recentSlotDurationMs: number,
    kaminoVaultprogramId?: Address,
    kaminoLendProgramId?: Address,
    cdnResources?: CdnResources,
    farmsProgramId?: Address
  ) {
    this._rpc = rpc;
    this.recentSlotDurationMs = recentSlotDurationMs;
    this._kaminoVaultProgramId = kaminoVaultprogramId ? kaminoVaultprogramId : kaminoVaultId;
    this._kaminoLendProgramId = kaminoLendProgramId ? kaminoLendProgramId : PROGRAM_ID;
    this._farmsProgramId = farmsProgramId;
    this._cdnResources = cdnResources;
  }

  getConnection() {
    return this._rpc;
  }

  getProgramID() {
    return this._kaminoVaultProgramId;
  }

  getRpc() {
    return this._rpc;
  }

  hasFarm() {
    return;
  }

  private async loadCdnResourcesOnce(): Promise<CdnResources | undefined> {
    if (this._cdnResources) {
      return this._cdnResources;
    }
    if (this._cdnResourcesPromise) {
      return this._cdnResourcesPromise;
    }

    this._cdnResourcesPromise = (async () => {
      const response = await fetch(`${CDN_ENDPOINT}/resources.json`);
      if (!response.ok) {
        console.error(`Failed to fetch CDN resources: ${response.status} ${response.statusText}`);
        return undefined;
      }

      const raw = (await response.json()) as CdnResourcesResponse;
      const delegatedVaultFarms = raw['mainnet-beta']?.delegatedVaultFarms;
      if (!delegatedVaultFarms) {
        return undefined;
      }

      const riskManagers = raw['mainnet-beta']?.riskManagers ?? {};
      const parsed: CdnResources = { delegatedVaultFarms, riskManagers };
      this._cdnResources = parsed;
      return parsed;
    })();

    return this._cdnResourcesPromise;
  }

  /**
   * Check if a vault has all the needed criteria to be released
   * - owner is multisig
   * - vaultFarm is set and it is a farm that is valid
   * - FLC farm is set and it is a farm that is valid (warning if not)
   * - check shares token metadata is set
   * - Check min deposit is not 0
   * - Check the vault has at least one allocation
   * - Check there are allocations with weight > 0 and cap > 0 (and give warning for each allocation which doesn't have cap == u64::MAX)
   * - Check CDN (using loadCdnResourcesOnce) that the vaultAdmin exists in the list of admins and has a description
   * @param vault - the vault to check
   * @returns - a promise that resolves to the release status of the vault
   */
  async checkVaultReleaseStatus(vault: KaminoVault): Promise<VaultReleaseCheckResult> {
    const result: VaultReleaseCheckResult = {
      errors: [],
      warnings: [],
      success: true,
    };

    const vaultState = await vault.getState();

    // 1. Check owner is multisig
    try {
      const isMultisig = await walletIsSquadsMultisig(vaultState.vaultAdminAuthority);
      if (!isMultisig) {
        result.errors.push(`Vault admin ${vaultState.vaultAdminAuthority} is not a Squads multisig`);
      }
    } catch (e) {
      result.errors.push(`Failed to check if vault admin ${vaultState.vaultAdminAuthority} is a multisig: ${e}`);
    }

    // 2. Check vaultFarm is set and valid
    if (vaultState.vaultFarm === DEFAULT_PUBLIC_KEY) {
      result.errors.push('Vault farm is not set');
    } else {
      const farmState = await FarmState.fetch(this._rpc, vaultState.vaultFarm);
      if (!farmState) {
        result.errors.push(`Vault farm ${vaultState.vaultFarm} could not be fetched (invalid or does not exist)`);
      }
    }

    // 3. Check FLC farm is set and valid (warning if not)
    if (vaultState.firstLossCapitalFarm === DEFAULT_PUBLIC_KEY) {
      result.warnings.push('First loss capital farm is not set');
    } else {
      const flcFarmState = await FarmState.fetch(this._rpc, vaultState.firstLossCapitalFarm);
      if (!flcFarmState) {
        result.warnings.push(
          `First loss capital farm ${vaultState.firstLossCapitalFarm} could not be fetched (invalid or does not exist)`
        );
      } else {
        if (!(await this.isFlcFarmValid(flcFarmState, vaultState))) {
          result.warnings.push(`First loss capital farm ${vaultState.firstLossCapitalFarm} is not valid`);
        }
      }
    }

    // 4. Check shares token metadata is set
    const [sharesMintMetadata] = await getKVaultSharesMetadataPda(vaultState.sharesMint);
    const metadataAccount = await fetchEncodedAccount(this._rpc, sharesMintMetadata, { commitment: 'processed' });
    if (!metadataAccount.exists) {
      result.errors.push(`Shares token metadata not set for shares mint ${vaultState.sharesMint}`);
    }

    // 5. Check min deposit is not 0
    if (vaultState.minDepositAmount.isZero()) {
      result.errors.push('Min deposit amount is 0');
    }

    // 6. Check the vault has at least one allocation
    const activeAllocations = vaultState.vaultAllocationStrategy.filter(
      (allocation) => allocation.reserve !== DEFAULT_PUBLIC_KEY
    );
    if (activeAllocations.length === 0) {
      result.errors.push('Vault has no allocations');
    }

    // 7. Check allocations have weight > 0 and cap > 0, warn if cap != u64::MAX
    for (const allocation of activeAllocations) {
      if (allocation.targetAllocationWeight.isZero()) {
        result.errors.push(`Allocation for reserve ${allocation.reserve} has weight 0`);
      }
      if (allocation.tokenAllocationCap.isZero()) {
        result.errors.push(`Allocation for reserve ${allocation.reserve} has cap 0`);
      } else if (allocation.tokenAllocationCap.toString() !== U64_MAX) {
        result.warnings.push(
          `Allocation for reserve ${
            allocation.reserve
          } has cap ${allocation.tokenAllocationCap.toString()} (not u64::MAX)`
        );
      }
    }

    // 9. Check CDN that the vault admin exists in riskManagers and has a description
    const cdnResources = await this.loadCdnResourcesOnce();
    if (!cdnResources) {
      result.errors.push('Could not fetch CDN resources to verify vault admin');
    } else {
      const adminEntries = cdnResources.riskManagers[vaultState.vaultAdminAuthority];
      if (!adminEntries || adminEntries.length === 0) {
        result.errors.push(`Vault admin ${vaultState.vaultAdminAuthority} not found in CDN riskManagers`);
      } else {
        const hasDescription = adminEntries.some(
          (entry: RiskManagerInfo) => entry.description && entry.description.trim().length > 0
        );
        if (!hasDescription) {
          result.errors.push(
            `Vault admin ${vaultState.vaultAdminAuthority} found in CDN riskManagers but has no description`
          );
        }
      }
    }

    result.success = result.errors.length === 0;
    return result;
  }

  /**
   * Prints a vault in a human readable form
   * @param vaultPubkey - the address of the vault
   * @param [vaultState] - optional parameter to pass the vault state directly; this will save a network call
   * @param [slot] - optional slot to use for calculations; if not provided, the latest confirmed slot will be fetched
   * @returns - void; prints the vault to the console
   */
  async printVault(vaultPubkey: Address, vaultState?: VaultState, slot?: Slot) {
    const vault = vaultState ? vaultState : await VaultState.fetch(this.getConnection(), vaultPubkey);

    if (!vault) {
      console.log(`Vault ${vaultPubkey.toString()} not found`);
      return;
    }

    const kaminoVault = KaminoVault.loadWithClientAndState(this, vaultPubkey, vault);
    const vaultName = this.decodeVaultName(vault.name);
    const currentSlot = slot ?? (await this.getConnection().getSlot({ commitment: 'confirmed' }).send());
    const tokensPerShare = await this.getTokensPerShareSingleVault(kaminoVault, currentSlot);
    const holdings = await this.getVaultHoldings(vault, currentSlot);

    const sharesIssued = new Decimal(vault.sharesIssued.toString()!).div(
      new Decimal(vault.sharesMintDecimals.toString())
    );

    console.log('Name: ', vaultName);
    console.log('Shares issued: ', sharesIssued);
    holdings.print();
    console.log('Tokens per share: ', tokensPerShare);
  }

  /**
   * This method initializes the kvault global config (one off, needs to be signed by program owner)
   * @param admin - the admin of the kvault program
   * @returns - an instruction to initialize the kvault global config
   */
  async initKvaultGlobalConfigIx(admin: TransactionSigner) {
    const globalConfigAddress = await getKvaultGlobalConfigPda(this.getProgramID());

    const programData = await programDataPda(this.getProgramID());
    const ix = initKVaultGlobalConfig(
      {
        payer: admin,
        globalConfig: globalConfigAddress,
        programData: programData,
        systemProgram: SYSTEM_PROGRAM_ADDRESS,
        rent: SYSVAR_RENT_ADDRESS,
      },
      undefined,
      this.getProgramID()
    );
    return ix;
  }

  /**
   * This method updates the kvault global config
   * @param mode - the mode to update the global config with
   * @returns - an instruction to update the global config
   */
  async updateGlobalConfigIx(mode: string, value: string) {
    console.log('in updateGlobalConfigIx');
    let modeEnum: UpdateKVaultGlobalConfigModeKind;
    switch (mode) {
      case 'PendingAdmin': {
        // Ensure value is a valid address string before converting
        if (!value || value.length < 32) {
          throw new Error(`Invalid address value: ${value}`);
        }
        const addr = address(value);
        modeEnum = new UpdateGlobalConfigMode.PendingAdmin([addr]);
        break;
      }
      case 'MinWithdrawalPenaltyLamports': {
        modeEnum = new UpdateGlobalConfigMode.MinWithdrawalPenaltyLamports([new BN(value)]);
        break;
      }
      case 'MinWithdrawalPenaltyBPS': {
        modeEnum = new UpdateGlobalConfigMode.MinWithdrawalPenaltyBPS([new BN(value)]);
        break;
      }
      default:
        throw new Error(`Unknown update mode: ${mode}`);
    }
    const args: UpdateKVaultGlobalConfigArgs = {
      update: modeEnum,
    };

    const globalConfigAddress = await getKvaultGlobalConfigPda(this.getProgramID());
    const globalConfigState = await KVaultGlobalConfig.fetch(this.getConnection(), globalConfigAddress);
    if (!globalConfigState) {
      throw new Error('Global config not found');
    }
    const admin = globalConfigState.globalAdmin;
    const accounts: UpdateKVaultGlobalConfigAccounts = {
      globalAdmin: noopSigner(admin),
      globalConfig: globalConfigAddress,
    };
    return updateKVaultGlobalConfig(args, accounts, undefined, this.getProgramID());
  }

  /**
   * This method accepts the ownership of the global config
   * @param admin - the admin of the transaction
   * @returns - an instruction to accept the ownership of the global config
   */
  async acceptGlobalConfigOwnershipIx(admin: TransactionSigner) {
    const globalConfigAddress = await getKvaultGlobalConfigPda(this.getProgramID());
    const accounts: UpdateGlobalConfigAdminAccounts = {
      pendingAdmin: admin,
      globalConfig: globalConfigAddress,
    };
    return updateGlobalConfigAdmin(accounts, undefined, this.getProgramID());
  }

  /**
   * This method will create a vault with a given config. The config can be changed later on, but it is recommended to set it up correctly from the start
   * @param vaultConfig - the config object used to create a vault
   * @param [useDevnetFarms] - whether to use devnet farms
   * @param [slot] - optional slot to use for lookup table creation; if not provided, the latest finalized slot will be fetched
   * @returns vault: the keypair of the vault, used to sign the initialization transaction; initVaultIxs: a struct with ixs to initialize the vault and its lookup table + populateLUTIxs, a list to populate the lookup table which has to be executed in a separate transaction
   */
  async createVaultIxs(
    vaultConfig: KaminoVaultConfig,
    useDevnetFarms: boolean = false,
    slot?: Slot
  ): Promise<{ vault: TransactionSigner; lut: Address; initVaultIxs: InitVaultIxs }> {
    const vaultState = await generateKeyPairSigner();
    const size = BigInt(VaultState.layout.span + 8);

    const createVaultIx = getCreateAccountInstruction({
      payer: vaultConfig.admin,
      space: size,
      lamports: await this.getConnection().getMinimumBalanceForRentExemption(size).send(),
      programAddress: this._kaminoVaultProgramId,
      newAccount: vaultState,
    });

    const [resolvedSlot, [tokenVault], [baseVaultAuthority], [sharesMint]] = await Promise.all([
      slot ? Promise.resolve(slot) : this.getConnection().getSlot({ commitment: 'finalized' }).send(),
      getProgramDerivedAddress({
        seeds: [Buffer.from(TOKEN_VAULT_SEED), addressEncoder.encode(vaultState.address)],
        programAddress: this._kaminoVaultProgramId,
      }),
      getProgramDerivedAddress({
        seeds: [Buffer.from(BASE_VAULT_AUTHORITY_SEED), addressEncoder.encode(vaultState.address)],
        programAddress: this._kaminoVaultProgramId,
      }),
      getProgramDerivedAddress({
        seeds: [Buffer.from(SHARES_SEED), addressEncoder.encode(vaultState.address)],
        programAddress: this._kaminoVaultProgramId,
      }),
    ]);

    let adminTokenAccount: Address;
    const prerequisiteIxs: Instruction[] = [];
    const cleanupIxs: Instruction[] = [];
    if (vaultConfig.tokenMint === WRAPPED_SOL_MINT) {
      const { wsolAta, createAtaIxs, closeAtaIxs } = await createWsolAtaIfMissing(
        this.getConnection(),
        new Decimal(VAULT_INITIAL_DEPOSIT),
        vaultConfig.admin,
        vaultConfig.tokenMintProgramId
      );
      adminTokenAccount = wsolAta;

      prerequisiteIxs.push(...createAtaIxs);
      cleanupIxs.push(...closeAtaIxs);
    } else {
      adminTokenAccount = (
        await findAssociatedTokenPda({
          mint: vaultConfig.tokenMint,
          tokenProgram: vaultConfig.tokenMintProgramId,
          owner: vaultConfig.admin.address,
        })
      )[0];
    }

    const initVaultAccounts: InitVaultAccounts = {
      adminAuthority: vaultConfig.admin,
      vaultState: vaultState.address,
      baseTokenMint: vaultConfig.tokenMint,
      tokenVault,
      baseVaultAuthority,
      sharesMint,
      systemProgram: SYSTEM_PROGRAM_ADDRESS,
      rent: SYSVAR_RENT_ADDRESS,
      tokenProgram: vaultConfig.tokenMintProgramId,
      sharesTokenProgram: TOKEN_PROGRAM_ADDRESS,
      adminTokenAccount,
    };
    const initVaultIx = initVault(initVaultAccounts, undefined, this._kaminoVaultProgramId);

    const createVaultFarm = await this.createVaultFarm(
      vaultConfig.admin,
      vaultState.address,
      sharesMint,
      useDevnetFarms
    );

    // create and set up the vault lookup table
    const [createLUTIx, lut] = await initLookupTableIx(vaultConfig.admin, resolvedSlot);

    const farmsGlobalConfig = useDevnetFarms ? FARMS_GLOBAL_CONFIG_DEVNET : FARMS_GLOBAL_CONFIG_MAINNET;
    const accountsToBeInserted: Address[] = [
      vaultConfig.admin.address,
      vaultState.address,
      vaultConfig.tokenMint,
      vaultConfig.tokenMintProgramId,
      baseVaultAuthority,
      sharesMint,
      SYSTEM_PROGRAM_ADDRESS,
      SYSVAR_RENT_ADDRESS,
      TOKEN_PROGRAM_ADDRESS,
      this._kaminoLendProgramId,
      SYSVAR_INSTRUCTIONS_ADDRESS,
      createVaultFarm.farm.address,
      farmsGlobalConfig,
    ];
    const insertIntoLUTIxs = await insertIntoLookupTableIxs(
      this.getConnection(),
      vaultConfig.admin,
      lut,
      accountsToBeInserted,
      []
    );

    const setLUTIx = await this.updateUninitialisedVaultConfigIx(
      vaultConfig.admin,
      vaultState.address,
      new VaultConfigField.LookupTable(),
      lut.toString()
    );

    const ixs = [createVaultIx, initVaultIx, setLUTIx];

    if (vaultConfig.getPerformanceFeeBps() > 0) {
      const setPerformanceFeeIx = await this.updateUninitialisedVaultConfigIx(
        vaultConfig.admin,
        vaultState.address,
        new VaultConfigField.PerformanceFeeBps(),
        vaultConfig.getPerformanceFeeBps().toString()
      );
      ixs.push(setPerformanceFeeIx);
    }
    if (vaultConfig.getManagementFeeBps() > 0) {
      const setManagementFeeIx = await this.updateUninitialisedVaultConfigIx(
        vaultConfig.admin,
        vaultState.address,
        new VaultConfigField.ManagementFeeBps(),
        vaultConfig.getManagementFeeBps().toString()
      );
      ixs.push(setManagementFeeIx);
    }
    if (vaultConfig.name && vaultConfig.name.length > 0) {
      const setNameIx = await this.updateUninitialisedVaultConfigIx(
        vaultConfig.admin,
        vaultState.address,
        new VaultConfigField.Name(),
        vaultConfig.name
      );
      ixs.push(setNameIx);
    }
    const setFarmIx = await this.updateUninitialisedVaultConfigIx(
      vaultConfig.admin,
      vaultState.address,
      new VaultConfigField.Farm(),
      createVaultFarm.farm.address
    );

    const metadataIx = await this.getSetSharesMetadataIx(
      this.getConnection(),
      vaultConfig.admin,
      vaultState.address,
      sharesMint,
      baseVaultAuthority,
      vaultConfig.vaultTokenSymbol,
      vaultConfig.vaultTokenName,
      undefined,
      this._kaminoVaultProgramId
    );

    return {
      vault: vaultState,
      lut,
      initVaultIxs: {
        createAtaIfNeededIxs: prerequisiteIxs,
        initVaultIxs: ixs,
        createLUTIx,
        populateLUTIxs: insertIntoLUTIxs,
        cleanupIxs,
        initSharesMetadataIx: metadataIx,
        createVaultFarm,
        setFarmToVaultIx: setFarmIx,
      },
    };
  }

  /**
   * This method creates a farm for a vault
   * @param signer - the signer of the transaction
   * @param vaultSharesMint - the mint of the vault shares
   * @param vaultAddress - the address of the vault (it doesn't need to be already initialized)
   * @returns a struct with the farm, the setup farm ixs and the update farm ixs
   */
  async createVaultFarm(
    signer: TransactionSigner,
    vaultAddress: Address,
    vaultSharesMint: Address,
    useDevnetFarms: boolean = false
  ): Promise<CreateVaultFarm> {
    const farmsSDK = new Farms(this._rpc, this._farmsProgramId);

    const globalConfig = useDevnetFarms ? FARMS_GLOBAL_CONFIG_DEVNET : FARMS_GLOBAL_CONFIG_MAINNET;
    const farm = await generateKeyPairSigner();
    const ixs = await farmsSDK.createFarmIxs(signer, farm, globalConfig, vaultSharesMint);

    const updatePendingFarmAdminIx = await farmsSDK.updateFarmConfigIx(
      signer,
      farm.address,
      DEFAULT_PUBLIC_KEY,
      new FarmConfigOption.UpdatePendingFarmAdmin(),
      FARMS_ADMIN_MAINNET,
      undefined,
      undefined,
      true
    );
    const updateFarmVaultIdIx = await farmsSDK.updateFarmConfigIx(
      signer,
      farm.address,
      DEFAULT_PUBLIC_KEY,
      new FarmConfigOption.UpdateVaultId(),
      vaultAddress,
      undefined,
      undefined,
      true
    );

    return {
      farm,
      setupFarmIxs: ixs,
      updateFarmIxs: [updatePendingFarmAdminIx, updateFarmVaultIdIx],
    };
  }

  /**
   * This method creates an instruction to set the shares metadata for a vault
   * @param rpc
   * @param vaultAdmin
   * @param vault - the vault to set the shares metadata for
   * @param sharesMint
   * @param baseVaultAuthority
   * @param tokenName - the name of the token in the vault (symbol; e.g. "USDC" which becomes "kVUSDC")
   * @param extraName - the extra string appended to the prefix("Kamino Vault USDC <extraName>")
   * @returns - an instruction to set the shares metadata for the vault
   */
  async getSetSharesMetadataIx(
    rpc: Rpc<SolanaRpcApi>,
    vaultAdmin: TransactionSigner,
    vault: Address,
    sharesMint: Address,
    baseVaultAuthority: Address,
    tokenName: string,
    extraName: string,
    metadataProgramId: Address = METADATA_PROGRAM_ID,
    kvaultProgramId?: Address
  ) {
    const kvaultProgramIdToUse = kvaultProgramId ?? this._kaminoVaultProgramId;
    const [sharesMintMetadata] = await getKVaultSharesMetadataPda(sharesMint, metadataProgramId);

    const { name, symbol, uri } = resolveMetadata(sharesMint, extraName, tokenName);

    const ix = !(await fetchEncodedAccount(rpc, sharesMintMetadata, { commitment: 'processed' })).exists
      ? await getInitializeKVaultSharesMetadataIx(
          vaultAdmin,
          vault,
          sharesMint,
          baseVaultAuthority,
          name,
          symbol,
          uri,
          metadataProgramId,
          kvaultProgramIdToUse
        )
      : await getUpdateSharesMetadataIx(
          vaultAdmin,
          vault,
          sharesMint,
          baseVaultAuthority,
          name,
          symbol,
          uri,
          metadataProgramId,
          kvaultProgramIdToUse
        );

    return ix;
  }

  /**
   * This method updates the vault reserve allocation config for an exiting vault reserve, or adds a new reserve to the vault if it does not exist.
   * @param vault - vault to be updated
   * @param reserveAllocationConfig - new reserve allocation config
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - a struct with an instruction to update the reserve allocation and an optional list of instructions to update the lookup table for the allocation changes
   */
  async updateReserveAllocationIxs(
    vault: KaminoVault,
    reserveAllocationConfig: ReserveAllocationConfig,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<UpdateReserveAllocationIxs> {
    const vaultState: VaultState = await vault.getState();
    const reserveState: Reserve = reserveAllocationConfig.getReserveState();

    const cTokenVault = await getCTokenVaultPda(
      vault.address,
      reserveAllocationConfig.getReserveAddress(),
      this._kaminoVaultProgramId
    );

    const reserveWhitelistEntryOption = await getReserveWhitelistEntryIfExists(
      reserveAllocationConfig.getReserveAddress(),
      this.getConnection(),
      this._kaminoVaultProgramId
    );

    const vaultAdmin = parseVaultAdmin(vaultState, vaultAdminAuthority);
    const updateReserveAllocationAccounts: UpdateReserveAllocationAccounts = {
      signer: vaultAdmin,
      vaultState: vault.address,
      baseVaultAuthority: vaultState.baseVaultAuthority,
      reserveCollateralMint: reserveState.collateral.mintPubkey,
      reserve: reserveAllocationConfig.getReserveAddress(),
      ctokenVault: cTokenVault,
      reserveWhitelistEntry: reserveWhitelistEntryOption,
      systemProgram: SYSTEM_PROGRAM_ADDRESS,
      rent: SYSVAR_RENT_ADDRESS,
      reserveCollateralTokenProgram: TOKEN_PROGRAM_ADDRESS,
    };

    const updateReserveAllocationArgs: UpdateReserveAllocationArgs = {
      weight: new BN(reserveAllocationConfig.targetAllocationWeight),
      cap: new BN(reserveAllocationConfig.getAllocationCapLamports().floor().toString()),
    };

    const updateReserveAllocationIx = updateReserveAllocation(
      updateReserveAllocationArgs,
      updateReserveAllocationAccounts,
      undefined,
      this._kaminoVaultProgramId
    );

    const accountsToAddToLut = [
      reserveAllocationConfig.getReserveAddress(),
      cTokenVault,
      ...this.getReserveAccountsToInsertInLut(reserveState),
    ];

    const [lendingMarketAuth] = await lendingMarketAuthPda(reserveState.lendingMarket, this._kaminoLendProgramId);
    accountsToAddToLut.push(lendingMarketAuth);

    const insertIntoLutIxs = await insertIntoLookupTableIxs(
      this.getConnection(),
      vaultAdmin,
      vaultState.vaultLookupTable,
      accountsToAddToLut
    );

    const updateReserveAllocationIxs: UpdateReserveAllocationIxs = {
      updateReserveAllocationIx,
      updateLUTIxs: insertIntoLutIxs,
    };

    return updateReserveAllocationIxs;
  }

  /**
   * This method updates the unallocated weight and cap of a vault (both are optional, if not provided the current values will be used)
   * @param vault - the vault to update the unallocated weight and cap for
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @param [unallocatedWeight] - the new unallocated weight to set. If not provided, the current unallocated weight will be used
   * @param [unallocatedCap] - the new unallocated cap to set. If not provided, the current unallocated cap will be used
   * @returns - a list of instructions to update the unallocated weight and cap
   */
  async updateVaultUnallocatedWeightAndCapIxs(
    vault: KaminoVault,
    vaultAdminAuthority?: TransactionSigner,
    unallocatedWeight?: BN,
    unallocatedCap?: BN
  ) {
    const vaultState = await vault.getState();

    const unallocatedWeightToUse = unallocatedWeight ? unallocatedWeight : vaultState.unallocatedWeight;
    const unallocatedCapToUse = unallocatedCap ? unallocatedCap : vaultState.unallocatedTokensCap;

    const ixs: Instruction[] = [];

    if (!unallocatedWeightToUse.eq(vaultState.unallocatedWeight)) {
      const updateVaultUnallocatedWeightIx = await this.updateVaultConfigIxs(
        vault,
        new VaultConfigField.UnallocatedWeight(),
        unallocatedWeightToUse.toString(),
        vaultAdminAuthority
      );
      ixs.push(updateVaultUnallocatedWeightIx.updateVaultConfigIx);
    }

    if (!unallocatedCapToUse.eq(vaultState.unallocatedTokensCap)) {
      const updateVaultUnallocatedCapIx = await this.updateVaultConfigIxs(
        vault,
        new VaultConfigField.UnallocatedTokensCap(),
        unallocatedCapToUse.toString(),
        vaultAdminAuthority
      );
      ixs.push(updateVaultUnallocatedCapIx.updateVaultConfigIx);
    }

    return ixs;
  }

  /**
   * This method withdraws all the funds from a reserve and blocks it from being invested by setting its weight and ctoken allocation to 0
   * @param vault - the vault to withdraw the funds from
   * @param reserve - the reserve to withdraw the funds from
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - a struct with an instruction to update the reserve allocation and an optional list of instructions to update the lookup table for the allocation changes
   */
  async withdrawEverythingAndBlockInvestReserve(
    vault: KaminoVault,
    reserve: Address,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<WithdrawAndBlockReserveIxs> {
    const vaultState = await vault.getState();

    const reserveIsPartOfAllocation = vaultState.vaultAllocationStrategy.some(
      (allocation) => allocation.reserve === reserve
    );

    const withdrawAndBlockReserveIxs: WithdrawAndBlockReserveIxs = {
      updateReserveAllocationIxs: [],
      investIxs: [],
    };
    if (!reserveIsPartOfAllocation) {
      return withdrawAndBlockReserveIxs;
    }

    const reserveState = await Reserve.fetch(this.getConnection(), reserve);
    if (reserveState === null) {
      return withdrawAndBlockReserveIxs;
    }
    const reserveWithAddress: ReserveWithAddress = {
      address: reserve,
      state: reserveState,
    };
    const reserveAllocationConfig = new ReserveAllocationConfig(reserveWithAddress, 0, new Decimal(0));

    const admin = vaultAdminAuthority ? vaultAdminAuthority : noopSigner(vaultState.vaultAdminAuthority);

    // update allocation to have 0 weight and 0 cap
    const updateAllocIxs = await this.updateReserveAllocationIxs(vault, reserveAllocationConfig, admin);

    const investIx = await this.investSingleReserveIxs(admin, vault, reserveWithAddress);
    withdrawAndBlockReserveIxs.updateReserveAllocationIxs = [updateAllocIxs.updateReserveAllocationIx];
    withdrawAndBlockReserveIxs.investIxs = investIx;

    return withdrawAndBlockReserveIxs;
  }

  /**
   * This method withdraws all the funds from all the reserves and blocks them from being invested by setting their weight and ctoken allocation to 0
   * @param vault - the vault to withdraw the invested funds from
   * @param [vaultReservesMap] - optional parameter to pass a map of the vault reserves. If not provided, the reserves will be loaded from the vault
   * @param [payer] - optional parameter to pass a different payer for the transaction. If not provided, the admin of the vault will be used; this is the payer for the invest ixs and it should have an ATA and some lamports (2x no_of_reserves) of the token vault
   * @returns - a struct with an instruction to update the reserve allocations (set weight and ctoken allocation to 0) and an a list of instructions to disinvest the funds in the reserves
   */
  async withdrawEverythingFromAllReservesAndBlockInvest(
    vault: KaminoVault,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    payer?: TransactionSigner
  ): Promise<WithdrawAndBlockReserveIxs> {
    const vaultState = await vault.getState();

    const reserves = this.getVaultReserves(vaultState);
    const withdrawAndBlockReserveIxs: WithdrawAndBlockReserveIxs = {
      updateReserveAllocationIxs: [],
      investIxs: [],
    };

    if (!vaultReservesMap) {
      vaultReservesMap = await this.loadVaultReserves(vaultState);
    }

    for (const reserve of reserves) {
      const reserveWithAddress: ReserveWithAddress = {
        address: reserve,
        state: vaultReservesMap.get(reserve)!.state,
      };
      const reserveAllocationConfig = new ReserveAllocationConfig(reserveWithAddress, 0, new Decimal(0));

      // update allocation to have 0 weight and 0 cap
      const updateAllocIxs = await this.updateReserveAllocationIxs(vault, reserveAllocationConfig, payer);
      withdrawAndBlockReserveIxs.updateReserveAllocationIxs.push(updateAllocIxs.updateReserveAllocationIx);
    }

    const investPayer = payer ? payer : noopSigner(vaultState.vaultAdminAuthority);
    const investIxs = await this.investAllReservesIxs(investPayer, vault, true);
    withdrawAndBlockReserveIxs.investIxs = investIxs;

    return withdrawAndBlockReserveIxs;
  }

  /**
   * This method disinvests all the funds from all the reserves and set their weight to 0; for vaults that are managed by external bot/crank, the bot can change the weight and invest in the reserves again
   * @param vault - the vault to disinvest the invested funds from
   * @param [vaultReservesMap] - optional parameter to pass a map of the vault reserves. If not provided, the reserves will be loaded from the vault
   * @param [payer] - optional parameter to pass a different payer for the transaction. If not provided, the admin of the vault will be used; this is the payer for the invest ixs and it should have an ATA and some lamports (2x no_of_reserves) of the token vault
   * @returns - a struct with an instruction to update the reserve allocations to 0 weight and a list of instructions to disinvest the funds in the reserves
   */
  async disinvestAllReservesIxs(
    vault: KaminoVault,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    payer?: TransactionSigner
  ): Promise<DisinvestAllReservesIxs> {
    const vaultState = await vault.getState();

    const reserves = this.getVaultReserves(vaultState);
    const disinvestAllReservesIxs: DisinvestAllReservesIxs = {
      updateReserveAllocationIxs: [],
      investIxs: [],
    };

    if (!vaultReservesMap) {
      vaultReservesMap = await this.loadVaultReserves(vaultState);
    }

    for (const reserve of reserves) {
      const reserveWithAddress: ReserveWithAddress = {
        address: reserve,
        state: vaultReservesMap.get(reserve)!.state,
      };
      const existingReserveAllocation = vaultState.vaultAllocationStrategy.find(
        (allocation) => allocation.reserve === reserve
      );
      if (!existingReserveAllocation) {
        continue;
      }
      const reserveAllocationConfig = new ReserveAllocationConfig(
        reserveWithAddress,
        0,
        lamportsToDecimal(
          new Decimal(existingReserveAllocation.tokenAllocationCap.toString()),
          reserveWithAddress.state.liquidity.mintDecimals.toNumber()
        )
      );

      // update allocation to have 0 weight and 0 cap
      const updateAllocIxs = await this.updateReserveAllocationIxs(vault, reserveAllocationConfig, payer);
      disinvestAllReservesIxs.updateReserveAllocationIxs.push(updateAllocIxs.updateReserveAllocationIx);
    }

    const investPayer = payer ? payer : noopSigner(vaultState.vaultAdminAuthority);
    const investIxs = await this.investAllReservesIxs(investPayer, vault, true);
    disinvestAllReservesIxs.investIxs = investIxs;

    return disinvestAllReservesIxs;
  }

  /**
   * This method removes a reserve from the vault allocation strategy if already part of the allocation strategy
   * @param vault - vault to remove the reserve from
   * @param reserve - reserve to remove from the vault allocation strategy
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - an instruction to remove the reserve from the vault allocation strategy or undefined if the reserve is not part of the allocation strategy
   */
  async removeReserveFromAllocationIx(
    vault: KaminoVault,
    reserve: Address,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<Instruction | undefined> {
    const vaultState = await vault.getState();
    const vaultAdmin = parseVaultAdmin(vaultState, vaultAdminAuthority);

    const reserveIsPartOfAllocation = vaultState.vaultAllocationStrategy.some(
      (allocation) => allocation.reserve === reserve
    );

    if (!reserveIsPartOfAllocation) {
      return undefined;
    }

    const accounts: RemoveAllocationAccounts = {
      vaultAdminAuthority: vaultAdmin,
      vaultState: vault.address,
      reserve,
    };

    return removeAllocation(accounts);
  }

  /**
   * Update a field of the vault. If the field is a pubkey it will return an extra instruction to add that account into the lookup table
   * @param vault the vault to update
   * @param mode the field to update (based on VaultConfigFieldKind enum)
   * @param value the value to update the field with
   * @param [adminAuthority] 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.
   *        The global admin should be passed in when wanting to change the AllowAllocationsInWhitelistedReservesOnly or AllowInvestInWhitelistedReservesOnly fields to false
   * @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
   * @param [skipLutUpdate] if true, the lookup table instructions will not be included in the returned instructions
   * @param errorOnOverride throw error if vault already has a farm
   * @returns a struct that contains the instruction to update the field and an optional list of instructions to update the lookup table
   */
  async updateVaultConfigIxs(
    vault: KaminoVault,
    mode: VaultConfigFieldKind,
    value: string,
    adminAuthority?: TransactionSigner,
    lutIxsSigner?: TransactionSigner,
    skipLutUpdate: boolean = false,
    errorOnOverride: boolean = true
  ): Promise<UpdateVaultConfigIxs> {
    const vaultState: VaultState = await vault.getState();
    const admin = parseVaultAdmin(vaultState, adminAuthority);

    const globalConfig = await getKvaultGlobalConfigPda(this._kaminoVaultProgramId);
    const updateVaultConfigAccs: UpdateVaultConfigAccounts = {
      signer: admin,
      globalConfig: globalConfig,
      vaultState: vault.address,
      klendProgram: this._kaminoLendProgramId,
    };

    if (mode.kind === new VaultConfigField.Farm().kind) {
      if (value != DEFAULT_PUBLIC_KEY && vaultState.vaultFarm != DEFAULT_PUBLIC_KEY) {
        if (errorOnOverride) {
          throw new Error('Vault already has a farm, if you want to override it set errorOnOverride to false');
        }
      }
    }

    const updateVaultConfigArgs: UpdateVaultConfigArgs = {
      entry: mode,
      data: this.getValueForModeAsBuffer(mode, value),
    };

    await this.updateVaultConfigValidations(mode, value, vaultState);

    const vaultReserves = this.getVaultReserves(vaultState);
    const vaultReservesState = await this.loadVaultReserves(vaultState);

    let updateVaultConfigIx = updateVaultConfig(
      updateVaultConfigArgs,
      updateVaultConfigAccs,
      undefined,
      this._kaminoVaultProgramId
    );
    updateVaultConfigIx = this.appendRemainingAccountsForVaultReserves(
      updateVaultConfigIx,
      vaultReserves,
      vaultReservesState
    );

    const updateLUTIxs: Instruction[] = [];

    if (!skipLutUpdate) {
      const lutIxsSignerAccount = lutIxsSigner ? lutIxsSigner : admin;

      if (mode.kind === new VaultConfigField.PendingVaultAdmin().kind) {
        const newPubkey = address(value);

        const insertIntoLutIxs = await insertIntoLookupTableIxs(
          this.getConnection(),
          lutIxsSignerAccount,
          vaultState.vaultLookupTable,
          [newPubkey]
        );
        updateLUTIxs.push(...insertIntoLutIxs);
      } else if (mode.kind === new VaultConfigField.Farm().kind) {
        const keysToAddToLUT = [address(value)];
        // if the farm already exist we want to read its state to add it to the LUT
        try {
          const farmState = await FarmState.fetch(this.getConnection(), keysToAddToLUT[0], this._farmsProgramId);
          keysToAddToLUT.push(
            farmState!.farmVault,
            farmState!.farmVaultsAuthority,
            farmState!.token.mint,
            farmState!.scopePrices,
            farmState!.globalConfig
          );
          const insertIntoLutIxs = await insertIntoLookupTableIxs(
            this.getConnection(),
            lutIxsSignerAccount,
            vaultState.vaultLookupTable,
            keysToAddToLUT
          );
          updateLUTIxs.push(...insertIntoLutIxs);
        } catch (error) {
          console.log(`Error fetching farm ${keysToAddToLUT[0].toString()} state`, error);
        }
      }
    }

    const updateVaultConfigIxs: UpdateVaultConfigIxs = {
      updateVaultConfigIx,
      updateLUTIxs,
    };

    return updateVaultConfigIxs;
  }

  /**
   * Update the vault performance fee (in bps).
   * @param vault - vault to update
   * @param feeBps - performance fee in basis points
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultPerfFeeIxs(
    vault: KaminoVault,
    feeBps: BN | number | string,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<UpdateVaultConfigIxs> {
    return this.updateVaultConfigIxs(
      vault,
      new VaultConfigField.PerformanceFeeBps(),
      feeBps.toString(),
      vaultAdminAuthority
    );
  }

  /**
   * Update the vault management fee (in bps).
   * @param vault - vault to update
   * @param feeBps - management fee in basis points
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultMgmtFeeIxs(
    vault: KaminoVault,
    feeBps: BN | number | string,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<UpdateVaultConfigIxs> {
    return this.updateVaultConfigIxs(
      vault,
      new VaultConfigField.ManagementFeeBps(),
      feeBps.toString(),
      vaultAdminAuthority
    );
  }

  /**
   * Update the pending admin for the vault (step 1/2 of the ownership transfer).
   * @param vault - vault to update
   * @param newAdmin - new pending admin pubkey
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @param [lutIxsSigner] - signer for LUT updates when adding the new admin
   * @param [skipLutUpdate] - if true, the LUT update instructions are not returned
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultPendingAdminIxs(
    vault: KaminoVault,
    newAdmin: Address,
    vaultAdminAuthority?: TransactionSigner,
    lutIxsSigner?: TransactionSigner,
    skipLutUpdate: boolean = false
  ): Promise<UpdateVaultConfigIxs> {
    return this.updateVaultConfigIxs(
      vault,
      new VaultConfigField.PendingVaultAdmin(),
      newAdmin,
      vaultAdminAuthority,
      lutIxsSigner,
      skipLutUpdate
    );
  }

  /**
   * Update the vault name.
   * @param vault - vault to update
   * @param name - new vault name
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultNameIxs(
    vault: KaminoVault,
    name: string,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<UpdateVaultConfigIxs> {
    return this.updateVaultConfigIxs(vault, new VaultConfigField.Name(), name, vaultAdminAuthority);
  }

  /**
   * Update the vault lookup table address.
   * @param vault - vault to update
   * @param lookupTable - new LUT address
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultLookupTableIxs(
    vault: KaminoVault,
    lookupTable: Address,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<UpdateVaultConfigIxs> {
    return this.updateVaultConfigIxs(vault, new VaultConfigField.LookupTable(), lookupTable, vaultAdminAuthority);
  }

  /**
   * Update the vault allocation admin.
   * @param vault - vault to update
   * @param allocationAdmin - new allocation admin pubkey
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultAllocationAdminIxs(
    vault: KaminoVault,
    allocationAdmin: Address,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<UpdateVaultConfigIxs> {
    return this.updateVaultConfigIxs(
      vault,
      new VaultConfigField.AllocationAdmin(),
      allocationAdmin,
      vaultAdminAuthority
    );
  }

  /**
   * Update the vault unallocated weight.
   * @param vault - vault to update
   * @param unallocatedWeight - new unallocated weight
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultUnallocatedWeightIxs(
    vault: KaminoVault,
    unallocatedWeight: BN | number | string,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<UpdateVaultConfigIxs> {
    return this.updateVaultConfigIxs(
      vault,
      new VaultConfigField.UnallocatedWeight(),
      unallocatedWeight.toString(),
      vaultAdminAuthority
    );
  }

  /**
   * Update the vault unallocated tokens cap.
   * @param vault - vault to update
   * @param unallocatedTokensCap - new unallocated tokens cap
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultUnallocatedTokensCapIxs(
    vault: KaminoVault,
    unallocatedTokensCap: BN | number | string,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<UpdateVaultConfigIxs> {
    return this.updateVaultConfigIxs(
      vault,
      new VaultConfigField.UnallocatedTokensCap(),
      unallocatedTokensCap.toString(),
      vaultAdminAuthority
    );
  }

  /**
   * Update the vault farm address.
   * @param vault - vault to update
   * @param farm - farm address
   * @param [errorOnOverride] - if true, it will throw if the vault already has a farm
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @param [lutIxsSigner] - signer for LUT updates when adding the farm
   * @param [skipLutUpdate] - if true, the LUT update instructions are not returned
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultFarmIxs(
    vault: KaminoVault,
    farm: Address,
    errorOnOverride: boolean = true,
    vaultAdminAuthority?: TransactionSigner,
    lutIxsSigner?: TransactionSigner,
    skipLutUpdate: boolean = false
  ): Promise<UpdateVaultConfigIxs> {
    return this.updateVaultConfigIxs(
      vault,
      new VaultConfigField.Farm(),
      farm,
      vaultAdminAuthority,
      lutIxsSigner,
      skipLutUpdate,
      errorOnOverride
    );
  }

  /**
   * Update the first loss capital farm address.
   * @param vault - vault to update
   * @param farm - farm address
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultFirstLossCapitalFarmIxs(
    vault: KaminoVault,
    farm: Address,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<UpdateVaultConfigIxs> {
    return this.updateVaultConfigIxs(vault, new VaultConfigField.FirstLossCapitalFarm(), farm, vaultAdminAuthority);
  }

  /**
   * Update the vault min deposit amount (in lamports).
   * @param vault - vault to update
   * @param minDepositAmount - new minimum deposit amount
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultMinDepositAmountIxs(
    vault: KaminoVault,
    minDepositAmount: BN | number | string,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<UpdateVaultConfigIxs> {
    return this.updateVaultConfigIxs(
      vault,
      new VaultConfigField.MinDepositAmount(),
      minDepositAmount.toString(),
      vaultAdminAuthority
    );
  }

  /**
   * Update the vault min withdraw amount (in lamports).
   * @param vault - vault to update
   * @param minWithdrawAmount - new minimum withdraw amount
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultMinWithdrawAmountIxs(
    vault: KaminoVault,
    minWithdrawAmount: BN | number | string,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<UpdateVaultConfigIxs> {
    return this.updateVaultConfigIxs(
      vault,
      new VaultConfigField.MinWithdrawAmount(),
      minWithdrawAmount.toString(),
      vaultAdminAuthority
    );
  }

  /**
   * Update the vault min invest amount (in lamports).
   * @param vault - vault to update
   * @param minInvestAmount - new minimum invest amount
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultMinInvestAmountIxs(
    vault: KaminoVault,
    minInvestAmount: BN | number | string,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<UpdateVaultConfigIxs> {
    return this.updateVaultConfigIxs(
      vault,
      new VaultConfigField.MinInvestAmount(),
      minInvestAmount.toString(),
      vaultAdminAuthority
    );
  }

  /**
   * Update the vault min invest delay (in slots).
   * @param vault - vault to update
   * @param minInvestDelaySlots - new minimum invest delay in slots
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultMinInvestDelaySlotsIxs(
    vault: KaminoVault,
    minInvestDelaySlots: BN | number | string,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<UpdateVaultConfigIxs> {
    return this.updateVaultConfigIxs(
      vault,
      new VaultConfigField.MinInvestDelaySlots(),
      minInvestDelaySlots.toString(),
      vaultAdminAuthority
    );
  }

  /**
   * Update the vault crank fund fee per reserve (in lamports).
   * @param vault - vault to update
   * @param crankFundFeePerReserve - new fee per reserve
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultCrankFundFeePerReserveIxs(
    vault: KaminoVault,
    crankFundFeePerReserve: BN | number | string,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<UpdateVaultConfigIxs> {
    return this.updateVaultConfigIxs(
      vault,
      new VaultConfigField.CrankFundFeePerReserve(),
      crankFundFeePerReserve.toString(),
      vaultAdminAuthority
    );
  }

  /**
   * Update the vault withdrawal penalty (in lamports).
   * @param vault - vault to update
   * @param withdrawalPenaltyLamports - new withdrawal penalty amount
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultWithdrawalPenaltyLamportsIxs(
    vault: KaminoVault,
    withdrawalPenaltyLamports: BN | number | string,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<UpdateVaultConfigIxs> {
    return this.updateVaultConfigIxs(
      vault,
      new VaultConfigField.WithdrawalPenaltyLamports(),
      withdrawalPenaltyLamports.toString(),
      vaultAdminAuthority
    );
  }

  /**
   * Update the vault withdrawal penalty (in bps).
   * @param vault - vault to update
   * @param withdrawalPenaltyBps - new withdrawal penalty bps
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultWithdrawalPenaltyBpsIxs(
    vault: KaminoVault,
    withdrawalPenaltyBps: BN | number | string,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<UpdateVaultConfigIxs> {
    return this.updateVaultConfigIxs(
      vault,
      new VaultConfigField.WithdrawalPenaltyBps(),
      withdrawalPenaltyBps.toString(),
      vaultAdminAuthority
    );
  }

  /**
   * Update whether allocations are restricted to whitelisted reserves only.
   * @param vault - vault to update
   * @param allowWhitelistedOnly - true to restrict, false to allow any reserve
   * @param [adminAuthority] - signer; pass global admin when setting to false
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultAllowAllocationsInWhitelistedReservesOnlyIxs(
    vault: KaminoVault,
    allowWhitelistedOnly: boolean | string,
    adminAuthority?: TransactionSigner
  ): Promise<UpdateVaultConfigIxs> {
    const value = typeof allowWhitelistedOnly === 'boolean' ? allowWhitelistedOnly.toString() : allowWhitelistedOnly;
    return this.updateVaultConfigIxs(
      vault,
      new VaultConfigField.AllowAllocationsInWhitelistedReservesOnly(),
      value,
      adminAuthority
    );
  }

  /**
   * Update whether invest is restricted to whitelisted reserves only.
   * @param vault - vault to update
   * @param allowWhitelistedOnly - true to restrict, false to allow any reserve
   * @param [adminAuthority] - signer; pass global admin when setting to false
   * @returns - a struct containing the update instruction and optional LUT updates
   */
  async updateVaultAllowInvestInWhitelistedReservesOnlyIxs(
    vault: KaminoVault,
    allowWhitelistedOnly: boolean | string,
    adminAuthority?: TransactionSigner
  ): Promise<UpdateVaultConfigIxs> {
    const value = typeof allowWhitelistedOnly === 'boolean' ? allowWhitelistedOnly.toString() : allowWhitelistedOnly;
    return this.updateVaultConfigIxs(
      vault,
      new VaultConfigField.AllowInvestInWhitelistedReservesOnly(),
      value,
      adminAuthority
    );
  }

  async updateVaultConfigValidations(mode: VaultConfigFieldKind, value: string, vaultState: VaultState) {
    if (
      mode.kind === new VaultConfigField.FirstLossCapitalFarm().kind ||
      mode.kind === new VaultConfigField.Farm().kind
    ) {
      const farmAddress = address(value);
      if (farmAddress === DEFAULT_PUBLIC_KEY) {
        return;
      }
      const farmState = await FarmState.fetch(this.getConnection(), farmAddress, this._farmsProgramId);
      if (!farmState) {
        throw new Error(`Farm ${farmAddress.toString()} not found for FirstLossCapitalFarm`);
      }
      if (
        mode.kind === new VaultConfigField.FirstLossCapitalFarm().kind &&
        !(await this.isFlcFarmValid(farmState, vaultState))
      ) {
        throw new Error(`Farm ${farmAddress.toString()} is not valid for FirstLossCapitalFarm`);
      }
    }
  }
  /**
   * Add or update a reserve whitelist entry. This controls whether the reserve is whitelisted for adding/updating
   * allocations or for invest, depending on the mode parameter.
   *
   * @param reserve - Address of the reserve to whitelist
   * @param mode - The whitelist mode: either 'Invest' or 'AddAllocation' with a value (1 = allow, 0 = deny)
   * @param globalAdmin - The global admin that signs the transaction
   * @returns - An instruction to add/update the whitelisted reserve
   */
  async addUpdateWhitelistedReserveIx(
    reserve: Address,
    mode: UpdateReserveWhitelistModeKind,
    globalAdmin: TransactionSigner
  ): Promise<Instruction> {
    const globalConfig = await getKvaultGlobalConfigPda(this._kaminoVaultProgramId);
    const reserveWhitelistEntry = await getReserveWhitelistEntryPda(reserve, this._kaminoVaultProgramId);

    const accounts: AddUpdateWhitelistedReserveAccounts = {
      globalAdmin,
      globalConfig,
      reserve,
      reserveWhitelistEntry,
      systemProgram: SYSTEM_PROGRAM_ADDRESS,
    };

    const args: AddUpdateWhitelistedReserveArgs = {
      update: mode,
    };

    return addUpdateWhitelistedReserve(args, accounts, undefined, this._kaminoVaultProgramId);
  }

  /** Sets the farm where the shares can be staked. This is store in vault state and a vault can only have one farm, so the new farm will ovveride the old farm
   * @param vault - vault to set the farm for
   * @param farm - the farm where the vault shares can be staked
   * @param [errorOnOverride] - if true, the function will throw an error if the vault already has a farm. If false, it will override the farm
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @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
   * @param [skipLutUpdate] - if true, the lookup table instructions will not be included in the returned instructions
   * @returns - a struct that contains the instruction to update the farm and an optional list of instructions to update the lookup table
   */
  async setVaultFarmIxs(
    vault: KaminoVault,
    farm: Address,
    errorOnOverride: boolean = true,
    vaultAdminAuthority?: TransactionSigner,
    lutIxsSigner?: TransactionSigner,
    skipLutUpdate: boolean = false
  ): Promise<UpdateVaultConfigIxs> {
    const vaultHasFarm = await vault.hasFarm();
    if (vaultHasFarm && errorOnOverride) {
      throw new Error('Vault already has a farm, if you want to override it set errorOnOverride to false');
    }
    return this.updateVaultConfigIxs(
      vault,
      new VaultConfigField.Farm(),
      farm,
      vaultAdminAuthority,
      lutIxsSigner,
      skipLutUpdate
    );
  }

  /**
   * This method updates the vault config during vault initialization, within the same transaction
   * where the vault is created. Use this when the vault state is not yet committed to the chain
   * and cannot be fetched via RPC. For updates to existing vaults, use updateVaultConfigIxs instead.
   *
   * @param admin - the admin that signs the transaction
   * @param vault - address of vault to be updated
   * @param mode - the field to be updated
   * @param value - the new value for the field to be updated (number or pubkey)
   * @returns - an instruction to update the vault config
   */
  private async updateUninitialisedVaultConfigIx(
    admin: TransactionSigner,
    vault: Address,
    mode: VaultConfigFieldKind,
    value: string
  ): Promise<Instruction> {
    const globalConfig = await getKvaultGlobalConfigPda(this._kaminoVaultProgramId);
    const updateVaultConfigAccs: UpdateVaultConfigAccounts = {
      signer: admin,
      globalConfig: globalConfig,
      vaultState: vault,
      klendProgram: this._kaminoLendProgramId,
    };

    const updateVaultConfigArgs: UpdateVaultConfigArgs = {
      entry: mode,
      data: this.getValueForModeAsBuffer(mode, value),
    };

    const updateVaultConfigIx = updateVaultConfig(
      updateVaultConfigArgs,
      updateVaultConfigAccs,
      undefined,
      this._kaminoVaultProgramId
    );

    return updateVaultConfigIx;
  }

  /**
   * This function creates the instruction for the `pendingAdmin` of the vault to accept to become the owner of the vault (step 2/2 of the ownership transfer)
   * @param vault - vault to change the ownership for
   * @param [pendingAdmin] - pending vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @param [slot] - optional slot to use for lookup table creation; if not provided, the latest finalized slot will be fetched
   * @returns - an instruction to accept the ownership of the vault and a list of instructions to update the lookup table
   */
  async acceptVaultOwnershipIxs(
    vault: KaminoVault,
    pendingAdmin?: TransactionSigner,
    slot?: Slot
  ): Promise<AcceptVaultOwnershipIxs> {
    const vaultState: VaultState = await vault.getState();
    const signer = parseVaultPendingAdmin(vaultState, pendingAdmin);

    const acceptOwneshipAccounts: UpdateAdminAccounts = {
      pendingAdmin: signer,
      vaultState: vault.address,
    };

    const acceptVaultOwnershipIx = updateAdmin(acceptOwneshipAccounts, undefined, this._kaminoVaultProgramId);

    // read the current LUT and create a new one for the new admin and backfill it
    const accountsInExistentLUT = (await getAccountsInLut(this.getConnection(), vaultState.vaultLookupTable)).filter(
      (account) => account !== vaultState.vaultAdminAuthority
    );

    const lutIxs: Instruction[] = [];
    const [initNewLutIx, newLut] = await initLookupTableIx(
      signer,
      slot ?? (await this.getConnection().getSlot({ commitment: 'finalized' }).send())
    );

    const insertIntoLUTIxs = await insertIntoLookupTableIxs(
      this.getConnection(),
      signer,
      newLut,
      accountsInExistentLUT,
      []
    );

    lutIxs.push(...insertIntoLUTIxs);

    const updateVaultConfigIxs = await this.updateVaultConfigIxs(
      vault,
      new VaultConfigField.LookupTable(),
      newLut.toString(),
      signer
    );
    lutIxs.push(updateVaultConfigIxs.updateVaultConfigIx);
    lutIxs.push(...updateVaultConfigIxs.updateLUTIxs);

    const acceptVaultOwnershipIxs: AcceptVaultOwnershipIxs = {
      acceptVaultOwnershipIx,
      initNewLUTIx: initNewLutIx,
      updateLUTIxs: lutIxs,
    };

    return acceptVaultOwnershipIxs;
  }

  /**
   * This function creates the instruction for the admin to give up a part of the pending fees (which will be accounted as part of the vault)
   * @param vault - vault to give up pending fees for
   * @param maxAmountToGiveUp - the maximum amount of fees to give up, in tokens
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - an instruction to give up the specified pending fees
   */
  async giveUpPendingFeesIx(
    vault: KaminoVault,
    maxAmountToGiveUp: Decimal,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<Instruction> {
    const vaultState: VaultState = await vault.getState();
    const vaultAdmin = parseVaultAdmin(vaultState, vaultAdminAuthority);

    const giveUpPendingFeesAccounts: GiveUpPendingFeesAccounts = {
      vaultAdminAuthority: vaultAdmin,
      vaultState: vault.address,
      klendProgram: this._kaminoLendProgramId,
    };

    const maxAmountToGiveUpLamports = numberToLamportsDecimal(
      maxAmountToGiveUp,
      vaultState.tokenMintDecimals.toNumber()
    );
    const giveUpPendingFeesArgs: GiveUpPendingFeesArgs = {
      maxAmountToGiveUp: new BN(maxAmountToGiveUpLamports.toString()),
    };

    return giveUpPendingFees(giveUpPendingFeesArgs, giveUpPendingFeesAccounts, undefined, this._kaminoVaultProgramId);
  }

  /**
   * This method withdraws all the pending fees from the vault to the owner's token ATA
   * @param vault - vault for which the admin withdraws the pending fees
   * @param slot - current slot, used to estimate the interest earned in the different reserves with allocation from the vault
   * @param [vaultReservesMap] - a hashmap from each reserve pubkey to the reserve state. Optional. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @param [vaultAdminAuthority] - vault admin - a noop vaultAdminAuthority is provided when absent for multisigs
   * @returns - list of instructions to withdraw all pending fees, including the ATA creation instructions if needed
   */
  async withdrawPendingFeesIxs(
    vault: KaminoVault,
    currentSlot?: Slot,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    vaultAdminAuthority?: TransactionSigner
  ): Promise<Instruction[]> {
    const slot = currentSlot ?? (await this.getConnection().getSlot({ commitment: 'confirmed' }).send());
    const vaultState: VaultState = await vault.getState();
    const vaultAdmin = parseVaultAdmin(vaultState, vaultAdminAuthority);
    const vaultReservesState = vaultReservesMap ? vaultReservesMap : await this.loadVaultReserves(vaultState);
    const [{ ata: adminTokenAta, createAtaIx }] = await createAtasIdempotent(vaultAdmin, [
      {
        mint: vaultState.tokenMint,
        tokenProgram: vaultState.tokenProgram,
      },
    ]);

    const tokensToWithdraw = new Fraction(vaultState.pendingFeesSf).toDecimal();
    let tokenLeftToWithdraw = tokensToWithdraw;
    tokenLeftToWithdraw = tokenLeftToWithdraw.sub(new Decimal(vaultState.tokenAvailable.toString()));
    const reservesToWithdraw: Address[] = [];

    if (tokenLeftToWithdraw.lte(0)) {
      // Availabe enough to withdraw all - using first reserve as it does not matter
      reservesToWithdraw.push(vaultState.vaultAllocationStrategy[0].reserve);
    } else {
      // Get decreasing order sorted available liquidity to withdraw from each reserve allocated to
      const reserveAllocationAvailableLiquidityToWithdraw = await this.getReserveAllocationAvailableLiquidityToWithdraw(
        vault,
        slot,
        vaultReservesState
      );
      // sort
      const reserveAllocationAvailableLiquidityToWithdrawSorted = new Map(
        [...reserveAllocationAvailableLiquidityToWithdraw.entries()].sort((a, b) => b[1].sub(a[1]).toNumber())
      );

      reserveAllocationAvailableLiquidityToWithdrawSorted.forEach((availableLiquidityToWithdraw, key) => {
        if (tokenLeftToWithdraw.gt(0)) {
          reservesToWithdraw.push(key);
          tokenLeftToWithdraw = tokenLeftToWithdraw.sub(availableLiquidityToWithdraw);
        }
      });
    }

    const reserveStates = await Reserve.fetchMultiple(
      this.getConnection(),
      reservesToWithdraw,
      this._kaminoLendProgramId
    );
    const withdrawIxs: Instruction[] = await Promise.all(
      reservesToWithdraw.map(async (reserve, index) => {
        if (reserveStates[index] === null) {
          throw new Error(`Reserve ${reserve} not found`);
        }

        const reserveState = reserveStates[index]!;
        const marketAddress = reserveState.lendingMarket;

        return this.withdrawPendingFeesIx(
          vaultAdmin,
          vault,
          vaultState,
          marketAddress,
          { address: reserve, state: reserveState },
          adminTokenAta
        );
      })
    );

    return [createAtaIx, ...withdrawIxs];
  }

  // async closeVaultIx(vault: KaminoVault): Promise<Instruction> {
  //   const vaultState: VaultState = await vault.getState(this.getConnection());

  //   const closeVaultAccounts: CloseVaultAccounts = {
  //     adminAuthority: vaultState.adminAuthority,
  //     vaultState: vault.address,
  //   };

  //   return closeVault(closeVaultAccounts, this._kaminoVaultProgramId);
  // }

  /**
   * This function creates instructions to deposit into a vault. It will also create ATA creation instructions for the vault shares that the user receives in return
   * @param user - user to deposit
   * @param vault - vault to deposit into (if the state is not provided, it will be fetched)
   * @param tokenAmount - token amount to be deposited, in decimals (will be converted in lamports)
   * @param [vaultReservesMap] - optional parameter; a hashmap from each reserve pubkey to the reserve state. Optional. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @param [farmState] - the state of the vault farm, if the vault has a farm. Optional. If not provided, it will be fetched
   * @returns - an instance of DepositIxs which contains the instructions to deposit in vault and the instructions to stake the shares in the farm if the vault has a farm as well as ixs to stake in the first loss capital farm if the vault has one - only one set on ixs so stake in a farm can be used -> staking can be either done in the farm or in the first loss capital farm
   */
  async depositIxs(
    user: TransactionSigner,
    vault: KaminoVault,
    tokenAmount: Decimal,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    farmState?: FarmState,
    payer?: TransactionSigner
  ): Promise<DepositIxs> {
    let vaultFarmState = farmState;
    const vaultState = await vault.getState();
    if (!farmState && (await vault.hasFarm(vaultState))) {
      const vaultFarmStateResult = await FarmState.fetch(
        this.getConnection(),
        vaultState.vaultFarm,
        this._farmsProgramId
      );
      if (vaultFarmStateResult) {
        vaultFarmState = vaultFarmStateResult;
      }
    }
    return this.buildShareEntryIxs('deposit', user, vault, tokenAmount, vaultReservesMap, vaultFarmState, payer);
  }

  async buySharesIxs(
    user: TransactionSigner,
    vault: KaminoVault,
    tokenAmount: Decimal,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    farmState?: FarmState,
    payer?: TransactionSigner
  ): Promise<DepositIxs> {
    return this.buildShareEntryIxs('buy', user, vault, tokenAmount, vaultReservesMap, farmState, payer);
  }

  private async buildShareEntryIxs(
    mode: 'deposit' | 'buy',
    user: TransactionSigner,
    vault: KaminoVault,
    tokenAmount: Decimal,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    farmState?: FarmState,
    payer?: TransactionSigner
  ): Promise<DepositIxs> {
    const vaultState = await vault.getState();

    const tokenProgramID = vaultState.tokenProgram;
    const userTokenAta = await getAssociatedTokenAddress(vaultState.tokenMint, user.address, tokenProgramID);
    const createAtasIxs: Instruction[] = [];
    const closeAtasIxs: Instruction[] = [];
    if (vaultState.tokenMint === WRAPPED_SOL_MINT) {
      const [{ ata: wsolAta, createAtaIx: createWsolAtaIxn }] = await createAtasIdempotent(
        user,
        [
          {
            mint: WRAPPED_SOL_MINT,
            tokenProgram: tokenProgramID,
          },
        ],
        payer
      );
      createAtasIxs.push(createWsolAtaIxn);
      const transferWsolIxs = getTransferWsolIxs(
        user,
        wsolAta,
        lamports(
          BigInt(numberToLamportsDecimal(tokenAmount, vaultState.tokenMintDecimals.toNumber()).ceil().toString())
        ),
        tokenProgramID
      );
      createAtasIxs.push(...transferWsolIxs);
    }

    const [{ ata: userSharesAta, createAtaIx: createSharesAtaIxs }] = await createAtasIdempotent(
      user,
      [
        {
          mint: vaultState.sharesMint,
          tokenProgram: TOKEN_PROGRAM_ADDRESS,
        },
      ],
      payer
    );
    createAtasIxs.push(createSharesAtaIxs);

    const eventAuthority = await getEventAuthorityPda(this._kaminoVaultProgramId);
    const tokenAmountLamports = numberToLamportsDecimal(tokenAmount, vaultState.tokenMintDecimals.toNumber()).floor();
    let entryIx: Instruction;
    if (mode === 'deposit') {
      const depositAccounts: DepositAccounts = {
        user,
        vaultState: vault.address,
        tokenVault: vaultState.tokenVault,
        tokenMint: vaultState.tokenMint,
        baseVaultAuthority: vaultState.baseVaultAuthority,
        sharesMint: vaultState.sharesMint,
        userTokenAta,
        userSharesAta,
        tokenProgram: tokenProgramID,
        klendProgram: this._kaminoLendProgramId,
        sharesTokenProgram: TOKEN_PROGRAM_ADDRESS,
        eventAuthority,
        program: this._kaminoVaultProgramId,
      };
      const depositArgs: DepositArgs = {
        maxAmount: new BN(tokenAmountLamports.toString()),
      };
      entryIx = deposit(depositArgs, depositAccounts, undefined, this._kaminoVaultProgramId);
    } else {
      const buyAccounts: BuyAccounts = {
        user,
        vaultState: vault.address,
        tokenVault: vaultState.tokenVault,
        tokenMint: vaultState.tokenMint,
        baseVaultAuthority: vaultState.baseVaultAuthority,
        sharesMint: vaultState.sharesMint,
        userTokenAta,
        userSharesAta,
        tokenProgram: tokenProgramID,
        klendProgram: this._kaminoLendProgramId,
        sharesTokenProgram: TOKEN_PROGRAM_ADDRESS,
        eventAuthority,
        program: this._kaminoVaultProgramId,
      };
      const buyArgs: BuyArgs = {
        maxAmount: new BN(tokenAmountLamports.toString()),
      };
      entryIx = buy(buyArgs, buyAccounts, undefined, this._kaminoVaultProgramId);
    }

    const vaultReserves = this.getVaultReserves(vaultState);
    const vaultReservesState = vaultReservesMap ? vaultReservesMap : await this.loadVaultReserves(vaultState);
    entryIx = this.appendRemainingAccountsForVaultReserves(entryIx, vaultReserves, vaultReservesState);

    const result: DepositIxs = {
      depositIxs: [...createAtasIxs, entryIx, ...closeAtasIxs],
      stakeInFarmIfNeededIxs: [],
      stakeInFlcFarmIfNeededIxs: [],
    };

    if (await vault.hasFarm()) {
      const stakeSharesIxs = await this.stakeSharesIxs(user, vault, undefined, farmState);
      result.stakeInFarmIfNeededIxs = stakeSharesIxs;
    }
    if (await vault.hasFlcFarm()) {
      const stakeSharesInFlcFarmIxs = await this.stakeSharesInFlcFarmIxs(user, vault, undefined, undefined);
      result.stakeInFlcFarmIfNeededIxs = stakeSharesInFlcFarmIxs;
    }
    return result;
  }

  /**
   * Returns the accounts needed for a vault deposit instruction, without building the instruction itself.
   * Includes the deposit accounts, the remaining accounts for vault reserves, and optionally the stake shares instructions if the vault has a farm.
   * @param user - the user depositing into the vault
   * @param vault - the vault to deposit into
   * @param [vaultReservesMap] - optional preloaded reserve states; if not provided they will be fetched
   * @param [farmState] - optional preloaded farm state; if not provided and the vault has a farm, it will be fetched
   * @returns the deposit accounts, remaining accounts, and optional stake shares instructions
   */
  async getDepositAccounts(
    user: TransactionSigner,
    vault: KaminoVault,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    farmState?: FarmState
  ): Promise<AllDepositAccounts> {
    const vaultState = await vault.getState();
    const tokenProgramID = vaultState.tokenProgram;
    const userTokenAta = await getAssociatedTokenAddress(vaultState.tokenMint, user.address, tokenProgramID);
    const userSharesAta = await getAssociatedTokenAddress(vaultState.sharesMint, user.address);
    const eventAuthority = await getEventAuthorityPda(this._kaminoVaultProgramId);

    const depositAccounts: DepositAccounts = {
      user,
      vaultState: vault.address,
      tokenVault: vaultState.tokenVault,
      tokenMint: vaultState.tokenMint,
      baseVaultAuthority: vaultState.baseVaultAuthority,
      sharesMint: vaultState.sharesMint,
      userTokenAta,
      userSharesAta,
      tokenProgram: tokenProgramID,
      klendProgram: this._kaminoLendProgramId,
      sharesTokenProgram: TOKEN_PROGRAM_ADDRESS,
      eventAuthority,
      program: this._kaminoVaultProgramId,
    };

    const vaultReserves = this.getVaultReserves(vaultState);
    const vaultReservesState = vaultReservesMap ? vaultReservesMap : await this.loadVaultReserves(vaultState);
    const remainingAccounts = this.buildRemainingAccountsForVaultReserves(vaultReserves, vaultReservesState);

    const result: AllDepositAccounts = {
      depositAccounts,
      remainingAccounts,
    };

    if (await vault.hasFarm()) {
      const stakeSharesIxs = await this.stakeSharesIxs(user, vault, undefined, farmState);
      result.stakeSharesIxs = stakeSharesIxs;
    }

    return result;
  }

  /**
   * Returns the accounts needed for a vault withdraw instruction, without building the instruction itself.
   * If a reserve is provided, builds the full WithdrawAccounts (withdraw from reserve). Otherwise builds WithdrawFromAvailableAccounts (withdraw from available liquidity only).
   * Also includes remaining accounts for vault reserves and optionally the unstake instructions if the vault has a farm.
   * @param user - the user withdrawing from the vault
   * @param vault - the vault to withdraw from
   * @param [reserve] - optional reserve to withdraw from; if omitted, builds accounts for withdrawing from available liquidity only
   * @param [vaultReservesMap] - optional preloaded reserve states; if not provided they will be fetched
   * @param [farmState] - optional preloaded farm state; if not provided and the vault has a farm, it will be fetched
   * @returns the withdraw accounts, remaining accounts, and optional unstake shares instructions
   */
  async getWithdrawAccounts(
    user: TransactionSigner,
    vault: KaminoVault,
    reserve?: ReserveWithAddress,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    farmState?: FarmState
  ): Promise<AllWithdrawAccounts> {
    const vaultState = await vault.getState();
    const userTokenAta = await getAssociatedTokenAddress(vaultState.tokenMint, user.address, vaultState.tokenProgram);
    const userSharesAta = await getAssociatedTokenAddress(vaultState.sharesMint, user.address);

    const globalConfig = await getKvaultGlobalConfigPda(this._kaminoVaultProgramId);
    const eventAuthority = await getEventAuthorityPda(this._kaminoVaultProgramId);

    let withdrawAccounts: WithdrawAccounts | WithdrawFromAvailableAccounts;

    if (reserve) {
      const marketAddress = reserve.state.lendingMarket;
      const [lendingMarketAuth] = await lendingMarketAuthPda(marketAddress, this._kaminoLendProgramId);

      withdrawAccounts = {
        withdrawFromAvailable: {
          user,
          vaultState: vault.address,
          globalConfig,
          tokenVault: vaultState.tokenVault,
          baseVaultAuthority: vaultState.baseVaultAuthority,
          userTokenAta,
          tokenMint: vaultState.tokenMint,
          userSharesAta,
          sharesMint: vaultState.sharesMint,
          tokenProgram: vaultState.tokenProgram,
          sharesTokenProgram: TOKEN_PROGRAM_ADDRESS,
          klendProgram: this._kaminoLendProgramId,
          eventAuthority,
          program: this._kaminoVaultProgramId,
        },
        withdrawFromReserveAccounts: {
          vaultState: vault.address,
          reserve: reserve.address,
          ctokenVault: await getCTokenVaultPda(vault.address, reserve.address, this._kaminoVaultProgramId),
          lendingMarket: marketAddress,
          lendingMarketAuthority: lendingMarketAuth,
          reserveLiquiditySupply: reserve.state.liquidity.supplyVault,
          reserveCollateralMint: reserve.state.collateral.mintPubkey,
          reserveCollateralTokenProgram: TOKEN_PROGRAM_ADDRESS,
          instructionSysvarAccount: SYSVAR_INSTRUCTIONS_ADDRESS,
        },
        eventAuthority,
        program: this._kaminoVaultProgramId,
      } as WithdrawAccounts;
    } else {
      withdrawAccounts = {
        user,
        vaultState: vault.address,
        globalConfig,
        tokenVault: vaultState.tokenVault,
        baseVaultAuthority: vaultState.baseVaultAuthority,
        userTokenAta,
        tokenMint: vaultState.tokenMint,
        userSharesAta,
        sharesMint: vaultState.sharesMint,
        tokenProgram: vaultState.tokenProgram,
        sharesTokenProgram: TOKEN_PROGRAM_ADDRESS,
        klendProgram: this._kaminoLendProgramId,
        eventAuthority,
        program: this._kaminoVaultProgramId,
      } as WithdrawFromAvailableAccounts;
    }

    const vaultReserves = this.getVaultReserves(vaultState);
    const vaultReservesState = vaultReservesMap ? vaultReservesMap : await this.loadVaultReserves(vaultState);
    const remainingAccounts = this.buildRemainingAccountsForVaultReserves(vaultReserves, vaultReservesState);

    const result: AllWithdrawAccounts = {
      withdrawAccounts,
      remainingAccounts,
    };

    const hasFarm = await vault.hasFarm();
    if (hasFarm) {
      let vaultFarmState = farmState;
      if (!vaultFarmState) {
        const vaultFarmStateResult = await FarmState.fetch(
          this.getConnection(),
          vaultState.vaultFarm,
          this._farmsProgramId
        );
        if (vaultFarmStateResult) {
          vaultFarmState = vaultFarmStateResult;
        }
      }
      const unstakeIxs = await getFarmUnstakeAndWithdrawIxs(
        this.getConnection(),
        user,
        new Decimal(U64_MAX.toString()),
        vaultState.vaultFarm,
        vaultFarmState
      );
      result.unstakeSharesIxs = [unstakeIxs.unstakeIx, unstakeIxs.withdrawIx];
    }

    return result;
  }

  /**
   * This function creates instructions to stake the shares in the vault farm if the vault has a farm
   * @param user - user to stake
   * @param vault - vault to deposit into its farm (if the state is not provided, it will be fetched)
   * @param [sharesAmount] - token amount to be deposited, in decimals (will be converted in lamports). Optional. If not provided, the user's share balance will be used
   * @param [farmState] - the state of the vault farm, if the vault has a farm. Optional. If not provided, it will be fetched
   * @returns - a list of instructions for the user to stake shares into the vault's farm, including the creation of prerequisite accounts if needed
   */
  async stakeSharesIxs(
    user: TransactionSigner,
    vault: KaminoVault,
    sharesAmount?: Decimal,
    farmState?: FarmState
  ): Promise<Instruction[]> {
    const vaultState = await vault.getState();

    let sharesToStakeLamports = new Decimal(U64_MAX);
    if (sharesAmount) {
      sharesToStakeLamports = numberToLamportsDecimal(sharesAmount, vaultState.sharesMintDecimals.toNumber());
    }

    // if tokens to be staked are 0 or vault has no farm there is no stake needed
    if (sharesToStakeLamports.lte(0) || !(await vault.hasFarm())) {
      return [];
    }

    // returns the ix to create the farm state account if needed and the ix to stake the shares
    return getFarmStakeIxs(this.getConnection(), user, sharesToStakeLamports, vaultState.vaultFarm, farmState);
  }

  /**
   * This function creates instructions to stake the shares in the vault firstLossCapital farm if the vault has a farm
   * @param user - user to stake
   * @param vault - vault to deposit into its flc farm (if the state is not provided, it will be fetched)
   * @param [sharesAmount] - token amount to be deposited, in decimals (will be converted in lamports). Optional. If not provided, the user's share balance will be used
   * @param [farmState] - the state of the vault flc farm, if the vault has a farm. Optional. If not provided, it will be fetched
   * @returns - a list of instructions for the user to stake shares into the vault's firstLossCapital farm, including the creation of prerequisite accounts if needed
   */
  async stakeSharesInFlcFarmIxs(
    user: TransactionSigner,
    vault: KaminoVault,
    sharesAmount?: Decimal,
    farmState?: FarmState
  ): Promise<Instruction[]> {
    const vaultState = await vault.getState();

    let sharesToStakeLamports = new Decimal(U64_MAX);
    if (sharesAmount) {
      sharesToStakeLamports = numberToLamportsDecimal(sharesAmount, vaultState.sharesMintDecimals.toNumber());
    }

    // if tokens to be staked are 0 or vault has no farm there is no stake needed
    if (sharesToStakeLamports.lte(0) || !(await vault.hasFlcFarm())) {
      return [];
    }

    // returns the ix to create the farm state account if needed and the ix to stake the shares
    return getFarmStakeIxs(
      this.getConnection(),
      user,
      sharesToStakeLamports,
      vaultState.firstLossCapitalFarm,
      farmState
    );
  }

  /**
   * This function will return a struct with the instructions to unstake from the farm if necessary and the instructions for the missing ATA creation instructions, as well as one or multiple withdraw instructions, based on how many reserves it's needed to withdraw from. This might have to be split in multiple transactions
   * @param user - user to withdraw
   * @param vault - vault to withdraw from
   * @param shareAmountToWithdraw - share amount to withdraw (in tokens, not lamports), in order to withdraw everything, any value > user share amount
   * @param slot - current slot, used to estimate the interest earned in the different reserves with allocation from the vault
   * @param [vaultReservesMap] - optional parameter; a hashmap from each reserve pubkey to the reserve state. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @param [farmState] - the state of the vault farm, if the vault has a farm. Optional. If not provided, it will be fetched
   * @returns an array of instructions to create missing ATAs if needed and the withdraw instructions
   */
  async withdrawIxs(
    user: TransactionSigner,
    vault: KaminoVault,
    shareAmountToWithdraw: Decimal,
    slot: Slot,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    farmState?: FarmState,
    payer?: TransactionSigner
  ): Promise<WithdrawIxs> {
    let vaultFarmState = farmState;
    const vaultState = await vault.getState();
    if (!farmState && (await vault.hasFarm(vaultState))) {
      const vaultFarmStateResult = await FarmState.fetch(
        this.getConnection(),
        vaultState.vaultFarm,
        this._farmsProgramId
      );
      if (vaultFarmStateResult) {
        vaultFarmState = vaultFarmStateResult;
      }
    }
    return this.buildShareExitIxs(
      'withdraw',
      user,
      vault,
      shareAmountToWithdraw,
      slot,
      vaultReservesMap,
      vaultFarmState,
      payer
    );
  }

  /**
   * This function will return the missing ATA creation instructions, as well as one or multiple withdraw instructions, based on how many reserves it's needed to withdraw from. This might have to be split in multiple transactions
   * @param user - user to sell shares for vault tokens
   * @param vault - vault to sell shares from
   * @param shareAmountToWithdraw - share amount to sell (in tokens, not lamports), in order to withdraw everything, any value > user share amount
   * @param slot - current slot, used to estimate the interest earned in the different reserves with allocation from the vault
   * @param [vaultReservesMap] - optional parameter; a hashmap from each reserve pubkey to the reserve state. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @param [farmState] - the state of the vault farm, if the vault has a farm. Optional. If not provided, it will be fetched
   * @returns an array of instructions to create missing ATAs if needed and the withdraw instructions
   */
  async sellSharesIxs(
    user: TransactionSigner,
    vault: KaminoVault,
    shareAmountToWithdraw: Decimal,
    slot: Slot,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    farmState?: FarmState,
    payer?: TransactionSigner
  ): Promise<WithdrawIxs> {
    return this.buildShareExitIxs('sell', user, vault, shareAmountToWithdraw, slot, vaultReservesMap, farmState, payer);
  }

  private async buildShareExitIxs(
    mode: 'withdraw' | 'sell',
    user: TransactionSigner,
    vault: KaminoVault,
    shareAmountToWithdraw: Decimal,
    slot: Slot,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    farmState?: FarmState,
    payer?: TransactionSigner
  ): Promise<WithdrawIxs> {
    const vaultState = await vault.getState();
    const hasFarm = await vault.hasFarm();

    const withdrawIxs: WithdrawIxs = {
      unstakeFromFarmIfNeededIxs: [],
      withdrawIxs: [],
      postWithdrawIxs: [],
    };

    // compute the total shares the user has (in ATA + in farm) and check if they want to withdraw everything or just a part
    let userSharesAtaBalance = new Decimal(0);
    const userSharesAta = await getAssociatedTokenAddress(vaultState.sharesMint, user.address);
    const userSharesAtaState = await fetchMaybeToken(this.getConnection(), userSharesAta);
    if (userSharesAtaState.exists) {
      const userSharesAtaBalanceInLamports = getTokenBalanceFromAccountInfoLamports(userSharesAtaState);
      userSharesAtaBalance = userSharesAtaBalanceInLamports.div(
        new Decimal(10).pow(vaultState.sharesMintDecimals.toString())
      );
    }

    let userSharesInFarm = new Decimal(0);
    if (hasFarm) {
      userSharesInFarm = await getUserSharesInTokensStakedInFarm(
        this.getConnection(),
        user.address,
        vaultState.vaultFarm,
        vaultState.sharesMintDecimals.toNumber()
      );
    }

    let sharesToWithdraw = shareAmountToWithdraw;
    const totalUserShares = userSharesAtaBalance.add(userSharesInFarm);
    let withdrawAllShares = false;
    if (sharesToWithdraw.gt(totalUserShares)) {
      sharesToWithdraw = new Decimal(U64_MAX.toString()).div(
        new Decimal(10).pow(vaultState.sharesMintDecimals.toString())
      );
      withdrawAllShares = true;
    }

    // if not enough shares in ATA unstake from farm
    const sharesInAtaAreEnoughForWithdraw = sharesToWithdraw.lte(userSharesAtaBalance);
    if (hasFarm && !sharesInAtaAreEnoughForWithdraw && userSharesInFarm.gt(0)) {
      // if we need to unstake we need to make sure share ata is created
      const [{ createAtaIx }] = await createAtasIdempotent(
        user,
        [
          {
            mint: vaultState.sharesMint,
            tokenProgram: TOKEN_PROGRAM_ADDRESS,
          },
        ],
        payer
      );
      withdrawIxs.unstakeFromFarmIfNeededIxs.push(createAtaIx);
      let shareLamportsToWithdraw = new Decimal(U64_MAX.toString());
      if (!withdrawAllShares) {
        const sharesToWithdrawFromFarm = sharesToWithdraw.sub(userSharesAtaBalance);
        shareLamportsToWithdraw = collToLamportsDecimal(
          sharesToWithdrawFromFarm,
          vaultState.sharesMintDecimals.toNumber()
        );
      }
      const unstakeAndWithdrawFromFarmIxs = await getFarmUnstakeAndWithdrawIxs(
        this.getConnection(),
        user,
        shareLamportsToWithdraw,
        vaultState.vaultFarm,
        farmState
      );
      withdrawIxs.unstakeFromFarmIfNeededIxs.push(unstakeAndWithdrawFromFarmIxs.unstakeIx);
      withdrawIxs.unstakeFromFarmIfNeededIxs.push(unstakeAndWithdrawFromFarmIxs.withdrawIx);
    }

    const hasAllocatedReserves = vaultState.vaultAllocationStrategy.some(
      (allocation) => allocation.reserve !== DEFAULT_PUBLIC_KEY
    );

    if (hasAllocatedReserves) {
      const reserveExitBuilder: ReserveExitInstructionBuilder =
        mode === 'withdraw'
          ? (params) =>
              this.withdrawIx(
                params.user,
                params.vault,
                params.vaultState,
                params.marketAddress,
                params.reserve,
                params.userSharesAta,
                params.userTokenAta,
                params.shareAmountLamports,
                params.vaultReservesState
              )
          : (params) =>
              this.sellIx(
                params.user,
                params.vault,
                params.vaultState,
                params.marketAddress,
                params.reserve,
                params.userSharesAta,
                params.userTokenAta,
                params.shareAmountLamports,
                params.vaultReservesState
              );
      const withdrawFromVaultIxs = await this.buildReserveExitIxs({
        user,
        vault,
        vaultState,
        shareAmount: sharesToWithdraw,
        allUserShares: totalUserShares,
        slot,
        vaultReservesMap,
        builder: reserveExitBuilder,
        payer,
      });
      withdrawIxs.withdrawIxs = withdrawFromVaultIxs;
    } else {
      const withdrawFromVaultIxs = await this.withdrawFromAvailableIxs(user, vault, sharesToWithdraw, payer);
      withdrawIxs.withdrawIxs = withdrawFromVaultIxs;
    }

    // if the vault is for SOL return the ix to unwrap the SOL
    if (vaultState.tokenMint === WRAPPED_SOL_MINT) {
      const userWsolAta = await getAssociatedTokenAddress(WRAPPED_SOL_MINT, user.address);
      const unwrapIx = getCloseAccountInstruction(
        {
          account: userWsolAta,
          owner: user,
          destination: user.address,
        },
        { programAddress: TOKEN_PROGRAM_ADDRESS }
      );
      withdrawIxs.postWithdrawIxs.push(unwrapIx);
    }

    // if we burn all of user's shares close its shares ATA
    const burnAllUserShares = sharesToWithdraw.gt(totalUserShares);
    if (burnAllUserShares) {
      const closeAtaIx = getCloseAccountInstruction(
        {
          account: userSharesAta,
          owner: user,
          destination: user.address,
        },
        { programAddress: TOKEN_PROGRAM_ADDRESS }
      );
      withdrawIxs.postWithdrawIxs.push(closeAtaIx);
    }

    return withdrawIxs;
  }

  private async withdrawFromAvailableIxs(
    user: TransactionSigner,
    vault: KaminoVault,
    shareAmount: Decimal,
    payer?: TransactionSigner
  ): Promise<Instruction[]> {
    const vaultState = await vault.getState();

    const userSharesAta = await getAssociatedTokenAddress(vaultState.sharesMint, user.address);
    const [{ ata: userTokenAta, createAtaIx }] = await createAtasIdempotent(
      user,
      [
        {
          mint: vaultState.tokenMint,
          tokenProgram: vaultState.tokenProgram,
        },
      ],
      payer
    );

    const shareLamportsToWithdraw = collToLamportsDecimal(shareAmount, vaultState.sharesMintDecimals.toNumber());
    const withdrawFromAvailableIxn = await this.withdrawFromAvailableIx(
      user,
      vault,
      vaultState,
      userSharesAta,
      userTokenAta,
      shareLamportsToWithdraw
    );

    return [createAtaIx, withdrawFromAvailableIxn];
  }

  private async buildReserveExitIxs({
    user,
    vault,
    vaultState,
    shareAmount,
    allUserShares,
    slot,
    vaultReservesMap,
    builder,
    payer,
  }: BuildReserveExitIxsParams): Promise<Instruction[]> {
    const vaultReservesState = vaultReservesMap ? vaultReservesMap : await this.loadVaultReserves(vaultState);
    const userSharesAta = await getAssociatedTokenAddress(vaultState.sharesMint, user.address);
    const [{ ata: userTokenAta, createAtaIx }] = await createAtasIdempotent(
      user,
      [
        {
          mint: vaultState.tokenMint,
          tokenProgram: vaultState.tokenProgram,
        },
      ],
      payer
    );

    const withdrawAllShares = shareAmount.gte(allUserShares);
    const actualSharesToWithdraw = shareAmount.lte(allUserShares) ? shareAmount : allUserShares;
    const shareLamportsToWithdraw = collToLamportsDecimal(
      actualSharesToWithdraw,
      vaultState.sharesMintDecimals.toNumber()
    );
    const tokensPerShare = await this.getTokensPerShareSingleVault(vault, slot);
    const sharesPerToken = new Decimal(1).div(tokensPerShare);
    const tokensToWithdraw = shareLamportsToWithdraw.mul(tokensPerShare);
    let tokenLeftToWithdraw = tokensToWithdraw;
    const availableTokens = new Decimal(vaultState.tokenAvailable.toString());
    tokenLeftToWithdraw = tokenLeftToWithdraw.sub(availableTokens);

    type ReserveWithTokensToWithdraw = { reserve: Address; shares: Decimal };

    const reserveWithSharesAmountToWithdraw: ReserveWithTokensToWithdraw[] = [];
    let isFirstWithdraw = true;

    if (tokenLeftToWithdraw.lte(0)) {
      const firstReserve = vaultState.vaultAllocationStrategy.find((reserve) => reserve.reserve !== DEFAULT_PUBLIC_KEY);
      if (!firstReserve) {
        throw new Error('No reserve available to satisfy withdraw request');
      }
      if (withdrawAllShares) {
        reserveWithSharesAmountToWithdraw.push({
          reserve: firstReserve.reserve,
          shares: new Decimal(U64_MAX.toString()),
        });
      } else {
        reserveWithSharesAmountToWithdraw.push({
          reserve: firstReserve.reserve,
          shares: shareLamportsToWithdraw,
        });
      }
    } else {
      const reserveAllocationAvailableLiquidityToWithdraw = await this.getReserveAllocationAvailableLiquidityToWithdraw(
        vault,
        slot,
        vaultReservesState
      );
      const reserveAllocationAvailableLiquidityToWithdrawSorted = [
        ...reserveAllocationAvailableLiquidityToWithdraw.entries(),
      ].sort((a, b) => b[1].sub(a[1]).toNumber());

      reserveAllocationAvailableLiquidityToWithdrawSorted.forEach(([key, availableLiquidityToWithdraw]) => {
        if (tokenLeftToWithdraw.gt(0)) {
          let tokensToWithdrawFromReserve = Decimal.min(tokenLeftToWithdraw, availableLiquidityToWithdraw);
          if (isFirstWithdraw) {
            tokensToWithdrawFromReserve = tokensToWithdrawFromReserve.add(availableTokens);
            isFirstWithdraw = false;
          }
          if (withdrawAllShares) {
            reserveWithSharesAmountToWithdraw.push({ reserve: key, shares: new Decimal(U64_MAX.toString()) });
          } else {
            const sharesToWithdrawFromReserve = tokensToWithdrawFromReserve.mul(sharesPerToken).floor();
            reserveWithSharesAmountToWithdraw.push({ reserve: key, shares: sharesToWithdrawFromReserve });
          }

          tokenLeftToWithdraw = tokenLeftToWithdraw.sub(tokensToWithdrawFromReserve);
        }
      });
    }

    const withdrawIxs: Instruction[] = [];
    withdrawIxs.push(createAtaIx);
    for (const reserveWithTokens of reserveWithSharesAmountToWithdraw) {
      const reserveState = vaultReservesState.get(reserveWithTokens.reserve);
      if (reserveState === undefined) {
        throw new Error(`Reserve ${reserveWithTokens.reserve} not found in vault reserves map`);
      }
      const marketAddress = reserveState.state.lendingMarket;

      const exitIx = await builder({
        user,
        vault,
        vaultState,
        marketAddress,
        reserve: { address: reserveWithTokens.reserve, state: reserveState.state },
        userSharesAta,
        userTokenAta,
        shareAmountLamports: reserveWithTokens.shares,
        vaultReservesState,
      });
      withdrawIxs.push(exitIx);
    }

    return withdrawIxs;
  }

  /**
   * This will trigger invest by balancing, based on weights, the reserve allocations of the vault. It can either withdraw or deposit into reserves to balance them. This is a function that should be cranked
   * @param payer wallet that pays the tx
   * @param vault - vault to invest from
   * @param skipComputationChecks - if true, the function will skip the computation checks and will invest all the reserves; it is useful for txs where we update reserve allocations and invest atomically
   * @returns - an array of invest instructions for each invest action required for the vault reserves
   */
  async investAllReservesIxs(
    payer: TransactionSigner,
    vault: KaminoVault,
    skipComputationChecks: boolean = false
  ): Promise<Instruction[]> {
    const vaultState = await vault.reloadState();
    const minInvestAmount = vaultState.minInvestAmount;
    const allReserves = this.getVaultReserves(vaultState);
    if (allReserves.length === 0) {
      throw new Error('No reserves found for the vault, please select at least one reserve for the vault');
    }
    const [allReservesStateMap, computedReservesAllocationTokens] = await Promise.all([
      this.loadVaultReserves(vaultState),
      this.getVaultComputedReservesAllocation(vaultState),
    ]);

    const tokenProgram = await getAccountOwner(this.getConnection(), vaultState.tokenMint);
    const [{ createAtaIx }] = await createAtasIdempotent(payer, [{ mint: vaultState.tokenMint, tokenProgram }]);
    // compute total vault holdings and expected distribution based on weights
    const curentVaultAllocations = this.getVaultAllocations(vaultState);
    const reservesToDisinvestFrom: Address[] = [];
    const reservesToInvestInto: Address[] = [];

    for (let index = 0; index < allReserves.length; index++) {
      const reservePubkey = allReserves[index];
      const reserveState = allReservesStateMap.get(reservePubkey)!;
      const computedAllocationTokens = computedReservesAllocationTokens.targetReservesAllocation.get(reservePubkey)!;
      const computedAllocationLamports = numberToLamportsDecimal(
        computedAllocationTokens,
        vaultState.tokenMintDecimals.toNumber()
      );
      const currentCTokenAllocation = curentVaultAllocations.get(reservePubkey)!.ctokenAllocation;
      const currentAllocationCap = curentVaultAllocations.get(reservePubkey)!.tokenAllocationCap;

      const reserveCollExchangeRate = reserveState.getCollateralExchangeRate();
      const reserveAllocationLamports = currentCTokenAllocation.div(reserveCollExchangeRate);
      const reserveAllocationLiquidityAmount = lamportsToDecimal(
        currentCTokenAllocation.div(reserveCollExchangeRate),
        vaultState.tokenMintDecimals.toNumber()
      );

      const diffInReserveTokens = computedAllocationTokens.sub(reserveAllocationLiquidityAmount);
      const diffInReserveLamports = collToLamportsDecimal(diffInReserveTokens, vaultState.tokenMintDecimals.toNumber());
      // it is possible that the tokens to invest are > minInvestAmountLamports but the ctokens it represent are 0, which will make an invest move 0 tokens
      const diffInCtokenLamports = reserveCollExchangeRate.mul(diffInReserveLamports.abs());
      const actualDiffInLamports = diffInCtokenLamports.floor().div(reserveCollExchangeRate).floor();

      // if the diff for the reserve is smaller than the min invest amount, we do not need to invest or disinvest
      const minInvestAmountLamports = new Decimal(minInvestAmount.toString());
      if (actualDiffInLamports.gt(minInvestAmountLamports) || skipComputationChecks) {
        if (computedAllocationTokens.lt(reserveAllocationLiquidityAmount)) {
          reservesToDisinvestFrom.push(reservePubkey);
        } else {
          const actualTargetLamports = currentAllocationCap.gt(computedAllocationLamports)
            ? computedAllocationLamports
            : currentAllocationCap;
          const lamportsToAddToReserve = actualTargetLamports.sub(reserveAllocationLamports);
          if (lamportsToAddToReserve.gt(minInvestAmountLamports)) {
            reservesToInvestInto.push(reservePubkey);
          }
        }
      }
    }

    const investIxsPromises: Promise<Instruction[]>[] = [];
    // invest first the reserves from which we disinvest, then the other ones
    for (const reserve of reservesToDisinvestFrom) {
      const reserveState = allReservesStateMap.get(reserve);
      if (reserveState === null) {
        throw new Error(`Reserve ${reserve} not found`);
      }
      const investIxsPromise = this.investSingleReserveIxs(
        payer,
        vault,
        {
          address: reserve,
          state: reserveState!.state,
        },
        allReservesStateMap,
        false
      );
      investIxsPromises.push(investIxsPromise);
    }

    for (const reserve of reservesToInvestInto) {
      const reserveState = allReservesStateMap.get(reserve);
      if (reserveState === null) {
        throw new Error(`Reserve ${reserve} not found`);
      }
      const investIxsPromise = this.investSingleReserveIxs(
        payer,
        vault,
        {
          address: reserve,
          state: reserveState!.state,
        },
        allReservesStateMap,
        false
      );
      investIxsPromises.push(investIxsPromise);
    }

    let investIxs: Instruction[] = [];
    investIxs.push(createAtaIx);
    investIxs = await Promise.all(investIxsPromises).then((ixs) => ixs.flat());

    return investIxs;
  }

  // todo: make sure we also check the ata of the investor for the vault token exists
  /**
   * This will trigger invest by balancing, based on weights, the reserve allocation of the vault. It can either withdraw or deposit into the given reserve to balance it
   * @param payer wallet pubkey - the instruction is permissionless and does not require the vault admin, due to rounding between cTokens and the underlying, the payer may have to contribute 1 or more lamports of the underlying from their token account
   * @param vault - vault to invest from
   * @param reserve - reserve to invest into or disinvest from
   * @param [vaultReservesMap] - optional parameter; a hashmap from each reserve pubkey to the reserve state. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @param [createAtaIfNeeded]
   * @returns - an array of invest instructions for each invest action required for the vault reserves
   */
  async investSingleReserveIxs(
    payer: TransactionSigner,
    vault: KaminoVault,
    reserve: ReserveWithAddress,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    createAtaIfNeeded: boolean = true
  ): Promise<Instruction[]> {
    const vaultState = await vault.getState();
    const cTokenVault = await getCTokenVaultPda(vault.address, reserve.address, this._kaminoVaultProgramId);
    const [lendingMarketAuth] = await lendingMarketAuthPda(reserve.state.lendingMarket, this._kaminoLendProgramId);

    const ixs: Instruction[] = [];

    const tokenProgram = await getAccountOwner(this.getConnection(), vaultState.tokenMint);
    const [{ ata: payerTokenAta, createAtaIx }] = await createAtasIdempotent(payer, [
      { mint: vaultState.tokenMint, tokenProgram },
    ]);
    if (createAtaIfNeeded) {
      ixs.push(createAtaIx);
    }

    const reserveWhitelistEntryOption = await getReserveWhitelistEntryIfExists(
      reserve.address,
      this.getConnection(),
      this._kaminoVaultProgramId
    );

    const investAccounts: InvestAccounts = {
      payer,
      vaultState: vault.address,
      tokenVault: vaultState.tokenVault,
      baseVaultAuthority: vaultState.baseVaultAuthority,
      ctokenVault: cTokenVault,
      reserve: reserve.address,
      /** CPI accounts */
      lendingMarket: reserve.state.lendingMarket,
      lendingMarketAuthority: lendingMarketAuth,
      reserveLiquiditySupply: reserve.state.liquidity.supplyVault,
      reserveCollateralMint: reserve.state.collateral.mintPubkey,
      reserveWhitelistEntry: reserveWhitelistEntryOption,
      klendProgram: this._kaminoLendProgramId,
      instructionSysvarAccount: SYSVAR_INSTRUCTIONS_ADDRESS,
      tokenProgram: tokenProgram,
      payerTokenAccount: payerTokenAta,
      tokenMint: vaultState.tokenMint,
      reserveCollateralTokenProgram: TOKEN_PROGRAM_ADDRESS,
    };

    let investIx = invest(investAccounts, undefined, this._kaminoVaultProgramId);

    const vaultReserves = this.getVaultReserves(vaultState);
    const vaultReservesState = vaultReservesMap ? vaultReservesMap : await this.loadVaultReserves(vaultState);
    investIx = this.appendRemainingAccountsForVaultReserves(investIx, vaultReserves, vaultReservesState);
    return [createAtaIx, investIx];
  }

  /** Convert a string to a u8 representation to be stored on chain */
  encodeVaultName(token: string): Uint8Array {
    const maxArray = new Uint8Array(40);
    const s: Uint8Array = new TextEncoder().encode(token);
    maxArray.set(s);
    return maxArray;
  }

  /**Convert an u8 array to a string */
  decodeVaultName(token: number[]): string {
    return decodeVaultName(token);
  }

  /** Helper to serialize value as Buffer for updateVaultConfig instruction */
  private getValueForModeAsBuffer(mode: VaultConfigFieldKind, value: string): Buffer {
    const isWhitelistOnlyFlag =
      mode.kind === new VaultConfigField.AllowInvestInWhitelistedReservesOnly().kind ||
      mode.kind === new VaultConfigField.AllowAllocationsInWhitelistedReservesOnly().kind;

    if (isWhitelistOnlyFlag) {
      const flag = parseBooleanFlag(value);
      return Buffer.from([flag]);
    } else if (isNaN(+value) || value == DEFAULT_PUBLIC_KEY) {
      if (mode.kind === new VaultConfigField.Name().kind) {
        const data = Array.from(this.encodeVaultName(value));
        return Buffer.from(data);
      } else {
        const data = address(value);
        return Buffer.from(addressEncoder.encode(data));
      }
    } else {
      const buffer = Buffer.alloc(8);
      buffer.writeBigUInt64LE(BigInt(value.toString()));
      return buffer;
    }
  }

  private async sellIx(
    user: TransactionSigner,
    vault: KaminoVault,
    vaultState: VaultState,
    marketAddress: Address,
    reserve: ReserveWithAddress,
    userSharesAta: Address,
    userTokenAta: Address,
    shareAmountLamports: Decimal,
    vaultReservesState: Map<Address, KaminoReserve>
  ): Promise<Instruction> {
    const [lendingMarketAuth] = await lendingMarketAuthPda(marketAddress, this._kaminoLendProgramId);

    const globalConfig = await getKvaultGlobalConfigPda(this._kaminoVaultProgramId);
    const eventAuthority = await getEventAuthorityPda(this._kaminoVaultProgramId);
    const sellAccounts: SellAccounts = {
      withdrawFromAvailable: {
        user,
        vaultState: vault.address,
        globalConfig: globalConfig,
        tokenVault: vaultState.tokenVault,
        baseVaultAuthority: vaultState.baseVaultAuthority,
        userTokenAta: userTokenAta,
        tokenMint: vaultState.tokenMint,
        userSharesAta: userSharesAta,
        sharesMint: vaultState.sharesMint,
        tokenProgram: vaultState.tokenProgram,
        sharesTokenProgram: TOKEN_PROGRAM_ADDRESS,
        klendProgram: this._kaminoLendProgramId,
        eventAuthority: eventAuthority,
        program: this._kaminoVaultProgramId,
      },
      withdrawFromReserveAccounts: {
        vaultState: vault.address,
        reserve: reserve.address,
        ctokenVault: await getCTokenVaultPda(vault.address, reserve.address, this._kaminoVaultProgramId),
        lendingMarket: marketAddress,
        lendingMarketAuthority: lendingMarketAuth,
        reserveLiquiditySupply: reserve.state.liquidity.supplyVault,
        reserveCollateralMint: reserve.state.collateral.mintPubkey,
        reserveCollateralTokenProgram: TOKEN_PROGRAM_ADDRESS,
        instructionSysvarAccount: SYSVAR_INSTRUCTIONS_ADDRESS,
      },
      eventAuthority: eventAuthority,
      program: this._kaminoVaultProgramId,
    };

    const sellArgs: SellArgs = {
      sharesAmount: new BN(shareAmountLamports.floor().toString()),
    };

    let sellIxn = sell(sellArgs, sellAccounts, undefined, this._kaminoVaultProgramId);

    const vaultReserves = this.getVaultReserves(vaultState);
    sellIxn = this.appendRemainingAccountsForVaultReserves(sellIxn, vaultReserves, vaultReservesState);

    return sellIxn;
  }

  private async withdrawIx(
    user: TransactionSigner,
    vault: KaminoVault,
    vaultState: VaultState,
    marketAddress: Address,
    reserve: ReserveWithAddress,
    userSharesAta: Address,
    userTokenAta: Address,
    shareAmountLamports: Decimal,
    vaultReservesState: Map<Address, KaminoReserve>
  ): Promise<Instruction> {
    const [lendingMarketAuth] = await lendingMarketAuthPda(marketAddress, this._kaminoLendProgramId);

    const globalConfig = await getKvaultGlobalConfigPda(this._kaminoVaultProgramId);
    const eventAuthority = await getEventAuthorityPda(this._kaminoVaultProgramId);
    const withdrawAccounts: WithdrawAccounts = {
      withdrawFromAvailable: {
        user,
        vaultState: vault.address,
        globalConfig: globalConfig,
        tokenVault: vaultState.tokenVault,
        baseVaultAuthority: vaultState.baseVaultAuthority,
        userTokenAta: userTokenAta,
        tokenMint: vaultState.tokenMint,
        userSharesAta: userSharesAta,
        sharesMint: vaultState.sharesMint,
        tokenProgram: vaultState.tokenProgram,
        sharesTokenProgram: TOKEN_PROGRAM_ADDRESS,
        klendProgram: this._kaminoLendProgramId,
        eventAuthority: eventAuthority,
        program: this._kaminoVaultProgramId,
      },
      withdrawFromReserveAccounts: {
        vaultState: vault.address,
        reserve: reserve.address,
        ctokenVault: await getCTokenVaultPda(vault.address, reserve.address, this._kaminoVaultProgramId),
        lendingMarket: marketAddress,
        lendingMarketAuthority: lendingMarketAuth,
        reserveLiquiditySupply: reserve.state.liquidity.supplyVault,
        reserveCollateralMint: reserve.state.collateral.mintPubkey,
        reserveCollateralTokenProgram: TOKEN_PROGRAM_ADDRESS,
        instructionSysvarAccount: SYSVAR_INSTRUCTIONS_ADDRESS,
      },
      eventAuthority: eventAuthority,
      program: this._kaminoVaultProgramId,
    };

    const withdrawArgs: WithdrawArgs = {
      sharesAmount: new BN(shareAmountLamports.floor().toString()),
    };

    let withdrawIxn = withdraw(withdrawArgs, withdrawAccounts, undefined, this._kaminoVaultProgramId);

    const vaultReserves = this.getVaultReserves(vaultState);
    withdrawIxn = this.appendRemainingAccountsForVaultReserves(withdrawIxn, vaultReserves, vaultReservesState);

    return withdrawIxn;
  }

  private async withdrawFromAvailableIx(
    user: TransactionSigner,
    vault: KaminoVault,
    vaultState: VaultState,
    userSharesAta: Address,
    userTokenAta: Address,
    shareAmountLamports: Decimal
  ): Promise<Instruction> {
    const globalConfig = await getKvaultGlobalConfigPda(this._kaminoVaultProgramId);
    const eventAuthority = await getEventAuthorityPda(this._kaminoVaultProgramId);
    const withdrawFromAvailableAccounts: WithdrawFromAvailableAccounts = {
      user,
      vaultState: vault.address,
      globalConfig: globalConfig,
      tokenVault: vaultState.tokenVault,
      baseVaultAuthority: vaultState.baseVaultAuthority,
      userTokenAta,
      tokenMint: vaultState.tokenMint,
      userSharesAta,
      sharesMint: vaultState.sharesMint,
      tokenProgram: vaultState.tokenProgram,
      sharesTokenProgram: TOKEN_PROGRAM_ADDRESS,
      klendProgram: this._kaminoLendProgramId,
      eventAuthority,
      program: this._kaminoVaultProgramId,
    };

    const withdrawFromAvailableArgs: WithdrawFromAvailableArgs = {
      sharesAmount: new BN(shareAmountLamports.floor().toString()),
    };

    return withdrawFromAvailable(
      withdrawFromAvailableArgs,
      withdrawFromAvailableAccounts,
      undefined,
      this._kaminoVaultProgramId
    );
  }

  private async withdrawPendingFeesIx(
    authority: TransactionSigner,
    vault: KaminoVault,
    vaultState: VaultState,
    marketAddress: Address,
    reserve: ReserveWithAddress,
    adminTokenAta: Address
  ): Promise<Instruction> {
    const [lendingMarketAuth] = await lendingMarketAuthPda(marketAddress, this._kaminoLendProgramId);

    const withdrawPendingFeesAccounts: WithdrawPendingFeesAccounts = {
      vaultAdminAuthority: authority,
      vaultState: vault.address,
      reserve: reserve.address,
      tokenVault: vaultState.tokenVault,
      ctokenVault: await getCTokenVaultPda(vault.address, reserve.address, this._kaminoVaultProgramId),
      baseVaultAuthority: vaultState.baseVaultAuthority,
      tokenAta: adminTokenAta,
      tokenMint: vaultState.tokenMint,
      tokenProgram: vaultState.tokenProgram,
      /** CPI accounts */
      lendingMarket: marketAddress,
      lendingMarketAuthority: lendingMarketAuth,
      reserveLiquiditySupply: reserve.state.liquidity.supplyVault,
      reserveCollateralMint: reserve.state.collateral.mintPubkey,
      klendProgram: this._kaminoLendProgramId,
      instructionSysvarAccount: SYSVAR_INSTRUCTIONS_ADDRESS,
      reserveCollateralTokenProgram: TOKEN_PROGRAM_ADDRESS,
    };

    let withdrawPendingFeesIxn = withdrawPendingFees(
      withdrawPendingFeesAccounts,
      undefined,
      this._kaminoVaultProgramId
    );

    const vaultReserves = this.getVaultReserves(vaultState);
    const vaultReservesState = await this.loadVaultReserves(vaultState);
    withdrawPendingFeesIxn = this.appendRemainingAccountsForVaultReserves(
      withdrawPendingFeesIxn,
      vaultReserves,
      vaultReservesState
    );

    return withdrawPendingFeesIxn;
  }

  /**
   * Sync a vault for lookup table; create and set the LUT for the vault if needed and fill it with all the needed accounts
   * @param authority - vault admin
   * @param vault the vault to sync and set the LUT for if needed
   * @param [vaultReservesMap] - optional parameter; a hashmap from each reserve pubkey to the reserve state. Optional. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @param [slot] - optional slot to use for lookup table creation; if not provided, the latest confirmed slot will be fetched
   * @returns a struct that contains a list of ix to create the LUT and assign it to the vault if needed + a list of ixs to insert all the accounts in the LUT
   */
  async syncVaultLookupTableIxs(
    authority: TransactionSigner,
    vault: KaminoVault,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    slot?: Slot
  ): Promise<SyncVaultLUTIxs> {
    const vaultState = await vault.getState();
    const allAccountsToBeInserted = [
      vault.address,
      vaultState.vaultAdminAuthority,
      vaultState.baseVaultAuthority,
      vaultState.tokenMint,
      vaultState.tokenVault,
      vaultState.sharesMint,
      vaultState.tokenProgram,
      this._kaminoLendProgramId,
    ];

    vaultState.vaultAllocationStrategy.forEach((allocation) => {
      allAccountsToBeInserted.push(allocation.reserve);
      allAccountsToBeInserted.push(allocation.ctokenVault);
    });

    if (vaultReservesMap) {
      vaultReservesMap.forEach((reserve) => {
        allAccountsToBeInserted.push(reserve.state.lendingMarket);
        allAccountsToBeInserted.push(reserve.state.farmCollateral);
        allAccountsToBeInserted.push(reserve.state.farmDebt);
        allAccountsToBeInserted.push(reserve.state.liquidity.supplyVault);
        allAccountsToBeInserted.push(reserve.state.liquidity.feeVault);
        allAccountsToBeInserted.push(reserve.state.collateral.mintPubkey);
        allAccountsToBeInserted.push(reserve.state.collateral.supplyVault);
      });
    } else {
      const vaultReservesState = await this.loadVaultReserves(vaultState);
      vaultReservesState.forEach((reserve) => {
        allAccountsToBeInserted.push(reserve.state.lendingMarket);
        allAccountsToBeInserted.push(reserve.state.farmCollateral);
        allAccountsToBeInserted.push(reserve.state.farmDebt);
        allAccountsToBeInserted.push(reserve.state.liquidity.supplyVault);
        allAccountsToBeInserted.push(reserve.state.liquidity.feeVault);
        allAccountsToBeInserted.push(reserve.state.collateral.mintPubkey);
        allAccountsToBeInserted.push(reserve.state.collateral.supplyVault);
      });
    }

    if (vaultState.vaultFarm !== DEFAULT_PUBLIC_KEY) {
      allAccountsToBeInserted.push(vaultState.vaultFarm);
    }

    const setupLUTIfNeededIxs: Instruction[] = [];
    let lut = vaultState.vaultLookupTable;
    if (lut === DEFAULT_PUBLIC_KEY) {
      const recentSlot = slot ?? (await this.getConnection().getSlot({ commitment: 'confirmed' }).send());
      const [ix, address] = await initLookupTableIx(authority, recentSlot);
      setupLUTIfNeededIxs.push(ix);
      lut = address;

      // set the new LUT for the vault
      const updateVaultConfigIxs = await this.updateVaultConfigIxs(
        vault,
        new VaultConfigField.LookupTable(),
        lut.toString()
      );
      setupLUTIfNeededIxs.push(updateVaultConfigIxs.updateVaultConfigIx);
    }

    const ixs: Instruction[] = [];
    let overriddenExistentAccounts: Address[] | undefined = undefined;
    if (vaultState.vaultLookupTable === DEFAULT_PUBLIC_KEY) {
      overriddenExistentAccounts = [];
    }
    ixs.push(
      ...(await insertIntoLookupTableIxs(
        this.getConnection(),
        authority,
        lut,
        allAccountsToBeInserted,
        overriddenExistentAccounts
      ))
    );

    return {
      setupLUTIfNeededIxs,
      syncLUTIxs: ixs,
    };
  }

  private getReserveAccountsToInsertInLut(reserveState: Reserve): Address[] {
    return [
      reserveState.lendingMarket,
      reserveState.farmCollateral,
      reserveState.farmDebt,
      reserveState.liquidity.mintPubkey,
      reserveState.liquidity.supplyVault,
      reserveState.liquidity.feeVault,
      reserveState.collateral.mintPubkey,
      reserveState.collateral.supplyVault,
    ];
  }

  /** Read the total holdings of a vault and the reserve weights and returns a map from each reserve to how many tokens should be deposited.
   * @param vaultState - the vault state to calculate the allocation for
   * @param [slot] - the slot for which to calculate the allocation. Optional. If not provided the function will fetch the current slot
   * @param [vaultReserves] - a hashmap from each reserve pubkey to the reserve state. Optional. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @param [currentSlot] - the latest confirmed slot. Optional. If provided the function will be  faster as it will not have to fetch the latest slot
   * @returns - a map from each reserve to how many tokens should be invested into
   */
  async getVaultComputedReservesAllocation(
    vaultState: VaultState,
    slot?: Slot,
    vaultReserves?: Map<Address, KaminoReserve>,
    currentSlot?: Slot
  ): Promise<VaultComputedAllocation> {
    // 1. Read the states
    const holdings = await this.getVaultHoldings(vaultState, slot, vaultReserves, currentSlot);
    const tokenMintDecimals = vaultState.tokenMintDecimals.toNumber();

    // if there are no vault reserves or all have weight 0 everything has to be in Available
    const allReservesPubkeys = this.getVaultReserves(vaultState);
    const reservesAllocations = this.getVaultAllocations(vaultState);
    const allReservesHaveWeight0 = allReservesPubkeys.every((reserve) => {
      const allocation = reservesAllocations.get(reserve);
      return allocation?.targetWeight.isZero();
    });
    if (allReservesPubkeys.length === 0 || allReservesHaveWeight0) {
      const computedHoldings = new Map<Address, Decimal>();
      allReservesPubkeys.forEach((reserve) => {
        computedHoldings.set(reserve, new Decimal(0));
      });
      return {
        targetUnallocatedAmount: holdings.totalAUMIncludingFees.sub(holdings.pendingFees),
        targetReservesAllocation: computedHoldings,
      };
    }

    const initialVaultAllocations = new Map<Address, ReserveAllocationOverview>();
    reservesAllocations.forEach((allocation, reserve) => {
      initialVaultAllocations.set(reserve, {
        targetWeight: allocation.targetWeight,
        tokenAllocationCap: lamportsToDecimal(allocation.tokenAllocationCap, tokenMintDecimals),
        ctokenAllocation: allocation.ctokenAllocation,
      });
    });

    // 2. Compute the allocation
    return this.computeReservesAllocation(
      holdings.totalAUMIncludingFees.sub(holdings.pendingFees),
      new Decimal(vaultState.unallocatedWeight.toString()),
      lamportsToDecimal(new Decimal(vaultState.unallocatedTokensCap.toString()), tokenMintDecimals),
      initialVaultAllocations,
      tokenMintDecimals
    );
  }

  private computeReservesAllocation(
    vaultAUM: Decimal,
    vaultUnallocatedWeight: Decimal,
    vaultUnallocatedCap: Decimal,
    initialVaultAllocations: Map<Address, ReserveAllocationOverview>,
    vaultTokenDecimals: number
  ) {
    return computeReservesAllocation(
      vaultAUM,
      vaultUnallocatedWeight,
      vaultUnallocatedCap,
      initialVaultAllocations,
      vaultTokenDecimals
    );
  }

  /**
   * This method returns the user shares balance for a given vault
   * @param user - user to calculate the shares balance for
   * @param vault - vault to calculate shares balance for
   * @returns - user share balance in tokens (not lamports)
   */
  async getUserSharesBalanceSingleVault(user: Address, vault: KaminoVault): Promise<UserSharesForVault> {
    const vaultState = await vault.getState();

    const userShares: UserSharesForVault = {
      unstakedShares: new Decimal(0),
      stakedShares: new Decimal(0),
      totalShares: new Decimal(0),
    };

    const userSharesTokenAccounts = await getAllStandardTokenProgramTokenAccounts(this.getConnection(), user);

    const userSharesTokenAccount = userSharesTokenAccounts.filter((tokenAccount) => {
      const accountData = tokenAccount.account.data;
      const mint = getTokenAccountMint(accountData);
      return mint === vaultState.sharesMint;
    });
    userShares.unstakedShares = userSharesTokenAccount.reduce((acc, tokenAccount) => {
      const accountData = tokenAccount.account.data;
      const amount = getTokenAccountAmount(accountData);
      if (amount !== null) {
        return acc.add(new Decimal(amount));
      }
      return acc;
    }, new Decimal(0));

    if (await vault.hasFarm()) {
      const userSharesInFarm = await getUserSharesInTokensStakedInFarm(
        this.getConnection(),
        user,
        vaultState.vaultFarm,
        vaultState.sharesMintDecimals.toNumber()
      );

      userShares.stakedShares = userSharesInFarm;
    }

    userShares.totalShares = userShares.unstakedShares.add(userShares.stakedShares);
    return userShares;
  }

  /**
   * This method returns the user shares balance for all existing vaults
   * @param user - user to calculate the shares balance for
   * @param [vaultsOverride] - the kamino vaults if already fetched, in order to reduce rpc calls.Optional
   * @returns - hash map with keys as vault address and value as user share balance in decimal (not lamports)
   */
  async getUserSharesBalanceAllVaults(
    user: Address,
    vaultsOverride?: Array<KaminoVault>
  ): Promise<Map<Address, UserSharesForVault>> {
    const vaults = vaultsOverride ? vaultsOverride : await this.getAllVaults();

    // read all user shares stake in vault farms
    const farmClient = new Farms(this.getConnection(), this._farmsProgramId);
    const allUserFarmStates = await farmClient.getAllUserStatesForUser(user);
    const allUserFarmStatesMap = new Map<Address, UserState>();
    allUserFarmStates.forEach((userFarmState) => {
      allUserFarmStatesMap.set(userFarmState.userState.farmState, userFarmState.userState);
    });
    // stores vault address for each userSharesAta
    const vaultUserShareBalance = new Map<Address, UserSharesForVault>();

    const allUserTokenAccounts = await getAllStandardTokenProgramTokenAccounts(this.getConnection(), user);
    const userSharesTokenAccountsPerVault = new Map<
      Address,
      AccountInfoWithPubkey<AccountInfoBase & AccountInfoWithJsonData>[]
    >();
    vaults.forEach(async (vault) => {
      const state = vault.state;
      if (!state) {
        throw new Error(`Vault ${vault.address} not fetched`);
      }

      const userSharesTokenAccounts = allUserTokenAccounts.filter((tokenAccount) => {
        const accountData = tokenAccount.account.data;
        const mint = getTokenAccountMint(accountData);
        return mint === state.sharesMint;
      });
      userSharesTokenAccountsPerVault.set(vault.address, userSharesTokenAccounts);

      if (await vault.hasFarm()) {
        const userFarmState = allUserFarmStatesMap.get(state.vaultFarm);
        if (userFarmState) {
          const stakedShares = getSharesInFarmUserPosition(userFarmState, state.sharesMintDecimals.toNumber());
          const userSharesBalance = vaultUserShareBalance.get(vault.address);
          if (userSharesBalance) {
            userSharesBalance.stakedShares = stakedShares;
            userSharesBalance.totalShares = userSharesBalance.unstakedShares.add(userSharesBalance.stakedShares);
            vaultUserShareBalance.set(vault.address, userSharesBalance);
          } else {
            vaultUserShareBalance.set(vault.address, {
              unstakedShares: new Decimal(0),
              stakedShares,
              totalShares: stakedShares,
            });
          }
        }
      }
    });

    userSharesTokenAccountsPerVault.forEach((userSharesTokenAccounts, vaultAddress) => {
      userSharesTokenAccounts.forEach((userSharesTokenAccount) => {
        let userSharesForVault = vaultUserShareBalance.get(vaultAddress);
        if (!userSharesForVault) {
          userSharesForVault = {
            unstakedShares: new Decimal(0),
            stakedShares: new Decimal(0),
            totalShares: new Decimal(0),
          };
        }

        if (!userSharesTokenAccount) {
          vaultUserShareBalance.set(vaultAddress, userSharesForVault);
        } else {
          const accountData = userSharesTokenAccount.account.data;
          const amount = getTokenAccountAmount(accountData);
          if (amount !== null) {
            userSharesForVault.unstakedShares = new Decimal(amount);
            userSharesForVault.totalShares = userSharesForVault.unstakedShares.add(userSharesForVault.stakedShares);
            vaultUserShareBalance.set(vaultAddress, userSharesForVault);
          }
        }
      });
    });

    return vaultUserShareBalance;
  }

  /**
   * This method returns the management and performance fee percentages
   * @param vaultState - vault to retrieve the fees percentages from
   * @returns - VaultFeesPct containing management and performance fee percentages
   */
  getVaultFeesPct(vaultState: VaultState): VaultFeesPct {
    return {
      managementFeePct: bpsToPct(new Decimal(vaultState.managementFeeBps.toString())),
      performanceFeePct: bpsToPct(new Decimal(vaultState.performanceFeeBps.toString())),
    };
  }

  /**
   * This method calculates the token per share value. This will always change based on interest earned from the vault, but calculating it requires a bunch of rpc requests. Caching this for a short duration would be optimal
   * @param vaultState - vault state to calculate tokensPerShare for
   * @param [slot] - the slot at which we retrieve the tokens per share. Optional. If not provided, the function will fetch the current slot
   * @param [vaultReservesMap] - hashmap from each reserve pubkey to the reserve state. Optional. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @param [currentSlot] - the latest confirmed slot. Optional. If provided the function will be  faster as it will not have to fetch the latest slot
   * @returns - token per share value
   */
  async getTokensPerShareSingleVault(
    vaultOrState: KaminoVault | VaultState,
    slot?: Slot,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    currentSlot?: Slot
  ): Promise<Decimal> {
    // Determine if we have a KaminoVault or VaultState
    const vaultState = 'getState' in vaultOrState ? await vaultOrState.getState() : vaultOrState;

    if (vaultState.sharesIssued.isZero()) {
      return new Decimal(0);
    }

    const vaultReservesState = vaultReservesMap ? vaultReservesMap : await this.loadVaultReserves(vaultState);

    const sharesDecimal = lamportsToDecimal(
      vaultState.sharesIssued.toString(),
      vaultState.sharesMintDecimals.toString()
    );

    const holdings = await this.getVaultHoldings(vaultState, slot, vaultReservesState, currentSlot);
    const netAUM = holdings.totalAUMIncludingFees.sub(holdings.pendingFees);

    return netAUM.div(sharesDecimal);
  }

  /**
   * This method calculates the token per share value. This will always change based on interest earned from the vault, but calculating it requires a bunch of rpc requests. Caching this for a short duration would be optimal
   * @param [vaultsOverride] - a list of vaults to get the tokens per share for; if provided with state it will not fetch the state again. Optional
   * @param [vaultReservesMap] - optional parameter; a hashmap from each reserve pubkey to the reserve state. Optional. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @param slot - current slot, used to estimate the interest earned in the different reserves with allocation from the vault
   * @returns - token per share value
   */
  async getTokensPerShareAllVaults(
    slot: Slot,
    vaultsOverride?: Array<KaminoVault>,
    vaultReservesMap?: Map<Address, KaminoReserve>
  ): Promise<Map<Address, Decimal>> {
    const vaults = vaultsOverride ? vaultsOverride : await this.getAllVaults();
    const vaultTokensPerShare = new Map<Address, Decimal>();
    for (const vault of vaults) {
      const tokensPerShare = await this.getTokensPerShareSingleVault(vault, slot, vaultReservesMap);
      vaultTokensPerShare.set(vault.address, tokensPerShare);
    }

    return vaultTokensPerShare;
  }

  /**
   * Get all vaults
   * @returns an array of all vaults
   */
  async getAllVaults(): Promise<KaminoVault[]> {
    const filters: (GetProgramAccountsDatasizeFilter | GetProgramAccountsMemcmpFilter)[] = [
      {
        dataSize: BigInt(VaultState.layout.span + 8),
      },
      {
        memcmp: {
          offset: 0n,
          bytes: base58Decoder.decode(VaultState.discriminator) as Base58EncodedBytes,
          encoding: 'base58',
        },
      },
    ];

    return await this.getAllVaultsWithFilter(filters);
  }

  /**
   * Get all vaults for a given token
   * @param token - the token to get all vaults for
   * @returns an array of all vaults for the given token
   */
  async getAllVaultsForToken(token: Address): Promise<Array<KaminoVault>> {
    const filters: (GetProgramAccountsDatasizeFilter | GetProgramAccountsMemcmpFilter)[] = [
      {
        dataSize: BigInt(VaultState.layout.span + 8),
      },
      {
        memcmp: {
          offset: 0n,
          bytes: base58Decoder.decode(VaultState.discriminator) as Base58EncodedBytes,
          encoding: 'base58',
        },
      },
      {
        memcmp: {
          offset: 80n, // tokenMint offset: 8 + 32 + 32 + 8 (discriminator + vaultAdminAuthority + baseVaultAuthority + baseVaultAuthorityBump)
          bytes: token.toString() as Base58EncodedBytes,
          encoding: 'base58',
        },
      },
    ];

    return await this.getAllVaultsWithFilter(filters);
  }

  private async getAllVaultsWithFilter(
    filters: (GetProgramAccountsDatasizeFilter | GetProgramAccountsMemcmpFilter)[]
  ): Promise<Array<KaminoVault>> {
    const kaminoVaults: Array<Account<Buffer>> = await getProgramAccounts(
      this.getConnection(),
      this._kaminoVaultProgramId,
      VaultState.layout.span + 8,
      filters
    );

    return kaminoVaults.map((kaminoVault) => {
      const kaminoVaultAccount = decodeVaultState(kaminoVault.data);
      if (!kaminoVaultAccount) {
        throw Error(`kaminoVault with pubkey ${kaminoVault.address} could not be decoded`);
      }

      return KaminoVault.loadWithClientAndState(this, kaminoVault.address, kaminoVaultAccount);
    });
  }

  /**
   * Get a list of kaminoVaults
   * @param vaults - a list of vaults to get the states for; if not provided, all vaults will be fetched
   * @returns a list of vaults
   */
  async getVaults(vaults?: Array<Address>): Promise<Array<KaminoVault | null>> {
    if (!vaults) {
      vaults = (await this.getAllVaults()).map((x) => x.address);
    }
    const vaultStates = await batchFetch(vaults, (chunk) => this.getVaultsStates(chunk));
    return vaults.map((vault, index) => {
      const state = vaultStates[index];
      return state ? KaminoVault.loadWithClientAndState(this, vault, state) : null;
    });
  }

  /**
   * This will return all the initialized whitelisted reserves accounts, including those that are not whitelisted but just have the PDA initialized
   * @returns a map from mint to the whitelisted reserves for that mint
   */
  async getAllWhitelistedReserves(): Promise<Map<Address, ReserveWhitelistEntry[]>> {
    const whitelistedReserves = await getProgramAccounts(
      this.getConnection(),
      this._kaminoVaultProgramId,
      ReserveWhitelistEntry.layout.span + 8,
      [
        {
          dataSize: BigInt(ReserveWhitelistEntry.layout.span + 8),
        },
        {
          memcmp: {
            offset: 0n,
            bytes: base58Decoder.decode(ReserveWhitelistEntry.discriminator) as Base58EncodedBytes,
            encoding: 'base58',
          },
        },
      ]
    );

    // todo: after release when the account structure is updated optimize the implementation by reading directly the mint from whitelisted account
    const whitelistedReservesMap: Map<Address, ReserveWhitelistEntry> = new Map();
    const reservesSet: Set<Address> = new Set();
    for (const whitelistedReserve of whitelistedReserves) {
      const decodedAcc = decodeReserveWhitelistEntry(whitelistedReserve.data);
      whitelistedReservesMap.set(decodedAcc.reserve, decodedAcc);
      reservesSet.add(decodedAcc.reserve);
    }

    const reservesList: Address[] = Array.from(reservesSet);
    const reservesState = await Reserve.fetchMultiple(this.getConnection(), reservesList, this._kaminoLendProgramId);

    const mintToWhitelistedReservesMap: Map<Address, ReserveWhitelistEntry[]> = new Map();
    const reservesWithState = reservesList.map((reserve, index) => [reserve, reservesState[index]] as const);
    for (const [reserve, reserveState] of reservesWithState) {
      if (!reserveState) {
        continue;
      }
      const mintPubkey = reserveState.liquidity.mintPubkey;
      if (!mintToWhitelistedReservesMap.has(mintPubkey)) {
        mintToWhitelistedReservesMap.set(mintPubkey, []);
      }
      mintToWhitelistedReservesMap.get(mintPubkey)!.push(whitelistedReservesMap.get(reserve)!);
    }

    return mintToWhitelistedReservesMap;
  }

  /**
   * This will return all the whitelisted reserves for the given mint; if a ReserveWhitelistEntry exists it doesn't mean it is whitelisted, the fields of the struct has to be read;
   * If multiple mints are needed it is recommended to call getAllWhitelistedReserves instead;
   * @param mint - the mint to get the whitelisted reserves for
   * @returns a list of whitelisted reserves
   */
  async getAllWhitelistedReservesForMint(mint: Address): Promise<ReserveWhitelistEntry[]> {
    // todo: use the impl below once the account structure is updated
    // const whitelistedReserves = await getProgramAccounts(
    //   this.getConnection(),
    //   this._kaminoVaultProgramId,
    //   ReserveWhitelistEntry.layout.span + 8,
    //   [
    //     {
    //       dataSize: BigInt(ReserveWhitelistEntry.layout.span + 8),
    //     },
    //     {
    //       memcmp: {
    //         offset: 0n,
    //         bytes: base58Decoder.decode(ReserveWhitelistEntry.discriminator) as Base58EncodedBytes,
    //         encoding: 'base58',
    //       },
    //     },
    //     {
    //       memcmp: {
    //         offset: 8n, // tokenMint offset: 8 discriminator
    //         bytes: mint.toString() as Base58EncodedBytes,
    //         encoding: 'base58',
    //       },
    //     },
    //   ]
    // );

    // return whitelistedReserves.map((whitelistedReserve) => decodeReserveWhitelistEntry(whitelistedReserve.data));

    const whitelistedReserves = await this.getAllWhitelistedReserves();
    return whitelistedReserves.get(mint) || [];
  }

  /**
   * This will return all the whitelisted reserves for the given markets
   * @param markets - the markets to get the whitelisted reserves for; if not provided, no whitelisted reserves will be fetched; for getting all whitelisted reserves use getAllWhitelistedReserves
   * @returns a map from market address to a map from reserve address to the whitelisting status
   */
  async getAllWhitelistedReservesForMarkets(
    markets?: KaminoMarket[]
  ): Promise<Map<Address, Map<Address, ReserveWhitelistEntry>>> {
    const whitelistedReservesMap: Map<Address, Map<Address, ReserveWhitelistEntry>> = new Map();
    if (!markets || markets.length === 0) {
      return whitelistedReservesMap;
    }

    // Aggregate all active reserves from provided markets
    const allReserves: KaminoReserve[] = [];
    for (const market of markets) {
      if (market.reservesActive) {
        for (const reserve of market.reservesActive.values()) {
          allReserves.push(reserve);
        }
      }
    }

    const whitelistMap = await this.fetchReservesWhitelistEntries(allReserves);

    // Group by market
    for (const reserve of allReserves) {
      const entry = whitelistMap.get(reserve.address)!;
      if (!whitelistedReservesMap.has(reserve.state.lendingMarket)) {
        whitelistedReservesMap.set(reserve.state.lendingMarket, new Map());
      }
      whitelistedReservesMap.get(reserve.state.lendingMarket)!.set(reserve.address, entry);
    }

    return whitelistedReservesMap;
  }

  /**
   * This will return the whitelisting status for the given reserves
   * @param reserves - the reserves to get the whitelisting status for
   * @returns a map from reserve address to the whitelisting status
   */
  async getReservesWhitelistingStatus(reserves: KaminoReserve[]): Promise<Map<Address, ReserveWhitelistEntry>> {
    return this.fetchReservesWhitelistEntries(reserves);
  }

  /**
   * Fetches the on-chain ReserveWhitelistEntry for each reserve. If the account does not exist,
   * a default entry with whitelistAddAllocation=0 and whitelistInvest=0 is used.
   * @param reserves - the reserves to fetch whitelist entries for
   * @returns a map from reserve address to ReserveWhitelistEntry
   */
  private async fetchReservesWhitelistEntries(reserves: KaminoReserve[]): Promise<Map<Address, ReserveWhitelistEntry>> {
    const whitelistMap = new Map<Address, ReserveWhitelistEntry>();
    if (!reserves || reserves.length === 0) {
      return whitelistMap;
    }

    const allReservesWhitelistPDAs = await getReservesWhitelistPDAs(
      reserves.map((reserve) => reserve.address),
      this._kaminoVaultProgramId
    );

    const accountsArrays = await batchFetch(allReservesWhitelistPDAs, async (chunk) => {
      const response = await this.getConnection().getMultipleAccounts(chunk, { commitment: 'processed' }).send();
      return response.value;
    });

    const allWhitelistEntriesAccounts = accountsArrays.flat();

    for (let i = 0; i < reserves.length; i++) {
      const reserve = reserves[i];
      const accountInfo = allWhitelistEntriesAccounts[i];
      let entry: ReserveWhitelistEntry = new ReserveWhitelistEntry({
        tokenMint: reserve.state.liquidity.mintPubkey,
        reserve: reserve.address,
        whitelistAddAllocation: 0,
        whitelistInvest: 0,
        padding: [],
      });
      if (accountInfo) {
        entry = decodeReserveWhitelistEntry(Buffer.from(accountInfo.data[0], 'base64'));
      }
      whitelistMap.set(reserve.address, entry);
    }

    return whitelistMap;
  }

  /**
   * This will return a map from each vault to the reserves that are not fully whitelisted (allocation + invest) but are part of the vault allocation.
   * Duplicate vaults (by address) are deduplicated.
   * @param vaults - the vaults to get the not whitelisted reserves in allocation for
   * @returns a map from vault address to the list of reserve addresses that are not fully whitelisted
   */
  async getReservesNotWhitelistedInAllocations(vaults: KaminoVault[]): Promise<Map<Address, Address[]>> {
    const result = new Map<Address, Address[]>();
    if (!vaults || vaults.length === 0) {
      return result;
    }

    const dedupedVaults = deduplicateVaults(vaults);
    const { vaultAllocations, whitelistMap } = await this.fetchVaultsAllocationsAndWhitelistStatus(dedupedVaults);

    for (const vault of dedupedVaults) {
      const notWhitelisted: Address[] = [];
      const reservesInAlloc = vaultAllocations.get(vault.address)!;
      for (const reserve of reservesInAlloc.keys()) {
        if (
          whitelistMap.get(reserve)?.whitelistAddAllocation === 0 ||
          whitelistMap.get(reserve)?.whitelistInvest === 0
        ) {
          notWhitelisted.push(reserve);
        }
      }
      result.set(vault.address, notWhitelisted);
    }

    return result;
  }

  /**
   * This will return a map from each vault to the reserves that are not matching the vault whitelisting requirements (allocation and invest) but are part of the vault allocation.
   * Duplicate vaults (by address) are deduplicated.
   * @param vaults - the vaults to get the not whitelisted reserves in allocation for
   * @returns a map from each vault to the reserves that are not whitelisted as requested (allocation + invest) and their whitelisting status
   */
  async getReservesAllocationsNotMatchingVaultWhitelistingRequirements(
    vaults: KaminoVault[]
  ): Promise<Map<Address, Map<Address, ReserveWhitelistEntry>>> {
    const result = new Map<Address, Map<Address, ReserveWhitelistEntry>>();
    if (!vaults || vaults.length === 0) {
      return result;
    }

    const dedupedVaults = deduplicateVaults(vaults);
    const { vaultAllocations, whitelistMap } = await this.fetchVaultsAllocationsAndWhitelistStatus(dedupedVaults);

    for (const vault of dedupedVaults) {
      result.set(vault.address, new Map());

      const vaultState = await vault.getState();
      const vaultRequiresAllocationWhitelisted = vaultState.allowAllocationsInWhitelistedReservesOnly === 1;
      const vaultRequiresInvestWhitelisted = vaultState.allowInvestInWhitelistedReservesOnly === 1;
      if (!vaultRequiresAllocationWhitelisted && !vaultRequiresInvestWhitelisted) {
        continue;
      }

      const reservesInAlloc = vaultAllocations.get(vault.address)!;
      for (const reserve of reservesInAlloc.keys()) {
        const whitelistEntry = whitelistMap.get(reserve)!;
        const allocationWhitelistedNotMet =
          vaultRequiresAllocationWhitelisted && whitelistEntry.whitelistAddAllocation === 0;
        const investWhitelistedNotMet = vaultRequiresInvestWhitelisted && whitelistEntry.whitelistInvest === 0;
        if (allocationWhitelistedNotMet || investWhitelistedNotMet) {
          result.get(vault.address)!.set(reserve, whitelistEntry);
        }
      }
    }

    return result;
  }

  /**
   * Collects all reserve addresses across vault allocations, initializes their KaminoReserve state,
   * and fetches whitelist entries for all of them. Also caches the per-vault allocation maps to avoid
   * redundant calls.
   * @param vaults - the vaults to collect allocations from
   * @returns the per-vault allocation maps and a global reserve-to-whitelist-entry map
   */
  private async fetchVaultsAllocationsAndWhitelistStatus(vaults: KaminoVault[]): Promise<{
    vaultAllocations: Map<Address, Map<Address, ReserveAllocationOverview>>;
    whitelistMap: Map<Address, ReserveWhitelistEntry>;
  }> {
    const vaultAllocations = new Map<Address, Map<Address, ReserveAllocationOverview>>();
    const allReserveAddresses = new Set<Address>();

    // load all vault states in parallel so vault.getVaultAllocations() below won't do any additional rpc calls
    Promise.all(
      vaults.map(async (vault) => {
        vault.getState();
      })
    );
    for (const vault of vaults) {
      const allocations = await vault.getVaultAllocations();
      vaultAllocations.set(vault.address, allocations);
      for (const reserve of allocations.keys()) {
        allReserveAddresses.add(reserve);
      }
    }

    const reservesAddressList = Array.from(allReserveAddresses);
    const reserves = await Promise.all(
      reservesAddressList.map((reserve) =>
        KaminoReserve.initializeFromAddress(reserve, this.getConnection(), this.recentSlotDurationMs)
      )
    );
    const whitelistMap = await this.fetchReservesWhitelistEntries(reserves);

    return { vaultAllocations, whitelistMap };
  }

  private async getVaultsStates(vaults: Address[]): Promise<Array<VaultState | null>> {
    return await VaultState.fetchMultiple(this.getConnection(), vaults, this._kaminoVaultProgramId);
  }

  /**
   * This will return the amount of token invested from the vault into the given reserve
   * @param vaultState - the kamino vault to get invested amount in reserve for
   * @param slot - current slot
   * @param reserve - the reserve state to get vault invested amount in
   * @returns vault amount supplied in reserve in decimal
   */
  getSuppliedInReserve(vaultState: VaultState, slot: Slot, reserve: KaminoReserve): Decimal {
    let referralFeeBps = 0;
    const denominator = reserve.state.config.protocolTakeRatePct / 100;
    if (denominator > 0) {
      referralFeeBps = new Fraction(reserve.state.liquidity.absoluteReferralRateSf)
        .toDecimal()
        .div(denominator)
        .floor()
        .toNumber();
    }
    const reserveCollExchangeRate = reserve.getEstimatedCollateralExchangeRate(slot, referralFeeBps);

    const reserveAllocation = vaultState.vaultAllocationStrategy.find(
      (allocation) => allocation.reserve === reserve.address
    );
    if (!reserveAllocation) {
      throw new Error(`Reserve ${reserve.address} not found in vault allocation strategy`);
    }

    const reserveAllocationLiquidityAmountLamports = new Decimal(reserveAllocation.ctokenAllocation.toString()).div(
      reserveCollExchangeRate
    );
    const reserveAllocationLiquidityAmount = lamportsToDecimal(
      reserveAllocationLiquidityAmountLamports,
      vaultState.tokenMintDecimals.toNumber()
    );
    return reserveAllocationLiquidityAmount;
  }

  /**
   * This will return the a map between reserve pubkey and the pct of the vault invested amount in each reserve
   * @param vaultState - the kamino vault to get reserves distribution for
   * @returns a map between reserve pubkey and the allocation pct for the reserve
   */
  getAllocationsDistribuionPct(vaultState: VaultState): Map<Address, Decimal> {
    const allocationsDistributionPct = new Map<Address, Decimal>();
    let totalAllocation = new Decimal(0);

    const filteredAllocations = vaultState.vaultAllocationStrategy.filter(
      (allocation) => allocation.reserve !== DEFAULT_PUBLIC_KEY
    );
    filteredAllocations.forEach((allocation) => {
      totalAllocation = totalAllocation.add(new Decimal(allocation.targetAllocationWeight.toString()));
    });

    filteredAllocations.forEach((allocation) => {
      allocationsDistributionPct.set(
        allocation.reserve,
        new Decimal(allocation.targetAllocationWeight.toString()).mul(new Decimal(100)).div(totalAllocation)
      );
    });

    return allocationsDistributionPct;
  }

  /**
   * This will return the a map between reserve pubkey and the allocation overview for the reserve
   * @param vaultState - the kamino vault to get reserves allocation overview for
   * @returns a map between reserve pubkey and the allocation overview for the reserve
   */
  getVaultAllocations(vaultState: VaultState): Map<Address, ReserveAllocationOverview> {
    const vaultAllocations = new Map<Address, ReserveAllocationOverview>();

    vaultState.vaultAllocationStrategy.map((allocation) => {
      if (allocation.reserve === DEFAULT_PUBLIC_KEY) {
        return;
      }

      const allocationOverview: ReserveAllocationOverview = {
        targetWeight: new Decimal(allocation.targetAllocationWeight.toString()),
        tokenAllocationCap: new Decimal(allocation.tokenAllocationCap.toString()),
        ctokenAllocation: new Decimal(allocation.ctokenAllocation.toString()),
      };
      vaultAllocations.set(allocation.reserve, allocationOverview);
    });

    return vaultAllocations;
  }

  /**
   * This will return an unsorted hash map of all reserves that the given vault has allocations for, toghether with the amount that can be withdrawn from each of the reserves
   * @param vault - the kamino vault to get available liquidity to withdraw for
   * @param slot - current slot
   *@param [vaultReservesMap] - a hashmap from each reserve pubkey to the reserve state
   * @returns an HashMap of reserves (key) with the amount available to withdraw for each (value)
   */
  private async getReserveAllocationAvailableLiquidityToWithdraw(
    vault: KaminoVault,
    slot: Slot,
    vaultReservesMap: Map<Address, KaminoReserve>
  ): Promise<Map<Address, Decimal>> {
    const vaultState = await vault.getState();

    const reserveAllocationAvailableLiquidityToWithdraw = new Map<Address, Decimal>();
    vaultState.vaultAllocationStrategy.forEach((allocationStrategy) => {
      if (allocationStrategy.reserve === DEFAULT_PUBLIC_KEY) {
        return;
      }
      const reserve = vaultReservesMap.get(allocationStrategy.reserve);
      if (reserve === undefined) {
        throw new Error(`Reserve ${allocationStrategy.reserve} not found`);
      }
      let referralFeeBps = 0;
      const denominator = reserve.state.config.protocolTakeRatePct / 100;
      if (denominator > 0) {
        referralFeeBps = new Fraction(reserve.state.liquidity.absoluteReferralRateSf)
          .toDecimal()
          .div(denominator)
          .floor()
          .toNumber();
      }
      const reserveCollExchangeRate = reserve.getEstimatedCollateralExchangeRate(slot, referralFeeBps);
      const reserveAllocationLiquidityAmount = new Decimal(allocationStrategy.ctokenAllocation.toString()).div(
        reserveCollExchangeRate
      );
      const reserveAvailableLiquidityAmount = reserve.getLiquidityAvailableAmount();
      reserveAllocationAvailableLiquidityToWithdraw.set(
        allocationStrategy.reserve,
        Decimal.min(reserveAllocationLiquidityAmount, reserveAvailableLiquidityAmount)
      );
    });

    return reserveAllocationAvailableLiquidityToWithdraw;
  }

  /**
   * This will get the list of all reserve pubkeys that the vault has allocations for ex
   * @param vault - the vault state to load reserves for
   * @returns a hashmap from each reserve pubkey to the reserve state
   */
  getVaultReserves(vault: VaultState): Address[] {
    return vault.vaultAllocationStrategy
      .filter((vaultAllocation) => vaultAllocation.reserve !== DEFAULT_PUBLIC_KEY)
      .map((vaultAllocation) => vaultAllocation.reserve);
  }

  /**
   * This will load the onchain state for all the reserves that the vault has allocations for
   * @param vaultState - the vault state to load reserves for
   * @returns a hashmap from each reserve pubkey to the reserve state
   */
  async loadVaultReserves(vaultState: VaultState): Promise<Map<Address, KaminoReserve>> {
    return this.loadVaultsReserves([vaultState]);
  }

  private async loadReserializedReserves(vaultReservesAddresses: Address[]) {
    if (vaultReservesAddresses.length === 0) {
      return [];
    }
    const reserveAccounts = await this.getConnection()
      .getMultipleAccounts(vaultReservesAddresses, { commitment: 'processed' })
      .send();
    return reserveAccounts.value.map((reserve, i) => {
      if (reserve === null) {
        // maybe reuse old here
        throw new Error(`Reserve account ${vaultReservesAddresses[i]} was not found`);
      }
      const reserveAccount = Reserve.decode(Buffer.from(reserve.data[0], 'base64'));
      if (!reserveAccount) {
        throw Error(`Could not parse reserve ${vaultReservesAddresses[i]}`);
      }
      return {
        address: vaultReservesAddresses[i],
        state: reserveAccount,
      };
    });
  }

  /**
   * This will load the onchain state for all the reserves that the vaults have allocations for, deduplicating the reserves
   * @param vaults - the vault states to load reserves for
   * @param oracleAccounts (optional) all reserve oracle accounts, if not supplied will make an additional rpc call to fetch these accounts
   * @returns a hashmap from each reserve pubkey to the reserve state
   */
  async loadVaultsReserves(
    vaults: VaultState[],
    oracleAccounts?: AllOracleAccounts
  ): Promise<Map<Address, KaminoReserve>> {
    const vaultReservesAddressesSet = new Set<Address>(vaults.flatMap((vault) => this.getVaultReserves(vault)));
    const vaultReservesAddresses = [...vaultReservesAddressesSet];
    const deserializedReserves = await batchFetch(vaultReservesAddresses, (chunk) =>
      this.loadReserializedReserves(chunk)
    );
    const [reservesAndOracles, cdnResourcesData] = await Promise.all([
      getTokenOracleData(this.getConnection(), deserializedReserves, oracleAccounts),
      fetchKaminoCdnData(),
    ]);
    const kaminoReserves = new Map<Address, KaminoReserve>();
    reservesAndOracles.forEach(([{ address: reserveAddress, state: reserve }, oracle]) => {
      if (!oracle) {
        throw Error(
          `Could not find oracle for ${parseTokenSymbol(
            reserve.config.tokenInfo.name
          )} (${reserveAddress}) reserve in market ${reserve.lendingMarket}`
        );
      }
      const kaminoReserve = KaminoReserve.initialize(
        reserveAddress,
        reserve,
        oracle,
        this.getConnection(),
        this.recentSlotDurationMs,
        cdnResourcesData
      );
      kaminoReserves.set(kaminoReserve.address, kaminoReserve);
    });

    return kaminoReserves;
  }

  /**
   * This will retrieve all the tokens that can be used as collateral by the users who borrow the token in the vault alongside details about the min and max loan to value ratio
   * @param vaultState - the vault state to load reserves for
   * @param [slot] - the slot for which to retrieve the vault collaterals for. Optional. If not provided the function will fetch the current slot
   * @param [vaultReservesMap] - hashmap from each reserve pubkey to the reserve state. Optional. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @param [kaminoMarkets] - a list of all the kamino markets. Optional. If provided the function will be significantly faster as it will not have to fetch the markets
   * @param oracleAccounts (optional) all reserve oracle accounts, if not supplied will make an additional rpc call to fetch these accounts
   * @returns a hashmap from each reserve pubkey to the market overview of the collaterals that can be used and the min and max loan to value ratio in that market
   */
  async getVaultCollaterals(
    vaultState: VaultState,
    slot: Slot,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    kaminoMarkets?: KaminoMarket[],
    oracleAccounts?: AllOracleAccounts
  ): Promise<Map<Address, MarketOverview>> {
    const vaultReservesStateMap = vaultReservesMap ? vaultReservesMap : await this.loadVaultReserves(vaultState);
    const vaultReservesState: KaminoReserve[] = [];

    const missingReserves = new Set<Address>([]);
    // filter the reserves that are not part of the vault allocation strategy
    vaultState.vaultAllocationStrategy.forEach(async (allocation) => {
      if (allocation.reserve === DEFAULT_PUBLIC_KEY) {
        return;
      }
      const reserve = vaultReservesStateMap.get(allocation.reserve);
      if (!reserve) {
        missingReserves.add(allocation.reserve);
        return;
      }

      vaultReservesState.push(reserve);
    });

    // read missing reserves
    const missingReserveAddresses = [...missingReserves];
    const missingReservesStates = (await Reserve.fetchMultiple(this.getConnection(), missingReserveAddresses))
      .map((reserve, index) => {
        if (!reserve) {
          return null;
        }
        return {
          address: missingReserveAddresses[index],
          state: reserve,
        };
      })
      .filter((state) => state !== null);
    const missingReservesAndOracles = await getTokenOracleData(
      this.getConnection(),
      missingReservesStates,
      oracleAccounts
    );
    missingReservesAndOracles.forEach(([{ address: reserveAddress, state: reserve }, oracle]) => {
      const fetchedReserve = new KaminoReserve(
        reserve,
        reserveAddress,
        oracle!,
        this.getConnection(),
        this.recentSlotDurationMs
      );
      vaultReservesState.push(fetchedReserve);
    });

    const vaultCollateralsPerReserve: Map<Address, MarketOverview> = new Map();

    for (const reserve of vaultReservesState) {
      // try to read the market from the provided list, if it doesn't exist fetch it
      let lendingMarket: KaminoMarket | undefined = undefined;
      if (kaminoMarkets) {
        lendingMarket = kaminoMarkets?.find((market) => reserve.state.lendingMarket === market.address);
      }

      if (!lendingMarket) {
        const fetchedLendingMarket = await KaminoMarket.load(
          this.getConnection(),
          reserve.state.lendingMarket,
          DEFAULT_RECENT_SLOT_DURATION_MS,
          this._kaminoLendProgramId,
          true,
          this._farmsProgramId
        );
        if (!fetchedLendingMarket) {
          throw Error(`Could not fetch lending market ${reserve.state.lendingMarket}`);
        }
        lendingMarket = fetchedLendingMarket;
      }

      const marketReserves = lendingMarket.getReserves();
      const marketOverview: MarketOverview = {
        address: reserve.state.lendingMarket,
        reservesAsCollateral: [],
        minLTVPct: new Decimal(0),
        maxLTVPct: new Decimal(100),
      };

      marketReserves
        .filter((marketReserve) => {
          return (
            marketReserve.state.config.liquidationThresholdPct > 0 &&
            marketReserve.address !== reserve.address &&
            marketReserve.state.config.status === 0
          );
        })
        .map((filteredReserve) => {
          const reserveAsCollateral: ReserveAsCollateral = {
            mint: filteredReserve.getLiquidityMint(),
            address: filteredReserve.address,
            liquidationLTVPct: new Decimal(filteredReserve.state.config.liquidationThresholdPct),
          };
          marketOverview.reservesAsCollateral.push(reserveAsCollateral);
          if (reserveAsCollateral.liquidationLTVPct.lt(marketOverview.minLTVPct) || marketOverview.minLTVPct.eq(0)) {
            marketOverview.minLTVPct = reserveAsCollateral.liquidationLTVPct;
          }
          if (reserveAsCollateral.liquidationLTVPct.gt(marketOverview.maxLTVPct) || marketOverview.maxLTVPct.eq(0)) {
            marketOverview.maxLTVPct = reserveAsCollateral.liquidationLTVPct;
          }
        });

      vaultCollateralsPerReserve.set(reserve.address, marketOverview);
    }

    return vaultCollateralsPerReserve;
  }

  /**
   * This will return an VaultHoldings object which contains the amount available (uninvested) in vault, total amount invested in reseves and a breakdown of the amount invested in each reserve
   * @param vault - the kamino vault to get available liquidity to withdraw for
   * @param [slot] - the slot for which to calculate the holdings. Optional. If not provided the function will fetch the current slot
   * @param [vaultReserves] - a hashmap from each reserve pubkey to the reserve state. Optional. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @param [currentSlot] - the latest confirmed slot. Optional. If provided the function will be  faster as it will not have to fetch the latest slot
   * @returns an VaultHoldings object representing the amount available (uninvested) in vault, total amount invested in reseves and a breakdown of the amount invested in each reserve
   */
  async getVaultHoldings(
    vault: VaultState,
    slot?: Slot,
    vaultReserves?: Map<Address, KaminoReserve>,
    currentSlot?: Slot
  ): Promise<VaultHoldings> {
    const vaultHoldings: VaultHoldings = new VaultHoldings({
      available: new Decimal(vault.tokenAvailable.toString()),
      invested: new Decimal(0),
      investedInReserves: new Map<Address, Decimal>(),
      totalAUMIncludingFees: new Decimal(0),
      pendingFees: new Decimal(0),
    });

    const currentSlotToUse = currentSlot ?? (await this.getConnection().getSlot({ commitment: 'confirmed' }).send());
    const vaultReservesState = vaultReserves ? vaultReserves : await this.loadVaultReserves(vault);
    const decimals = new Decimal(vault.tokenMintDecimals.toString());

    vault.vaultAllocationStrategy.forEach((allocationStrategy) => {
      if (allocationStrategy.reserve === DEFAULT_PUBLIC_KEY) {
        return;
      }

      const reserve = vaultReservesState.get(allocationStrategy.reserve);
      if (reserve === undefined) {
        throw new Error(`Reserve ${allocationStrategy.reserve} not found`);
      }

      let reserveCollExchangeRate: Decimal;

      if (slot) {
        reserveCollExchangeRate = reserve.getEstimatedCollateralExchangeRate(slot, 0);
      } else {
        reserveCollExchangeRate = reserve.getCollateralExchangeRate();
      }
      const reserveAllocationLiquidityAmount = new Decimal(allocationStrategy.ctokenAllocation.toString()).div(
        reserveCollExchangeRate
      );

      vaultHoldings.invested = vaultHoldings.invested.add(reserveAllocationLiquidityAmount);
      vaultHoldings.investedInReserves.set(
        allocationStrategy.reserve,
        lamportsToDecimal(reserveAllocationLiquidityAmount, decimals)
      );
    });

    const currentPendingFees = new Fraction(vault.pendingFeesSf).toDecimal();
    let totalPendingFees = currentPendingFees;

    // if there is a slot passed and it is in the future we need to estimate the fees from current time until that moment
    if (slot && slot > currentSlotToUse) {
      const currentTimestampSec = new Date().getTime() / 1000;
      const timeAtPassedSlot =
        currentTimestampSec + Number.parseInt((slot - currentSlotToUse).toString()) * this.recentSlotDurationMs;
      const timeUntilPassedSlot = timeAtPassedSlot - currentTimestampSec;

      const managementFeeFactor = new Decimal(timeUntilPassedSlot)
        .mul(new Decimal(vault.managementFeeBps.toString()))
        .div(new Decimal(SECONDS_PER_YEAR))
        .div(FullBPSDecimal);
      const prevAUM = lamportsToDecimal(new Fraction(vault.prevAumSf).toDecimal(), vault.tokenMintDecimals.toNumber());
      const simulatedMgmtFee = prevAUM.mul(managementFeeFactor);
      totalPendingFees = totalPendingFees.add(simulatedMgmtFee);

      const simulatedEarnedInterest = vaultHoldings.invested
        .add(vaultHoldings.available)
        .sub(prevAUM)
        .sub(simulatedMgmtFee);
      const simulatedPerformanceFee = simulatedEarnedInterest
        .mul(new Decimal(vault.performanceFeeBps.toString()))
        .div(FullBPSDecimal);
      totalPendingFees = totalPendingFees.add(simulatedPerformanceFee);
    }

    const totalAvailableDecimal = lamportsToDecimal(vaultHoldings.available, decimals);
    const totalInvestedDecimal = lamportsToDecimal(vaultHoldings.invested, decimals);
    const pendingFees = lamportsToDecimal(totalPendingFees, decimals);
    return new VaultHoldings({
      available: totalAvailableDecimal,
      invested: totalInvestedDecimal,
      investedInReserves: vaultHoldings.investedInReserves,
      totalAUMIncludingFees: totalAvailableDecimal.add(totalInvestedDecimal),
      pendingFees: pendingFees,
    });
  }

  /**
   * This will return an VaultOverview object that encapsulates all the information about the vault, including the holdings, reserves details, theoretical APY, utilization ratio and total borrowed amount
   * @param vault - the kamino vault to get available liquidity to withdraw for
   * @param price - the price of the token in the vault (e.g. USDC)
   * @param [slot] - the slot for which to retrieve the vault overview for. Optional. If not provided the function will fetch the current slot
   * @param [vaultReservesMap] - hashmap from each reserve pubkey to the reserve state. Optional. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @param [currentSlot] - the latest confirmed slot. Optional. If provided the function will be  faster as it will not have to fetch the latest slot
   * @returns an VaultOverview object with details about the tokens available and invested in the vault, denominated in tokens and USD
   */
  async getVaultHoldingsWithPrice(
    vault: VaultState,
    price: Decimal,
    slot?: Slot,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    currentSlot?: Slot
  ): Promise<VaultHoldingsWithUSDValue> {
    const holdings = await this.getVaultHoldings(vault, slot, vaultReservesMap, currentSlot);

    const investedInReservesUSD = new Map<Address, Decimal>();
    holdings.investedInReserves.forEach((amount, reserve) => {
      investedInReservesUSD.set(reserve, amount.mul(price));
    });
    return {
      holdings: holdings,
      availableUSD: holdings.available.mul(price),
      investedUSD: holdings.invested.mul(price),
      investedInReservesUSD: investedInReservesUSD,
      totalUSDIncludingFees: holdings.totalAUMIncludingFees.mul(price),
      pendingFeesUSD: holdings.pendingFees.mul(price),
    };
  }

  /** Retrieves the maximum instant withdrawable amount for a vault based on the available liquidity in the vault allocations
   * @param vault - the kamino vault to get the maximum instant withdrawable amount for
   * @returns the maximum instant withdrawable amount for the vault
   */
  async getMaxInstantWithdrawableAmount(
    vault: KaminoVault,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    slot?: Slot
  ): Promise<Decimal> {
    const latestSlot = slot ? slot : await this.getConnection().getSlot().send();
    const vaultState = await vault.getState();
    const vaultReservesState = vaultReservesMap ? vaultReservesMap : await this.loadVaultReserves(vaultState);

    let maxWithdrawableAmount = new Decimal(vaultState.tokenAvailable.toString());
    const allocations = this.getVaultAllocations(vaultState);
    for (const [reserveAddress, allocation] of allocations) {
      if (reserveAddress === DEFAULT_PUBLIC_KEY) {
        continue;
      }
      const reserve = vaultReservesState.get(reserveAddress);
      if (reserve === undefined) {
        throw new Error(`Reserve ${reserveAddress} not found`);
      }
      const reserveAvailableLiquidity = reserve.getLiquidityAvailableAmount();
      const investedInReserve = allocation.ctokenAllocation.div(
        reserve.getEstimatedCollateralExchangeRate(latestSlot, 0)
      );
      const instantWithdrawableAmount = Decimal.min(reserveAvailableLiquidity, investedInReserve);
      maxWithdrawableAmount = maxWithdrawableAmount.add(instantWithdrawableAmount);
    }

    return maxWithdrawableAmount;
  }

  /**
   * This will return an VaultOverview object that encapsulates all the information about the vault, including the holdings, reserves details, theoretical APY, utilization ratio and total borrowed amount
   * @param vault - the kamino vault to get available liquidity to withdraw for
   * @param vaultTokenPrice - the price of the token in the vault (e.g. USDC)
   * @param [slot] - the slot for which to retrieve the vault overview for. Optional. If not provided the function will fetch the current slot
   * @param [vaultReservesMap] - hashmap from each reserve pubkey to the reserve state. Optional. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @param [kaminoMarkets] - a list of all kamino markets. Optional. If provided the function will be significantly faster as it will not have to fetch the markets
   * @param [currentSlot] - the latest confirmed slot. Optional. If provided the function will be  faster as it will not have to fetch the latest slot
   * @param [tokensPrices] - a hashmap from a token pubkey to the price of the token in USD. Optional. If some tokens are not in the map, the function will fetch the price
   * @returns an VaultOverview object with details about the tokens available and invested in the vault, denominated in tokens and USD, along sie APYs
   */
  async getVaultOverview(
    vault: KaminoVault,
    vaultTokenPrice: Decimal,
    slot?: Slot,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    kaminoMarkets?: KaminoMarket[],
    currentSlot?: Slot,
    tokensPrices?: Map<Address, Decimal>
  ): Promise<VaultOverview> {
    const vaultState = await vault.getState();
    const vaultReservesState = vaultReservesMap ? vaultReservesMap : await this.loadVaultReserves(vaultState);

    const vaultHoldingsWithUSDValuePromise = this.getVaultHoldingsWithPrice(
      vaultState,
      vaultTokenPrice,
      slot,
      vaultReservesState,
      currentSlot
    );

    const slotForOverview = currentSlot ?? slot ?? (await this.getConnection().getSlot().send());
    const farmsClient = new Farms(this.getConnection(), this._farmsProgramId);

    const vaultTheoreticalAPYPromise = this.getVaultTheoreticalAPY(vaultState, slotForOverview, vaultReservesState);
    const vaultActualAPYPromise = this.getVaultActualAPY(vaultState, slotForOverview, vaultReservesState);
    const totalInvestedAndBorrowedPromise = this.getTotalBorrowedAndInvested(
      vaultState,
      slotForOverview,
      vaultReservesState
    );
    const vaultCollateralsPromise = this.getVaultCollaterals(
      vaultState,
      slotForOverview,
      vaultReservesState,
      kaminoMarkets
    );
    const reservesOverviewPromise = this.getVaultReservesDetails(vaultState, slotForOverview, vaultReservesState);
    const vaultFarmIncentivesPromise = this.getVaultRewardsAPY(
      vault,
      vaultTokenPrice,
      farmsClient,
      slotForOverview,
      tokensPrices
    );
    const vaultReservesFarmIncentivesPromise = this.getVaultReservesFarmsIncentives(
      vault,
      vaultTokenPrice,
      farmsClient,
      slotForOverview,
      vaultReservesState,
      tokensPrices
    );
    const vaultDelegatedFarmIncentivesPromise = this.getVaultDelegatedFarmRewardsAPY(
      vault,
      vaultTokenPrice,
      farmsClient,
      slotForOverview,
      tokensPrices
    );
    const vaultFlcFarmStatsPromise = this.getVaultFlcFarmStats(vault);
    const vaultWithdrawPenaltiesPromise = this.getVaultWithdrawPenalties(vault);

    // all the async part of the functions above just read the vaultReservesState which is read beforehand, so excepting vaultCollateralsPromise they should do no additional network calls
    const [
      vaultHoldingsWithUSDValue,
      vaultTheoreticalAPYs,
      vaultActualAPYs,
      totalInvestedAndBorrowed,
      vaultCollaterals,
      reservesOverview,
      vaultFarmIncentives,
      vaultReservesFarmIncentives,
      vaultDelegatedFarmIncentives,
      vaultFlcFarmStats,
      vaultWithdrawPenalties,
    ] = await Promise.all([
      vaultHoldingsWithUSDValuePromise,
      vaultTheoreticalAPYPromise,
      vaultActualAPYPromise,
      totalInvestedAndBorrowedPromise,
      vaultCollateralsPromise,
      reservesOverviewPromise,
      vaultFarmIncentivesPromise,
      vaultReservesFarmIncentivesPromise,
      vaultDelegatedFarmIncentivesPromise,
      vaultFlcFarmStatsPromise,
      vaultWithdrawPenaltiesPromise,
    ]);

    return {
      holdingsUSD: vaultHoldingsWithUSDValue,
      reservesOverview: reservesOverview,
      vaultCollaterals: vaultCollaterals,
      actualSupplyAPY: vaultActualAPYs,
      theoreticalSupplyAPY: vaultTheoreticalAPYs,
      vaultFarmIncentives: vaultFarmIncentives,
      reservesFarmsIncentives: vaultReservesFarmIncentives,
      delegatedFarmIncentives: vaultDelegatedFarmIncentives,
      totalBorrowed: totalInvestedAndBorrowed.totalBorrowed,
      totalBorrowedUSD: totalInvestedAndBorrowed.totalBorrowed.mul(vaultTokenPrice),
      utilizationRatio: totalInvestedAndBorrowed.utilizationRatio,
      totalSupplied: totalInvestedAndBorrowed.totalInvested,
      totalSuppliedUSD: totalInvestedAndBorrowed.totalInvested.mul(vaultTokenPrice),
      flcFarmStats: vaultFlcFarmStats,
      withdrawalPenalties: vaultWithdrawPenalties,
    };
  }

  /**
   * This will return the withdrawal penalties for a vault
   * @param vault - the kamino vault to get the withdrawal penalties for
   * @param globalConfig - the global config to use for the withdrawal penalties. Optional. If not provided, the function will fetch the global config from the connection
   * @returns the withdrawal penalties for the vault, in lamports and bps; for each withdraw the penalty is computed and the bax between fixed amount and bps amount is taken
   */
  async getVaultWithdrawPenalties(vault: KaminoVault, globalConfig?: KVaultGlobalConfig): Promise<WithdrawPenalties> {
    const vaultState = await vault.getState();
    const globalConfigState = globalConfig
      ? globalConfig
      : await KVaultGlobalConfig.fetch(this.getConnection(), await getKvaultGlobalConfigPda(this.getProgramID()));
    if (!globalConfigState) {
      throw new Error('KVault Global config not found');
    }
    const vaultWithdrawalPenaltyLamports = new Decimal(vaultState.withdrawalPenaltyLamports.toString());
    const globalWithdrawalPenaltyLamports = new Decimal(globalConfigState.withdrawalPenaltyLamports.toString());
    const withdrawalPenaltyLamports = vaultWithdrawalPenaltyLamports.gt(globalWithdrawalPenaltyLamports)
      ? vaultWithdrawalPenaltyLamports
      : globalWithdrawalPenaltyLamports;

    const vaultWithdrawalPenaltyBps = new Decimal(vaultState.withdrawalPenaltyBps.toString());
    const globalWithdrawalPenaltyBps = new Decimal(globalConfigState.withdrawalPenaltyBps.toString());
    const withdrawalPenaltyBps = vaultWithdrawalPenaltyBps.gt(globalWithdrawalPenaltyBps)
      ? vaultWithdrawalPenaltyBps
      : globalWithdrawalPenaltyBps;

    return {
      withdrawalPenaltyLamports: withdrawalPenaltyLamports,
      withdrawalPenaltyBps: withdrawalPenaltyBps,
    };
  }

  /**
   * This will return an aggregation of the current state of the vault with all the invested amounts and the utilization ratio of the vault
   * @param vault - the kamino vault to get available liquidity to withdraw for
   * @param slot - current slot
   * @param [vaultReservesMap] - hashmap from each reserve pubkey to the reserve state. Optional. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @returns an VaultReserveTotalBorrowedAndInvested object with the total invested amount, total borrowed amount and the utilization ratio of the vault
   */
  async getTotalBorrowedAndInvested(
    vault: VaultState,
    slot: Slot,
    vaultReservesMap?: Map<Address, KaminoReserve>
  ): Promise<VaultReserveTotalBorrowedAndInvested> {
    const vaultReservesState = vaultReservesMap ? vaultReservesMap : await this.loadVaultReserves(vault);

    const totalAvailable = lamportsToDecimal(
      new Decimal(vault.tokenAvailable.toString()),
      new Decimal(vault.tokenMintDecimals.toString())
    );
    let totalInvested = new Decimal(0);
    let totalBorrowed = new Decimal(0);

    vault.vaultAllocationStrategy.forEach((allocationStrategy) => {
      if (allocationStrategy.reserve === DEFAULT_PUBLIC_KEY) {
        return;
      }

      const reserve = vaultReservesState.get(allocationStrategy.reserve);
      if (reserve === undefined) {
        throw new Error(`Reserve ${allocationStrategy.reserve} not found`);
      }

      const reserveCollExchangeRate = reserve.getEstimatedCollateralExchangeRate(slot, 0);
      const reserveAllocationLiquidityAmountLamports = new Decimal(allocationStrategy.ctokenAllocation.toString()).div(
        reserveCollExchangeRate
      );
      const reserveAllocationLiquidityAmount = lamportsToDecimal(
        reserveAllocationLiquidityAmountLamports,
        vault.tokenMintDecimals.toString()
      );

      const utilizationRatio = reserve.getEstimatedUtilizationRatio(slot, 0);
      totalInvested = totalInvested.add(reserveAllocationLiquidityAmount);
      totalBorrowed = totalBorrowed.add(reserveAllocationLiquidityAmount.mul(utilizationRatio));
    });

    let utilizationRatio = new Decimal(0);
    if (!totalInvested.isZero()) {
      utilizationRatio = totalBorrowed.div(totalInvested.add(totalAvailable));
    }

    return {
      totalInvested: totalInvested,
      totalBorrowed: totalBorrowed,
      utilizationRatio: utilizationRatio,
    };
  }

  /**
   * This will return a map of the cumulative rewards issued for all the delegated farms
   * @param [vaults] - the vaults to get the cumulative rewards for; if not provided, the function will get the cumulative rewards for all the vaults
   * @returns a map of the cumulative rewards issued for all the delegated farms, per token, in lamports
   */
  async getCumulativeDelegatedFarmsRewardsIssuedForAllVaults(vaults?: Address[]): Promise<Map<Address, Decimal>> {
    const vaultsWithDelegatedFarms = await this.getVaultsWithDelegatedFarm();
    const delegatedFarmsAddresses: Address[] = [];
    if (vaults) {
      vaults.forEach((vault) => {
        const delegatedFarm = vaultsWithDelegatedFarms.get(vault);
        if (delegatedFarm) {
          delegatedFarmsAddresses.push(delegatedFarm);
        }
      });
    } else {
      delegatedFarmsAddresses.push(...Array.from(vaultsWithDelegatedFarms.values()));
    }

    const farmsSDK = new Farms(this.getConnection(), this._farmsProgramId);
    const delegatedFarmsStates = await farmsSDK.fetchMultipleFarmStatesWithCheckedSize(delegatedFarmsAddresses);

    const cumulativeRewardsPerToken = new Map<Address, Decimal>();
    for (const delegatedFarmState of delegatedFarmsStates) {
      if (!delegatedFarmState) {
        continue;
      }

      delegatedFarmState.rewardInfos.forEach((rewardInfo) => {
        if (rewardInfo.token.mint === DEFAULT_PUBLIC_KEY) {
          return;
        }
        const rewardTokenMint = rewardInfo.token.mint;
        if (cumulativeRewardsPerToken.has(rewardTokenMint)) {
          cumulativeRewardsPerToken.set(
            rewardTokenMint,
            cumulativeRewardsPerToken
              .get(rewardTokenMint)!
              .add(new Decimal(rewardInfo.rewardsIssuedCumulative.toString()))
          );
        } else {
          cumulativeRewardsPerToken.set(rewardTokenMint, new Decimal(rewardInfo.rewardsIssuedCumulative.toString()));
        }
      });
    }

    return cumulativeRewardsPerToken;
  }

  /**
   * This will return an overview of each reserve that is part of the vault allocation
   * @param vault - the kamino vault to get available liquidity to withdraw for
   * @param slot - current slot
   * @param [vaultReservesMap] - hashmap from each reserve pubkey to the reserve state. Optional. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @returns a hashmap from vault reserve pubkey to ReserveOverview object
   */
  async getVaultReservesDetails(
    vault: VaultState,
    slot: Slot,
    vaultReserves?: Map<Address, KaminoReserve>
  ): Promise<Map<Address, ReserveOverview>> {
    const vaultReservesState = vaultReserves ? vaultReserves : await this.loadVaultReserves(vault);
    const reservesDetails = new Map<Address, ReserveOverview>();

    vault.vaultAllocationStrategy.forEach((allocationStrategy) => {
      if (allocationStrategy.reserve === DEFAULT_PUBLIC_KEY) {
        return;
      }

      const reserve = vaultReservesState.get(allocationStrategy.reserve);
      if (reserve === undefined) {
        throw new Error(`Reserve ${allocationStrategy.reserve} not found`);
      }

      const suppliedInReserve = this.getSuppliedInReserve(vault, slot, reserve);
      const utilizationRatio = new Decimal(reserve.getEstimatedUtilizationRatio(slot, 0));
      const reserveOverview: ReserveOverview = {
        supplyAPY: new Decimal(reserve.totalSupplyAPY(slot)),
        utilizationRatio: utilizationRatio,
        liquidationThresholdPct: new Decimal(reserve.state.config.liquidationThresholdPct),
        totalBorrowedAmount: reserve.getBorrowedAmount(),
        amountBorrowedFromSupplied: suppliedInReserve.mul(utilizationRatio),
        market: reserve.state.lendingMarket,
        suppliedAmount: suppliedInReserve,
      };
      reservesDetails.set(allocationStrategy.reserve, reserveOverview);
    });

    return reservesDetails;
  }

  /**
   * This will return the APY of the vault under the assumption that all the available tokens in the vault are all the time invested in the reserves as requested by the weights; for percentage it needs multiplication by 100
   * @param vault - the kamino vault to get APY for
   * @param slot - current slot
   * @param [vaultReservesMap] - hashmap from each reserve pubkey to the reserve state. Optional. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @returns a struct containing estimated gross APY and net APY (gross - vault fees) for the vault
   */
  async getVaultTheoreticalAPY(
    vault: VaultState,
    slot: Slot,
    vaultReservesMap?: Map<Address, KaminoReserve>
  ): Promise<APYs> {
    const vaultReservesState = vaultReservesMap ? vaultReservesMap : await this.loadVaultReserves(vault);

    let totalWeights = new Decimal(0);
    let totalAPY = new Decimal(0);
    vault.vaultAllocationStrategy.forEach((allocationStrategy) => {
      if (allocationStrategy.reserve === DEFAULT_PUBLIC_KEY) {
        return;
      }

      const reserve = vaultReservesState.get(allocationStrategy.reserve);
      if (reserve === undefined) {
        throw new Error(`Reserve ${allocationStrategy.reserve} not found`);
      }

      const reserveAPY = new Decimal(reserve.totalSupplyAPY(slot));
      const weight = new Decimal(allocationStrategy.targetAllocationWeight.toString());
      const weightedAPY = reserveAPY.mul(weight);
      totalAPY = totalAPY.add(weightedAPY);
      totalWeights = totalWeights.add(weight);
    });
    if (totalWeights.isZero()) {
      return {
        grossAPY: new Decimal(0),
        netAPY: new Decimal(0),
      };
    }

    const grossAPY = totalAPY.div(totalWeights);
    const netAPY = grossAPY
      .mul(new Decimal(1).sub(new Decimal(vault.performanceFeeBps.toString()).div(FullBPSDecimal)))
      .mul(new Decimal(1).sub(new Decimal(vault.managementFeeBps.toString()).div(FullBPSDecimal)));
    return {
      grossAPY,
      netAPY,
    };
  }

  /**
   * This will return the APY of the vault based on the current invested amounts; for percentage it needs multiplication by 100
   * @param vault - the kamino vault to get APY for
   * @param slot - current slot
   * @param [vaultReservesMap] - hashmap from each reserve pubkey to the reserve state. Optional. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @returns a struct containing estimated gross APY and net APY (gross - vault fees) for the vault
   */
  async getVaultActualAPY(
    vault: VaultState,
    slot: Slot,
    vaultReservesMap?: Map<Address, KaminoReserve>
  ): Promise<APYs> {
    const vaultReservesState = vaultReservesMap ? vaultReservesMap : await this.loadVaultReserves(vault);

    let totalAUM = new Decimal(vault.tokenAvailable.toString());
    let totalAPY = new Decimal(0);
    vault.vaultAllocationStrategy.forEach((allocationStrategy) => {
      if (allocationStrategy.reserve === DEFAULT_PUBLIC_KEY) {
        return;
      }

      const reserve = vaultReservesState.get(allocationStrategy.reserve);
      if (reserve === undefined) {
        throw new Error(`Reserve ${allocationStrategy.reserve} not found`);
      }

      const reserveAPY = new Decimal(reserve.totalSupplyAPY(slot));
      const exchangeRate = reserve.getEstimatedCollateralExchangeRate(slot, 0);
      const investedInReserve = exchangeRate.mul(new Decimal(allocationStrategy.ctokenAllocation.toString()));

      const weightedAPY = reserveAPY.mul(investedInReserve);
      totalAPY = totalAPY.add(weightedAPY);
      totalAUM = totalAUM.add(investedInReserve);
    });
    if (totalAUM.isZero()) {
      return {
        grossAPY: new Decimal(0),
        netAPY: new Decimal(0),
      };
    }

    const grossAPY = totalAPY.div(totalAUM);
    const netAPY = grossAPY
      .mul(new Decimal(1).sub(new Decimal(vault.performanceFeeBps.toString()).div(FullBPSDecimal)))
      .mul(new Decimal(1).sub(new Decimal(vault.managementFeeBps.toString()).div(FullBPSDecimal)));
    return {
      grossAPY,
      netAPY,
    };
  }

  /**
   * Retrive the total amount of interest earned by the vault since its inception, up to the last interaction with the vault on chain, including what was charged as fees
   * @param vaultState the kamino vault state to get total net yield for
   * @returns a struct containing a Decimal representing the net number of tokens earned by the vault since its inception and the timestamp of the last fee charge
   */
  async getVaultCumulativeInterest(vaultState: VaultState): Promise<VaultCumulativeInterestWithTimestamp> {
    const netYieldLamports = new Fraction(vaultState.cumulativeEarnedInterestSf).toDecimal();
    const cumulativeInterest = lamportsToDecimal(netYieldLamports, vaultState.tokenMintDecimals.toString());
    return {
      cumulativeInterest: cumulativeInterest,
      timestamp: vaultState.lastFeeChargeTimestamp.toNumber(),
    };
  }

  /**
   * Simulate the current holdings of the vault and the earned interest
   * @param vaultState the kamino vault state to get simulated holdings and earnings for
   * @param [vaultReservesMap] - hashmap from each reserve pubkey to the reserve state. Optional. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @param [slot] - the current slot. Optional. If not provided it will fetch the current slot
   * @param [previousNetAUM] - the previous AUM of the vault to compute the earned interest relative to this value. Optional. If not provided the function will estimate the total AUM at the slot of the last state update on chain
   * @param [currentSlot] - the latest confirmed slot. Optional. If provided the function will be  faster as it will not have to fetch the latest slot
   * @returns a struct of simulated vault holdings and earned interest
   */
  async calculateSimulatedHoldingsWithInterest(
    vaultState: VaultState,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    slot?: Slot,
    previousNetAUM?: Decimal,
    currentSlot?: Slot
  ): Promise<SimulatedVaultHoldingsWithEarnedInterest> {
    let prevAUM: Decimal;
    let pendingFees = ZERO;

    if (previousNetAUM) {
      prevAUM = previousNetAUM;
    } else {
      const tokenDecimals = vaultState.tokenMintDecimals.toNumber();
      prevAUM = lamportsToDecimal(new Fraction(vaultState.prevAumSf).toDecimal(), tokenDecimals);
      pendingFees = lamportsToDecimal(new Fraction(vaultState.pendingFeesSf).toDecimal(), tokenDecimals);
    }

    let fetchedLatestSlot: Slot | undefined = undefined;
    if (!slot || !currentSlot) {
      fetchedLatestSlot = await this.getConnection().getSlot({ commitment: 'confirmed' }).send();
    }
    const latestSlot = slot ? slot : fetchedLatestSlot!;
    const latestCurrentSlot = currentSlot ? currentSlot : fetchedLatestSlot!;

    const currentHoldings = await this.getVaultHoldings(vaultState, latestSlot, vaultReservesMap, latestCurrentSlot);
    const earnedInterest = currentHoldings.totalAUMIncludingFees.sub(prevAUM).sub(pendingFees);

    return {
      holdings: currentHoldings,
      earnedInterest: earnedInterest,
    };
  }

  /**
   * Simulate the current holdings and compute the fees that would be charged
   * @param vaultState the kamino vault state to get simulated fees for
   * @param [simulatedCurrentHoldingsWithInterest] the simulated holdings and interest earned by the vault. Optional
   * @param [currentTimestamp] the current date. Optional. If not provided it will fetch the current unix timestamp
   * @param [vaultReservesMap] - hashmap from each reserve pubkey to the reserve state. Optional. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @param [slot] - the slot at which to compute the fees. Optional. If not provided it will fetch the current slot
   * @param [previousNetAUM] - the previous AUM of the vault to compute the fees relative to this value. Optional. If not provided the function will estimate the total AUM at the slot of the last state update on chain
   * @param [currentSlot] - the latest confirmed slot. Optional. If provided the function will be  faster as it will not have to fetch the latest slot
   * @returns a VaultFees struct of simulated management and interest fees
   */
  async calculateSimulatedFees(
    vaultState: VaultState,
    simulatedCurrentHoldingsWithInterest?: SimulatedVaultHoldingsWithEarnedInterest,
    currentTimestamp?: Date,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    slot?: Slot,
    previousNetAUM?: Decimal,
    currentSlot?: Slot
  ): Promise<VaultFees> {
    const timestampNowInSeconds = currentTimestamp ? currentTimestamp.valueOf() / 1000 : Date.now() / 1000;
    const timestampLastUpdate = vaultState.lastFeeChargeTimestamp.toNumber();
    const timeElapsed = timestampNowInSeconds - timestampLastUpdate;

    const simulatedCurrentHoldings = simulatedCurrentHoldingsWithInterest
      ? simulatedCurrentHoldingsWithInterest
      : await this.calculateSimulatedHoldingsWithInterest(
          vaultState,
          vaultReservesMap,
          slot,
          previousNetAUM,
          currentSlot
        );

    const performanceFee = simulatedCurrentHoldings.earnedInterest.mul(
      new Decimal(vaultState.performanceFeeBps.toString()).div(FullBPSDecimal)
    );

    const managementFeeFactor = new Decimal(timeElapsed)
      .mul(new Decimal(vaultState.managementFeeBps.toString()))
      .div(new Decimal(SECONDS_PER_YEAR))
      .div(FullBPSDecimal);
    const prevAUM = lamportsToDecimal(
      new Fraction(vaultState.prevAumSf).toDecimal(),
      vaultState.tokenMintDecimals.toNumber()
    );
    const mgmtFee = prevAUM.mul(managementFeeFactor);

    return {
      managementFee: mgmtFee,
      performanceFee: performanceFee,
    };
  }

  /**
   * This will compute the PDA that is used as delegatee in Farms program to compute the user state PDA for vault depositor investing in vault with reserve having a supply farm
   */
  computeUserFarmStateDelegateePDAForUserInVault(
    farmsProgramId: Address,
    vault: Address,
    reserve: Address,
    user: Address
  ): Promise<ProgramDerivedAddress> {
    return getProgramDerivedAddress({
      seeds: [addressEncoder.encode(reserve), addressEncoder.encode(vault), addressEncoder.encode(user)],
      programAddress: farmsProgramId,
    });
  }

  /**
   * Compute the delegatee PDA for the user farm state for a vault delegate farm
   * @param farmProgramID - the program ID of the farm program
   * @param vault - the address of the vault
   * @param farm - the address of the delegated farm
   * @param user - the address of the user
   * @returns the PDA of the delegatee user farm state for the delegated farm
   */
  async computeUserFarmStateDelegateePDAForUserInDelegatedVaultFarm(
    farmProgramID: Address,
    vault: Address,
    farm: Address,
    user: Address
  ): Promise<ProgramDerivedAddress> {
    return getProgramDerivedAddress({
      seeds: [addressEncoder.encode(vault), addressEncoder.encode(farm), addressEncoder.encode(user)],
      programAddress: farmProgramID,
    });
  }

  /**
   * Compute the user state PDA for a user in a delegated vault farm
   * @param farmProgramID - the program ID of the farm program
   * @param vault - the address of the vault
   * @param farm - the address of the delegated farm
   * @param user - the address of the user
   * @returns the PDA of the user state for the delegated farm
   */
  async computeUserStatePDAForUserInDelegatedVaultFarm(
    farmProgramID: Address,
    vault: Address,
    farm: Address,
    user: Address
  ): Promise<Address> {
    const delegateePDA = await this.computeDelegateeForUserInDelegatedFarm(farmProgramID, vault, farm, user);
    return getUserStatePDA(farmProgramID, farm, delegateePDA);
  }

  async computeDelegateeForUserInDelegatedFarm(
    farmProgramID: Address,
    vault: Address,
    farm: Address,
    user: Address
  ): Promise<Address> {
    const delegateePDA = await this.computeUserFarmStateDelegateePDAForUserInDelegatedVaultFarm(
      farmProgramID,
      vault,
      farm,
      user
    );
    return delegateePDA[0];
  }

  /**
   * Read the APY of the farm built on top of the vault (farm in vaultState.vaultFarm)
   * @param vaultOrState - the vault or state to read the farm APY for
   * @param vaultTokenPrice - the price of the vault token in USD (e.g. 1.0 for USDC)
   * @param [farmsClient] - the farms client to use. Optional. If not provided, the function will create a new one
   * @param [slot] - the slot to read the farm APY for. Optional. If not provided, the function will read the current slot
   * @param tokensPrices cached token prices
   * @returns the APY of the farm built on top of the vault
   */
  async getVaultRewardsAPY(
    vaultOrState: KaminoVault | VaultState,
    vaultTokenPrice: Decimal,
    farmsClient?: Farms,
    slot?: Slot,
    tokensPrices?: Map<Address, Decimal>
  ): Promise<FarmIncentives> {
    // Determine if we have a KaminoVault or VaultState
    const vaultState = 'getState' in vaultOrState ? await vaultOrState.getState() : vaultOrState;
    if (vaultState.vaultFarm === DEFAULT_PUBLIC_KEY) {
      return {
        incentivesStats: [],
        totalIncentivesApy: 0,
      };
    }
    const kFarmsClient = farmsClient ? farmsClient : new Farms(this.getConnection(), this._farmsProgramId);
    const farmState = await FarmState.fetch(
      kFarmsClient.getConnection(),
      vaultState.vaultFarm,
      kFarmsClient.getProgramID()
    );

    if (!farmState) {
      // a vault may have a badly configured farm that does not exist on chain but isn't set as a default pubkey by mistake
      return {
        incentivesStats: [],
        totalIncentivesApy: 0,
      };
    }

    const tokensPerShare = await this.getTokensPerShareSingleVault(vaultState, slot);
    const sharePrice = tokensPerShare.mul(vaultTokenPrice);
    const stakedTokenMintDecimals = vaultState.sharesMintDecimals.toNumber();

    return getFarmIncentivesWithExistentState(
      kFarmsClient,
      vaultState.vaultFarm,
      farmState,
      sharePrice,
      stakedTokenMintDecimals,
      tokensPrices
    );
  }

  /**
   * Read the APY of the delegated farm providing incentives for vault depositors
   * @param vault - the vault to read the farm APY for
   * @param vaultTokenPrice - the price of the vault token in USD (e.g. 1.0 for USDC)
   * @param [farmsClient] - the farms client to use. Optional. If not provided, the function will create a new one
   * @param [slot] - the slot to read the farm APY for. Optional. If not provided, the function will read the current slot
   * @param [tokensPrices] - the prices of the tokens in USD. Optional. If not provided, the function will fetch the prices
   * @returns the APY of the delegated farm providing incentives for vault depositors
   */
  async getVaultDelegatedFarmRewardsAPY(
    vault: KaminoVault,
    vaultTokenPrice: Decimal,
    farmsClient?: Farms,
    slot?: Slot,
    tokensPrices?: Map<Address, Decimal>
  ): Promise<FarmIncentives> {
    const delegatedFarm = await this.getDelegatedFarmForVault(vault.address);
    if (!delegatedFarm) {
      return {
        incentivesStats: [],
        totalIncentivesApy: 0,
      };
    }

    const vaultState = await vault.getState();
    const tokensPerShare = await this.getTokensPerShareSingleVault(vaultState, slot);
    const sharePrice = tokensPerShare.mul(vaultTokenPrice);
    const stakedTokenMintDecimals = vaultState.sharesMintDecimals.toNumber();

    const kFarmsClient = farmsClient ? farmsClient : new Farms(this.getConnection(), this._farmsProgramId);
    const farmState = await FarmState.fetch(kFarmsClient.getConnection(), delegatedFarm, kFarmsClient.getProgramID());

    if (!farmState) {
      // a vault may have a badly configured farm that does not exist on chain but isn't set as a default pubkey by mistake
      return {
        incentivesStats: [],
        totalIncentivesApy: 0,
      };
    }
    return getFarmIncentivesWithExistentState(
      kFarmsClient,
      delegatedFarm,
      farmState,
      sharePrice,
      stakedTokenMintDecimals,
      tokensPrices
    );
  }

  /**
   * Get all the token mints of the vault, vault farm rewards and the allocation  rewards
   * @param vaults - the vaults to get the token mints for
   * @param [vaultReservesMap] - the vault reserves map to get the reserves for; if not provided, the function will fetch the reserves
   * @param farmsMap - the farms map to get the farms for
   * @returns a map of token mints (keys) and number of decimals (values)
   */
  async getAllVaultsTokenMintsIncludingRewards(
    vaults: KaminoVault[],
    vaultReservesMap?: Map<Address, KaminoReserve>,
    farmsMap?: Map<Address, FarmState>
  ) {
    const vaultsTokenMints = new Map<Address, number>();

    const kFarmsMap = farmsMap ? farmsMap : new Map<Address, FarmState>();

    const farmsToFetch = new Set<Address>();
    const reservesToFetch = new Set<Address>();

    for (const vault of vaults) {
      const vaultState = await vault.getState();
      vaultsTokenMints.set(vaultState.tokenMint, vaultState.tokenMintDecimals.toNumber());
      const hasFarm = await vault.hasFarm();
      if (hasFarm) {
        const farmAddress = vaultState.vaultFarm;
        if (!kFarmsMap.has(farmAddress)) {
          farmsToFetch.add(farmAddress);
        } else {
          const farmState = kFarmsMap.get(farmAddress)!;
          farmState.rewardInfos.forEach((rewardInfo) => {
            if (rewardInfo.token.mint !== DEFAULT_PUBLIC_KEY) {
              vaultsTokenMints.set(rewardInfo.token.mint, rewardInfo.token.decimals.toNumber());
            }
          });
        }
      }

      const reserves = vaultState.vaultAllocationStrategy.map((allocationStrategy) => allocationStrategy.reserve);
      reserves.forEach((reserve) => {
        if (reserve === DEFAULT_PUBLIC_KEY) {
          return;
        }

        if (vaultReservesMap && !vaultReservesMap.has(reserve)) {
          const reserveState = vaultReservesMap.get(reserve)!;
          const supplyFarm = reserveState.state.farmCollateral;
          if (supplyFarm !== DEFAULT_PUBLIC_KEY) {
            if (!kFarmsMap.has(supplyFarm)) {
              farmsToFetch.add(supplyFarm);
            } else {
              const farmState = kFarmsMap.get(supplyFarm)!;
              farmState.rewardInfos.forEach((rewardInfo) => {
                if (rewardInfo.token.mint !== DEFAULT_PUBLIC_KEY) {
                  vaultsTokenMints.set(rewardInfo.token.mint, rewardInfo.token.decimals.toNumber());
                }
              });
            }
          }
        } else {
          reservesToFetch.add(reserve);
        }
      });
    }

    // fetch the reserves first so we can add their farms to farms to be fetched, if needed
    const missingReservesStates = await Reserve.fetchMultiple(this.getConnection(), Array.from(reservesToFetch));

    missingReservesStates.forEach((reserveState) => {
      if (reserveState) {
        const supplyFarm = reserveState.farmCollateral;
        if (supplyFarm !== DEFAULT_PUBLIC_KEY) {
          if (!kFarmsMap.has(supplyFarm)) {
            farmsToFetch.add(supplyFarm);
          } else {
            const farmState = kFarmsMap.get(supplyFarm)!;
            farmState.rewardInfos.forEach((rewardInfo) => {
              if (rewardInfo.token.mint !== DEFAULT_PUBLIC_KEY) {
                vaultsTokenMints.set(rewardInfo.token.mint, rewardInfo.token.decimals.toNumber());
              }
            });
          }
        }
      }
    });

    // fetch the missing farms
    const missingFarmsStates = await FarmState.fetchMultiple(
      this.getConnection(),
      Array.from(farmsToFetch),
      this._farmsProgramId
    );
    missingFarmsStates.forEach((farmState) => {
      if (farmState) {
        farmState.rewardInfos.forEach((rewardInfo) => {
          if (rewardInfo.token.mint !== DEFAULT_PUBLIC_KEY) {
            vaultsTokenMints.set(rewardInfo.token.mint, rewardInfo.token.decimals.toNumber());
          }
        });
      }
    });

    return vaultsTokenMints;
  }

  async getVaultReservesFarmsIncentives(
    vaultOrState: KaminoVault | VaultState,
    vaultTokenPrice: Decimal,
    farmsClient?: Farms,
    slot?: Slot,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    tokensPrices?: Map<Address, Decimal>
  ): Promise<VaultReservesFarmsIncentives> {
    const vaultState = 'getState' in vaultOrState ? await vaultOrState.getState() : vaultOrState;

    const vaultReservesState = vaultReservesMap ? vaultReservesMap : await this.loadVaultReserves(vaultState);
    const currentSlot = slot ?? (await this.getConnection().getSlot({ commitment: 'confirmed' }).send());

    const holdings = await this.getVaultHoldings(vaultState, currentSlot, vaultReservesState);

    const vaultReservesAddresses = vaultState.vaultAllocationStrategy.map(
      (allocationStrategy) => allocationStrategy.reserve
    );

    const vaultReservesFarmsIncentives = new Map<Address, FarmIncentives>();
    let totalIncentivesApy = new Decimal(0);

    const kFarmsClient = farmsClient ? farmsClient : new Farms(this.getConnection(), this._farmsProgramId);
    for (const reserveAddress of vaultReservesAddresses) {
      if (reserveAddress === DEFAULT_PUBLIC_KEY) {
        continue;
      }

      const reserveState = vaultReservesState.get(reserveAddress);
      if (reserveState === undefined) {
        console.log(`Reserve to read farm incentives for not found: ${reserveAddress}`);
        vaultReservesFarmsIncentives.set(reserveAddress, {
          incentivesStats: [],
          totalIncentivesApy: 0,
        });
        continue;
      }

      const reserveFarmIncentives = await getReserveFarmRewardsAPY(
        this._rpc,
        this.recentSlotDurationMs,
        reserveAddress,
        vaultTokenPrice,
        this._kaminoLendProgramId,
        kFarmsClient,
        currentSlot,
        reserveState.state,
        tokensPrices
      );
      vaultReservesFarmsIncentives.set(reserveAddress, reserveFarmIncentives.collateralFarmIncentives);

      const investedInReserve = holdings.investedInReserves.get(reserveAddress);
      const weightedReserveAPY = new Decimal(reserveFarmIncentives.collateralFarmIncentives.totalIncentivesApy)
        .mul(investedInReserve ?? 0)
        .div(holdings.totalAUMIncludingFees);
      totalIncentivesApy = totalIncentivesApy.add(weightedReserveAPY);
    }

    return {
      reserveFarmsIncentives: vaultReservesFarmsIncentives,
      totalIncentivesAPY: totalIncentivesApy,
    };
  }

  async getVaultFlcFarmStats(vaultOrState: KaminoVault | VaultState): Promise<FlcFarmStats | undefined> {
    const vaultState = 'getState' in vaultOrState ? await vaultOrState.getState() : vaultOrState;

    if (vaultState.firstLossCapitalFarm === DEFAULT_PUBLIC_KEY) {
      return undefined;
    }

    const kFarmsClient = new Farms(this.getConnection(), this._farmsProgramId);

    const flcFarmState = await FarmState.fetch(
      this.getConnection(),
      vaultState.firstLossCapitalFarm,
      this._farmsProgramId
    );

    if (!flcFarmState) {
      return undefined;
    }

    if (!(await this.isFlcFarmValid(flcFarmState, vaultState))) {
      return undefined;
    }

    const userStates = await kFarmsClient.getAllUserStatesForFarm(vaultState.firstLossCapitalFarm);
    const pendingUnstakes: FarmPendingUnstakeInfo[] = [];

    for (const { userState, key } of userStates) {
      const pendingWithdrawalUnstake = new Decimal(scaleDownWads(userState.pendingWithdrawalUnstakeScaled));
      if (pendingWithdrawalUnstake.gt(0)) {
        pendingUnstakes.push({
          userStateAddress: key,
          pendingUnstakeAmountLamports: pendingWithdrawalUnstake,
          pendingUnstakeAvailableAtTimestamp: userState.pendingWithdrawalUnstakeTs.toNumber(),
        });
      }
    }

    return {
      address: vaultState.firstLossCapitalFarm,
      farmState: flcFarmState,
      totalStakedShares: new Decimal(scaleDownWads(flcFarmState.totalActiveStakeScaled)),
      withdrawalCooldownDurationSeconds: flcFarmState.withdrawalCooldownPeriod,
      isPendingUnstake: pendingUnstakes.length > 0,
      pendingUnstakeInfo: pendingUnstakes,
    };
  }

  async isFlcFarmValid(flcFarmState: FarmState, vaultOrState: KaminoVault | VaultState): Promise<boolean> {
    const vaultState = 'getState' in vaultOrState ? await vaultOrState.getState() : vaultOrState;

    if (flcFarmState.timeUnit !== 0) {
      // timeUnit = 0 -> seconds
      return false;
    }

    if (flcFarmState.withdrawalCooldownPeriod === 0) {
      // invalid FLC farm, should have > 0 withdrawal cooldown
      return false;
    }

    if (flcFarmState.token.mint !== vaultState.sharesMint) {
      // staked token mint should be the vault shares mint
      return false;
    }
    return true;
  }

  /// reads the pending rewards for a user in the vault farm
  /// @param user - the user address
  /// @param vault - the vault
  /// @returns a map of the pending rewards token mint and amount in lamports
  async getUserPendingRewardsInVaultFarm(user: Address, vault: KaminoVault): Promise<Map<Address, Decimal>> {
    const vaultState = await vault.getState();
    const hasFarm = await vault.hasFarm();
    if (!hasFarm) {
      return new Map<Address, Decimal>();
    }

    const farmClient = new Farms(this.getConnection(), this._farmsProgramId);
    const userState = await getUserStatePDA(farmClient.getProgramID(), vaultState.vaultFarm, user);
    return getUserPendingRewardsInFarm(this.getConnection(), userState, vaultState.vaultFarm, this._farmsProgramId);
  }

  /// reads the pending rewards for a user in a delegated vault farm
  /// @param user - the user address
  /// @param vaultAddress - the address of the vault
  /// @returns a map of the pending rewards token mint and amount in lamports
  async getUserPendingRewardsInVaultDelegatedFarm(
    user: Address,
    vaultAddress: Address
  ): Promise<Map<Address, Decimal>> {
    const delegatedFarm = await this.getDelegatedFarmForVault(vaultAddress);
    if (!delegatedFarm) {
      return new Map<Address, Decimal>();
    }

    const farmClient = new Farms(this.getConnection(), this._farmsProgramId);
    const userState = await this.computeUserStatePDAForUserInDelegatedVaultFarm(
      farmClient.getProgramID(),
      vaultAddress,
      delegatedFarm,
      user
    );

    return getUserPendingRewardsInFarm(this.getConnection(), userState, delegatedFarm, this._farmsProgramId);
  }

  /// gets the delegated farm for a vault
  async getDelegatedFarmForVault(vault: Address): Promise<Address | undefined> {
    const resources = await this.loadCdnResourcesOnce();
    const delegatedVaultFarms = resources?.delegatedVaultFarms;
    if (!delegatedVaultFarms) {
      return undefined;
    }
    const delegatedFarmWithVault = delegatedVaultFarms.find((vaultWithFarm) => vaultWithFarm.vault === vault);
    if (!delegatedFarmWithVault) {
      return undefined;
    }
    return address(delegatedFarmWithVault.farm);
  }

  /**
   * gets all the delegated farms addresses
   * @returns a list of delegated farms addresses
   */
  async getAllDelegatedFarms(): Promise<Address[]> {
    const vaultsWithDelegatedFarm = await this.getVaultsWithDelegatedFarm();
    return Array.from(vaultsWithDelegatedFarm.values());
  }

  /**
   * This will return a map of the vault address and the delegated farm address for that vault
   * @returns a map of the vault address and the delegated farm address for that vault
   */
  async getVaultsWithDelegatedFarm(): Promise<Map<Address, Address>> {
    const resources = await this.loadCdnResourcesOnce();
    const delegatedVaultFarms = resources?.delegatedVaultFarms;
    if (!delegatedVaultFarms) {
      return new Map<Address, Address>();
    }

    return new Map(
      delegatedVaultFarms.map((delegatedFarm) => [address(delegatedFarm.vault), address(delegatedFarm.farm)])
    );
  }

  /// reads the pending rewards for a user in the reserves farms of a vault
  /// @param user - the user address
  /// @param vault - the vault
  /// @param [vaultReservesMap] - the vault reserves map to get the reserves for; if not provided, the function will fetch the reserves
  /// @returns a map of the pending rewards token mint and amount in lamports
  async getUserPendingRewardsInVaultReservesFarms(
    user: Address,
    vault: KaminoVault,
    vaultReservesMap?: Map<Address, KaminoReserve>
  ): Promise<Map<Address, Decimal>> {
    const vaultState = await vault.getState();

    const vaultReservesState = vaultReservesMap ? vaultReservesMap : await this.loadVaultReserves(vaultState);

    const vaultReserves = vaultState.vaultAllocationStrategy
      .map((allocationStrategy) => allocationStrategy.reserve)
      .filter((reserve) => reserve !== DEFAULT_PUBLIC_KEY);
    const pendingRewardsPerToken: Map<Address, Decimal> = new Map();

    const farmClient = new Farms(this.getConnection(), this._farmsProgramId);
    for (const reserveAddress of vaultReserves) {
      const reserveState = vaultReservesState.get(reserveAddress);
      if (!reserveState) {
        console.log(`Reserve to read farm incentives for not found: ${reserveAddress}`);
        continue;
      }

      if (reserveState.state.farmCollateral === DEFAULT_PUBLIC_KEY) {
        continue;
      }

      const delegatee = await this.computeUserFarmStateDelegateePDAForUserInVault(
        farmClient.getProgramID(),
        vault.address,
        reserveAddress,
        user
      );
      const userState = await getUserStatePDA(
        farmClient.getProgramID(),
        reserveState.state.farmCollateral,
        delegatee[0]
      );
      const pendingRewards = await getUserPendingRewardsInFarm(
        this.getConnection(),
        userState,
        reserveState.state.farmCollateral,
        this._farmsProgramId
      );
      pendingRewards.forEach((reward, token) => {
        const existingReward = pendingRewardsPerToken.get(token);
        if (existingReward) {
          pendingRewardsPerToken.set(token, existingReward.add(reward));
        } else {
          pendingRewardsPerToken.set(token, reward);
        }
      });
    }

    return pendingRewardsPerToken;
  }

  /// reads the pending rewards for a user in the vault farm, the reserves farms of the vault and the delegated vault farm
  /// @param user - the user address
  /// @param vault - the vault
  /// @param [vaultReservesMap] - the vault reserves map to get the reserves for; if not provided, the function will fetch the reserves
  /// @returns a struct containing the pending rewards in the vault farm, the reserves farms of the vault and the delegated vault farm, and the total pending rewards in lamports
  async getAllPendingRewardsForUserInVault(
    user: Address,
    vault: KaminoVault,
    vaultReservesMap?: Map<Address, KaminoReserve>
  ): Promise<PendingRewardsForUserInVault> {
    const pendingRewardsInVaultFarm = await this.getUserPendingRewardsInVaultFarm(user, vault);
    const pendingRewardsInVaultReservesFarms = await this.getUserPendingRewardsInVaultReservesFarms(
      user,
      vault,
      vaultReservesMap
    );
    const pendingRewardsInVaultDelegatedFarm = await this.getUserPendingRewardsInVaultDelegatedFarm(
      user,
      vault.address
    );

    const totalPendingRewards = new Map<Address, Decimal>();
    pendingRewardsInVaultFarm.forEach((reward, token) => {
      const existingReward = totalPendingRewards.get(token);
      if (existingReward) {
        totalPendingRewards.set(token, existingReward.add(reward));
      } else {
        totalPendingRewards.set(token, reward);
      }
    });
    pendingRewardsInVaultReservesFarms.forEach((reward, token) => {
      const existingReward = totalPendingRewards.get(token);
      if (existingReward) {
        totalPendingRewards.set(token, existingReward.add(reward));
      } else {
        totalPendingRewards.set(token, reward);
      }
    });
    pendingRewardsInVaultDelegatedFarm.forEach((reward, token) => {
      const existingReward = totalPendingRewards.get(token);
      if (existingReward) {
        totalPendingRewards.set(token, existingReward.add(reward));
      } else {
        totalPendingRewards.set(token, reward);
      }
    });

    return {
      pendingRewardsInVaultFarm,
      pendingRewardsInVaultReservesFarms,
      pendingRewardsInVaultDelegatedFarm,
      totalPendingRewards,
    };
  }

  /**
   * This function will return the instructions to claim the rewards for the farm of a vault, the delegated farm of the vault and the reserves farms of the vault
   * @param user - the user to claim the rewards
   * @param vault - the vault
   * @param [vaultReservesMap] - the vault reserves map to get the reserves for; if not provided, the function will fetch the reserves
   * @returns the instructions to claim the rewards for the farm of the vault, the delegated farm of the vault and the reserves farms of the vault
   */
  async getClaimAllRewardsForVaultIxs(
    user: TransactionSigner,
    vault: KaminoVault,
    vaultReservesMap?: Map<Address, KaminoReserve>
  ): Promise<Instruction[]> {
    const [vaultFarmIxs, delegatedFarmIxs, reservesFarmsIxs] = await Promise.all([
      this.getClaimVaultFarmRewardsIxs(user, vault),
      this.getClaimVaultDelegatedFarmRewardsIxs(user, vault),
      this.getClaimVaultReservesFarmsRewardsIxs(user, vault, vaultReservesMap),
    ]);

    return [...new Set([...vaultFarmIxs, ...delegatedFarmIxs, ...reservesFarmsIxs])];
  }

  /**
   * This function will return the instructions to claim the rewards for the farm of a vault
   * @param user - the user to claim the rewards
   * @param vault - the vault
   * @returns the instructions to claim the rewards for the farm of the vault
   */
  async getClaimVaultFarmRewardsIxs(user: TransactionSigner, vault: KaminoVault): Promise<Instruction[]> {
    const vaultState = await vault.getState();
    const hasFarm = await vault.hasFarm();
    if (!hasFarm) {
      return [];
    }

    const farmClient = new Farms(this.getConnection(), this._farmsProgramId);
    const pendingRewardsInVaultFarm = await this.getUserPendingRewardsInVaultFarm(user.address, vault);
    // if there are no pending rewards of their total is 0 no ix is needed
    const totalPendingRewards = Array.from(pendingRewardsInVaultFarm.values()).reduce(
      (acc, reward) => acc.add(reward),
      new Decimal(0)
    );
    if (totalPendingRewards.eq(0)) {
      return [];
    }
    return farmClient.claimForUserForFarmAllRewardsIx(user, user.address, vaultState.vaultFarm, false);
  }

  /**
   * This function will return the instructions to claim the rewards for the delegated farm of a vault
   * @param user - the user to claim the rewards
   * @param vault - the vault
   * @returns the instructions to claim the rewards for the delegated farm of the vault
   */
  async getClaimVaultDelegatedFarmRewardsIxs(user: TransactionSigner, vault: KaminoVault): Promise<Instruction[]> {
    const delegatedFarm = await this.getDelegatedFarmForVault(vault.address);
    if (!delegatedFarm) {
      return [];
    }

    const farmClient = new Farms(this.getConnection(), this._farmsProgramId);

    const delegatee = await this.computeDelegateeForUserInDelegatedFarm(
      farmClient.getProgramID(),
      vault.address,
      delegatedFarm,
      user.address
    );
    const userState = await getUserStatePDA(farmClient.getProgramID(), delegatedFarm, delegatee);
    // check if the user state exists
    const userStateExists = await fetchEncodedAccount(this.getConnection(), userState);
    if (!userStateExists.exists) {
      return [];
    }

    return farmClient.claimForUserForFarmAllRewardsIx(user, user.address, delegatedFarm, true, [delegatee]);
  }

  /**
   * This function will return the instructions to claim the rewards for the reserves farms of a vault
   * @param user - the user to claim the rewards
   * @param vault - the vault
   * @param [vaultReservesMap] - the vault reserves map to get the reserves for; if not provided, the function will fetch the reserves
   * @returns the instructions to claim the rewards for the reserves farms of the vault
   */
  async getClaimVaultReservesFarmsRewardsIxs(
    user: TransactionSigner,
    vault: KaminoVault,
    vaultReservesMap?: Map<Address, KaminoReserve>
  ): Promise<Instruction[]> {
    const vaultState = await vault.getState();

    const vaultReservesState = vaultReservesMap ? vaultReservesMap : await this.loadVaultReserves(vaultState);

    const vaultReserves = vaultState.vaultAllocationStrategy
      .map((allocationStrategy) => allocationStrategy.reserve)
      .filter((reserve) => reserve !== DEFAULT_PUBLIC_KEY);

    const ixs: Instruction[] = [];
    const farmClient = new Farms(this.getConnection(), this._farmsProgramId);
    for (const reserveAddress of vaultReserves) {
      const reserveState = vaultReservesState.get(reserveAddress);
      if (!reserveState) {
        console.log(`Reserve to read farm incentives for not found: ${reserveAddress}`);
        continue;
      }

      if (reserveState.state.farmCollateral === DEFAULT_PUBLIC_KEY) {
        continue;
      }

      const delegatee = await this.computeUserFarmStateDelegateePDAForUserInVault(
        farmClient.getProgramID(),
        vault.address,
        reserveAddress,
        user.address
      );
      const userState = await getUserStatePDA(
        farmClient.getProgramID(),
        reserveState.state.farmCollateral,
        delegatee[0]
      );

      const pendingRewards = await getUserPendingRewardsInFarm(
        this.getConnection(),
        userState,
        reserveState.state.farmCollateral,
        this._farmsProgramId
      );
      const totalPendingRewards = Array.from(pendingRewards.values()).reduce(
        (acc, reward) => acc.add(reward),
        new Decimal(0)
      );
      if (totalPendingRewards.eq(0)) {
        continue;
      }
      const ix = await farmClient.claimForUserForFarmAllRewardsIx(
        user,
        user.address,
        reserveState.state.farmCollateral,
        true,
        [delegatee[0]]
      );
      ixs.push(...ix);
    }

    return ixs;
  }

  private buildRemainingAccountsForVaultReserves(
    vaultReserves: Address[],
    vaultReservesState: Map<Address, KaminoReserve>
  ): AccountMeta[] {
    let vaultReservesAccountMetas: AccountMeta[] = [];
    let vaultReservesLendingMarkets: AccountMeta[] = [];
    vaultReserves.forEach((reserve) => {
      const reserveState = vaultReservesState.get(reserve);
      if (reserveState === undefined) {
        throw new Error(`Reserve ${reserve} not found`);
      }
      vaultReservesAccountMetas = vaultReservesAccountMetas.concat([{ address: reserve, role: AccountRole.WRITABLE }]);
      vaultReservesLendingMarkets = vaultReservesLendingMarkets.concat([
        { address: reserveState.state.lendingMarket, role: AccountRole.READONLY },
      ]);
    });
    return [...vaultReservesAccountMetas, ...vaultReservesLendingMarkets];
  }

  private appendRemainingAccountsForVaultReserves(
    ix: Instruction,
    vaultReserves: Address[],
    vaultReservesState: Map<Address, KaminoReserve>
  ): Instruction {
    const remainingAccounts = this.buildRemainingAccountsForVaultReserves(vaultReserves, vaultReservesState);
    return {
      ...ix,
      accounts: ix.accounts?.concat(remainingAccounts),
    };
  }
} // KaminoVaultClient

export class KaminoVault {
  readonly address: Address;
  state: VaultState | undefined | null;
  programId: Address;
  client: KaminoVaultClient;
  vaultReservesStateCache: Map<Address, KaminoReserve> | undefined;

  constructor(
    rpc: Rpc<SolanaRpcApi>,
    vaultAddress: Address,
    state?: VaultState,
    programId: Address = kaminoVaultId,
    recentSlotDurationMs: number = DEFAULT_RECENT_SLOT_DURATION_MS
  ) {
    this.address = vaultAddress;
    this.state = state;
    this.programId = programId;
    this.client = new KaminoVaultClient(rpc, recentSlotDurationMs);
  }

  static loadWithClientAndState(client: KaminoVaultClient, vaultAddress: Address, state: VaultState): KaminoVault {
    const vault = new KaminoVault(client.getConnection(), vaultAddress);
    vault.state = state;
    vault.programId = client.getProgramID();
    vault.client = client;
    return vault;
  }

  async getState(): Promise<VaultState> {
    if (!this.state) {
      const res = await VaultState.fetch(this.client.getConnection(), this.address, this.programId);
      if (!res) {
        throw new Error('Invalid vault');
      }
      this.state = res;
      return res;
    } else {
      return this.state;
    }
  }

  async reloadVaultReserves(): Promise<void> {
    this.vaultReservesStateCache = await this.client.loadVaultReserves(this.state!);
  }

  async reloadState(): Promise<VaultState> {
    this.state = await VaultState.fetch(this.client.getConnection(), this.address, this.programId);
    if (!this.state) {
      throw new Error('Could not fetch vault');
    }
    return this.state;
  }

  async hasFarm(vaultState?: VaultState): Promise<boolean> {
    const state = vaultState ?? (await this.getState());
    return state.vaultFarm !== DEFAULT_PUBLIC_KEY;
  }

  async hasFlcFarm(): Promise<boolean> {
    const state = await this.getState();
    return state.firstLossCapitalFarm !== DEFAULT_PUBLIC_KEY;
  }

  /**
   * This will return an VaultHoldings object which contains the amount available (uninvested) in vault, total amount invested in reseves and a breakdown of the amount invested in each reserve
   * @returns an VaultHoldings object representing the amount available (uninvested) in vault, total amount invested in reseves and a breakdown of the amount invested in each reserve
   */
  async getVaultHoldings(): Promise<VaultHoldings> {
    if (!this.state || !this.vaultReservesStateCache) {
      await this.reloadState();
      await this.reloadVaultReserves();
    }

    return await this.client.getVaultHoldings(this.state!, undefined, this.vaultReservesStateCache!, undefined);
  }

  /**
   * This will return the a map between reserve pubkey and the allocation overview for the reserve
   * @returns a map between reserve pubkey and the allocation overview for the reserve
   */
  async getVaultAllocations(): Promise<Map<Address, ReserveAllocationOverview>> {
    if (!this.state) {
      await this.reloadState();
    }

    return this.client.getVaultAllocations(this.state!);
  }

  /**
   * This will return the APY of the vault based on the current invested amounts and the theoretical APY if all the available tokens were invested
   * @returns a struct containing actualAPY and theoreticalAPY for the vault
   */
  async getAPYs(slot?: Slot): Promise<VaultAPYs> {
    if (!this.state || !this.vaultReservesStateCache) {
      await this.reloadState();
      await this.reloadVaultReserves();
    }

    const latestSlot = slot ?? (await this.client.getConnection().getSlot({ commitment: 'confirmed' }).send());
    const actualApy = await this.client.getVaultActualAPY(this.state!, latestSlot, this.vaultReservesStateCache!);
    const theoreticalApy = await this.client.getVaultTheoreticalAPY(
      this.state!,
      latestSlot,
      this.vaultReservesStateCache!
    );

    return {
      actualAPY: actualApy,
      theoreticalAPY: theoreticalApy,
    };
  }

  /**
   * This method returns the exchange rate of the vault (tokens per share)
   * @returns - Decimal representing the exchange rate (tokens per share)
   */
  async getExchangeRate(slot?: Slot): Promise<Decimal> {
    if (!this.state || !this.vaultReservesStateCache) {
      await this.reloadState();
      await this.reloadVaultReserves();
    }

    const latestSlot = slot ?? (await this.client.getConnection().getSlot({ commitment: 'confirmed' }).send());
    const tokensPerShare = await this.client.getTokensPerShareSingleVault(
      this.state!,
      latestSlot,
      this.vaultReservesStateCache
    );
    return tokensPerShare;
  }

  /**
   * This method returns the user shares balance for a given vault
   * @param user - user to calculate the shares balance for
   * @param vault - vault to calculate shares balance for
   * @returns - a struct of user share balance (staked in vault farm if the vault has a farm and unstaked) in decimal (not lamports)
   */
  async getUserShares(user: Address): Promise<UserSharesForVault> {
    return this.client.getUserSharesBalanceSingleVault(user, this);
  }

  /**
   * This function creates instructions to deposit into a vault. It will also create ATA creation instructions for the vault shares that the user receives in return
   * @param user - user to deposit
   * @param tokenAmount - token amount to be deposited, in decimals (will be converted in lamports)
   * @param [vaultReservesMap] - optional parameter; a hashmap from each reserve pubkey to the reserve state. Optional. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @param [farmState] - the state of the vault farm, if the vault has a farm. Optional. If not provided, it will be fetched
   * @returns - an instance of DepositIxs which contains the instructions to deposit in vault and the instructions to stake the shares in the farm if the vault has a farm
   */
  async depositIxs(
    user: TransactionSigner,
    tokenAmount: Decimal,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    farmState?: FarmState,
    payer?: TransactionSigner
  ): Promise<DepositIxs> {
    if (vaultReservesMap) {
      this.vaultReservesStateCache = vaultReservesMap;
    }
    return this.client.depositIxs(user, this, tokenAmount, this.vaultReservesStateCache, farmState, payer);
  }

  /**
   * This function will return the missing ATA creation instructions, as well as one or multiple withdraw instructions, based on how many reserves it's needed to withdraw from. This might have to be split in multiple transactions
   * @param user - user to withdraw
   * @param shareAmount - share amount to withdraw (in tokens, not lamports), in order to withdraw everything, any value > user share amount
   * @param slot - current slot, used to estimate the interest earned in the different reserves with allocation from the vault
   * @param [vaultReservesMap] - optional parameter; a hashmap from each reserve pubkey to the reserve state. If provided the function will be significantly faster as it will not have to fetch the reserves
   * @param [farmState] - the state of the vault farm, if the vault has a farm. Optional. If not provided, it will be fetched
   * @param [payer] - optional parameter to pass a different payer for ATA creation rent. If not provided, the user will be used
   * @returns an array of instructions to create missing ATAs if needed and the withdraw instructions
   */
  async withdrawIxs(
    user: TransactionSigner,
    shareAmount: Decimal,
    slot?: Slot,
    vaultReservesMap?: Map<Address, KaminoReserve>,
    farmState?: FarmState,
    payer?: TransactionSigner
  ): Promise<WithdrawIxs> {
    if (vaultReservesMap) {
      this.vaultReservesStateCache = vaultReservesMap;
    }

    const currentSlot = slot ?? (await this.client.getConnection().getSlot({ commitment: 'confirmed' }).send());

    return this.client.withdrawIxs(
      user,
      this,
      shareAmount,
      currentSlot,
      this.vaultReservesStateCache,
      farmState,
      payer
    );
  }
}

/**
 * Used to initialize a Kamino Vault
 */
export class KaminoVaultConfig {
  /** The admin of the vault */
  readonly admin: TransactionSigner;
  /** The token mint for the vault */
  readonly tokenMint: Address;
  /** The token mint program id */
  readonly tokenMintProgramId: Address;
  /** The performance fee rate of the vault, as percents, expressed as a decimal */
  readonly performanceFeeRatePercentage: Decimal;
  /** The management fee rate of the vault, as percents, expressed as a decimal */
  readonly managementFeeRatePercentage: Decimal;
  /** The name to be stored on chain for the vault (max 40 characters). */
  readonly name: string;
  /** The symbol of the vault token to be stored (max 5 characters). E.g. USDC for a vault using USDC as token. */
  readonly vaultTokenSymbol: string;
  /** The name of the vault token to be stored (max 10 characters), after the prefix `Kamino Vault <vaultTokenSymbol>`. E.g. USDC Vault for a vault using USDC as token. */
  readonly vaultTokenName: string;
  constructor(args: {
    admin: TransactionSigner;
    tokenMint: Address;
    tokenMintProgramId: Address;
    performanceFeeRatePercentage: Decimal;
    managementFeeRatePercentage: Decimal;
    name: string;
    vaultTokenSymbol: string;
    vaultTokenName: string;
  }) {
    this.admin = args.admin;
    this.tokenMint = args.tokenMint;
    this.performanceFeeRatePercentage = args.performanceFeeRatePercentage;
    this.managementFeeRatePercentage = args.managementFeeRatePercentage;
    this.tokenMintProgramId = args.tokenMintProgramId;
    this.name = args.name;
    this.vaultTokenSymbol = args.vaultTokenSymbol;
    this.vaultTokenName = args.vaultTokenName;
  }

  getPerformanceFeeBps(): number {
    return this.performanceFeeRatePercentage.mul(100).toNumber();
  }

  getManagementFeeBps(): number {
    return this.managementFeeRatePercentage.mul(100).toNumber();
  }
}

export class ReserveAllocationConfig {
  readonly reserve: ReserveWithAddress;
  readonly targetAllocationWeight: number;
  readonly allocationCapDecimal: Decimal;

  constructor(reserve: ReserveWithAddress, targetAllocationWeight: number, allocationCapDecimal: Decimal) {
    this.reserve = reserve;
    this.targetAllocationWeight = targetAllocationWeight;
    this.allocationCapDecimal = allocationCapDecimal;
  }

  getAllocationCapLamports(): Decimal {
    return numberToLamportsDecimal(this.allocationCapDecimal, this.reserve.state.liquidity.mintDecimals.toNumber());
  }

  getReserveState(): Reserve {
    return this.reserve.state;
  }

  getReserveAddress(): Address {
    return this.reserve.address;
  }
}

export async function getCTokenVaultPda(
  vaultAddress: Address,
  reserveAddress: Address,
  kaminoVaultProgramId: Address
): Promise<Address> {
  return (
    await getProgramDerivedAddress({
      seeds: [
        Buffer.from(CTOKEN_VAULT_SEED),
        addressEncoder.encode(vaultAddress),
        addressEncoder.encode(reserveAddress),
      ],
      programAddress: kaminoVaultProgramId,
    })
  )[0];
}

export async function getEventAuthorityPda(kaminoVaultProgramId: Address): Promise<Address> {
  return (
    await getProgramDerivedAddress({
      seeds: [Buffer.from(EVENT_AUTHORITY_SEED)],
      programAddress: kaminoVaultProgramId,
    })
  )[0];
}

export async function getKvaultGlobalConfigPda(kaminoVaultProgramId: Address): Promise<Address> {
  return (
    await getProgramDerivedAddress({
      seeds: [Buffer.from(GLOBAL_CONFIG_STATE_SEED)],
      programAddress: kaminoVaultProgramId,
    })
  )[0];
}

export async function getReserveWhitelistEntryPda(
  reserveAddress: Address,
  kaminoVaultProgramId: Address
): Promise<Address> {
  return (
    await getProgramDerivedAddress({
      seeds: [Buffer.from(WHITELISTED_RESERVES_SEED), addressEncoder.encode(reserveAddress)],
      programAddress: kaminoVaultProgramId,
    })
  )[0];
}

async function getReserveWhitelistEntryIfExists(
  reserveAddress: Address,
  rpc: Rpc<SolanaRpcApi>,
  kaminoVaultProgramId: Address
): Promise<Option<Address>> {
  const reserveWhitelistEntry = await getReserveWhitelistEntryPda(reserveAddress, kaminoVaultProgramId);
  const reserveWhitelistEntryAccount = await fetchEncodedAccount(rpc, reserveWhitelistEntry, {
    commitment: 'processed',
  });
  return reserveWhitelistEntryAccount.exists ? some(reserveWhitelistEntry) : none<Address>();
}

async function getReservesWhitelistPDAs(reserves: Address[], kaminoVaultProgramId: Address): Promise<Address[]> {
  return Promise.all(reserves.map((reserve) => getReserveWhitelistEntryPda(reserve, kaminoVaultProgramId)));
}

function deduplicateVaults(vaults: KaminoVault[]): KaminoVault[] {
  const seen = new Set<Address>();
  return vaults.filter((vault) => {
    if (seen.has(vault.address)) {
      return false;
    }
    seen.add(vault.address);
    return true;
  });
}

function parseVaultAdmin(vault: VaultState, signer?: TransactionSigner) {
  return signer ?? noopSigner(vault.vaultAdminAuthority);
}

function parseVaultPendingAdmin(vault: VaultState, signer?: TransactionSigner) {
  return signer ?? noopSigner(vault.pendingAdmin);
}

export type VaultHolder = {
  holderPubkey: Address;
  amount: Decimal;
};

export type APY = {
  grossAPY: Decimal;
  netAPY: Decimal;
};

export type VaultAPYs = {
  theoreticalAPY: APY;
  actualAPY: APY;
};

export class VaultHoldings {
  available: Decimal;
  invested: Decimal;
  investedInReserves: Map<Address, Decimal>;
  pendingFees: Decimal;
  totalAUMIncludingFees: Decimal;

  constructor(params: {
    available: Decimal;
    invested: Decimal;
    investedInReserves: Map<Address, Decimal>;
    pendingFees: Decimal;
    totalAUMIncludingFees: Decimal;
  }) {
    this.available = params.available;
    this.invested = params.invested;
    this.investedInReserves = params.investedInReserves;
    this.pendingFees = params.pendingFees;
    this.totalAUMIncludingFees = params.totalAUMIncludingFees;
  }

  asJSON() {
    return {
      available: this.available.toString(),
      invested: this.invested.toString(),
      totalAUMIncludingFees: this.totalAUMIncludingFees.toString(),
      pendingFees: this.pendingFees.toString(),
      investedInReserves: pubkeyHashMapToJson(this.investedInReserves),
    };
  }

  print() {
    console.log('Holdings:');
    console.log('  Available:', this.available.toString());
    console.log('  Invested:', this.invested.toString());
    console.log('  Total AUM including fees:', this.totalAUMIncludingFees.toString());
    console.log('  Pending fees:', this.pendingFees.toString());
    console.log('  Invested in reserves:', pubkeyHashMapToJson(this.investedInReserves));
  }
}

/**
 * earnedInterest represents the interest earned from now until the slot provided in the future
 */
export type SimulatedVaultHoldingsWithEarnedInterest = {
  holdings: VaultHoldings;
  earnedInterest: Decimal;
};

export type VaultHoldingsWithUSDValue = {
  holdings: VaultHoldings;
  availableUSD: Decimal;
  investedUSD: Decimal;
  investedInReservesUSD: Map<Address, Decimal>;
  totalUSDIncludingFees: Decimal;
  pendingFeesUSD: Decimal;
};

export type ReserveOverview = {
  supplyAPY: Decimal;
  utilizationRatio: Decimal;
  liquidationThresholdPct: Decimal;
  totalBorrowedAmount: Decimal;
  amountBorrowedFromSupplied: Decimal;
  suppliedAmount: Decimal;
  market: Address;
};

export type VaultReserveTotalBorrowedAndInvested = {
  totalInvested: Decimal;
  totalBorrowed: Decimal;
  utilizationRatio: Decimal;
};

export type MarketOverview = {
  address: Address;
  reservesAsCollateral: ReserveAsCollateral[]; // this MarketOverview has the reserve the caller calls for as the debt reserve and all the others as collateral reserves, so the debt reserve is not included here
  minLTVPct: Decimal;
  maxLTVPct: Decimal;
};

export type ReserveAsCollateral = {
  mint: Address;
  liquidationLTVPct: Decimal;
  address: Address;
};

export type VaultOverview = {
  holdingsUSD: VaultHoldingsWithUSDValue;
  reservesOverview: Map<Address, ReserveOverview>;
  vaultCollaterals: Map<Address, MarketOverview>;
  theoreticalSupplyAPY: APYs;
  actualSupplyAPY: APYs;
  vaultFarmIncentives: FarmIncentives;
  reservesFarmsIncentives: VaultReservesFarmsIncentives;
  delegatedFarmIncentives: FarmIncentives;
  totalBorrowed: Decimal;
  totalBorrowedUSD: Decimal;
  totalSupplied: Decimal;
  totalSuppliedUSD: Decimal;
  utilizationRatio: Decimal;
  flcFarmStats: FlcFarmStats | undefined;
  withdrawalPenalties: WithdrawPenalties;
};

export type VaultReservesFarmsIncentives = {
  reserveFarmsIncentives: Map<Address, FarmIncentives>;
  totalIncentivesAPY: Decimal;
};

export type FlcFarmStats = {
  address: Address;
  farmState: FarmState;
  totalStakedShares: Decimal;
  withdrawalCooldownDurationSeconds: number;
  isPendingUnstake: boolean;
  pendingUnstakeInfo: FarmPendingUnstakeInfo[];
};

export type FarmPendingUnstakeInfo = {
  userStateAddress: Address;
  pendingUnstakeAmountLamports: Decimal;
  pendingUnstakeAvailableAtTimestamp: number;
};

export type VaultFeesPct = {
  managementFeePct: Decimal;
  performanceFeePct: Decimal;
};

export type VaultFees = {
  managementFee: Decimal;
  performanceFee: Decimal;
};

export type VaultCumulativeInterestWithTimestamp = {
  cumulativeInterest: Decimal;
  timestamp: number;
};

export type PendingRewardsForUserInVault = {
  pendingRewardsInVaultFarm: Map<Address, Decimal>;
  pendingRewardsInVaultDelegatedFarm: Map<Address, Decimal>;
  pendingRewardsInVaultReservesFarms: Map<Address, Decimal>;
  totalPendingRewards: Map<Address, Decimal>;
};

type ReserveExitBuilderParams = {
  user: TransactionSigner;
  vault: KaminoVault;
  vaultState: VaultState;
  marketAddress: Address;
  reserve: ReserveWithAddress;
  userSharesAta: Address;
  userTokenAta: Address;
  shareAmountLamports: Decimal;
  vaultReservesState: Map<Address, KaminoReserve>;
};

type ReserveExitInstructionBuilder = (params: ReserveExitBuilderParams) => Promise<Instruction>;

type BuildReserveExitIxsParams = {
  user: TransactionSigner;
  vault: KaminoVault;
  vaultState: VaultState;
  shareAmount: Decimal;
  allUserShares: Decimal;
  slot: Slot;
  vaultReservesMap?: Map<Address, KaminoReserve>;
  builder: ReserveExitInstructionBuilder;
  payer?: TransactionSigner;
};

export type WithdrawPenalties = {
  withdrawalPenaltyLamports: Decimal;
  withdrawalPenaltyBps: Decimal;
};
