Import the required packages for Solana RPC and Jupiter Earn SDK operations.
1
Import Dependencies
Import the packages you need for Solana RPC and Jupiter Lend (Earn) SDK operations.
Copy
Ask AI
import { Connection, PublicKey, Transaction } from "@solana/web3.js";import BN from "bn.js";import { getDepositIxs, getMintIxs } from "@jup-ag/lend/earn";
2
Build instructions
Use the Jupiter Earn SDK to generate withdraw instructions.
Copy
Ask AI
const { ixs: withdrawIxs } = await getWithdrawIxs({ new Connection("https://api.mainnet-beta.solana.com"), new PublicKey("9a7qBeHBtjKk46YhfPJmBQG5zPmk4TcqpYbdJmJqcMup"), asset: new PublicKey("9a7qBeHBtjKk46YhfPJmBQG5zPmk4TcqpYbdJmJqcMup"), // USDC Mint amount: new BN(10_000_000), // 10 USDC (6 decimals)});console.log('Withdraw Instructions:', withdrawIxs);
Important
amount = underlying token amount (e.g. USDC)
Not vault shares
To withdraw by vault shares, use getRedeemIxs
Max withdraw or redeem all
To withdraw all of your position (full underlying amount), fetch the user’s position with getUserLendingPositionByAsset from @jup-ag/lend/earn, then pass userPosition.underlyingAssets as amount to getWithdrawIxs. To redeem all shares (burn all jlTokens for underlying), use getRedeemIxs with userPosition.lendingTokenShares as shares.
This example builds, signs, and sends a withdraw transaction.
1
Import Dependencies
Import the packages you need for Solana RPC and Jupiter Lend (Earn) SDK operations.
Copy
Ask AI
import { Connection, Keypair, PublicKey, Transaction, sendAndConfirmTransaction,} from "@solana/web3.js";import BN from "bn.js";import { getWithdrawIxs } from "@jup-ag/lend/earn";import fs from "fs";import path from "path";
2
Set up connection and withdraw parameters
Set your RPC, signer, asset mint, and withdraw amount.
Copy
Ask AI
const RPC_URL = "https://api.mainnet-beta.solana.com";const userKeypair = Keypair.generate(); // <-- Replace this with your walletconst ASSET_MINT = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"); // Asset mint to withdraw (USDC for example)const WITHDRAW_AMOUNT = new BN(10_000_000); // Amount of asset to withdraw, in smallest units (e.g., 1 USDC = 1_000_000)
If your signer is stored as a local JSON keypair file, you can load it using the helper function below.