import { ObligationCollateralLayout, ObligationLayout, ObligationLiquidityLayout, PortProfileCollateralData, PortProfileData, PortProfileLoanData, ProtoObligation, } from "../structs"; import * as BufferLayout from "@solana/buffer-layout"; export const PortProfileParser = (buffer: Buffer): PortProfileData => { const { version, lastUpdate, lendingMarket, owner, depositedValue, borrowedValue, allowedBorrowValue, unhealthyBorrowValue, depositsLen, borrowsLen, dataFlat, } = ObligationLayout.decode(buffer) as ProtoObligation; const depositsBuffer = dataFlat.slice( 0, depositsLen * ObligationCollateralLayout.span ); const deposits = BufferLayout.seq( ObligationCollateralLayout, depositsLen ).decode(depositsBuffer) as PortProfileCollateralData[]; const borrowsBuffer = dataFlat.slice( depositsBuffer.length, depositsBuffer.length + borrowsLen * ObligationLiquidityLayout.span ); const borrows = BufferLayout.seq( ObligationLiquidityLayout, borrowsLen ).decode(borrowsBuffer) as PortProfileLoanData[]; return { version, lastUpdate, lendingMarket, owner, depositedValue, borrowedValue, allowedBorrowValue, unhealthyBorrowValue, deposits, borrows, } as PortProfileData; };