Skip to main content

Functions

Below, you'll find all functions you can use for calculating fees/interest on loans/mortgages. Be aware not all of these functions can fit every use case.

computeInterest()

This function calculates the interest rate for a loan based on various parameters. The parameters are as follows:

  • amount: The amount being borrowed.
  • interestRate: The base interest rate for the loan.
  • duration: The number of days the loan is taken for.
  • totalAmount: The total amount of money in the lending pool.
  • borrowedAmount: The total amount of money already borrowed from the pool.
  • baseInterestRate: The minimum interest rate that can be charged on the loan.
  • maxDuration: The maximum number of days the loan can be taken for.
  • curveRate: A coefficient that adjusts the interest rate based on the ratio of liquidity to total pool size.
  • curveRateDay: A coefficient that adjusts the interest rate based on the ratio of duration to maximum loan duration. The formula first calculates the remaining liquidity in the pool by subtracting the borrowed amount and the amount being borrowed from the total amount. It then calculates the interest curve by adding the interest rate and multiplying it by the coefficient (1 - (liquidity/totalAmount)) * curveRate.

The final interest rate is then calculated by taking the maximum value between the base interest rate and the product of the interest curve and the exponential function of the curve rate day multiplied by the duration divided by the maximum duration.

It is important to note that this formula is just an example and it can be adjusted or modified to suit specific use cases. The values for the coefficients and the base interest rate should be determined based on the desired lending strategy and market conditions.

const connection = new Connection("<Your RPC URL>");
const {publicKey} = Keypair.generate();

const rain = new Rain(
connection,
publicKey,
);

const {
computeInterest
} = rain.utils;

const { rainFees, interestPercentage, interestLamports } = computeInterest(
new BN(30 * LAMPORTS_PER_SOL), // Loan amount.
7, // Duration in days.
8, // `interestRate` set by the pool.
new BN(1000 * LAMPORTS_PER_SOL), // Total amount in pool.
new BN(200 * LAMPORTS_PER_SOL), // Already borrowed amount.
19, // `baseInterest` set by the pool.
7, // Max loan duration accepted by the pool.
10, // `curveRate` set by the pool.
17 // `curveRateDay` set by the pool.
);

getFeesDetailed

getFeesDetailed() function is a high-level function, which uses computeInterest() under the hood to calculate fees.

It only requires three parameters:

  • pool - data of the pool you're borrowing from.
  • amount - loan amount.
  • duration - loan duration.

Usage:

const connection = new Connection("<Your RPC URL>");
const {publicKey} = Keypair.generate();

const rain = new Rain(
connection,
publicKey,
);

const {
getPoolFromOwnerAddress,
getFeesDetailed
} = rain.utils;

const pool = await getPoolFromOwnerAddress(connection, new PublicKey("<Pool Owner Address>"));

const { feesInSol, rainFees, duration, feesInPercentage, amountToLoan } = getFeesDetailed(
pool, // Pool data.
20 * LAMPORTS_PER_SOL, // Loan amount.
7 // Loan duration in days.
);

getMortgagesFeesDetailed()

Above functions are used to calculate fees on a loan. If you need to calculate fees on a mortgage, you should utilize getMortgagesFeesDetailed() function.

Usage:

const connection = new Connection("<Your RPC URL>");
const {publicKey} = Keypair.generate();

const rain = new Rain(
connection,
publicKey,
);

const {
getPoolFromOwnerAddress,
getMortgagesFeesDetailed
} = rain.utils;

const pool = await getPoolFromOwnerAddress(connection, new PublicKey("<Pool Owner Address>"));

const { feesInSol, rainFees, duration, feesInPercentage, amountToLoan } = getMortgagesFeesDetailed(
pool, // Pool data.
20 * LAMPORTS_PER_SOL, // Mortgage amount.
7 // Mortgage duration in days.
);