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)
const poolManager = new PoolManager(programs, provider)
const pools = await poolManager.loadAll()
const walletTokens = await MarginAccount.loadTokens(poolManager.programs, localWalletPubkey)
const marginAccounts = await MarginAccount.loadAllByOwner({
programs: poolManager.programs,
provider: poolManager.provider,
pools,
walletTokens,
owner: localWalletPubkey
})
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"
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[] = []
await connection.requestAirdrop(walletPubkey, LAMPORTS_PER_SOL)
config = await MarginClient.getConfig("devnet")
programs = MarginClient.getPrograms(provider, config)
poolManager = new PoolManager(programs, provider)
pools = await poolManager.loadAll()
marginAccount = await MarginAccount.createAccount({
programs,
provider,
owner: walletPubkey,
seed: 0,
pools
})
await marginAccount.refresh()
await marginAccount.withCreateAccount(instructions)