Router Program
The global coordinator managing collateral and cross-shard operations
The Router Program is the central coordinator of the PercolatePerps protocol, managing global state and cross-shard operations.
// Router Program - Core Structure
pub struct Router {
pub authority: Pubkey,
pub escrows: HashMap<Pubkey, UserEscrow>,
pub collateral_vaults: HashMap<Pubkey, CollateralVault>,
pub slab_registry: Vec<Pubkey>,
}
pub struct UserEscrow {
pub owner: Pubkey,
pub collateral: HashMap<Pubkey, u64>, // mint -> amount
pub margin_used: u64,
pub margin_available: u64,
}
#[program]
pub mod router {
pub fn deposit_collateral(
ctx: Context<DepositCollateral>,
amount: u64,
) -> Result<()> {
let escrow = &mut ctx.accounts.user_escrow;
escrow.collateral
.entry(ctx.accounts.mint.key())
.and_modify(|e| *e += amount)
.or_insert(amount);
Ok(())
}
}Key Responsibilities
Collateral Management
Handles custody of all user collateral across different asset types including USDC, SOL, and other supported tokens. Ensures secure storage and efficient allocation.
Portfolio Margin
Calculates cross-margin requirements across all user positions and slabs, enabling capital-efficient trading with reduced margin requirements.
pub fn calculate_portfolio_margin(
escrow: &UserEscrow,
positions: &[Position],
) -> u64 {
let mut total_margin = 0u64;
for pos in positions {
total_margin += pos.size * pos.mark_price / LEVERAGE;
}
total_margin
}Cross-Shard Routing
Routes orders and operations to appropriate slabs, coordinating multi-shard transactions and ensuring atomic execution.