Router
The Router serves as a central entry point for user interactions with the Satoshi Perps protocol. It manages plugins, handles token approvals, and provides functionality for token swapping and position modifications through authorized plugins.Contract Overview
The Router handles:- Managing plugin registrations and permissions
- Handling token approvals for the Vault
- Wrapping and unwrapping ETH for interactions
- Providing interfaces for plugins to modify positions
- Facilitating token swaps through the Vault
The Router uses a plugin-based architecture to allow modular extensions of functionality while maintaining security.
Key Functions
Plugin Management
Token Approvals
Position Management via Plugins
Token Swapping
Plugin Architecture
The Router implements a permission system that allows plugins to execute actions on behalf of users:- Users must first register a plugin using
addPlugin - Then they must approve the plugin using
approvePlugin - Only after both steps can a plugin execute actions on behalf of the user
Plugin Authorization Flow
ETH Handling
The Router provides convenience functions for handling ETH:Integration with Vault
The Router interacts with the Vault in several ways:- Position Management: Forwards position modifications to the Vault
- Token Swaps: Uses the Vault’s swap functionality
- Token Approvals: Manages approvals for the Vault to use tokens
Security Considerations
The Router implements several security features:- Plugin Validation: Ensures plugins are authorized by the user
- Permission System: Only approved plugins can execute actions
- Reentrancy Protection: Prevents reentrancy attacks
- Access Controls: Restricts sensitive functions to authorized callers
The Router does not store user funds directly. Funds are always managed by the Vault contract.
Example: Using the Router with PositionManager
1
Register the PositionManager
First, a user registers the PositionManager as a plugin.
2
Approve Tokens
The user approves tokens to be used by the Router.
3
Interact via PositionManager
Now the user can interact with the PositionManager, which uses the Router as an intermediary.
Common Use Cases
Position Manager Integration
The Router enables the PositionManager to modify positions in the Vault on behalf of users
Order Book Integration
The Router allows the OrderBook to execute limit orders when conditions are met
Position Router Integration
Enables the PositionRouter to process queued position requests
Custom Plugins
Developers can create custom plugins that interact with the protocol via the Router