DeFi Basics on Solana
8 examples to get you started with DeFi integrations - 5 basic and 3 intermediate. Covers swaps, AMMs, lending, LSTs, oracles, and composable CPI flows on Agave 4.1.1.
Search across all documentation pages
8 examples to get you started with DeFi integrations - 5 basic and 3 intermediate. Covers swaps, AMMs, lending, LSTs, oracles, and composable CPI flows on Agave 4.1.1.
@solana/kit 7.0.0 for TypeScript client examples.npm install @solana/kit@7.0.0 @solana/spl-tokenEvery DeFi action starts with knowing what the user holds.
import { createSolanaRpc, address } from "@solana/kit";
import { fetchMint, fetchToken } from "@solana-program/token";
const rpc = createSolanaRpc("https://api.mainnet-beta.solana.com");
const owner = address("YOUR_WALLET_PUBKEY");
const mint = address("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"); // USDC
const atas = await rpc.getTokenAccountsByOwner(owner, { mint }, { encoding: "jsonParsed" }).send();
console.log(atas.value);jsonParsed to avoid manual layout decoding in clients.Related: Swaps with Jupiter - routing after balance check
Aggregators return a route and serialized transaction bytes.
const inputMint = "So11111111111111111111111111111111111111112"; // wSOL
const outputMint = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"; // USDC
const amount = "100000000"; // 0.1 SOL in lamports
const quoteRes = await fetch(
`https://quote-api.jup.ag/v6/quote?inputMint=${inputMint}&outputMint=${outputMint}&amount=${amount}&slippageBps=50`
);
const quote = await quoteRes.json();
console.log(quote.outAmount, quote.routePlan);slippageBps is basis points (50 = 0.5%).routePlan showing which AMM legs execute.Related: AMMs (Raydium, Orca, Meteora) - what Jupiter routes through
AMMs on Solana use the same x * y = k invariant as EVM DEXes, but state lives in program-owned accounts.
// Simplified constant-product output (fee omitted)
pub fn amount_out(amount_in: u64, reserve_in: u64, reserve_out: u64) -> u64 {
let numerator = (amount_in as u128) * (reserve_out as u128);
let denominator = (reserve_in as u128) + (amount_in as u128);
(numerator / denominator) as u64
}Related: AMMs (Raydium, Orca, Meteora) - pool layouts
Lending and perps depend on fresh oracle prices.
import { createSolanaRpc, address } from "@solana/kit";
const rpc = createSolanaRpc("https://api.mainnet-beta.solana.com");
// Pyth SOL/USD price feed account (mainnet)
const priceAccount = address("H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG");
const account = await rpc.getAccountInfo(priceAccount, { encoding: "base64" }).send();
console.log(account.value?.data);publish_slot staleness against a max age.Related: Oracles - Pyth and Switchboard integration
Most pools quote against wrapped SOL, not native lamports in the wallet.
spl-token wrap 0.5
spl-token accountsspl-token wrap creates/funds the wSOL ATA and syncs native balance.Related: Swaps with Jupiter - wSOL in route legs
Turn a quote into a signed versioned transaction.
import { createSolanaRpc, createSolanaRpcSubscriptions, address } from "@solana/kit";
import { getBase58Codec } from "@solana/codecs-strings";
const swapRes = await fetch("https://quote-api.jup.ag/v6/swap", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
quoteResponse: quote,
userPublicKey: "YOUR_WALLET_PUBKEY",
wrapAndUnwrapSol: true,
dynamicComputeUnitLimit: true,
}),
});
const { swapTransaction } = await swapRes.json();
const txBytes = getBase58Codec().decode(swapTransaction);
// Sign and send with your wallet signerdynamicComputeUnitLimit simulates CU usage and sets a safe budget.wrapAndUnwrapSol handles wSOL lifecycle for SOL-involved routes.Related: DeFi Integrations Best Practices - slippage and simulation policy
Money markets read oracle prices inside the borrow instruction.
// Anchor-style account validation sketch
#[account(
constraint = oracle.key() == market.oracle_pubkey @ ErrorCode::BadOracle,
)]
pub oracle: AccountInfo<'info>,Related: Lending & Borrowing - Kamino and MarginFi patterns
Liquid staking tokens trade like any SPL mint but represent staked SOL.
const jitoSolMint = "J1toso1uCk3RLmjorhTtrVwY9HJ7X8V9yYac6Y7kGCPn";
// Swap SOL -> jitoSOL via Jupiter, then deposit jitoSOL as collateralRelated: Liquid Staking (LSTs) - mint addresses and rates
Stack versions: This page was written for Agave 4.1.1, Solana CLI 3.0.10, Anchor 0.32.1, anchor-lang 0.32.1, Rust 1.91.1, @solana/kit 7.0.0, Surfpool 0.12.0, and LiteSVM 0.6.x.
Reviewed by Chris St. John·Last updated Jul 16, 2026