"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EVENT_AUTHORITY_SEED = exports.BASE_SEED_FARM_USER_STATE = exports.BASE_SEED_USER_STATE = exports.OWNER_QUEUED_COLLATERAL_VAULT_SEED = exports.WITHDRAW_TICKET_SEED = exports.BASE_SEED_GLOBAL_CONFIG_STATE = exports.BASE_SEED_SHORT_URL = exports.BASE_SEED_REFERRER_STATE = exports.BASE_SEED_REFERRER_TOKEN_STATE = exports.BASE_SEED_USER_METADATA = exports.RESERVE_COLL_SUPPLY_SEED = exports.RESERVE_COLL_MINT_SEED = exports.FEE_RECEIVER_SEED = exports.RESERVE_LIQ_SUPPLY_SEED = exports.LENDING_MARKET_AUTH_SEED = void 0;
exports.reservePdas = reservePdas;
exports.lendingMarketAuthPda = lendingMarketAuthPda;
exports.reserveLiqSupplyPda = reserveLiqSupplyPda;
exports.reserveFeeVaultPda = reserveFeeVaultPda;
exports.reserveCollateralMintPda = reserveCollateralMintPda;
exports.reserveCollateralSupplyPda = reserveCollateralSupplyPda;
exports.userMetadataPda = userMetadataPda;
exports.referrerTokenStatePda = referrerTokenStatePda;
exports.referrerStatePda = referrerStatePda;
exports.shortUrlPda = shortUrlPda;
exports.globalConfigPda = globalConfigPda;
exports.programDataPda = programDataPda;
exports.obligationFarmStatePda = obligationFarmStatePda;
exports.getKVaultSharesMetadataPda = getKVaultSharesMetadataPda;
exports.withdrawTicketPda = withdrawTicketPda;
exports.ownerQueuedCollateralVaultPda = ownerQueuedCollateralVaultPda;
exports.getEventAuthorityPda = getEventAuthorityPda;
const kit_1 = require("@solana/kit");
const programId_1 = require("../@codegen/klend/programId");
const programId_2 = require("@kamino-finance/farms-sdk/dist/@codegen/farms/programId");
const vault_1 = require("../classes/vault");
const buffer_1 = require("buffer");
/**
 * Lending market authority seed
 */
exports.LENDING_MARKET_AUTH_SEED = 'lma';
/**
 * Reserve liquidity supply seed
 */
exports.RESERVE_LIQ_SUPPLY_SEED = 'reserve_liq_supply';
/**
 * Reserve fee vault seed
 */
exports.FEE_RECEIVER_SEED = 'fee_receiver';
/**
 * Reserve collateral mint seed
 */
exports.RESERVE_COLL_MINT_SEED = 'reserve_coll_mint';
/**
 * Reserve collateral supply seed
 */
exports.RESERVE_COLL_SUPPLY_SEED = 'reserve_coll_supply';
/**
 * User metadata seed
 */
exports.BASE_SEED_USER_METADATA = 'user_meta';
/**
 * Referrer token state seed
 */
exports.BASE_SEED_REFERRER_TOKEN_STATE = 'referrer_acc';
/**
 * Referrer state seed
 */
exports.BASE_SEED_REFERRER_STATE = 'ref_state';
/**
 * Short url seed
 */
exports.BASE_SEED_SHORT_URL = 'short_url';
/**
 * Short url seed
 */
exports.BASE_SEED_GLOBAL_CONFIG_STATE = 'global_config';
/**
 * Withdraw ticket seed
 */
exports.WITHDRAW_TICKET_SEED = 'withdraw_ticket';
/**
 * Owner queued collateral vault seed
 */
exports.OWNER_QUEUED_COLLATERAL_VAULT_SEED = 'owner_queued_collateral_vault';
/**
 * Farm user state seed
 */
exports.BASE_SEED_USER_STATE = 'user';
/**
 * User farm state seed
 */
exports.BASE_SEED_FARM_USER_STATE = buffer_1.Buffer.from('user');
/**
 * Event authority seed
 */
exports.EVENT_AUTHORITY_SEED = '__event_authority';
const addressEncoder = (0, kit_1.getAddressEncoder)();
/**
 * Returns all the PDAs for the given reserve
 * @param programId
 * @param market
 * @param mint
 * @returns ReservePdas
 */
