Skip to main content

Pools

Pools are the core of Rain.fi protocol. Pool is a space (technically an account on Solana blockchain) used for borrowing and lending.

While working with the Rain SDK, Pools are probably the most important element that you have to understand. You'll work with them quite often.

Let's look at how you can interact & manage Pools!

getAllPoolAvailable()

The getAllPoolAvailable() function is used to fetch all pools currently existing in the Rain program. As you can see below, usage is very straightforward.

Function also accepts custom filters as the second 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 { getAllPoolAvailable } = rain.utils;

const pools = await getAllPoolAvailable(
connection,
[{
key: "owner",
value: "<Address Here>"
}]
// ^ In this particular case, function will only
// return pools owned by the address you input.
// To get ALL existing pools, don't use any filtering.
);

console.log({pools});

getPoolFromOwnerAddress()

Each owner can initialize only one Pool on the Rain protocol. This rule allows you to get all information about the pool knowing only owner's address.

That's exactly what getPoolFromOwnerAddress() function does. It accepts owner address as a parameter and returns data of the owned pool.

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 { getPoolFromOwnerAddress } = 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 pool = await getPoolFromOwnerAddress(
connection,
poolOwner,
);

console.log({pool});

getFiltersPool()

We've already discussed similar function in the Collections section - getFiltersCollections(). getFiltersPool() is almost identical.

It accepts list of human-readable filters as the parameter, parses them and returns filters that you can pass to the getProgramAccounts() method from Solana SDK.

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 {
getFiltersPool
} = rain.utils;

const filters = await getFiltersPool(
[{
key: "owner",
value: "<Owner Address Here>"
}]
);

const programAccounts = await connection.getProgramAccounts(
new PublicKey("RainEraPU5yDoJmTrHdYynK9739GkEfDsE4ffqce2BR"),
// ^ Address of official Rain.fi program deployment.
{filters}
);

console.log({programAccounts});

getLoansFromPool()

As the name suggests, getLoansFromPool() function is used to fetch loans issued by a particular pool.

Except connection and pool owner address, function accepts 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});