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

lenfi-sdk

v0.0.7

Published

SDK library to interact with Lenfi protocol.

Downloads

603

Readme

lenfi-sdk

The lenfi-sdk is a TypeScript library that provides an interface to interact with the Lenfi protocol on the Cardano blockchain. This SDK simplifies the process of depositing, borrowing, repaying, and liquidating assets within the Lenfi ecosystem.

Available Functions

Here's a list of all available functions in the lenfi-sdk:

  1. createDeposit: Deposit assets into a Lenfi pool.
  2. createLoan: Borrow assets from a Lenfi pool.
  3. repayLoan: Repay a loan taken from a Lenfi pool.
  4. createLiquidation: Liquidate an undercollateralized loan.
  5. createWithdrawal: Withdraw assets from a Lenfi pool.
  6. createBatcherDeposit: Create a batcher deposit order.
  7. createBatcherBorrow: Create a batcher borrow order.
  8. createBatcherRepay: Create a batcher repay order.
  9. createBatcherWithdraw: Create a batcher withdraw order.
  10. executeBatcherDeposit: Execute a batcher deposit order.
  11. executeBatcherBorrow: Execute a batcher borrow order.
  12. executeBatcherRepay: Execute a batcher repay order.
  13. executeBatcherWithdraw: Execute a batcher withdraw order.
  14. cancelBatcherOrder: Cancel a batcher order.
  15. claimeLiquidated: Claim liquidated assets after a liquidation.

Direct Transactions vs. Batcher Transactions

By default, all transactions should be created to interact with the pool directly. This is the most efficient method during normal usage. However, during times of high network congestion or pool usage, you have the option to create a batcher transaction.

Batcher transactions work as follows:

  1. Instead of interacting with the pool directly, you create an "order" transaction (using functions like createBatcherDeposit, createBatcherBorrow, etc.).
  2. This order is then picked up and executed by a third party (often called a "keeper" or "bot") for a fee of 2 ADA.
  3. The third party uses functions like executeBatcherDeposit, executeBatcherBorrow, etc., to process your order.

This batcher system helps to manage high-traffic periods by allowing transactions to be bundled and executed more efficiently. It also provides a way for transactions to be processed even when the user may not have enough ADA to cover network fees during congested periods.

Use batcher transactions when:

  • The network is congested and transaction fees are high
  • The pool is experiencing high usage and direct transactions are failing
  • You want to ensure your transaction will be processed even if you're not online

Remember, while batcher transactions provide benefits during high-usage periods, they come with a 2 ADA fee and may not be processed as immediately as direct transactions.

You can also use 'Execute' batcher orders to collect that fee.

Installation

To install the lenfi-sdk, use npm:

npm install lenfi-sdk

Prerequisites

Before using the SDK, make sure you have:

  1. A Blockfrost API key
  2. Access to a Cardano wallet (address)

Usage

Here are examples of how to use the main functions provided by the lenfi-sdk:

Initializing Lucid

For all operations, you'll need to initialize the Lucid library:

import { Blockfrost, Lucid } from "lucid-cardano";

const initLucid = async (blockfrostApiKey: string, address: string) => {
  const lucid = await Lucid.new(
    new Blockfrost(
      "https://cardano-mainnet.blockfrost.io/api/v0",
      blockfrostApiKey
    ),
    "Mainnet"
  );

  lucid.selectWalletFrom({ address });
  return lucid;
};

Depositing

To deposit assets into a Lenfi pool:

import { createDeposit, DepositParams } from "lenfi-sdk";

const depositExample = async () => {
  const lucid = await initLucid(blockfrostApiKey, userAddress);

  const depositParams: DepositParams = {
    lucid,
    balanceToDeposit: 51000000n,
    poolTokenName: "7876ebac44945a88855442692b86400776e0a2987c5f54a19b457d86",
    lpValidatorTxHash: "17d2a5a56aacc0905b0abc6d40beee70a207155acf7e712f18d0c59c95fc5cba",
    lpValidatorTxOutput: 0,
  };

  const depositResult = await createDeposit(depositParams);
  console.log(depositResult);
};

Borrowing

To borrow assets from a Lenfi pool:

