Open this lesson in your favourite AI. It'll walk you through the why, explain the demo, and quiz you on the try-it list.
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.
Use these three in order. Each builds on the one before.
In one paragraph, explain Solana rent and rent-exempt accounts.
Walk me through how Anchor's #[account(init, ...)] computes rent.
Design a vault protocol where 100k users each have a position account. How do you minimize aggregate rent burden?
// 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>,
}