"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadReserveWhitelistStatus = loadReserveWhitelistStatus;
exports.printReserveWhitelistStatus = printReserveWhitelistStatus;
exports.checkReserveWhitelistCommand = checkReserveWhitelistCommand;
exports.formatApy = formatApy;
exports.printVaultReserveAllocations = printVaultReserveAllocations;
exports.printVaultReserveFarmIncentives = printVaultReserveFarmIncentives;
const kit_1 = require("@solana/kit");
const decimal_js_1 = __importDefault(require("decimal.js"));
const accounts_1 = require("../@codegen/kvault/accounts");
const vault_1 = require("../classes/vault");
const lib_1 = require("../lib");
const ManagerEnv_1 = require("./tx/ManagerEnv");
async function loadReserveWhitelistStatus(env, reserveAddress) {
    const pda = await (0, vault_1.getReserveWhitelistEntryPda)(reserveAddress, env.kvaultProgramId);
    const entry = await accounts_1.ReserveWhitelistEntry.fetch(env.c.rpc, pda, env.kvaultProgramId);
    const whitelistInvest = entry ? entry.whitelistInvest : 0;
    const whitelistAddAllocation = entry ? entry.whitelistAddAllocation : 0;
    return {
        reserveAddress,
        pda,
        entry,
        whitelistInvest,
        whitelistAddAllocation,
        whitelistedForInvest: whitelistInvest !== 0,
        whitelistedForAddAllocation: whitelistAddAllocation !== 0,
    };
}
function printReserveWhitelistStatus(status) {
    console.log(`Reserve ${status.reserveAddress}`);
    console.log(`  PDA: ${status.pda}${status.entry ? '' : ' (not initialized)'}`);
    if (status.entry) {
        console.log(`  tokenMint: ${status.entry.tokenMint}`);
    }
    console.log(`  invest: ${status.whitelistedForInvest ? 'whitelisted' : 'not whitelisted'}`);
    console.log(`  addAllocation: ${status.whitelistedForAddAllocation ? 'whitelisted' : 'not whitelisted'}`);
    console.log(`  whitelistInvest: ${status.whitelistInvest}`);
    console.log(`  whitelistAddAllocation: ${status.whitelistAddAllocation}`);
    console.log(`  fullyWhitelisted: ${status.whitelistedForInvest && status.whitelistedForAddAllocation ? 'yes' : 'no'}`);
}
async function checkReserveWhitelistCommand({ reserve, staging, devnet, }) {
    const env = await (0, ManagerEnv_1.initEnv)(staging, undefined, undefined, undefined, devnet);
    const reserveAddress = (0, kit_1.address)(reserve);
    printReserveWhitelistStatus(await loadReserveWhitelistStatus(env, reserveAddress));
}
function formatApy(value) {
    const decimal = new decimal_js_1.default(value);
    return `${decimal.toString()} (${decimal.mul(100).toFixed(6)}%)`;
}
function printVaultReserveAllocations(allocations, vaultReservesMap) {
    console.log('Reserve allocations:');
    if (allocations.size === 0) {
        console.log('  None');
        return;
    }
    const totalWeight = Array.from(allocations.values()).reduce((sum, allocation) => sum.add(allocation.targetWeight), new decimal_js_1.default(0));
    allocations.forEach((allocation, reserveAddress) => {
        const reserve = vaultReservesMap.get(reserveAddress);
        const targetWeightPct = totalWeight.isZero() ? new decimal_js_1.default(0) : allocation.targetWeight.mul(100).div(totalWeight);
        console.log(`  reserve ${reserveAddress}`);
        if (reserve) {
            console.log(`    symbol: ${reserve.symbol}`);
            console.log(`    mint: ${reserve.getLiquidityMint()}`);
        }
        console.log(`    targetWeight: ${allocation.targetWeight.toString()}`);
        console.log(`    targetWeightPct: ${targetWeightPct.toString()}%`);
        console.log(`    tokenAllocationCapLamports: ${allocation.tokenAllocationCap.toString()}`);
        if (reserve) {
            console.log(`    tokenAllocationCap: ${(0, lib_1.lamportsToDecimal)(allocation.tokenAllocationCap, reserve.getMintDecimals()).toString()}`);
        }
        console.log(`    ctokenAllocation: ${allocation.ctokenAllocation.toString()}`);
    });
}
function printFarmIncentives(prefix, incentives) {
    console.log(`${prefix}totalIncentivesApy: ${formatApy(incentives.totalIncentivesApy)}`);
    if (incentives.incentivesStats.length === 0) {
        console.log(`${prefix}rewards: none`);
        return;
    }
    incentives.incentivesStats.forEach((reward, index) => {
        console.log(`${prefix}reward ${index + 1}:`);
        console.log(`${prefix}  mint: ${reward.rewardMint}`);
        console.log(`${prefix}  decimals: ${reward.rewardDecimals.toString()}`);
        console.log(`${prefix}  hasRewardAvailable: ${reward.hasRewardAvailable}`);
        console.log(`${prefix}  incentivesApy: ${formatApy(reward.incentivesApy)}`);
        console.log(`${prefix}  value: ${reward.value.toString()}`);
        console.log(`${prefix}  dailyRewards: ${reward.dailyRewards.toString()}`);
        console.log(`${prefix}  weeklyRewards: ${reward.weeklyRewards.toString()}`);
        console.log(`${prefix}  monthlyRewards: ${reward.monthlyRewards.toString()}`);
        console.log(`${prefix}  yearlyRewards: ${reward.yearlyRewards.toString()}`);
    });
}
function printVaultReserveFarmIncentives(reserveFarmsIncentives, totalIncentivesAPY) {
    console.log('Reserve farm incentives:');
    if (reserveFarmsIncentives.size === 0) {
        console.log('  None');
    }
    reserveFarmsIncentives.forEach((incentives, reserveAddress) => {
        console.log(`  reserve ${reserveAddress}`);
        printFarmIncentives('    ', incentives);
    });
    console.log(`  totalWeightedIncentivesAPY: ${formatApy(totalIncentivesAPY)}`);
}
//# sourceMappingURL=print.js.map