async function reservePdas(programId, reserve) {
    const [[liquiditySupplyVault], [collateralMint], [collateralSupplyVault], [feeVault]] = await Promise.all([
        reserveLiqSupplyPda(reserve, programId),
        reserveCollateralMintPda(reserve, programId),
        reserveCollateralSupplyPda(reserve, programId),
        reserveFeeVaultPda(reserve, programId),
    ]);
    return {
        liquiditySupplyVault,
        collateralMint,
        collateralSupplyVault,
        feeVault,
    };
}
/**
 * Returns the PDA and bump for the lending market authority
 * @param lendingMarket
 * @param programId
 * @returns [authority, bump]
 */
function lendingMarketAuthPda(lendingMarket, programId = programId_1.PROGRAM_ID) {
    return (0, kit_1.getProgramDerivedAddress)({
        seeds: [buffer_1.Buffer.from(exports.LENDING_MARKET_AUTH_SEED), addressEncoder.encode(lendingMarket)],
        programAddress: programId,
    });
}
/**
 * Returns the PDA and bump for the reserve liquidity supply
 * @param lendingMarket
 * @param mint
 * @param programId
 * @returns [pda, bump]
 */
async function reserveLiqSupplyPda(reserve, programId = programId_1.PROGRAM_ID) {
    return (0, kit_1.getProgramDerivedAddress)({
        seeds: [buffer_1.Buffer.from(exports.RESERVE_LIQ_SUPPLY_SEED), addressEncoder.encode(reserve)],
        programAddress: programId,
    });
}
/**
 * Returns the PDA and bump for the reserve fee vault
 * @param lendingMarket
 * @param mint
 * @param programId
 * @returns [vaultPda, bump]
 */
async function reserveFeeVaultPda(reserve, programId = programId_1.PROGRAM_ID) {
    return (0, kit_1.getProgramDerivedAddress)({
        seeds: [buffer_1.Buffer.from(exports.FEE_RECEIVER_SEED), addressEncoder.encode(reserve)],
        programAddress: programId,
    });
}
/**
 * Returns the PDA and bump for the reserve collateral mint
 * @param lendingMarket
 * @param mint
 * @param programId
 * @returns [mintPda, bump]
 */
async function reserveCollateralMintPda(reserve, programId = programId_1.PROGRAM_ID) {
    return (0, kit_1.getProgramDerivedAddress)({
        seeds: [buffer_1.Buffer.from(exports.RESERVE_COLL_MINT_SEED), addressEncoder.encode(reserve)],
        programAddress: programId,
    });
}
/**
 * Returns the PDA and bump for the reserve collateral supply
 * @param lendingMarket
 * @param mint
 * @param programId
 * @returns [pda, bump]
 */
function reserveCollateralSupplyPda(reserve, programId = programId_1.PROGRAM_ID) {
    return (0, kit_1.getProgramDerivedAddress)({
        seeds: [buffer_1.Buffer.from(exports.RESERVE_COLL_SUPPLY_SEED), addressEncoder.encode(reserve)],
        programAddress: programId,
    });
}
/**
 * Returns the PDA and bump for the user metadata state
 * @param user
 * @param programId
 * @returns [pda, bump]
 */
function userMetadataPda(user, programId = programId_1.PROGRAM_ID) {
    return (0, kit_1.getProgramDerivedAddress)({
        seeds: [buffer_1.Buffer.from(exports.BASE_SEED_USER_METADATA), addressEncoder.encode(user)],
        programAddress: programId,
    });
}
/**
 * Returns the PDA and bump for the referrer account for a mint
 * @param referrer
 * @param reserve
 * @param programId
 * @returns pda
 */
async function referrerTokenStatePda(referrer, reserve, programId = programId_1.PROGRAM_ID) {
    const [address] = await (0, kit_1.getProgramDerivedAddress)({
        seeds: [
            buffer_1.Buffer.from(exports.BASE_SEED_REFERRER_TOKEN_STATE),
            addressEncoder.encode(referrer),
            addressEncoder.encode(reserve),
        ],
        programAddress: programId,
    });
    return address;
}
/**
 * Returns the PDA and bump for the referrer state
 * @param referrer
 * @param programId
 * @returns [pda, bump]
 */
