Skip to main content

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

Allows users to register a plugin address that can execute actions on their behalf.
Removes a previously registered plugin.

Token Approvals

Approves a plugin to use the user’s tokens via the Router.
Approves a specific token to be spent by the specified spender.

Position Management via Plugins

Allows an authorized plugin to increase a position for the specified account.
Allows an authorized plugin to decrease a position for the specified account.

Token Swapping

Swaps tokens through the Vault’s swap mechanism.
Swaps ETH to tokens through the Vault’s swap mechanism.

Plugin Architecture

The Router implements a permission system that allows plugins to execute actions on behalf of users:
  1. Users must first register a plugin using addPlugin
  2. Then they must approve the plugin using approvePlugin
  3. Only after both steps can a plugin execute actions on behalf of the user
Only approve plugins that you trust, as they will have significant control over your funds and positions in the protocol.

Plugin Authorization Flow

ETH Handling

The Router provides convenience functions for handling ETH:
Converts ETH to WETH and sends it to the Vault.
Unwraps WETH to ETH and sends it to the specified receiver.

Integration with Vault

The Router interacts with the Vault in several ways:
  1. Position Management: Forwards position modifications to the Vault
  2. Token Swaps: Uses the Vault’s swap functionality
  3. 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

Extending with Custom Plugins

The Router’s plugin architecture enables developers to extend the protocol’s functionality: