Skip to main content

Others

We've already discussed utility functions for interacting with Pools, Loans, Mortgages and Collections.

However, that's not everything what Rain SDK has to offer! It also gives you other useful utilities that will help you and speed up your development process.

Let's see them!

computeAmount()

computeAmount() function allows you to calculate loan amount depending on the collection's floor price and pool's LTV (loan-to-value ratio).

To calculate, use this snipped:

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

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

const {
computeAmount,
getPoolFromOwnerAddress,
getCollection,
} = rain.utils;

const pool = await getPoolFromOwnerAddress(
connection,
new PublicKey("prC6wjf6Qu3ZgBxMdpTcknReCberDnRitqkEHhG59yv")
);

const collection = await getCollection(
connection,
pool.collections[0].collection // First collection whitelisted by the pool.
);

const { loanToValue } = pool;
const { floorPrice } = collection;

const loanAmount = computeAmount(
loanToValue,
floorPrice
);

console.log({loanAmount});

getFeesDetailed()

getFeesDetailed() function allows you to calculate fees that user will have to pay in order to borrow given amount from a pool.

As you can see below, it accepts 6 parameters:

  • pool - pool data as a SplPool instance.
  • amount - in the code snipped below, we're using maximum possible amount.
  • duration - duration of the loan you're taking. In the code snipped below, we're using the longest possible period.
const connection = new Connection("<Your RPC URL>");
const {publicKey} = Keypair.generate();

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

const {
getPoolFromOwnerAddress,
getCollection,
computeAmount,
getFeesDetailed
} = rain.utils;

const pool = await getPoolFromOwnerAddress(
connection,
new PublicKey("prC6wjf6Qu3ZgBxMdpTcknReCberDnRitqkEHhG59yv")
);

const collection = await getCollection(
connection,
pool.collections[0].collection
);

const { floorPrice } = collection;
const { loanToValue, maxDuration } = pool;
const maxBorrowableAmount = computeAmount(loanToValue, floorPrice);
const maxDurationInDays = maxDuration / 60 / 60 / 24;

const fees = await getFeesDetailed(
pool,
maxBorrowableAmount,
maxDurationInDays, // Max duration has to be specified in DAYS.
);

console.log({fees});

In this case, first we fetch the pool by owner's address. We will later use received SplPool data. In the second step, we're taking one of the collections supported by fetched pool and fetch its data.

When both pool's and collection's data is ready, we invoke getFeesDetailed() function and pass parameters listed above. As a result, function returns list of fees:

{
fees: {
feesInPercentage: 2.9380731966593006,
feesInSol: 238718447.2285682,
rainFees: 11935922.36142841,
amountToLoan: 8125000000,
duration: 7
}
}

computeInterest()

As the function's name suggests, this function gives you an ability to calculate interest (in Solana) based on the passed parameters.

It accepts four parameters - pool data (SplPool type), floor price of the item you want to collateralize, amount you want to borrow, and loan duration (in days).

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

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

const {
getPoolFromOwnerAddress,
getCollection,
computeInterest
} = rain.utils;

const pool = await getPoolFromOwnerAddress(
connection,
new PublicKey("prC6wjf6Qu3ZgBxMdpTcknReCberDnRitqkEHhG59yv")
);

const collection = await getCollection(
connection,
pool.collections[0].collection
);

const { floorPrice } = collection;
const { loanToValue, maxDuration } = pool;
const maxBorrowableAmount = floorPrice * (loanToValue / 10000);
const maxDurationInDays = maxDuration / 60 / 60 / 24;

const interest = computeInterest(
pool,
floorPrice,
maxBorrowableAmount,
maxDurationInDays
);

console.log({interest});

getAllUsersStats()

Use getAllUsersStats() function to fetch stats of all Rain.fi users. Function accepts Connection as the only parameter.

Usage:

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

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

const {
getAllUsersStats
} = rain.utils;

const allUsersStats = await getAllUsersStats(
connection
);

console.log({allUsersStats});

Function returns data in an array of objects of following format:

{
owner: PublicKey;
totalLoan: number;
totalMortgage: number;
totalLiquidation: number;
currentLoan: number;
currentMortgage: number;
createdAt: number;
}

getAllUsersMintStats()

As the name suggests, getAllUsersMintStats() gives you ability to fetch mint stats of all users in Rain protocol.

Function accepts Solana Connection as the first and only parameter.

Usage:

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

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

const {
getAllUsersMintStats
} = rain.utils;

const allUsersMintStats = await getAllUsersMintStats(
connection
);

console.log({allUsersMintStats});

getFullUserStats()

getFullUserStats() function allows you to fetch all stats for a particular address. Pass Connection as the first parameter. As the second parameter, function accepts an address to check. As the third, array of mints to check.

Usage:

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

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

const {
getFullUserStats
} = rain.utils;

const addressToCheck = new PublicKey("<Address>");
const stats = await getFullUserStats(
connection,
addressToCheck,
[
"So11111111111111111111111111111111111111112", // Solana
"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // USDC
]
);

console.log({stats});