PositionRouter
The PositionRouter is a crucial component of the Satoshi Perps protocol that implements a gas-efficient queue-based system for position execution. It allows users to submit position requests that are executed in batches by keepers, significantly reducing gas costs for users.For visual flowcharts of the position creation and execution process, see the Protocol Flow Charts page.
Contract Overview
The PositionRouter handles:- Creating requests to increase or decrease positions
- Queueing position requests for batch execution
- Executing position requests in an efficient manner
- Managing execution fees for position requests
- Providing callbacks to notify external contracts when positions are executed
The PositionRouter is designed to handle high-volume trading with minimal gas costs per trade.
Key Functions
Position Creation
Position Decrease
Position Execution
Batch Execution
Request Queue Mechanics
The PositionRouter maintains two separate queues:- Increase Position Queue: For requests to open or add to positions
- Decrease Position Queue: For requests to reduce or close positions
- A unique request key
- The parameters needed for execution
- A timestamp indicating when the request was created
- Whether the request has been executed
Callback System
The PositionRouter implements a callback mechanism to notify external contracts when positions are executed:Execution Fee Mechanism
To incentivize keepers to execute position requests, the PositionRouter requires an execution fee for each request:- Users include an execution fee when creating a position request
- The fee is refunded if the request fails to execute
- When a request is executed successfully, the fee is sent to the keeper
The minimum execution fee is defined by the
minExecutionFee parameter and can be adjusted by the contract admin.Integration with Vault
The PositionRouter interacts with the Vault contract to execute positions:- When an increase position request is executed, the PositionRouter:
- Transfers tokens from the user (if not already done)
- Calls Vault’s increasePosition function
- When a decrease position request is executed, the PositionRouter:
- Calls Vault’s decreasePosition function
- Handles token transfers back to the user
Callback Gas Limits
The PositionRouter implements configurable gas limits for position execution callbacks:callbackGasLimit: Default gas limit for all callbackscustomCallbackGasLimits: Custom gas limits for specific callback targets
Position Cancellation
Users can cancel their pending position requests if they haven’t been executed within the allowed time window:- The request exists
- The caller is the request creator
- The request has not been executed
- The execution timeframe has expired
Position Keepers
The PositionRouter uses a keeper system to manage who can execute position requests:Security Considerations
The PositionRouter implements several security features:- Frontrunning Protection: Price bounds protect users from price manipulation
- Slippage Protection: Minimum output amounts ensure fair execution
- Delay Parameters: Configurable minimum and maximum delay between request and execution
- Access Control: Role-based permissions for execution and configuration
- Circuit Breakers: The contract can be paused in emergency situations
- Gas Limit Controls: Prevents callback functions from using excessive gas
Example Usage
1
Submit an Increase Position Request
A trader wants to open a 5x long ETH position with 1 ETH as collateral.
2
Keeper Executes the Request
A keeper monitors the pending requests and executes them.
3
Submit a Decrease Position Request
Later, the trader wants to close half of their position.