@kamino-finance/klend-sdk
Version:
Typescript SDK for interacting with the Kamino Lending (klend) protocol
97 lines • 4 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.extendLookupTableIxs = void 0;
exports.printAddressLookupTable = printAddressLookupTable;
exports.createLookupTableIx = createLookupTableIx;
exports.extendLookupTableChunkIx = extendLookupTableChunkIx;
exports.initLookupTableIx = initLookupTableIx;
exports.deactivateLookupTableIx = deactivateLookupTableIx;
exports.closeLookupTableIx = closeLookupTableIx;
exports.getAccountsInLUT = getAccountsInLUT;
const web3_js_1 = require("@solana/web3.js");
async function printAddressLookupTable(connection, lookupTablePk) {
const lookupTableAccount = (await connection.getAddressLookupTable(lookupTablePk)).value;
if (!lookupTableAccount) {
console.error('Lookup table is not found');
}
console.log(`Lookup table account, ${lookupTablePk.toString()}`);
lookupTableAccount?.state.addresses.forEach((address, index) => {
console.log(`Address: ${address.toString()} at index ${index}`);
});
}
async function createLookupTableIx(connection, wallet) {
return initLookupTableIx(wallet, await connection.getSlot('confirmed'));
}
function extendLookupTableChunkIx(wallet, lookupTablePk, keys, payer = web3_js_1.PublicKey.default) {
return web3_js_1.AddressLookupTableProgram.extendLookupTable({
authority: wallet,
payer: payer.equals(web3_js_1.PublicKey.default) ? wallet : payer,
lookupTable: lookupTablePk,
addresses: keys,
});
}
const extendLookupTableIxs = (wallet, table, keys, payer = web3_js_1.PublicKey.default) => {
const chunkSize = 25;
const extendLookupIxs = [];
for (let i = 0; i < keys.length; i += chunkSize) {
const chunk = keys.slice(i, i + chunkSize);
extendLookupIxs.push(extendLookupTableChunkIx(wallet, table, chunk, payer));
}
return extendLookupIxs;
};
exports.extendLookupTableIxs = extendLookupTableIxs;
/**
* This method retuns an instruction that creates a lookup table, alongside the pubkey of the lookup table
* @param payer - the owner of the lookup table
* @param slot - the current slot
* @returns - the instruction to create the lookup table and its address
*/
function initLookupTableIx(payer, slot) {
const [ix, address] = web3_js_1.AddressLookupTableProgram.createLookupTable({
authority: payer,
payer,
recentSlot: slot,
});
return [ix, address];
}
/**
* This method retuns an instruction that deactivates a lookup table, which is needed to close it
* @param payer - the owner of the lookup table
* @param lookupTable - the lookup table to deactivate
* @returns - the instruction to deactivate the lookup table
*/
function deactivateLookupTableIx(payer, lookupTable) {
const ix = web3_js_1.AddressLookupTableProgram.deactivateLookupTable({
authority: payer,
lookupTable: lookupTable,
});
return ix;
}
/**
* This method returns an instruction that closes a lookup table. That lookup table needs to be disabled at least 500 blocks before closing it.
* @param payer - the owner of the lookup table
* @param lookupTable - the lookup table to close
* @returns - the instruction to close the lookup table
*/
/// this require the LUT to be deactivated at least 500 blocks before
function closeLookupTableIx(payer, lookupTable) {
const ix = web3_js_1.AddressLookupTableProgram.closeLookupTable({
authority: payer,
recipient: payer,
lookupTable: lookupTable,
});
return ix;
}
/**
* Returns the accounts in a lookup table
* @param lookupTable - lookup table to get the accounts from
* @returns - an array of accounts in the lookup table
*/
async function getAccountsInLUT(connection, lookupTable) {
const lutState = await connection.getAddressLookupTable(lookupTable);
if (!lutState || !lutState.value) {
throw new Error(`Lookup table ${lookupTable} not found`);
}
return lutState.value.state.addresses;
}
//# sourceMappingURL=lookupTable.js.map
;