import { createLoan, BorrowParams } from "lenfi-sdk";
import { getValidityRange } from "lenfi-sdk/utils/helpers";

const borrowExample = async () => {
  const lucid = await initLucid(blockfrostApiKey, userAddress);
  const validityRange = getValidityRange(lucid);

  const borrowParams: BorrowParams = {
    lucid,
    validityRange,
    loanAmount: 51_000_000n,
    collateralAmount: 200_000_000n,
    poolTokenName: "7876ebac44945a88855442692b86400776e0a2987c5f54a19b457d86",
    collateralTokenPrice: await fetchTokenPrice(collateralTokenId),
    loanTokenPrice: await fetchTokenPrice(loanTokenId),
  };

  const borrowResult = await createLoan(borrowParams);
  console.log(borrowResult);
};

Repaying

To repay a loan:

import { repayLoan, RepayParams } from "lenfi-sdk";
import { getValidityRange } from "lenfi-sdk/utils/helpers";

const repayExample = async () => {
  const lucid = await initLucid(blockfrostApiKey, userAddress);
  const validityRange = getValidityRange(lucid);

  const repayParams: RepayParams = {
    lucid,
    validityRange,
    poolTokenName: "7876ebac44945a88855442692b86400776e0a2987c5f54a19b457d86",
    loanTxHash: "f5fc37c67071904be218dcb727aba30065f95312e051f007b747b43495af9b92",
    loanTxOutputIndex: 1,
  };

  const repayResult = await repayLoan(repayParams);
  console.log(repayResult);
};

Liquidating

To liquidate an undercollateralized loan:

import { createLiquidation, LiquidateParams } from "lenfi-sdk";
import { getValidityRange } from "lenfi-sdk/utils/helpers";

const liquidateExample = async () => {
  const lucid = await initLucid(blockfrostApiKey, userAddress);
  const validityRange = getValidityRange(lucid);

  const liquidateParams: LiquidateParams = {
    lucid,
    validityRange,
    poolTokenName: "7876ebac44945a88855442692b86400776e0a2987c5f54a19b457d86",
    loanTxHash: "f5fc37c67071904be218dcb727aba30065f95312e051f007b747b43495af9b92",
    loanTxOutputIndex: 1,
    loanTokenPrice: await fetchTokenPrice(loanTokenId),
    collateralTokenPrice: await fetchTokenPrice(collateralTokenId),
  };

  const liquidateResult = await createLiquidation(liquidateParams);
  console.log(liquidateResult);
};

Withdrawing

To withdraw assets from a Lenfi pool:

import { createWithdrawal, WithdrawParams } from "lenfi-sdk";

const withdrawExample = async () => {
  const lucid = await initLucid(blockfrostApiKey, userAddress);

  const withdrawParams: WithdrawParams = {
    lucid,
    amountToWithdraw: 51000000n,
    poolTokenName: "7876ebac44945a88855442692b86400776e0a2987c5f54a19b457d86",
    lpValidatorTxHash: "17d2a5a56aacc0905b0abc6d40beee70a207155acf7e712f18d0c59c95fc5cba",
    lpValidatorTxOutput: 0,
  };

  const withdrawResult = await createWithdrawal(withdrawParams);
  console.log(withdrawResult);
};

Batcher Deposit

To create a batcher deposit order:

import { createBatcherDeposit, BatcherDepositParams } from "lenfi-sdk";

const batcherDepositExample = async () => {
  const lucid = await initLucid(blockfrostApiKey, userAddress);

  const depositParams: BatcherDepositParams = {
    lucid,
    balanceToDeposit: 51_000_000n,
    poolTokenName: "7876ebac44945a88855442692b86400776e0a2987c5f54a19b457d86",
  };

  const depositResult = await createBatcherDeposit(depositParams);
  console.log(depositResult);
};

Batcher Borrow

To create a batcher borrow order:

import { createBatcherBorrow, BatcherBorrowParams } from "lenfi-sdk";
import { getValidityRange } from "lenfi-sdk/utils/helpers";

