# RIP-1: Modular Sub-accounts (/docs/risex/rips/rip-1)

import { Callout } from 'fumadocs-ui/components/callout';

<Callout type="info">
  **Status:** Draft
</Callout>

RIP-1 is an open standard for programmable trading accounts on RISEx. It enables permissionless deployment of modular sub-accounts that add capabilities to RISEx. RIP-1 unlocks custom sub-accounts for portfolio margin, liquidation protection, automated strategies and more.

Analogous to what browser extensions enable for browsing. Before extensions, you got what Google shipped. After extensions, anyone could add ad blockers, password managers, dev tools. The browser became a platform. RIP-1 does the same for perps.

## Overview

RIP-1 introduces two core primitives:

**`BaseSubAccount`**: A contract that handles all standard RISEx interactions (order placement, authorization, deposits/withdrawals) with a consistent owner/manager access control model. Builders inherit this contract and override hooks to implement custom logic.

**`SubAccountFactory`**: Deploys sub-accounts via minimal proxies and maintains a registry mapping `user → subAccount[]`. Deployments are deterministic for discoverability.

BaseSubAccount abstracts away RISEx interaction complexity, letting builders focus on their specific logic (lending integration, rebalancing, liquidation routing, scoped access control) without reimplementing core trading infrastructure.

***

## Specification

### BaseSubAccount

The BaseSubAccount contract implements the following core lifecycle:

| Function     | Description                                |
| ------------ | ------------------------------------------ |
| `deposit`    | Transfer collateral into the sub-account   |
| `withdraw`   | Transfer collateral out of the sub-account |
| `placeOrder` | Submit orders to RISEx orderbook           |
| `execute`    | Arbitrary execution for custom logic       |

Access control follows an owner/manager model:

* **Owner**: Full control, can withdraw funds and update managers
* **Manager**: Can execute trading operations but cannot withdraw

Sub-accounts inherit BaseSubAccount and override hooks for custom behavior: health checks before/after operations, custom liquidation conditions, integration with external protocols, and automated rebalancing logic.

### SubAccountFactory

The factory contract:

* Deploys sub-accounts as minimal proxies (EIP-1167)
* Maintains an onchain registry of all sub-accounts per user
* Enables deterministic addressing for discoverability
* Emits events for all deployments for indexing

### Authorization

RIP-1 sub-accounts integrate with RISEx's existing authorization system:

* Session keys for gasless trading via API
* Operator patterns for delegated execution
* `registerSigner` for keeper automation

Sub-accounts are fully compatible with the RISEx API. All existing flows (`placeOrderWithPermit`, etc.) work without modification.

***

## Example Use Cases

| Sub-account Type           | Description                                                                             |
| -------------------------- | --------------------------------------------------------------------------------------- |
| **Portfolio Margin**       | Cross-collateralization with external money markets (Morpho, Spine) via unified account |
| **Liquidation Protection** | Keepers perform automated deleveraging before liquidation                               |
| **Basis Trade Vaults**     | Single-user automated strategies with keeper rebalancing                                |
| **Broker Accounts**        | Scoped access control for managed trading                                               |

***

## Building a Sub-account

1. Inherit `BaseSubAccount`
2. Override hooks for your custom logic
3. Deploy via `SubAccountFactory`
4. Register with RISEx API (if using session keys)

No coordination with core team required. No permission required.

```solidity
contract MySubAccount is BaseSubAccount {
    function _beforeOrder(Order memory order) internal override {
        // Custom pre-trade logic
    }

    function _afterOrder(bytes32 orderId) internal override {
        // Custom post-trade logic
    }
}
```

***

## Resources

* BaseSubAccount Repository: [github.com/risechain/risex-subaccounts](https://github.com/risechain/risex-subaccounts)
* RISEx Authorization Docs: [Authorization](/docs/risex/contracts/contract-interface/authorization)
* RISEx API Reference: [API](/docs/risex/api)
