← Protocols
marginfi
Lending / Yield·Solana

marginfi

01Description

Solana lending and borrowing protocol (marginfi v2) with cross-collateralized accounts, isolated banks, emode, and a TypeScript client for programmatic deposits, borrows, withdrawals, and liquidations.

02Best for
  • 01Solana lending markets
  • 02cross-collateral borrowing accounts
  • 03isolated risk banks for long-tail assets
  • 04automated liquidation bots
  • 05LST collateral (jitoSOL, mSOL, INF) deposits
03Install
  • pnpm add @mrgnlabs/marginfi-client-v2 @mrgnlabs/mrgn-common @solana/web3.js
04Environment variables
VariableScopeDescription
SOLANA_RPC_URLServerSolana mainnet RPC URL. Bank/oracle account loads benefit from a paid RPC.
05Prompt snippet
Use `@mrgnlabs/marginfi-client-v2`. Create a client: `const client = await MarginfiClient.fetch(getConfig('production'), wallet, connection)`. Fetch or create a margin account: `const acct = await client.getMarginfiAccountByAuthority(walletPubkey)` or `await client.createMarginfiAccount()`. Operate via `acct.deposit(amount, bankAddress)`, `acct.borrow(amount, bankAddress)`, `acct.repay(amount, bankAddress, repayAll)`, `acct.withdraw(amount, bankAddress, withdrawAll)`. Each method returns a transaction signature after sending. Read positions with `acct.activeBalances` and health with `acct.computeHealthComponents(MarginRequirementType.Maintenance)`.
06Gotchas
  • marginfi accounts are PDA-derived per authority — a wallet can have multiple `MarginfiAccount`s; pick by index or label, do not assume one account per user.
  • Health factor calculations require fresh oracle data — most banks use Pyth, some use Switchboard. Stale oracles (slot age > `maxAge`) cause txs to revert with `OracleStale`.
  • Liquidations are permissionless and atomic — a bot can liquidate the moment your health hits 1.0; build front-end warnings against `MaintenanceHealth` not `InitialHealth`.
  • Set `ComputeBudgetProgram.setComputeUnitLimit({ units: 1_000_000 })` and a priority fee on every action — multi-bank accounts pull many oracle accounts and easily exceed 200k default CU.
  • Isolated banks cannot be used as collateral for borrows in other banks — verify `bank.config.riskTier` before composing positions or the borrow ix will fail.
  • Emode (efficiency mode) groups correlated assets for higher LTV; switching emode requires zero outstanding borrows in the account.
07Alternatives