fbonds-core
Version:
Banx protocol sdk
133 lines (132 loc) • 10.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getActivity = void 0;
const constants_1 = require("../../../../constants");
const helpers_1 = require("../../../../helpers");
const types_1 = require("../../../../types");
const helpers_2 = require("../helpers");
const anchor_1 = require("@coral-xyz/anchor");
var ActivityEvent;
(function (ActivityEvent) {
ActivityEvent["Initialized"] = "Initialized";
ActivityEvent["RepaidFull"] = "RepaidFull";
ActivityEvent["RepaidPartial"] = "RepaidPartial";
ActivityEvent["Terminated"] = "Terminated";
ActivityEvent["Liquidated"] = "Liquidated";
ActivityEvent["RefinancedByLender"] = "RefinancedByLender";
ActivityEvent["RefinancedByAuction"] = "RefinancedByAuction";
ActivityEvent["Reborrowed"] = "Reborrowed";
//TODO
// RepaymentCall = 'RepaymentCall',
})(ActivityEvent || (ActivityEvent = {}));
const getActivity = ({ fraktBond, bondTradeTransaction }) => {
const { redeemResult } = bondTradeTransaction;
const activities = [];
//? bondTradeTransaction can contain more than one activity. Cases: Initialized + {terminated(optional)} + {any end case}
//? that's what the extra checks are for.
const hasInitializedActivity = redeemResult === types_1.RedeemResult.DirectBorrow;
if (hasInitializedActivity) {
activities.push(createInitializedActivity({ fraktBond, bondTradeTransaction }));
}
const hasTerminatedActivity = !bondTradeTransaction.terminationStartedAt.isZero();
if (hasTerminatedActivity) {
activities.push(createTerminatedActivity({ fraktBond, bondTradeTransaction }));
}
const redeemResultNextActivity = createActivityByRedeemResultNext({ fraktBond, bondTradeTransaction });
if (redeemResultNextActivity)
activities.push(redeemResultNextActivity);
//? Sort by timestamp asc
return activities.sort((a, b) => (0, helpers_1.sortCompareBN)(a.timestamp, b.timestamp, 'asc'));
};
exports.getActivity = getActivity;
const createActivityByRedeemResultNext = ({ fraktBond, bondTradeTransaction, }) => {
const { redeemResultNext } = bondTradeTransaction;
if (redeemResultNext === types_1.RedeemResult.DirectRepaid)
return createRepaidFullActivity({ fraktBond, bondTradeTransaction });
if (redeemResultNext === types_1.RedeemResult.PartialRepay)
return createRepaidPartialActivity({ fraktBond, bondTradeTransaction });
if (redeemResultNext === types_1.RedeemResult.Claimed)
return createLiquidatedActivity({ fraktBond, bondTradeTransaction });
if (redeemResultNext === types_1.RedeemResult.InstantRefinanced)
return createRefinancedByLenderActivity({ fraktBond, bondTradeTransaction });
if (redeemResultNext === types_1.RedeemResult.RefinancedByAuction)
return createRefinancedByAuctionActivity({ fraktBond, bondTradeTransaction });
if (redeemResultNext === types_1.RedeemResult.Reborrow)
return createReborrowedActivity({ fraktBond, bondTradeTransaction });
return undefined;
};
const createInitializedActivity = ({ fraktBond, bondTradeTransaction }) => {
return Object.assign(Object.assign({ event: ActivityEvent.Initialized }, getActivityPubkeyFields({ fraktBond, bondTradeTransaction })), { timestamp: bondTradeTransaction.soldAt, originalLentAmount: bondTradeTransaction.borrowerOriginalLent, accuredInterestLender: new anchor_1.BN(0), accuredInterestProtocol: new anchor_1.BN(0), repaidAmount: new anchor_1.BN(0), apr: bondTradeTransaction.amountOfBonds });
};
const createTerminatedActivity = ({ fraktBond, bondTradeTransaction }) => {
const { interestLender, interestProtocol } = calculateAccuredInterest(bondTradeTransaction, bondTradeTransaction.terminationStartedAt);
return Object.assign(Object.assign({ event: ActivityEvent.Terminated }, getActivityPubkeyFields({ fraktBond, bondTradeTransaction })), { timestamp: bondTradeTransaction.terminationStartedAt, originalLentAmount: bondTradeTransaction.borrowerOriginalLent, accuredInterestLender: interestLender, accuredInterestProtocol: interestProtocol, repaidAmount: constants_1.ZERO_BN, apr: bondTradeTransaction.amountOfBonds });
};
const createRepaidPartialActivity = ({ fraktBond, bondTradeTransaction }) => {
const currentLoanValue = bondTradeTransaction.solAmount.add(bondTradeTransaction.feeAmount);
const { interestLender, interestProtocol } = calculateAccuredInterest(bondTradeTransaction, bondTradeTransaction.redeemedAt);
return Object.assign(Object.assign({ event: ActivityEvent.RepaidPartial }, getActivityPubkeyFields({ fraktBond, bondTradeTransaction })), { timestamp: bondTradeTransaction.redeemedAt, originalLentAmount: bondTradeTransaction.borrowerOriginalLent, accuredInterestLender: interestLender, accuredInterestProtocol: interestProtocol, repaidAmount: currentLoanValue.add(interestLender).add(interestProtocol), apr: bondTradeTransaction.amountOfBonds });
};
const createRepaidFullActivity = ({ fraktBond, bondTradeTransaction }) => {
const currentLoanValue = bondTradeTransaction.solAmount.add(bondTradeTransaction.feeAmount);
const { interestLender, interestProtocol } = calculateAccuredInterest(bondTradeTransaction, bondTradeTransaction.redeemedAt);
return Object.assign(Object.assign({ event: ActivityEvent.RepaidFull }, getActivityPubkeyFields({ fraktBond, bondTradeTransaction })), { timestamp: bondTradeTransaction.redeemedAt, originalLentAmount: bondTradeTransaction.borrowerOriginalLent, accuredInterestLender: interestLender, accuredInterestProtocol: interestProtocol, repaidAmount: currentLoanValue.add(interestLender).add(interestProtocol), apr: bondTradeTransaction.amountOfBonds });
};
const createLiquidatedActivity = ({ fraktBond, bondTradeTransaction }) => {
const { interestLender, interestProtocol } = calculateAccuredInterest(bondTradeTransaction, bondTradeTransaction.redeemedAt);
return Object.assign(Object.assign({ event: ActivityEvent.Liquidated }, getActivityPubkeyFields({ fraktBond, bondTradeTransaction })), { timestamp: bondTradeTransaction.redeemedAt, originalLentAmount: bondTradeTransaction.borrowerOriginalLent, accuredInterestLender: interestLender, accuredInterestProtocol: interestProtocol, repaidAmount: constants_1.ZERO_BN, apr: bondTradeTransaction.amountOfBonds });
};
const createRefinancedByLenderActivity = ({ fraktBond, bondTradeTransaction }) => {
const currentLoanValue = bondTradeTransaction.solAmount.add(bondTradeTransaction.feeAmount);
const { interestLender, interestProtocol } = calculateAccuredInterest(bondTradeTransaction, bondTradeTransaction.redeemedAt);
return Object.assign(Object.assign({ event: ActivityEvent.RefinancedByLender }, getActivityPubkeyFields({ fraktBond, bondTradeTransaction })), { timestamp: bondTradeTransaction.redeemedAt, originalLentAmount: bondTradeTransaction.borrowerOriginalLent, accuredInterestLender: interestLender, accuredInterestProtocol: interestProtocol, repaidAmount: currentLoanValue.add(interestLender).add(interestProtocol), apr: bondTradeTransaction.amountOfBonds });
};
const createRefinancedByAuctionActivity = ({ fraktBond, bondTradeTransaction }) => {
const currentLoanValue = bondTradeTransaction.solAmount.add(bondTradeTransaction.feeAmount);
const { interestLender, interestProtocol } = calculateAccuredInterest(bondTradeTransaction, bondTradeTransaction.redeemedAt);
return Object.assign(Object.assign({ event: ActivityEvent.RefinancedByAuction }, getActivityPubkeyFields({ fraktBond, bondTradeTransaction })), { timestamp: bondTradeTransaction.redeemedAt, originalLentAmount: bondTradeTransaction.borrowerOriginalLent, accuredInterestLender: interestLender, accuredInterestProtocol: interestProtocol, repaidAmount: currentLoanValue.add(interestLender).add(interestProtocol), apr: bondTradeTransaction.amountOfBonds });
};
const createReborrowedActivity = ({ fraktBond, bondTradeTransaction }) => {
const currentLoanValue = bondTradeTransaction.solAmount.add(bondTradeTransaction.feeAmount);
const { interestLender, interestProtocol } = calculateAccuredInterest(bondTradeTransaction, bondTradeTransaction.redeemedAt);
return Object.assign(Object.assign({ event: ActivityEvent.Reborrowed }, getActivityPubkeyFields({ fraktBond, bondTradeTransaction })), { timestamp: bondTradeTransaction.redeemedAt, originalLentAmount: bondTradeTransaction.borrowerOriginalLent, accuredInterestLender: interestLender, accuredInterestProtocol: interestProtocol, repaidAmount: currentLoanValue.add(interestLender).add(interestProtocol), apr: bondTradeTransaction.amountOfBonds });
};
const calculateAccuredInterest = (bondTradeTransaction, endTime) => {
const isTokenLending = bondTradeTransaction.bondTradeTransactionType === types_1.BondTradeTransactionV2Type.AutoReceiveAndReceiveSpl;
const currentLoanValue = bondTradeTransaction.solAmount.add(bondTradeTransaction.feeAmount);
const interestLender = (0, helpers_2.calculateCurrentInterestSolPureBN)({
loanValue: currentLoanValue,
startTime: bondTradeTransaction.soldAt,
currentTime: endTime,
rateBasePoints: bondTradeTransaction.amountOfBonds,
});
const interestProtocol = isTokenLending
? constants_1.ZERO_BN
: (0, helpers_2.calculateCurrentInterestSolPureBN)({
loanValue: currentLoanValue,
startTime: bondTradeTransaction.soldAt,
currentTime: endTime,
rateBasePoints: bondTradeTransaction.protocolInterestFee,
});
return { interestLender, interestProtocol };
};
const getActivityPubkeyFields = ({ fraktBond, bondTradeTransaction, }) => {
return {
fbond: fraktBond.publicKey,
bondTradeTransaction: bondTradeTransaction.publicKey,
tokenMint: fraktBond.fbondTokenMint,
borrower: fraktBond.fbondIssuer,
lender: bondTradeTransaction.user,
};
};
//* redeemResult redeemResultNext
//* DirectBorrow InstantRefinanced
//* InstantRefinanced None
//* Start cases
//? Initialized
//* Middle cases
//? Terminating
//* End cases
//? DirectRepaid, Liquidated, RefinancedByLender, RefinancedByAuction, Reborrow, RepaidPartial
//* Tripple cases
//? Initialized + Terminating + Liquidate/Repay/Refinance