@kamino-finance/klend-sdk
Version:
Typescript SDK for interacting with the Kamino Lending (klend) protocol
845 lines • 241 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VaultHoldings = exports.ReserveAllocationConfig = exports.KaminoVaultConfig = exports.KaminoVault = exports.KaminoVaultClient = exports.DEFAULT_CU_PER_TX = exports.INITIAL_DEPOSIT_LAMPORTS = exports.METADATA_PROGRAM_ID = exports.METADATA_SEED = exports.kaminoVaultStagingId = exports.kaminoVaultId = void 0;
exports.getCTokenVaultPda = getCTokenVaultPda;
exports.getEventAuthorityPda = getEventAuthorityPda;
exports.getKvaultGlobalConfigPda = getKvaultGlobalConfigPda;
exports.getReserveWhitelistEntryPda = getReserveWhitelistEntryPda;
const bn_js_1 = __importDefault(require("bn.js"));
const kit_1 = require("@solana/kit");
const lib_1 = require("../lib");
const instructions_1 = require("../@codegen/kvault/instructions");
const types_1 = require("../@codegen/kvault/types");
const accounts_1 = require("../@codegen/kvault/accounts");
const decimal_js_1 = __importDefault(require("decimal.js"));
const utils_1 = require("./utils");
const programId_1 = require("../@codegen/klend/programId");
const fraction_1 = require("./fraction");
const utils_2 = require("../utils");
const utils_3 = require("../utils");
const kliquidity_sdk_1 = require("@kamino-finance/kliquidity-sdk");
const CreationParameters_1 = require("@kamino-finance/kliquidity-sdk/dist/utils/CreationParameters");
const dist_1 = require("@kamino-finance/farms-sdk/dist");
const utils_4 = require("../utils");
const farm_utils_1 = require("./farm_utils");
const system_1 = require("@solana-program/system");
const metadata_1 = require("../utils/metadata");
const vault_1 = require("../utils/vault");
const token_2022_1 = require("@solana-program/token-2022");
const token_1 = require("@solana-program/token");
const sysvars_1 = require("@solana/sysvars");
const signer_1 = require("../utils/signer");
const farms_sdk_1 = require("@kamino-finance/farms-sdk");
const vaultAllocation_1 = require("../utils/vaultAllocation");
const farmUtils_1 = require("../utils/farmUtils");
const readCdnData_1 = require("../utils/readCdnData");
const multisig_1 = require("../utils/multisig");
const updateGlobalConfigAdmin_1 = require("../@codegen/kvault/instructions/updateGlobalConfigAdmin");
exports.kaminoVaultId = (0, kit_1.address)('KvauGMspG5k6rtzrqqn7WNn3oZdyKqLKwK2XWQ8FLjd');
exports.kaminoVaultStagingId = (0, kit_1.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';
exports.METADATA_SEED = 'metadata';
const GLOBAL_CONFIG_STATE_SEED = 'global_config';
const WHITELISTED_RESERVES_SEED = 'whitelisted_reserves';
exports.METADATA_PROGRAM_ID = (0, kit_1.address)('metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s');
exports.INITIAL_DEPOSIT_LAMPORTS = 1000;
exports.DEFAULT_CU_PER_TX = 1_400_000;
const addressEncoder = (0, kit_1.getAddressEncoder)();
const base58Decoder = (0, kit_1.getBase58Decoder)();
/**
* KaminoVaultClient is a class that provides a high-level interface to interact with the Kamino Vault program.
*/
class KaminoVaultClient {
_rpc;
_kaminoVaultProgramId;
_kaminoLendProgramId;
_farmsProgramId;
recentSlotDurationMs;
// CDN cache
_cdnResources;
_cdnResourcesPromise;
constructor(rpc, recentSlotDurationMs, kaminoVaultprogramId, kaminoLendProgramId, cdnResources, farmsProgramId) {
this._rpc = rpc;
this.recentSlotDurationMs = recentSlotDurationMs;
this._kaminoVaultProgramId = kaminoVaultprogramId ? kaminoVaultprogramId : exports.kaminoVaultId;
this._kaminoLendProgramId = kaminoLendProgramId ? kaminoLendProgramId : programId_1.PROGRAM_ID;
this._farmsProgramId = farmsProgramId;
this._cdnResources = cdnResources;
}
getConnection() {
return this._rpc;
}
getProgramID() {
return this._kaminoVaultProgramId;
}
getRpc() {
return this._rpc;
}
hasFarm() {
return;
}
async loadCdnResourcesOnce() {
if (this._cdnResources) {
return this._cdnResources;
}
if (this._cdnResourcesPromise) {
return this._cdnResourcesPromise;
}
this._cdnResourcesPromise = (async () => {
const response = await fetch(`${utils_2.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());
const delegatedVaultFarms = raw['mainnet-beta']?.delegatedVaultFarms;
if (!delegatedVaultFarms) {
return undefined;
}
const riskManagers = raw['mainnet-beta']?.riskManagers ?? {};
const parsed = { 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) {
const result = {
errors: [],
warnings: [],
success: true,
};
const vaultState = await vault.getState();
// 1. Check owner is multisig
try {
const isMultisig = await (0, multisig_1.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 === lib_1.DEFAULT_PUBLIC_KEY) {
result.errors.push('Vault farm is not set');
}
else {
const farmState = await dist_1.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 === lib_1.DEFAULT_PUBLIC_KEY) {
result.warnings.push('First loss capital farm is not set');
}
else {
const flcFarmState = await dist_1.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 (0, utils_2.getKVaultSharesMetadataPda)(vaultState.sharesMint);
const metadataAccount = await (0, kit_1.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 !== lib_1.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() !== utils_2.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) => 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, vaultState, slot) {
const vault = vaultState ? vaultState : await accounts_1.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_js_1.default(vault.sharesIssued.toString()).div(new decimal_js_1.default(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) {
const globalConfigAddress = await getKvaultGlobalConfigPda(this.getProgramID());
const programData = await (0, utils_2.programDataPda)(this.getProgramID());
const ix = (0, instructions_1.initKVaultGlobalConfig)({
payer: admin,
globalConfig: globalConfigAddress,
programData: programData,
systemProgram: system_1.SYSTEM_PROGRAM_ADDRESS,
rent: sysvars_1.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, value) {
console.log('in updateGlobalConfigIx');
let modeEnum;
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 = (0, kit_1.address)(value);
modeEnum = new types_1.UpdateGlobalConfigMode.PendingAdmin([addr]);
break;
}
case 'MinWithdrawalPenaltyLamports': {
modeEnum = new types_1.UpdateGlobalConfigMode.MinWithdrawalPenaltyLamports([new bn_js_1.default(value)]);
break;
}
case 'MinWithdrawalPenaltyBPS': {
modeEnum = new types_1.UpdateGlobalConfigMode.MinWithdrawalPenaltyBPS([new bn_js_1.default(value)]);
break;
}
default:
throw new Error(`Unknown update mode: ${mode}`);
}
const args = {
update: modeEnum,
};
const globalConfigAddress = await getKvaultGlobalConfigPda(this.getProgramID());
const globalConfigState = await lib_1.KVaultGlobalConfig.fetch(this.getConnection(), globalConfigAddress);
if (!globalConfigState) {
throw new Error('Global config not found');
}
const admin = globalConfigState.globalAdmin;
const accounts = {
globalAdmin: (0, signer_1.noopSigner)(admin),
globalConfig: globalConfigAddress,
};
return (0, instructions_1.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) {
const globalConfigAddress = await getKvaultGlobalConfigPda(this.getProgramID());
const accounts = {
pendingAdmin: admin,
globalConfig: globalConfigAddress,
};
return (0, updateGlobalConfigAdmin_1.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, useDevnetFarms = false, slot) {
const vaultState = await (0, kit_1.generateKeyPairSigner)();
const size = BigInt(accounts_1.VaultState.layout.span + 8);
const createVaultIx = (0, system_1.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(),
(0, kit_1.getProgramDerivedAddress)({
seeds: [Buffer.from(TOKEN_VAULT_SEED), addressEncoder.encode(vaultState.address)],
programAddress: this._kaminoVaultProgramId,
}),
(0, kit_1.getProgramDerivedAddress)({
seeds: [Buffer.from(BASE_VAULT_AUTHORITY_SEED), addressEncoder.encode(vaultState.address)],
programAddress: this._kaminoVaultProgramId,
}),
(0, kit_1.getProgramDerivedAddress)({
seeds: [Buffer.from(SHARES_SEED), addressEncoder.encode(vaultState.address)],
programAddress: this._kaminoVaultProgramId,
}),
]);
let adminTokenAccount;
const prerequisiteIxs = [];
const cleanupIxs = [];
if (vaultConfig.tokenMint === lib_1.WRAPPED_SOL_MINT) {
const { wsolAta, createAtaIxs, closeAtaIxs } = await (0, utils_2.createWsolAtaIfMissing)(this.getConnection(), new decimal_js_1.default(utils_2.VAULT_INITIAL_DEPOSIT), vaultConfig.admin, vaultConfig.tokenMintProgramId);
adminTokenAccount = wsolAta;
prerequisiteIxs.push(...createAtaIxs);
cleanupIxs.push(...closeAtaIxs);
}
else {
adminTokenAccount = (await (0, token_2022_1.findAssociatedTokenPda)({
mint: vaultConfig.tokenMint,
tokenProgram: vaultConfig.tokenMintProgramId,
owner: vaultConfig.admin.address,
}))[0];
}
const initVaultAccounts = {
adminAuthority: vaultConfig.admin,
vaultState: vaultState.address,
baseTokenMint: vaultConfig.tokenMint,
tokenVault,
baseVaultAuthority,
sharesMint,
systemProgram: system_1.SYSTEM_PROGRAM_ADDRESS,
rent: sysvars_1.SYSVAR_RENT_ADDRESS,
tokenProgram: vaultConfig.tokenMintProgramId,
sharesTokenProgram: token_1.TOKEN_PROGRAM_ADDRESS,
adminTokenAccount,
};
const initVaultIx = (0, instructions_1.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 (0, utils_4.initLookupTableIx)(vaultConfig.admin, resolvedSlot);
const farmsGlobalConfig = useDevnetFarms ? farm_utils_1.FARMS_GLOBAL_CONFIG_DEVNET : farm_utils_1.FARMS_GLOBAL_CONFIG_MAINNET;
const accountsToBeInserted = [
vaultConfig.admin.address,
vaultState.address,
vaultConfig.tokenMint,
vaultConfig.tokenMintProgramId,
baseVaultAuthority,
sharesMint,
system_1.SYSTEM_PROGRAM_ADDRESS,
sysvars_1.SYSVAR_RENT_ADDRESS,
token_1.TOKEN_PROGRAM_ADDRESS,
this._kaminoLendProgramId,
sysvars_1.SYSVAR_INSTRUCTIONS_ADDRESS,
createVaultFarm.farm.address,
farmsGlobalConfig,
];
const insertIntoLUTIxs = await (0, utils_4.insertIntoLookupTableIxs)(this.getConnection(), vaultConfig.admin, lut, accountsToBeInserted, []);
const setLUTIx = await this.updateUninitialisedVaultConfigIx(vaultConfig.admin, vaultState.address, new types_1.VaultConfigField.LookupTable(), lut.toString());
const ixs = [createVaultIx, initVaultIx, setLUTIx];
if (vaultConfig.getPerformanceFeeBps() > 0) {
const setPerformanceFeeIx = await this.updateUninitialisedVaultConfigIx(vaultConfig.admin, vaultState.address, new types_1.VaultConfigField.PerformanceFeeBps(), vaultConfig.getPerformanceFeeBps().toString());
ixs.push(setPerformanceFeeIx);
}
if (vaultConfig.getManagementFeeBps() > 0) {
const setManagementFeeIx = await this.updateUninitialisedVaultConfigIx(vaultConfig.admin, vaultState.address, new types_1.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 types_1.VaultConfigField.Name(), vaultConfig.name);
ixs.push(setNameIx);
}
const setFarmIx = await this.updateUninitialisedVaultConfigIx(vaultConfig.admin, vaultState.address, new types_1.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, vaultAddress, vaultSharesMint, useDevnetFarms = false) {
const farmsSDK = new farms_sdk_1.Farms(this._rpc, this._farmsProgramId);
const globalConfig = useDevnetFarms ? farm_utils_1.FARMS_GLOBAL_CONFIG_DEVNET : farm_utils_1.FARMS_GLOBAL_CONFIG_MAINNET;
const farm = await (0, kit_1.generateKeyPairSigner)();
const ixs = await farmsSDK.createFarmIxs(signer, farm, globalConfig, vaultSharesMint);
const updatePendingFarmAdminIx = await farmsSDK.updateFarmConfigIx(signer, farm.address, lib_1.DEFAULT_PUBLIC_KEY, new dist_1.FarmConfigOption.UpdatePendingFarmAdmin(), farm_utils_1.FARMS_ADMIN_MAINNET, undefined, undefined, true);
const updateFarmVaultIdIx = await farmsSDK.updateFarmConfigIx(signer, farm.address, lib_1.DEFAULT_PUBLIC_KEY, new dist_1.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, vaultAdmin, vault, sharesMint, baseVaultAuthority, tokenName, extraName, metadataProgramId = exports.METADATA_PROGRAM_ID, kvaultProgramId) {
const kvaultProgramIdToUse = kvaultProgramId ?? this._kaminoVaultProgramId;
const [sharesMintMetadata] = await (0, utils_2.getKVaultSharesMetadataPda)(sharesMint, metadataProgramId);
const { name, symbol, uri } = (0, metadata_1.resolveMetadata)(sharesMint, extraName, tokenName);
const ix = !(await (0, kit_1.fetchEncodedAccount)(rpc, sharesMintMetadata, { commitment: 'processed' })).exists
? await (0, metadata_1.getInitializeKVaultSharesMetadataIx)(vaultAdmin, vault, sharesMint, baseVaultAuthority, name, symbol, uri, metadataProgramId, kvaultProgramIdToUse)
: await (0, metadata_1.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, reserveAllocationConfig, vaultAdminAuthority) {
const vaultState = await vault.getState();
const reserveState = 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 = {
signer: vaultAdmin,
vaultState: vault.address,
baseVaultAuthority: vaultState.baseVaultAuthority,
reserveCollateralMint: reserveState.collateral.mintPubkey,
reserve: reserveAllocationConfig.getReserveAddress(),
ctokenVault: cTokenVault,
reserveWhitelistEntry: reserveWhitelistEntryOption,
systemProgram: system_1.SYSTEM_PROGRAM_ADDRESS,
rent: sysvars_1.SYSVAR_RENT_ADDRESS,
reserveCollateralTokenProgram: token_1.TOKEN_PROGRAM_ADDRESS,
};
const updateReserveAllocationArgs = {
weight: new bn_js_1.default(reserveAllocationConfig.targetAllocationWeight),
cap: new bn_js_1.default(reserveAllocationConfig.getAllocationCapLamports().floor().toString()),
};
const updateReserveAllocationIx = (0, instructions_1.updateReserveAllocation)(updateReserveAllocationArgs, updateReserveAllocationAccounts, undefined, this._kaminoVaultProgramId);
const accountsToAddToLut = [
reserveAllocationConfig.getReserveAddress(),
cTokenVault,
...this.getReserveAccountsToInsertInLut(reserveState),
];
const [lendingMarketAuth] = await (0, utils_2.lendingMarketAuthPda)(reserveState.lendingMarket, this._kaminoLendProgramId);
accountsToAddToLut.push(lendingMarketAuth);
const insertIntoLutIxs = await (0, utils_4.insertIntoLookupTableIxs)(this.getConnection(), vaultAdmin, vaultState.vaultLookupTable, accountsToAddToLut);
const 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, vaultAdminAuthority, unallocatedWeight, unallocatedCap) {
const vaultState = await vault.getState();
const unallocatedWeightToUse = unallocatedWeight ? unallocatedWeight : vaultState.unallocatedWeight;
const unallocatedCapToUse = unallocatedCap ? unallocatedCap : vaultState.unallocatedTokensCap;
const ixs = [];
if (!unallocatedWeightToUse.eq(vaultState.unallocatedWeight)) {
const updateVaultUnallocatedWeightIx = await this.updateVaultConfigIxs(vault, new types_1.VaultConfigField.UnallocatedWeight(), unallocatedWeightToUse.toString(), vaultAdminAuthority);
ixs.push(updateVaultUnallocatedWeightIx.updateVaultConfigIx);
}
if (!unallocatedCapToUse.eq(vaultState.unallocatedTokensCap)) {
const updateVaultUnallocatedCapIx = await this.updateVaultConfigIxs(vault, new types_1.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, reserve, vaultAdminAuthority) {
const vaultState = await vault.getState();
const reserveIsPartOfAllocation = vaultState.vaultAllocationStrategy.some((allocation) => allocation.reserve === reserve);
const withdrawAndBlockReserveIxs = {
updateReserveAllocationIxs: [],
investIxs: [],
};
if (!reserveIsPartOfAllocation) {
return withdrawAndBlockReserveIxs;
}
const reserveState = await lib_1.Reserve.fetch(this.getConnection(), reserve);
if (reserveState === null) {
return withdrawAndBlockReserveIxs;
}
const reserveWithAddress = {
address: reserve,
state: reserveState,
};
const reserveAllocationConfig = new ReserveAllocationConfig(reserveWithAddress, 0, new decimal_js_1.default(0));
const admin = vaultAdminAuthority ? vaultAdminAuthority : (0, signer_1.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, vaultReservesMap, payer) {
const vaultState = await vault.getState();
const reserves = this.getVaultReserves(vaultState);
const withdrawAndBlockReserveIxs = {
updateReserveAllocationIxs: [],
investIxs: [],
};
if (!vaultReservesMap) {
vaultReservesMap = await this.loadVaultReserves(vaultState);
}
for (const reserve of reserves) {
const reserveWithAddress = {
address: reserve,
state: vaultReservesMap.get(reserve).state,
};
const reserveAllocationConfig = new ReserveAllocationConfig(reserveWithAddress, 0, new decimal_js_1.default(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 : (0, signer_1.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, vaultReservesMap, payer) {
const vaultState = await vault.getState();
const reserves = this.getVaultReserves(vaultState);
const disinvestAllReservesIxs = {
updateReserveAllocationIxs: [],
investIxs: [],
};
if (!vaultReservesMap) {
vaultReservesMap = await this.loadVaultReserves(vaultState);
}
for (const reserve of reserves) {
const 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, (0, lib_1.lamportsToDecimal)(new decimal_js_1.default(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 : (0, signer_1.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, reserve, vaultAdminAuthority) {
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 = {
vaultAdminAuthority: vaultAdmin,
vaultState: vault.address,
reserve,
};
return (0, instructions_1.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, mode, value, adminAuthority, lutIxsSigner, skipLutUpdate = false, errorOnOverride = true) {
const vaultState = await vault.getState();
const admin = parseVaultAdmin(vaultState, adminAuthority);
const globalConfig = await getKvaultGlobalConfigPda(this._kaminoVaultProgramId);
const updateVaultConfigAccs = {
signer: admin,
globalConfig: globalConfig,
vaultState: vault.address,
klendProgram: this._kaminoLendProgramId,
};
if (mode.kind === new types_1.VaultConfigField.Farm().kind) {
if (value != lib_1.DEFAULT_PUBLIC_KEY && vaultState.vaultFarm != lib_1.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 = {
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 = (0, instructions_1.updateVaultConfig)(updateVaultConfigArgs, updateVaultConfigAccs, undefined, this._kaminoVaultProgramId);
updateVaultConfigIx = this.appendRemainingAccountsForVaultReserves(updateVaultConfigIx, vaultReserves, vaultReservesState);
const updateLUTIxs = [];
if (!skipLutUpdate) {
const lutIxsSignerAccount = lutIxsSigner ? lutIxsSigner : admin;
if (mode.kind === new types_1.VaultConfigField.PendingVaultAdmin().kind) {
const newPubkey = (0, kit_1.address)(value);
const insertIntoLutIxs = await (0, utils_4.insertIntoLookupTableIxs)(this.getConnection(), lutIxsSignerAccount, vaultState.vaultLookupTable, [newPubkey]);
updateLUTIxs.push(...insertIntoLutIxs);
}
else if (mode.kind === new types_1.VaultConfigField.Farm().kind) {
const keysToAddToLUT = [(0, kit_1.address)(value)];
// if the farm already exist we want to read its state to add it to the LUT
try {
const farmState = await dist_1.FarmState.fetch(this.getConnection(), keysToAddToLUT[0], this._farmsProgramId);
keysToAddToLUT.push(farmState.farmVault, farmState.farmVaultsAuthority, farmState.token.mint, farmState.scopePrices, farmState.globalConfig);
const insertIntoLutIxs = await (0, utils_4.insertIntoLookupTableIxs)(this.getConnection(), lutIxsSignerAccount, vaultState.vaultLookupTable, keysToAddToLUT);
updateLUTIxs.push(...insertIntoLutIxs);
}
catch (error) {
console.log(`Error fetching farm ${keysToAddToLUT[0].toString()} state`, error);
}
}
}
const 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, feeBps, vaultAdminAuthority) {
return this.updateVaultConfigIxs(vault, new types_1.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, feeBps, vaultAdminAuthority) {
return this.updateVaultConfigIxs(vault, new types_1.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, newAdmin, vaultAdminAuthority, lutIxsSigner, skipLutUpdate = false) {
return this.updateVaultConfigIxs(vault, new types_1.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, name, vaultAdminAuthority) {
return this.updateVaultConfigIxs(vault, new types_1.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, lookupTable, vaultAdminAuthority) {
return this.updateVaultConfigIxs(vault, new types_1.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, allocationAdmin, vaultAdminAuthority) {
return this.updateVaultConfigIxs(vault, new types_1.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, unallocatedWeight, vaultAdminAuthority) {
return this.updateVaultConfigIxs(vault, new types_1.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, unallocatedTokensCap, vaultAdminAuthority) {
return this.updateVaultConfigIxs(vault, new types_1.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, farm, errorOnOverride = true, vaultAdminAuthority, lutIxsSigner, skipLutUpdate = false) {
return this.updateVaultConfigIxs(vault, new types_1.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, farm, vaultAdminAuthority) {
return this.updateVaultConfigIxs(vault, new types_1.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, minDepositAmount, vaultAdminAuthority) {
return this.updateVaultConfigIxs(vault, new types_1.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, minWithdrawAmount, vaultAdminAuthority) {
return this.updateVaultConfigIxs(vault, new types_1.VaultConfigField.MinWithdrawAmount