Smart Contract API Reference

Complete API reference documentation for the liquid staking protocol smart contracts. Includes detailed information about functions, account structures, error codes, and events.

Smart Contract API Reference

Complete API reference for the liquid staking protocol smart contracts

Core Functions

Essential functions for pool initialization and management

initialize_pool

Initializes a new stake pool with the specified configuration

Parameters

  • authority: Pubkey - Pool authority account
  • stake_token_mint: Pubkey - Stake token mint account
  • validator_list: Pubkey - Validator list account
  • config: PoolConfig - Pool configuration parameters

Example

1
2// Initialize a new stake pool
3pub fn initialize_pool(
4    ctx: Context<InitializePool>,
5    config: PoolConfig,
6) -> Result<()> {
7    let pool = &mut ctx.accounts.pool;
8    
9    // Set initial pool configuration
10    pool.authority = ctx.accounts.authority.key();
11    pool.stake_token_mint = ctx.accounts.stake_token_mint.key();
12    pool.validator_list = ctx.accounts.validator_list.key();
13    
14    // Initialize pool parameters
15    pool.min_stake = config.min_stake;
16    pool.max_validators = config.max_validators;
17    pool.commission_percentage = config.commission_percentage;
18    pool.withdrawal_timelock = config.withdrawal_timelock;
19    
20    // Set initial pool state
21    pool.total_staked = 0;
22    pool.total_shares = 0;
23    pool.is_paused = false;
24    
25    Ok(())
26}

Staking Functions

Functions for staking and unstaking operations

stake

Stakes SOL into the pool and mints stake tokens

Parameters

  • amount: u64 - Amount of SOL to stake (in lamports)
  • user: Signer - User's wallet account
  • pool_account: AccountInfo - Pool's SOL account
  • user_token_account: AccountInfo - User's stake token account

Example

1
2// Stake SOL into the pool
3pub fn stake(
4    ctx: Context<Stake>,
5    amount: u64,
6) -> Result<()> {
7    // Validate minimum stake amount
8    require!(
9        amount >= ctx.accounts.pool.min_stake,
10        CustomError::BelowMinimumStake
11    );
12    
13    // Calculate shares to mint
14    let shares = if ctx.accounts.pool.total_shares == 0 {
15        amount
16    } else {
17        amount
18            .checked_mul(ctx.accounts.pool.total_shares)?
19            .checked_div(ctx.accounts.pool.total_staked)?
20    };
21    
22    // Transfer SOL to pool
23    system_program::transfer(
24        CpiContext::new(
25            ctx.accounts.system_program.to_account_info(),
26            system_program::Transfer {
27                from: ctx.accounts.user.to_account_info(),
28                to: ctx.accounts.pool_account.to_account_info(),
29            },
30        ),
31        amount,
32    )?;
33    
34    // Mint stake tokens to user
35    token::mint_to(
36        CpiContext::new_with_signer(
37            ctx.accounts.token_program.to_account_info(),
38            token::MintTo {
39                mint: ctx.accounts.stake_token_mint.to_account_info(),
40                to: ctx.accounts.user_token_account.to_account_info(),
41                authority: ctx.accounts.pool_authority.to_account_info(),
42            },
43            &[&[ctx.accounts.pool.key().as_ref(), &[pool_authority_bump]]],
44        ),
45        shares,
46    )?;
47    
48    // Update pool state
49    ctx.accounts.pool.total_staked = ctx.accounts.pool.total_staked.checked_add(amount)?;
50    ctx.accounts.pool.total_shares = ctx.accounts.pool.total_shares.checked_add(shares)?;
51    
52    Ok(())
53}

unstake

Initiates unstaking process by burning stake tokens and creating a withdrawal request

Parameters

  • shares: u64 - Amount of stake tokens to burn
  • user: Signer - User's wallet account
  • withdrawal: AccountInfo - Withdrawal request account
  • user_token_account: AccountInfo - User's stake token account

Example

1
2// Unstake SOL from the pool
3pub fn unstake(
4    ctx: Context<Unstake>,
5    shares: u64,
6) -> Result<()> {
7    // Calculate SOL amount to withdraw
8    let amount = shares
9        .checked_mul(ctx.accounts.pool.total_staked)?
10        .checked_div(ctx.accounts.pool.total_shares)?;
11    
12    // Create withdrawal request
13    let withdrawal = &mut ctx.accounts.withdrawal;
14    withdrawal.owner = ctx.accounts.user.key();
15    withdrawal.amount = amount;
16    withdrawal.request_time = Clock::get()?.unix_timestamp;
17    
18    // Burn stake tokens
19    token::burn(
20        CpiContext::new(
21            ctx.accounts.token_program.to_account_info(),
22            token::Burn {
23                mint: ctx.accounts.stake_token_mint.to_account_info(),
24                from: ctx.accounts.user_token_account.to_account_info(),
25                authority: ctx.accounts.user.to_account_info(),
26            },
27        ),
28        shares,
29    )?;
30    
31    // Update pool state
32    ctx.accounts.pool.total_staked = ctx.accounts.pool.total_staked.checked_sub(amount)?;
33    ctx.accounts.pool.total_shares = ctx.accounts.pool.total_shares.checked_sub(shares)?;
34    
35    Ok(())
36}

Account Structures

Data structures for program accounts

Pool Account

pub struct Pool { pub authority: Pubkey, pub stake_token_mint: Pubkey, pub validator_list: Pubkey, pub total_staked: u64, pub total_shares: u64, pub min_stake: u64, pub max_validators: u32, pub commission_percentage: u8, pub withdrawal_timelock: i64, pub is_paused: bool, }

Validator Info

pub struct ValidatorInfo { pub validator_account: Pubkey, pub stake_account: Pubkey, pub active_balance: u64, pub transient_balance: u64, pub last_update: i64, pub status: ValidatorStatus, }

Withdrawal Request

pub struct WithdrawalRequest { pub owner: Pubkey, pub amount: u64, pub request_time: i64, pub status: WithdrawalStatus, }

Error Codes

Program error codes and their descriptions

#[error_code] pub enum CustomError { #[msg("Amount is below minimum stake requirement")] BelowMinimumStake, #[msg("Pool has reached maximum validator limit")] MaxValidatorsReached, #[msg("Withdrawal timelock period has not expired")] TimelockNotExpired, #[msg("Invalid validator status")] InvalidValidatorStatus, #[msg("Pool is paused")] PoolPaused, #[msg("Insufficient stake balance")] InsufficientStakeBalance, }

Events

Program events emitted during operations

Stake Event

#[event] pub struct StakeEvent { pub user: Pubkey, pub pool: Pubkey, pub amount: u64, pub shares: u64, pub timestamp: i64, }

Unstake Event

#[event] pub struct UnstakeEvent { pub user: Pubkey, pub pool: Pubkey, pub shares: u64, pub amount: u64, pub timestamp: i64, }

Was this helpful?