Collections
Collection
is a Solana account owned by Rain.fi program. It stores data about single
NFT collection that has been approved to be collateralized on Rain.
Rain SDK provides multiple ways to work and manage Collections. Let's dive deeper!
getAvailableCollections()
getAvailableCollections()
function allows you to fetch all whitelisted
collections, as well as additional data about them - name, creator, unique id, floor price, etc.
const connection = new Connection("<Your RPC URL>");
const {publicKey} = Keypair.generate();
const rain = new Rain(
connection,
publicKey,
);
const {
getAvailableCollections,
// ^ Get all collections supported by Rain.fi.
} = rain.utils;
const collections = await getAvailableCollections(connection);
console.log({collections});
getCollection()
getCollection()
function allows you to fetch data of a single NFT collection.
Of course, this collection has to be whitelisted on Rain. getCollection()
accepts
a collectionId
as an argument - that's unique number assigned to every collection that has
ever been whitelisted on Rain. To use this function, you have to know the ID of your
collection (you can use getAvailableCollections()
and filter results by name, creator, etc)
const connection = new Connection("<Your RPC URL>");
const {publicKey} = Keypair.generate();
const rain = new Rain(
connection,
publicKey,
);
const {
getCollection
// ^ Get data of a particular collection based on its ID.
} = rain.utils;
// 517 is ID of 'Bold Badgers' collection.
const collection = await getCollection(connection, 517);
console.log({collection});
getWhitelistedCollectionFromPool()
getWhitelistedCollectionFromPool()
is pretty self-explanatory. The function
allows you to fetch all collections whitelisted by a particular pool.
const connection = new Connection("<Your RPC URL>");
const {publicKey} = Keypair.generate();
const rain = new Rain(
connection,
publicKey,
);
// 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("<Pool Owner Address>")
const {
getWhitelistedCollectionFromPool
// Get all collections whitelisted by particular pool.
} = rain.utils;
const collections = await getWhitelistedCollectionFromPool(connection, poolOwner);
console.log({collections});
getLoansFromCollectionId()
getLoansFromCollectionId()
function allows you to get all loans, where item from particular collection
was used as a collateral. That includes ongoing, repaid and liquidated loans.
Function also allows you to apply additional filters - borrower, loan duration, amount, status, etc.
const connection = new Connection("<Your RPC URL>");
const {publicKey} = Keypair.generate();
const rain = new Rain(
connection,
publicKey,
);
const {
getLoansFromCollectionId
} = rain.utils;
const loans = await getLoansFromCollectionId(
connection, // Solana Connection instance.
517, // Collection's unique ID.
[{
key: "borrower",
value: "<Your Address>"
}]
// ^ Array of additional filters. In this particular
// case, function will only return YOUR loans.
);
console.log({loans});
getMortgagesFromCollectionId()
getLoansFromCollectionId()
function allows you to get all mortgages, which were taken to fund item from particular collection.
That includes ongoing, repaid and liquidated mortgages.
Function also allows you to apply additional filters - borrower, mortgage duration, amount, current status, etc.
const connection = new Connection("<Your RPC URL>");
const {publicKey} = Keypair.generate();
const rain = new Rain(
connection,
publicKey,
);
const {
getMortgagesFromCollectionId
} = rain.utils;
const mortgages = await getMortgagesFromCollectionId(
connection, // Solana Connection instance.
517, // Collection's unique ID.
[{
key: "borrower",
value: "<Your Address>"
}]
// ^ Array of additional filters. In this particular
// case, function will only return YOUR mortgages.
);
console.log({mortgages});
getFiltersCollections()
getFiltersCollections()
is more low-level than most of the functions we've discussed before.
It accepts list of human-readable filters as the only parameter.
Then, filters are parsed and function returns an array of filters that you can pass to
getProgramAccounts()
method from Solana SDK. This way, you'll get all program accounts of collections
passing specified filters.
import { getProgramAccounts } from '@solana/web3.js'
const connection = new Connection("<Your RPC URL>");
const {publicKey} = Keypair.generate();
const rain = new Rain(
connection,
publicKey,
);
const {
getFiltersCollections
} = rain.utils;
const filters = getFiltersCollections([{
key: "is_locked",
value: ""
// ^ All collections that are not locked.
}]);
const programAccounts = await connection.getProgramAccounts(
new PublicKey("RainEraPU5yDoJmTrHdYynK9739GkEfDsE4ffqce2BR"),
// ^ Address of official Rain.fi program deployment.
{filters}
);
console.log({programAccounts});
getCustomLoanOffersFromCollectionId()
The getCustomLoanOffersFromCollectionId()
function is used to fetch
customised loan offers made for a particular collection. Similarly to the
getLoansFromCollectionId()
, it accepts collectionId
as a parameter. It also accepts
custom filters (as optional parameter).
const connection = new Connection("<Your RPC URL>");
const {publicKey} = Keypair.generate();
const rain = new Rain(
connection,
publicKey,
);
const {
getCustomLoanOffersFromCollectionId
} = rain.utils;
const customLoanOffers = await getCustomLoanOffersFromCollectionId(
connection, // Solana Connection instance.
517, // Collection's unique ID.
);
console.log({customLoanOffers});
getWhitelistedCollectionFromIds()
The getWhitelistedCollectionFromIds()
function accepts array of collectionId
as a parameter.
As a result, it returns data of all passed collections.
As an optional parameter, function accepts SplPool
, which is simply pool data. If
this parameter is given, function will also calculate maxAmountToBorrow
for each of the collections.
const connection = new Connection("<Your RPC URL>");
const {publicKey} = Keypair.generate();
const rain = new Rain(
connection,
publicKey,
);
const {
getWhitelistedCollectionFromIds,
getPoolFromOwnerAddress,
} = rain.utils;
// Pool data will be needed in the next step.
const poolOwner = new PublicKey("prC6wjf6Qu3ZgBxMdpTcknReCberDnRitqkEHhG59yv");
const pool = await getPoolFromOwnerAddress(
connection,
poolOwner
);
const collections = [879, 880];
const collectionsData = await getWhitelistedCollectionFromIds(
connection,
collections.map(collection => {
return {
collection,
collectionLtv: 0, // Deprecated. This 0 is only needed as a placeholder.
}
}),
pool
// ^ We pass previously fetched pool data as the third,
// optional parameter.
);
console.log({collectionsData});