function referrerStatePda(referrer, programId = programId_1.PROGRAM_ID) {
    return (0, kit_1.getProgramDerivedAddress)({
        seeds: [buffer_1.Buffer.from(exports.BASE_SEED_REFERRER_STATE), addressEncoder.encode(referrer)],
        programAddress: programId,
    });
}
/**
 * Returns the PDA and bump for the short url
 * @param shortUrl
 * @param programId
 * @returns pda
 */
async function shortUrlPda(shortUrl, programId = programId_1.PROGRAM_ID) {
    const [address] = await (0, kit_1.getProgramDerivedAddress)({
        seeds: [buffer_1.Buffer.from(exports.BASE_SEED_SHORT_URL), buffer_1.Buffer.from(shortUrl)],
        programAddress: programId,
    });
    return address;
}
/**
 * Returns the PDA and bump for the global config state.
 * @param programId
 * @returns pda
 */
async function globalConfigPda(programId = programId_1.PROGRAM_ID) {
    const [address] = await (0, kit_1.getProgramDerivedAddress)({
        seeds: [buffer_1.Buffer.from(exports.BASE_SEED_GLOBAL_CONFIG_STATE)],
        programAddress: programId,
    });
    return address;
}
/**
 * Returns the PDA and bump for the program data.
 * @param programId
 * @returns pda
 */
async function programDataPda(programId = programId_1.PROGRAM_ID) {
    const [pda] = await (0, kit_1.getProgramDerivedAddress)({
        seeds: [addressEncoder.encode(programId)],
        programAddress: (0, kit_1.address)('BPFLoaderUpgradeab1e11111111111111111111111'),
    });
    return pda;
}
/**
 * Returns the PDA for the obligation farm state
 * @param farm
 * @param obligation
 * @returns pda
 */
async function obligationFarmStatePda(farm, obligation, farmsProgramId = programId_2.PROGRAM_ID) {
    const [address] = await (0, kit_1.getProgramDerivedAddress)({
        seeds: [buffer_1.Buffer.from(exports.BASE_SEED_USER_STATE), addressEncoder.encode(farm), addressEncoder.encode(obligation)],
        programAddress: farmsProgramId,
    });
    return address;
}
/**
 * Returns the PDA for the kVault shares metadata
 * @param mint
 * @returns [pda, bump]
 */
async function getKVaultSharesMetadataPda(mint, metadataProgramId = vault_1.METADATA_PROGRAM_ID) {
    return (0, kit_1.getProgramDerivedAddress)({
        seeds: [buffer_1.Buffer.from(vault_1.METADATA_SEED), addressEncoder.encode(metadataProgramId), addressEncoder.encode(mint)],
        programAddress: metadataProgramId,
    });
}
/**
 * Returns the PDA for a withdraw ticket
 * @param reserve
 * @param sequenceNumber
 * @param programId
 * @returns pda
 */
async function withdrawTicketPda(reserve, sequenceNumber, programId = programId_1.PROGRAM_ID) {
    const sequenceNumberBuffer = buffer_1.Buffer.alloc(8);
    sequenceNumberBuffer.writeBigUInt64LE(sequenceNumber);
    const [address] = await (0, kit_1.getProgramDerivedAddress)({
        seeds: [buffer_1.Buffer.from(exports.WITHDRAW_TICKET_SEED), addressEncoder.encode(reserve), sequenceNumberBuffer],
        programAddress: programId,
    });
    return address;
}
/**
 * Returns the PDA for the owner's queued collateral vault
 * @param reserve
 * @param owner
 * @param programId
 * @returns pda
 */
async function ownerQueuedCollateralVaultPda(reserve, owner, programId = programId_1.PROGRAM_ID) {
    const [address] = await (0, kit_1.getProgramDerivedAddress)({
        seeds: [
            buffer_1.Buffer.from(exports.OWNER_QUEUED_COLLATERAL_VAULT_SEED),
            addressEncoder.encode(reserve),
            addressEncoder.encode(owner),
        ],
        programAddress: programId,
    });
    return address;
}
/**
 * Returns the PDA for the event authority
 * @param programId The program ID to derive the event authority for
 * @returns The event authority address
 */
async function getEventAuthorityPda(programId) {
    const [address] = await (0, kit_1.getProgramDerivedAddress)({
        seeds: [buffer_1.Buffer.from(exports.EVENT_AUTHORITY_SEED)],
        programAddress: programId,
    });
    return address;
}
//# sourceMappingURL=seeds.js.map