Skip to main content

Mortgages

Mortgages are the newest feature brought to the Rain.fi program. They allow users to buy a collectible with partial Rain funding.

Even if user can't afford to buy the item, by taking mortgage and paying partially for the item, they can still take part into market and take advantage of market moves.

Read further to find out how to interact with mortgages and manage them from the Rain SDK!

getMortgageFromAddress()

As the function name suggests, it allows us to fetch mortgages by address.

As parameters, function accepts the address, address role (pool/borrower) and optional filters. You can also specify if function should include ended raffles.

As a result, you get all mortgages matching specified data.

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

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

const {
getMortgageFromAddress
} = rain.utils;

const mortgages = await getMortgageFromAddress(
connection,
new PublicKey("<Address Here>"),
"borrower", // (or "pool")
false, // ended mortgages WILL NOT be skipped
[{
key: "mint",
value: "<NFT Mint Address>"
}]
// ^ Optional filters. In this particular case,
// only mortgages taken for this particular NFT
// will be returned.
);

console.log({mortgages});

getMortgageForSale()

The getMortgageForSale() function gives you an ability to fetch mortgages listed for sale. To start working with it, specify a collection and apply optional filters.

As a result, get a list of mortgages listed for sale, matching specified data.

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

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

const {
getMortgageForSale
} = rain.utils;

const mortgagesForSale = await getMortgageForSale(
connection,
517, // collectionId, unique ID of each collection listed on Rain.
[{
key: "borrower",
value: "<Address Here>"
}]
);

console.log({mortgagesForSale});

In this particular case, we're fetching all mortgages listed for sale, taken for items from Bold Badgers NFT collection (517 is unique ID of that collection). We're also applying additional filter - we only want the function to return mortgages where borrower's address is matching the one we specify.

getMortgageForSaleHistory()

We've just discussed getMortgageForSale() function, allowing you to fetch mortgages currently listed for sale.

getMortgageForSaleHistory() is similar - it allows you to explore historical listings of the mortgages.

It supports custom filtering - just pass the array of filters as the second parameter.

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

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

const {
getMortgageForSaleHistory
} = rain.utils;

const mortgagesForSale = await getMortgageForSaleHistory(
connection,
[{
key: "borrower",
value: "<Address Here>"
}]
);

console.log({mortgagesForSale});

As you can see, in this case we're fetching historical listings of mortgages, where borrower address matches the one we're specifying.

getMortgagesFeesDetailed()

Use getMortgagesFeesDetailed() to calculate fees, that you (or your user) will have to pay to get a mortgage.

Function accepts five parameters:

  • pool - pool data. Can be fetched using getPoolFromOwnerAddress().
  • amount - amount that you (or your user) will pay. The rest will be paid by the pool. Currently, Rain only supports 50/50 payment ratio. If you are purchasing item from floor, you can use floorPrice / 2.
  • maxDuration - max mortgage duration (in days).

Remember, the function only works for pools that have enabled mortgages. Either way it will return zeros.

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

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

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

// Fetch pool you want to use for the mortgage.
const pool = await getPoolFromOwnerAddress(
connection,
new PublicKey("FDfhQ9t7Nq8U4Y2xXynmd9bQW8HMdgbVS5PggeuAaff3"),
);
const { loanToValue, maxDuration } = pool;

const collection = await getCollection(
connection,
pool.collections[0].collection // First whitelisted collection.
);
const { floorPrice } = collection;

const detailedFees = await getMortgagesFeesDetailed(
pool,
floorPrice / 2,
maxDuration / 60 / 60 / 24
);

console.log({ detailedFees });

Returned data format:

{
feesInPercentage: number;
feesInSol: number;
rainFees: number;
amountToLoan: number;
duration: number;
}

getFiltersMortgageRequest() & getFiltersMortgage()

These two functions are pretty similar, so we decided to document both at the same time.

In previous chapters, we discussed getFiltersLoanRequest() and getFiltersLoan(), which are 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 mortgages and mortgages requests.

Usage:

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

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

const {
getFiltersMortgageRequest,
getFiltersMortgage
} = rain.utils;

const mortgageRequestsFilters = await getFiltersMortgageRequest([{
key: "borrower",
value: "<Address Here>"
}]);

const mortgagesFilters = await getFiltersMortgage([{
key: "borrower",
value: "<Address Here>"
}]);

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

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

console.log({mortgagesRequestsProgramAccounts, mortgagesProgramAccounts});