const batcherBorrowExample = async () => {
  const lucid = await initLucid(blockfrostApiKey, userAddress);
  const validityRange = getValidityRange(lucid);

  const borrowParams: BatcherBorrowParams = {
    lucid,
    validityRange,
    loanAmount: 51_000_000n,
    collateralAmount: 200_000_000n,
    poolTokenName: "7876ebac44945a88855442692b86400776e0a2987c5f54a19b457d86",
    collateralTokenPrice: await fetchTokenPrice(collateralTokenId),
    loanTokenPrice: await fetchTokenPrice(loanTokenId),
  };

  const borrowResult = await createBatcherBorrow(borrowParams);
  console.log(borrowResult);
};

Batcher Repay

To create a batcher repay order:

import { createBatcherRepay, BatcherRepayParams } from "lenfi-sdk";
import { getValidityRange } from "lenfi-sdk/utils/helpers";

const batcherRepayExample = async () => {
  const lucid = await initLucid(blockfrostApiKey, userAddress);
  const validityRange = getValidityRange(lucid);

  const repayParams: BatcherRepayParams = {
    lucid,
    validityRange,
    poolTokenName: "7876ebac44945a88855442692b86400776e0a2987c5f54a19b457d86",
    loanTxHash: "f5fc37c67071904be218dcb727aba30065f95312e051f007b747b43495af9b92",
    loanTxOutputIndex: 1,
  };

  const repayResult = await createBatcherRepay(repayParams);
  console.log(repayResult);
};

Batcher Withdraw

To create a batcher withdraw order:

import { createBatcherWithdraw, BatcherWithdrawParams } from "lenfi-sdk";

const batcherWithdrawExample = async () => {
  const lucid = await initLucid(blockfrostApiKey, userAddress);

  const withdrawParams: BatcherWithdrawParams = {
    lucid,
    amountToWithdraw: 51_000_000n,
    poolTokenName: "7876ebac44945a88855442692b86400776e0a2987c5f54a19b457d86",
  };

  const withdrawResult = await createBatcherWithdraw(withdrawParams);
  console.log(withdrawResult);
};

Execute Batcher Order

To execute a batcher order (example for deposit):

import { executeBatcherDeposit, BatcherExecuteDepositParams } from "lenfi-sdk";

const executeBatcherDepositExample = async () => {
  const lucid = await initLucid(blockfrostApiKey, userAddress);

  const executeParams: BatcherExecuteDepositParams = {
    lucid,
    orderTxHash: "f5fc37c67071904be218dcb727aba30065f95312e051f007b747b43495af9b92",
    orderTxOutputIndex: 0,
  };

  const executeResult = await executeBatcherDeposit(executeParams);
  console.log(executeResult);
};

Cancel Batcher Order

To cancel a batcher order:

import { cancelBatcherOrder, CancelBatcherOrderParams } from "lenfi-sdk";

const cancelBatcherOrderExample = async () => {
  const lucid = await initLucid(blockfrostApiKey, userAddress);

  const cancelParams: CancelBatcherOrderParams = {
    lucid,
    poolTokenName: "7876ebac44945a88855442692b86400776e0a2987c5f54a19b457d86",
    cancelTxHash: "f5fc37c67071904be218dcb727aba30065f95312e051f007b747b43495af9b92",
    cancelTxIndex: 0,
  };

  const cancelResult = await cancelBatcherOrder(cancelParams);
  console.log(cancelResult);
};

Claim Liquidated Assets

To claim liquidated assets after a liquidation:

import { claimeLiquidated, ClaimLiquidatedParams } from "lenfi-sdk";

const claimLiquidatedExample = async () => {
  const lucid = await initLucid(blockfrostApiKey, userAddress);

  const claimParams: ClaimLiquidatedParams = {
    lucid,
    liquidationTxHash: "f5fc37c67071904be218dcb727aba30065f95312e051f007b747b43495af9b92",
    liquidationTxOutputIndex: 0,
  };

  const claimResult = await claimeLiquidated(claimParams);
  console.log(claimResult);
};

To find more examples, please check the __tests__ folder.

Transaction Handling

The returned object contains a transaction body (CBOR) which can be signed and submitted to the Cardano blockchain.

Error Handling

All functions return a result object with a success boolean. If success is false, check the error property for details about what went wrong.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.