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

@jet-lab/margin

v0.2.22

Published

Library for interacting with the Jet margin on-chain programs

Downloads

1,001

Readme

Version Docs Discord License

Install

Add your preferred library to your project.

with npm

$ npm i @jet-lab/margin

...or with yarn

$ yarn add @jet-lab/margin

Usage

View the typedocs for the full package documentation and available API.

View more examples for usage reference.

Instantiating the Client

Loading first margin account if local wallet created the first margin account

import { MarginAccount, MarginClient } from "@jet-lab/margin"
import { AnchorProvider, Wallet } from "@project-serum/anchor"
import { Connection, Keypair } from "@solana/web3.js"

const connection = new Connection("https://api.devnet.solana.com", "recent")
const options = AnchorProvider.defaultOptions()
const wallet = Wallet.local()
const provider = new AnchorProvider(connection, wallet, options)

const programs = MarginClient.getPrograms(provider, "devnet")
const marginAccount = await MarginAccount.load(programs, provider, wallet.publicKey, 0)

Monitoring Margin Account Health

Loading margin accounts and getting a margin account's risk indicator

import { MarginAccount, MarginClient, PoolManager } from "@jet-lab/margin"
import { Connection } from "@solana/web3.js"
import { AnchorProvider, Wallet } from "@project-serum/anchor"

const config = await MarginClient.getConfig("devnet")
const connection = new Connection("https://api.devnet.solana.com",  options.commitment)
const options = AnchorProvider.defaultOptions()
const wallet = Wallet.local()
const localWalletPubkey = wallet.publicKey
const provider = new AnchorProvider(connection, wallet, options)

const programs = MarginClient.getPrograms(provider, config)

//Load margin pools
const poolManager = new PoolManager(programs, provider)
const pools = await poolManager.loadAll()

//Load wallet tokens
const walletTokens = await MarginAccount.loadTokens(poolManager.programs, localWalletPubkey)

//Load all margin accounts - users can have multiple margin accounts eventually
const marginAccounts = await MarginAccount.loadAllByOwner({
  programs: poolManager.programs,
  provider: poolManager.provider,
  pools,
  walletTokens,
  owner: localWalletPubkey
})

//Print risk level of a margin account
if (marginAccounts) {
  console.log(
    `Public key ${localWalletPubkey} risk indicator is ${marginAccounts[0].riskIndicator}`
  )
} else {
  console.log("We have trouble getting margin accounts")
}

Crafting instructions

In scenarios where the integration process needs to create instructions without sending transactions. The following example creates instruction for creating a new margin account.

View more examples for creating instructions associated with the MarginAccount Class:

import {
  MarginClient,
  MarginPrograms,
  MarginAccount,
  Pool,
  PoolManager,
  PoolTokenChange,
  MarginConfig,

} from "@jet-lab/margin"
import { Connection, Keypair, LAMPORTS_PER_SOL, TransactionInstruction } from "@solana/web3.js"
import { AnchorProvider, Wallet } from "@project-serum/anchor"

// Setup 
const walletKepair = Keypair.generate()
const walletPubkey = walletKepair.publicKey

const options = AnchorProvider.defaultOptions()
const connection = new Connection("https://api.devnet.solana.com", options.commitment)
const wallet = new Wallet(walletKepair)
const provider = new AnchorProvider(connection, wallet, options)

let config: MarginConfig
let programs: MarginPrograms
let poolManager: PoolManager
let pools: Record<string, Pool>
let marginAccount: MarginAccount
const instructions: TransactionInstruction[] = []

// Airdrop
await connection.requestAirdrop(walletPubkey, LAMPORTS_PER_SOL)

// Load programs
config = await MarginClient.getConfig("devnet")
programs = MarginClient.getPrograms(provider, config)

// Load margin pools
poolManager = new PoolManager(programs, provider)
pools = await poolManager.loadAll()

// Create MarginAccount
marginAccount = await MarginAccount.createAccount({
  programs,
  provider,
  owner: walletPubkey,
  seed: 0,
  pools
})

await marginAccount.refresh()
  
// Creates instruction for create a new margin account for wallet
await marginAccount.withCreateAccount(instructions)