npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rainfi/sdk

v1.3.43

Published

This package is used to interact with Rain.fi protocol on Solana

Downloads

353

Readme

  • This code is given as is and is unaudited. Use at your own risk.
  • The examples might not be always up to date since the package is in active development.

Table of contents:

Installation

Yarn:

yarn add @rainfi/sdk

NPM:

npm i @rainfi/sdk

Usage

import { Pool, Rain } from "@rainfi/sdk";
import { Connection, LAMPORTS_PER_SOL, Keypair, PublicKey } from "@solana/web3.js";

const connection = new Connection("https://api.mainnet-beta.solana.com", "confirmed");
const publicKey = new Keypair().publicKey // Your pubkey
const poolOwner = new PublicKey("OWNER_OF_POOL_PUBKEY")

// Let you interact with a specific pool only
const pool = new Pool(connection, poolOwner, publicKey)

// Let you interact with the whole protocol
const rain = new Rain(connection, publicKey)

Examples

Pool Class

Let you interact with a specific pool only. Useful when you want to intergarte it on your website.

import { Pool } from "@rainfi/sdk";
import { Connection, LAMPORTS_PER_SOL, Keypair, PublicKey } from "@solana/web3.js";

const connection = new Connection("https://api.mainnet-beta.solana.com", "confirmed");
const publicKey = new Keypair().publicKey // Your pubkey
const poolOwner = new PublicKey(OWNER_OF_POOL)

const pool = new Pool(connection, poolOwner, publicKey)

// To fetch/refresh data of the pool
const poolData = await pool.fetch()

// To fetch all loans/mortgages from the pool, including ongoing, liquidated and repaid ones
const allLoans = await pool.loans('all')
const mortgagesOpen = await pool.mortgages('all')

// To get all ongoing loans from pool
const loansOpen = await pool.loans('ongoing')
const mortgagesOpen = await pool.mortgages('ongoing')

// To get all loan history from pool
const loansHistory = await pool.loans('history')

// You can also pass the borrower pubkey to get the loans related to your pool from a specific address
const allLoans = await pool.loans('all', publicKey)
const loansOpen = await pool.mortgages('all', publicKey)

// To get whitelisted collections from the pool
const poolCollections = await pool.collections()

// Here, we just take the first collections in the pool as example
const collection = poolCollections[0]

//Create the borrow instruction
const borrowIxs = await pool.borrow(
  new PublicKey("Eb89RFb5MZh4QWaae7m3pMQf5S1eNwUvoaYcpiDGdBfE"),   // NFT mint address string that you want to collateralize
  3,                                                               // The duration of the loan in DAYS
  20 * LAMPORTS_PER_SOL,                                           // Amount you want to borrow, must be lower than collection.maxAmountToBorrow
  collection.floorPrice                                            // FloorPrice of the collection, used to avoid front run
)

//Create the repay instruction

const loanAddress = loansOpen[0].accountAddress // address of the account holding data for the loan

const repayIx = await pool.repay(loanAddress)

// Create buy instruction, current supported marketplace: Solanart, Hadeswap, and auction house

const buyIxs = await pool.buy({
  nftMint: PublicKey,                                       // NFT mint pubkey
  seller?: nftSellerPubkey,                                 // NFT seller pubkey
  auctionHousePubkey?: auctionHousePubkey,                  // AH address
  escrowAddress?: escrowAddress,                            // escrow address holding the NFT
  marketplace: 'solanart' | 'auction_house' | 'hadeswap',   // marketplace where NFT is listed
  floorPrice: collection.floorPrice,                        // collection floor price in LAMPORTS
  price: 120 * LAMPORTS_PER_SOL,                            // NFT price in LAMPORTS
  amount: (120 * LAMPORTS_PER_SOL) * 0.50,                  // amount paid by the borrower, should 50% of the price of the NFT in LAMPORTS
  duration: number                                          // The duration of the mortgage in DAYS
})

const repayMortgageIxs = await pool.repayMortgage(
  mortgageAddress,                                       // mortgage account address
  amount,                                                // Amount to repay in LAMPORTS
)

// send your instructions

Rain Class

Let you interact with the whole protocol and its features/functions with any pool.

Create a pool


import { Rain } from "@rainfi/sdk";
import { Connection, LAMPORTS_PER_SOL } from "@solana/web3.js";

const connection = new Connection("https://api.mainnet-beta.solana.com", "confirmed");
const publicKey = new Keypair().publicKey // Your pubkey
const rain = new Rain(connection, publicKey)

const instruction = await rain.createPool(
  {
    loanToValue: 50,                                              // 50% of the floor price of the NFT
    maxAmount: 100,                                               // Max amount that can be used per loan in SOL, should default to size of the pool
    interestType: 1,                                              // Enable dynamic interest or not
    interestRate: (!dynamicInterest && loanInterest * 100) || 0,  // If dynamicInterest is used, we set the interest rate, else we set to 0 since program will overwrite
    interestCurve: 25,                                            // Interest curve depending on your parameter see docs
    interestGap: 19,                                              // Interest curve depending on your parameter see docs
    maxDuration: 14,                                              // In days
    compound: true,                                               // Compound interest or not, put the profit back in the pool or not
    amount: 100,                                                  // pool size = 100 SOL
    collections: [
      {
        collection: 301,    // This is the rain ID of the collection
        collectionLtv: 5000 // This is the loan to value of your collection, 5000 means 50%
      },
      {
        collection: 793,    // This is the rain ID of the collection
        collectionLtv: 7000 // This is the loan to value of your collection, 7000 means 70%
      },
      {
        collection: 292,    // This is the rain ID of the collection
        collectionLtv: 6000 // This is the loan to value of your collection, 6000 means 60%
      }
    ]// Ids of the collections that you want to authorize lending on. They can be obtained using getAvailableCollections()
  }
)

// wrap and send your instructions

Borrow

import { Rain } from "@rainfi/sdk";
import { Connection, LAMPORTS_PER_SOL, Keypair, PublicKey } from "@solana/web3.js";

const connection = new Connection("https://api.mainnet-beta.solana.com", "confirmed");
const publicKey = new Keypair().publicKey // Your pubkey
const rain = new Rain(connection, publicKey)

// Get all pools available
const poolAvailable = await rain.utils.getAllPoolAvailable(connection)

// Select the pool
const pool = poolAvailable[0]

// To get all collections and their data, use that function.
const allCollections = await rain.utils.getAvailableCollections(connection)

// Here, we just want the data of Degenerate Ape Academy collection
const collection = await rain.utils.getCollection(connection, 292) // 292 is the ID of the DAA collection on rain

const duration = 3 // The duration of the loan in DAYS

//Create a borrow instruction
const borrowIxs = await rain.borrow({
  nftMint: new PublicKey("Eb89RFb5MZh4QWaae7m3pMQf5S1eNwUvoaYcpiDGdBfE"),   // NFT mint address string that you want to collateralize
  poolOwner: pool.owner,                                                    // The owner of the pool
  duration: duration,                                                       // The duration of the loan in DAYS
  amount: 20 * LAMPORTS_PER_SOL,                                            // Amount you want to borrow, must be lower than loanToValue
  floorPrice: collection.floorPrice                                         // FloorPrice of the collection, used to avoid front run
})

// wrap and send your instructions

License

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion by you shall be licensed at the discretion of the repository maintainers without any additional terms or conditions.

TODO

  • Add examples and docs for mortgages, functions are already in the package itself though
  • Add more examples about use cases
  • actually do the TODO