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

@blend-capital/blend-sdk

v2.1.2

Published

Javascript SDK for the Blend Protocol

Downloads

537

Readme

blend-sdk-js

Blend Protocol SDK for Javascript to assist in interacting with the Blend Protocol.

Getting Started

  1. Install it using npm:
npm install @blend-capital/blend-sdk

Usage

Each Blend Protocol contract exports a contract class for interacting with the contract. Some contracts, like the Pool, include additional classes to help read ledger state.

NOTE: There is an outstanding issue passing XDR objects between packages. See the following issue for more details: https://github.com/stellar/js-stellar-base/issues/617

Both the blend-utils and blend-ui repositories make extensive use of this SDK for reference.

Supply an Asset to a Pool

The contract class creates an operation as a base64 XDR string that can be used with the @stellar/stellar-sdk TransactionBuilder class. When interacting with Soroban contracts, the invoker must populate the SorobanData portion of the transaction. The @stellar/stellar-sdk provides an easy way to do this by simulating the transaction against an RPC. For more details about how to submit a Soroban operation to the Stellar network, please see: https://developers.stellar.org/docs/build/guides/transactions/invoke-contract-tx-sdk

import { PoolContract, RequestType } from '@blend-capital/blend-sdk';
import { xdr } from '@stellar/stellar-sdk';

const asset: Address = // C... the Address of the asset to lend
const user: Address = // G... some Address
const to_lend: bigint = // ... the amount to lend as a bigint fixed point number with the tokens decimals 

const pool_contract = new PoolContract(poolId);
const supply_op = xdr.Operation.fromXDR(
    pool_contract.submit({
        from: user,
        spender: user,
        to: user,
        requests: [
            {
                amount: to_lend,
                request_type: RequestType.SupplyCollateral,
                address: asset,
            },
        ],
    }),
    'base64'
);

// build a transaction with the operation and submit it to the network

To submit other actions against the pool, you can modify the Requests array to contain the actions to take against the pool for from, spender, and to.

Read ledger data for a Pool and a User

Helper classes are included to help read ledger data for a pool from an RPC efficiently. The following code reads the ledger data for the pool, it's reserves, and the state of it's backstop.

import {
  Backstop,
  BackstopPool,
  BackstopPoolEst,
  BackstopPoolUser,
  BackstopPoolUserEst,
  Network,
  Pool,
  PoolEstimate,
  PoolOracle,
  PositionsEstimate,
} from '@blend-capital/blend-sdk';

const network: Network = {
  rpc: // rpc URL,
  passphrase: // Stellar network passphrase,
  // optional, allows you to connect to a local RPC instance
  opts: { allowHttp: true },
};

const backstop_id = // C... the address of the backstop contract
const pool_id = // C... the address of the pool contract
const user_id = // G... the address of a user that has taken a position in the pool

// Load the pool data from the ledger to check things like supported reserves, current interest rates
// and other pool specific information like emissions.
const pool = await Pool.load(network, pool_id);

// If price is needed, load the pool's oracle.
const pool_oracle = await pool.loadOracle();

// Additional estimates aggregate information using oracle prices. For example,
// the PoolEstimate class calculates things like total value supplied and borrowed.
const pool_est = PoolEstimate.build(pool.reserves, pool_oracle);

// The pool also allows you to directly load a user's position and emissions information.
const pool_user = await pool.loadUser(user_id);

// Additional estimates for their positions calculate things like borrow limit, net apr, ect.
const user_est = PositionsEstimate.build(pool, pool_oracle, pool_user.positions);

// Load the backstop data from the ledger to check things like the status of the backstop token and the
// configuration of the backstop
const backstop = await Backstop.load(network, backstop_id);

// Load a pool's backstop data to get information like backstop size, Q4W percentage, and more.
const backstop_pool = await BackstopPool.load(network, backstop_id, pool_id);

// Additional estimates calculate the total number of BLND/USDC tokens the backstop holds.
const backstop_pool_est = BackstopPoolEst.build(backstop.backstopToken, backstop_pool.poolBalance);

// Load a user's position in a pool's backstop
const backstop_pool_user = await BackstopPoolUser.load(network, backstop_id, pool_id, user_id);

// Additional estimates calcualte total number of BLND/USDC tokens the user holds the status
// of any queued withdrawals, and the unclaimed emissions.
const backstop_pool_user_est = BackstopPoolUserEst.build(backstop, backstop_pool, backstop_pool_user);