@famousfoxfederation/citrus-sdk
v0.3.3
Published
SDK to interact with the Citrus P2P lending platform
Downloads
12
Readme
Citrus SDK
Citrus is a P2P lending platform on Solana by Famous Fox Federation. This package can be used to interact with the program directly.
Installation
npm i @famousfoxfederation/citrus-sdk
Update
Starting, 27/04/2023 you can make offers directly using LTV. Offers with LTV set have principal set to 0, you can fill this in using the floor prices from the fetchCollections
call.
Usage
Create a CitrusSDK object with a wallet
and connection
. You can use Wallet from @coral-xyz/anchor or AnchorWallet from @solana/wallet-adapter.
import { Wallet, web3 } from "@coral-xyz/anchor";
import { CitrusSdk } from "@famousfoxfederation/citrus-sdk";
const keypair = web3.Keypair.fromSecretKey(process.env.SECRETKEY);
const wallet = new Wallet(keypair);
const conn = new web3.Connection(process.env.RPC_NODE, { commitment: 'confirmed' });
const sdk = new CitrusSdk(wallet, conn);
const collections = await sdk.fetchCollections();
Methods
The SDK provides the following methods to fetch data:
fetchCollections
: Get list of collections available for lending on Citrus.fetchLoan
: Get loan information from a loan account.fetchLoans
: Get loan information for multiple loan accounts.fetchUserLoans
: Get loans lent or borrowed by a user.fetchCollectionLoans
: Get loans available or active for a collection.calculateInterest
: Calculate interest for given terms
Note: These methods use getProgramAccounts
under the hood and are subject to performance issues. You can also use the Hellomoon Citrus API for faster and efficient data fetching.
And the following methods to make transactions:
offerLoan
: Offer a loan with given terms for a collection.cancelLoanOffer
: Cancel an offer created with the method above.borrowLoan
: Borrow an open loan offer with an NFT.borrowTxn
: Transaction object to borrow a loan.repayLoan
: Repay an active loan.claimLoan
: Claim a defaulted loan.requestLoan
: List an NFT for loan.cancelLoanRequest
: Cancel the listing made above.lend
: Fulfil a loan request.reborrow
: Borrow a loan by repaying the active one.
Additionally, the SDK also provides a couple onchain listeners to get loans created, updated or cancelled in real time.
loanUpdateListener
: Stream loans created or updated.loanCancelListener
: Stream loans cancelled.
Example
const collections = await sdk.fetchCollections();
const fff = collections.find((c) => c.name === 'Famous Fox Federation');
const terms: OfferTerms = {
principal: 40,
apy: 140,
duration: 7,
};
const { loanAccount } = await sdk.offerLoan(terms, new web3.PublicKey(fff!.id));
console.log(`Created new loan offer ${loanAccount} with interest ${sdk.calculateInterest(terms) / web3.LAMPORTS_PER_SOL} SOL`);
await sdk.cancelLoanOffer(new web3.PublicKey(loanAccount));
console.log(`Cancelled loan offer ${loanAccount}`);