"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Obligation = void 0;
/* eslint-disable @typescript-eslint/no-unused-vars */
const kit_1 = require("@solana/kit");
/* eslint-enable @typescript-eslint/no-unused-vars */
const bn_js_1 = __importDefault(require("bn.js")); // eslint-disable-line @typescript-eslint/no-unused-vars
const borsh = __importStar(require("@coral-xyz/borsh")); // eslint-disable-line @typescript-eslint/no-unused-vars
const utils_1 = require("../utils"); // eslint-disable-line @typescript-eslint/no-unused-vars
const types = __importStar(require("../types")); // eslint-disable-line @typescript-eslint/no-unused-vars
const programId_1 = require("../programId");
class Obligation {
    tag;
    lastUpdate;
    lendingMarket;
    owner;
    deposits;
    lowestReserveDepositLiquidationLtv;
    depositedValueSf;
    borrows;
    borrowFactorAdjustedDebtValueSf;
    borrowedAssetsMarketValueSf;
    allowedBorrowValueSf;
    unhealthyBorrowValueSf;
    paddingDeprecatedAssetTiers;
    elevationGroup;
    numOfObsoleteDepositReserves;
    hasDebt;
    referrer;
    borrowingDisabled;
    autodeleverageTargetLtvPct;
    lowestReserveDepositMaxLtvPct;
    numOfObsoleteBorrowReserves;
    ownershipTransferState;
    reserved;
    highestBorrowFactorPct;
    autodeleverageMarginCallStartedTimestamp;
    obligationOrders;
    borrowOrder;
    pendingOwner;
    padding3;
    static discriminator = Buffer.from([
        168, 206, 141, 106, 88, 76, 172, 167,
    ]);
    static layout = borsh.struct([
        borsh.u64("tag"),
        types.LastUpdate.layout("lastUpdate"),
        (0, utils_1.borshAddress)("lendingMarket"),
        (0, utils_1.borshAddress)("owner"),
        borsh.array(types.ObligationCollateral.layout(), 8, "deposits"),
        borsh.u64("lowestReserveDepositLiquidationLtv"),
        borsh.u128("depositedValueSf"),
        borsh.array(types.ObligationLiquidity.layout(), 5, "borrows"),
        borsh.u128("borrowFactorAdjustedDebtValueSf"),
        borsh.u128("borrowedAssetsMarketValueSf"),
        borsh.u128("allowedBorrowValueSf"),
        borsh.u128("unhealthyBorrowValueSf"),
        borsh.array(borsh.u8(), 13, "paddingDeprecatedAssetTiers"),
        borsh.u8("elevationGroup"),
        borsh.u8("numOfObsoleteDepositReserves"),
        borsh.u8("hasDebt"),
        (0, utils_1.borshAddress)("referrer"),
        borsh.u8("borrowingDisabled"),
        borsh.u8("autodeleverageTargetLtvPct"),
        borsh.u8("lowestReserveDepositMaxLtvPct"),
        borsh.u8("numOfObsoleteBorrowReserves"),
        borsh.u8("ownershipTransferState"),
        borsh.array(borsh.u8(), 3, "reserved"),
        borsh.u64("highestBorrowFactorPct"),
        borsh.u64("autodeleverageMarginCallStartedTimestamp"),
        borsh.array(types.ObligationOrder.layout(), 2, "obligationOrders"),
        types.BorrowOrder.layout("borrowOrder"),
        (0, utils_1.borshAddress)("pendingOwner"),
        borsh.array(borsh.u64(), 69, "padding3"),
    ]);
    constructor(fields) {
        this.tag = fields.tag;
        this.lastUpdate = new types.LastUpdate({ ...fields.lastUpdate });
        this.lendingMarket = fields.lendingMarket;
        this.owner = fields.owner;
        this.deposits = fields.deposits.map((item) => new types.ObligationCollateral({ ...item }));
        this.lowestReserveDepositLiquidationLtv =
            fields.lowestReserveDepositLiquidationLtv;
        this.depositedValueSf = fields.depositedValueSf;
        this.borrows = fields.borrows.map((item) => new types.ObligationLiquidity({ ...item }));
        this.borrowFactorAdjustedDebtValueSf =
            fields.borrowFactorAdjustedDebtValueSf;
        this.borrowedAssetsMarketValueSf = fields.borrowedAssetsMarketValueSf;
        this.allowedBorrowValueSf = fields.allowedBorrowValueSf;
        this.unhealthyBorrowValueSf = fields.unhealthyBorrowValueSf;
        this.paddingDeprecatedAssetTiers = fields.paddingDeprecatedAssetTiers;
        this.elevationGroup = fields.elevationGroup;
        this.numOfObsoleteDepositReserves = fields.numOfObsoleteDepositReserves;
        this.hasDebt = fields.hasDebt;
        this.referrer = fields.referrer;
        this.borrowingDisabled = fields.borrowingDisabled;
        this.autodeleverageTargetLtvPct = fields.autodeleverageTargetLtvPct;
        this.lowestReserveDepositMaxLtvPct = fields.lowestReserveDepositMaxLtvPct;
        this.numOfObsoleteBorrowReserves = fields.numOfObsoleteBorrowReserves;
        this.ownershipTransferState = fields.ownershipTransferState;
        this.reserved = fields.reserved;
        this.highestBorrowFactorPct = fields.highestBorrowFactorPct;
        this.autodeleverageMarginCallStartedTimestamp =
            fields.autodeleverageMarginCallStartedTimestamp;
        this.obligationOrders = fields.obligationOrders.map((item) => new types.ObligationOrder({ ...item }));
        this.borrowOrder = new types.BorrowOrder({ ...fields.borrowOrder });
        this.pendingOwner = fields.pendingOwner;
        this.padding3 = fields.padding3;
    }
    static async fetch(rpc, address, programId = programId_1.PROGRAM_ID) {
        const info = await (0, kit_1.fetchEncodedAccount)(rpc, address);
        if (!info.exists) {
            return null;
        }
        if (info.programAddress !== programId) {
            throw new Error(`ObligationFields account ${address} belongs to wrong program ${info.programAddress}, expected ${programId}`);
        }
        return this.decode(Buffer.from(info.data));
    }
    static async fetchMultiple(rpc, addresses, programId = programId_1.PROGRAM_ID) {
        const infos = await (0, kit_1.fetchEncodedAccounts)(rpc, addresses);
        return infos.map((info) => {
            if (!info.exists) {
                return null;
            }
            if (info.programAddress !== programId) {
                throw new Error(`ObligationFields account ${info.address} belongs to wrong program ${info.programAddress}, expected ${programId}`);
            }
            return this.decode(Buffer.from(info.data));
        });
    }
    static decode(data) {
        if (!data.slice(0, 8).equals(Obligation.discriminator)) {
            throw new Error("invalid account discriminator");
        }
        const dec = Obligation.layout.decode(data.slice(8));
        return new Obligation({
            tag: dec.tag,
            lastUpdate: types.LastUpdate.fromDecoded(dec.lastUpdate),
            lendingMarket: dec.lendingMarket,
            owner: dec.owner,
            deposits: dec.deposits.map((item /* eslint-disable-line @typescript-eslint/no-explicit-any */) => types.ObligationCollateral.fromDecoded(item)),
            lowestReserveDepositLiquidationLtv: dec.lowestReserveDepositLiquidationLtv,
            depositedValueSf: dec.depositedValueSf,
            borrows: dec.borrows.map((item /* eslint-disable-line @typescript-eslint/no-explicit-any */) => types.ObligationLiquidity.fromDecoded(item)),
            borrowFactorAdjustedDebtValueSf: dec.borrowFactorAdjustedDebtValueSf,
            borrowedAssetsMarketValueSf: dec.borrowedAssetsMarketValueSf,
            allowedBorrowValueSf: dec.allowedBorrowValueSf,
            unhealthyBorrowValueSf: dec.unhealthyBorrowValueSf,
            paddingDeprecatedAssetTiers: dec.paddingDeprecatedAssetTiers,
            elevationGroup: dec.elevationGroup,
            numOfObsoleteDepositReserves: dec.numOfObsoleteDepositReserves,
            hasDebt: dec.hasDebt,
            referrer: dec.referrer,
            borrowingDisabled: dec.borrowingDisabled,
            autodeleverageTargetLtvPct: dec.autodeleverageTargetLtvPct,
            lowestReserveDepositMaxLtvPct: dec.lowestReserveDepositMaxLtvPct,
            numOfObsoleteBorrowReserves: dec.numOfObsoleteBorrowReserves,
            ownershipTransferState: dec.ownershipTransferState,
            reserved: dec.reserved,
            highestBorrowFactorPct: dec.highestBorrowFactorPct,
            autodeleverageMarginCallStartedTimestamp: dec.autodeleverageMarginCallStartedTimestamp,
            obligationOrders: dec.obligationOrders.map((item /* eslint-disable-line @typescript-eslint/no-explicit-any */) => types.ObligationOrder.fromDecoded(item)),
            borrowOrder: types.BorrowOrder.fromDecoded(dec.borrowOrder),
            pendingOwner: dec.pendingOwner,
            padding3: dec.padding3,
        });
    }
    toJSON() {
        return {
            tag: this.tag.toString(),
            lastUpdate: this.lastUpdate.toJSON(),
            lendingMarket: this.lendingMarket,
            owner: this.owner,
            deposits: this.deposits.map((item) => item.toJSON()),
            lowestReserveDepositLiquidationLtv: this.lowestReserveDepositLiquidationLtv.toString(),
            depositedValueSf: this.depositedValueSf.toString(),
            borrows: this.borrows.map((item) => item.toJSON()),
            borrowFactorAdjustedDebtValueSf: this.borrowFactorAdjustedDebtValueSf.toString(),
            borrowedAssetsMarketValueSf: this.borrowedAssetsMarketValueSf.toString(),
            allowedBorrowValueSf: this.allowedBorrowValueSf.toString(),
            unhealthyBorrowValueSf: this.unhealthyBorrowValueSf.toString(),
            paddingDeprecatedAssetTiers: this.paddingDeprecatedAssetTiers,
            elevationGroup: this.elevationGroup,
            numOfObsoleteDepositReserves: this.numOfObsoleteDepositReserves,
            hasDebt: this.hasDebt,
            referrer: this.referrer,
            borrowingDisabled: this.borrowingDisabled,
            autodeleverageTargetLtvPct: this.autodeleverageTargetLtvPct,
            lowestReserveDepositMaxLtvPct: this.lowestReserveDepositMaxLtvPct,
            numOfObsoleteBorrowReserves: this.numOfObsoleteBorrowReserves,
            ownershipTransferState: this.ownershipTransferState,
            reserved: this.reserved,
            highestBorrowFactorPct: this.highestBorrowFactorPct.toString(),
            autodeleverageMarginCallStartedTimestamp: this.autodeleverageMarginCallStartedTimestamp.toString(),
            obligationOrders: this.obligationOrders.map((item) => item.toJSON()),
            borrowOrder: this.borrowOrder.toJSON(),
            pendingOwner: this.pendingOwner,
            padding3: this.padding3.map((item) => item.toString()),
        };
    }
    static fromJSON(obj) {
        return new Obligation({
            tag: new bn_js_1.default(obj.tag),
            lastUpdate: types.LastUpdate.fromJSON(obj.lastUpdate),
            lendingMarket: (0, kit_1.address)(obj.lendingMarket),
            owner: (0, kit_1.address)(obj.owner),
            deposits: obj.deposits.map((item) => types.ObligationCollateral.fromJSON(item)),
            lowestReserveDepositLiquidationLtv: new bn_js_1.default(obj.lowestReserveDepositLiquidationLtv),
            depositedValueSf: new bn_js_1.default(obj.depositedValueSf),
            borrows: obj.borrows.map((item) => types.ObligationLiquidity.fromJSON(item)),
            borrowFactorAdjustedDebtValueSf: new bn_js_1.default(obj.borrowFactorAdjustedDebtValueSf),
            borrowedAssetsMarketValueSf: new bn_js_1.default(obj.borrowedAssetsMarketValueSf),
            allowedBorrowValueSf: new bn_js_1.default(obj.allowedBorrowValueSf),
            unhealthyBorrowValueSf: new bn_js_1.default(obj.unhealthyBorrowValueSf),
            paddingDeprecatedAssetTiers: obj.paddingDeprecatedAssetTiers,
            elevationGroup: obj.elevationGroup,
            numOfObsoleteDepositReserves: obj.numOfObsoleteDepositReserves,
            hasDebt: obj.hasDebt,
            referrer: (0, kit_1.address)(obj.referrer),
            borrowingDisabled: obj.borrowingDisabled,
            autodeleverageTargetLtvPct: obj.autodeleverageTargetLtvPct,
            lowestReserveDepositMaxLtvPct: obj.lowestReserveDepositMaxLtvPct,
            numOfObsoleteBorrowReserves: obj.numOfObsoleteBorrowReserves,
            ownershipTransferState: obj.ownershipTransferState,
            reserved: obj.reserved,
            highestBorrowFactorPct: new bn_js_1.default(obj.highestBorrowFactorPct),
            autodeleverageMarginCallStartedTimestamp: new bn_js_1.default(obj.autodeleverageMarginCallStartedTimestamp),
            obligationOrders: obj.obligationOrders.map((item) => types.ObligationOrder.fromJSON(item)),
            borrowOrder: types.BorrowOrder.fromJSON(obj.borrowOrder),
            pendingOwner: (0, kit_1.address)(obj.pendingOwner),
            padding3: obj.padding3.map((item) => new bn_js_1.default(item)),
        });
    }
}
exports.Obligation = Obligation;
//# sourceMappingURL=Obligation.js.map