Pick a depth. Each prompt opens in your AI pre-loaded with the lesson. Click a row to preview the prompt.
Solana charges rent for every account (storage slot). For DeFi, this means every position, vault share, and order book entry has a fixed SOL cost to maintain. The rent-exempt deposit is refundable when the account is closed — but accounts that get orphaned (forgotten positions) tie up rent capital. Smart protocol design treats account lifecycle as a first-class concern.
Calculate rent for a position account.
// Anchor automatically computes rent-exempt minimum.
// Manual calculation: (data_len + 128) bytes * lamports_per_byte_year * 2 years
const MARGINFI_POSITION_SIZE: u64 = 256; // example account layout size
const LAMPORTS_PER_BYTE_YEAR: u64 = 3_480;
const RENT_EXEMPT_YEARS: u64 = 2;
let rent_lamports = (MARGINFI_POSITION_SIZE + 128)
* LAMPORTS_PER_BYTE_YEAR
* RENT_EXEMPT_YEARS;
// = (256 + 128) * 3_480 * 2 = 2,672,640 lamports = 0.00267 SOL
// Anchor pattern:
#[derive(Accounts)]
pub struct OpenPosition<'info> {
#[account(
init,
payer = user,
space = 8 + Position::INIT_SPACE,
seeds = [b"position", user.key().as_ref()],
bump,
)]
pub position: Account<'info, Position>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>,
}
// When closing:
#[derive(Accounts)]
pub struct ClosePosition<'info> {
#[account(mut, close = user)] // close = user refunds rent to user
pub position: Account<'info, Position>,
#[account(mut)]
pub user: Signer<'info>,
}