Loans
The main utility of the Rain.fi protocol are loans. Through pools which we've discussed here, Rain gives its users ability to fund and take loans.
Let's now discuss how to interact with loans and how to manage them using Rain.fi SDK.
getLoansFromBorrower()
The getLoansFromBorrower()
function gives you ability to see all loans taken
by a particular borrower, including yourself.
It takes borrower's PublicKey
as a parameter and returns an array of loans, as well as their data.
If needed, function can also take an array of filters that you want to apply on the results.
import { Rain } from '@rainfi/sdk';
import {Connection, clusterApiUrl, Keypair, PublicKey} from "@solana/web3.js";
const connection = new Connection("<Your RPC URL>");
const {publicKey} = Keypair.generate();
const rain = new Rain(
connection,
publicKey,
);
const borrower = new PublicKey("<Address Here>");
const { getLoansFromBorrower } = rain.utils;
const loans = await getLoansFromBorrower(
connection,
borrower,
);
console.log({loans});
getLoansFromPool()
We've just discussed fetching loans by borrower's address. Now, we'll focus on
getLoansFromPool()
- function that gives you ability to fetch all loans funded by
a particular pool.
Except connection and pool owner address, function accepts (doesn't require) custom filters as the third parameter.
import { Rain } from '@rainfi/sdk';
import {Connection, clusterApiUrl, Keypair, PublicKey} from "@solana/web3.js";
const connection = new Connection("<Your RPC URL>");
const {publicKey} = Keypair.generate();
const rain = new Rain(
connection,
publicKey,
);
const { getLoansFromPool } = rain.utils;
// Each owner can have only one pool. That allows us
// to fetch data of a particular pool by it's owner.
const poolOwner = new PublicKey("FDfhQ9t7Nq8U4Y2xXynmd9bQW8HMdgbVS5PggeuAaff3");
const loans = await getLoansFromPool(
connection,
poolOwner,
[{
key: "borrower",
value: "<Address Here>"
}]
// ^ Array of custom filters. In this particular
// case, function will only return loans that matches
// passed borrower address.
);
console.log({loans});
getCustomLoanOffersFromBorrower()
If you can't find any pool that you are interested in, Rain gives you ability to send your custom offers to pool owners.
Rain also gives you tools to manage custom offers from the SDK - getCustomLoanOffersFromBorrower()
function is one of them.
The function takes borrower address as a parameter and returns all custom loan offers sent by that address.
If you want to filter the results, function also accepts array of filters as the third, optional parameter.
import { Rain } from '@rainfi/sdk';
import {Connection, clusterApiUrl, Keypair, PublicKey} from "@solana/web3.js";
const connection = new Connection("<Your RPC URL>");
const {publicKey} = Keypair.generate();
const rain = new Rain(
connection,
publicKey,
);
const { getCustomLoanOffersFromBorrower } = rain.utils;
const borrower = new PublicKey("<Your Address>");
const customLoanOffers = await getCustomLoanOffersFromBorrower(
connection,
borrower
);
console.log({customLoanOffers});
getCustomLoanOffersFromLender()
We've just discussed getCustomLoanOffersFromBorrower()
function. Now, we're going to
focus on getCustomLoanOffersFromLender()
. As you can see, they are pretty similar.
The only difference is the role of the PublicKey
that you pass as an argument. In the previous function
it was borrower. Now, it's a lender (pool owner).
import { Rain } from '@rainfi/sdk';
import {Connection, clusterApiUrl, Keypair, PublicKey} from "@solana/web3.js";
const connection = new Connection("<Your RPC URL>");
const {publicKey} = Keypair.generate();
const rain = new Rain(
connection,
publicKey,
);
const { getCustomLoanOffersFromLender } = rain.utils;
const lender = new PublicKey("<Your Address>");
const customLoanOffers = await getCustomLoanOffersFromLender(
connection,
lender
);
console.log({customLoanOffers});
getFiltersLoanRequest() & getFiltersLoan()
These two functions are pretty similar, so we decided to document both at the same time.
They are more low-level than functions that we've discussed so far.
In previous chapters, we discussed getFiltersCollections()
, which is almost
identical to the two functions that we're focusing on now.
These two functions give you ability to parse human-readable filters to
filters compatibile with Solana Web3 SDK. Then, you can pass returned filters to the getProgramAccounts()
method from Solana SDK.
This way, you'll get addresses of program-owned accounts associated with loans and loans requests.
import { Rain } from '@rainfi/sdk';
import {Connection, getProgramAccounts, Keypair, PublicKey} from "@solana/web3.js";
const connection = new Connection("<Your RPC URL>");
const {publicKey} = Keypair.generate();
const rain = new Rain(
connection,
publicKey,
);
const {
getFiltersLoanRequest,
getFiltersLoan
} = rain.utils;
const loanRequestFilters = await getFiltersLoanRequest([{
key: "borrower",
value: "<Address Here>"
}]);
const loanFilters = await getFiltersLoan([{
key: "borrower",
value: "<Address Here>"
}]);
const loanRequestProgramAccounts = await connection.getProgramAccounts(
new PublicKey("RainEraPU5yDoJmTrHdYynK9739GkEfDsE4ffqce2BR"),
// ^ Address of official Rain.fi program deployment.
{filters: loanRequestFilters}
);
const loanProgramAccounts = await connection.getProgramAccounts(
new PublicKey("RainEraPU5yDoJmTrHdYynK9739GkEfDsE4ffqce2BR"),
// ^ Address of official Rain.fi program deployment.
{filters: loanFilters}
);
console.log({loanProgramAccounts, loanRequestProgramAccounts});