Vault
The Vault is the central contract in the Satoshi Perps protocol that manages all positions, collateral, liquidations, and funding rates. It serves as the primary storage layer for the protocol’s state.Contract Overview
The Vault handles:- Storing and managing user positions
- Tracking collateral for all positions
- Executing position increases and decreases
- Managing funding rates
- Handling liquidations
- Tracking global open interest
The Vault contract is designed with modularity in mind, delegating certain functions to companion contracts like VaultUtils.
Key Functions
Position Management
Liquidation
Position Data Access
realisedPnl
is stored internally as an int256
to allow for negative values, but is returned as a uint256
with a boolean flag to indicate profit/loss.
Pool and Token Management
buyUSDG
and sellUSDG
functions in sequence.
Token Configuration
Upgradability
Position Mechanics
The Vault tracks positions using a multi-key approach:- Account address
- Collateral token
- Index token
- Direction (long/short)
- Size (the total position size in USD)
- Collateral (amount of collateral in USD)
- Average entry price
- Entry funding rate
- Reserved amounts
- Realized PnL (stored as int256 to allow for negative values)
- Last increased timestamp
Open Interest Tracking
The Vault tracks and limits the total open interest for each token:- Adds the size delta to the token’s longOpenInterest (for longs) or shortOpenInterest (for shorts)
- Validates that the new open interest doesn’t exceed maxLongOpenInterest or maxShortOpenInterest
- Reverts the transaction if limits are exceeded
setTokenConfig
function:
Leverage Calculation
Leverage is calculated assize / collateral
. For example:
- With 1 ETH collateral worth $2000
- Position size of $10,000
- Leverage = 5x
Funding Rate Mechanism
The Vault implements a dynamic funding rate to balance longs and shorts:- The difference between long and short open interest
- Elapsed time since the last update
- Funding rate factors configured in the contract
Liquidation Mechanism
Positions become eligible for liquidation when:- Position leverage exceeds maximum allowed leverage
- Collateral minus losses falls below maintenance margin requirement
Liquidators can call
liquidatePosition()
on eligible positions and receive a portion of the remaining collateral as a reward.Integration Points
The Vault integrates with:- PositionRouter: For executing position changes through a queue-based system
- PositionManager: For direct position management with role-based access
- OrderBook: For executing limit orders
- VaultUtils: For utility functions related to positions and fees
- VaultPriceFeed: For price feed data used in position calculations
Security Considerations
The Vault implements several security features:- Access controls for critical functions
- Circuit breakers for maximum position sizes
- Slippage protection
- Oracle price validation
- Reentrancy protection
All sensitive functions are protected by the
nonReentrant
modifier to prevent reentrancy attacks.Position Flows
For detailed flowcharts showing the process of opening positions, closing positions, and liquidations, see the Protocol Flow Charts page.Example: Position Lifecycle
1
Opening a Position
A trader wants to open a 5x long ETH position with 1 ETH as collateral.
- The trader calls the PositionRouter’s createIncreasePosition function
- The request is queued for execution
- When executed, the Vault records a new position with:
- Size: 2,000)
- Collateral: $2,000
- Average Price: Current ETH price
2
Managing the Position
The trader decides to add more collateral to reduce leverage.
- Trader calls PositionRouter’s createIncreasePosition with just collateral (no size delta)
- The Vault updates the position’s collateral
- New leverage: 3,000 = 3.33x
3
Closing the Position
The trader decides to close the position when ETH price is $2,200.
- Trader calls PositionRouter’s createDecreasePosition for full size
- The Vault calculates profit: (2,000) * (10,000 / 1,000
- Trader receives: 1,000 (profit) - fees