# Rasa Solver TP/SL Implementation

Rasa’s conditional orders handler lets you set take-profit and stop-losses to an existing position.

Instead of polling and closing positions manually, you send a conditional order to Rasa and it will automatically request the close when your trigger price is hit.

This version of the API:

* Uses the **instant trading login flow** (no more manual JSON signing)
* Requires **MultiAccount delegation** so Rasa can request closes on your behalf
* Supports **multiple conditional orders per quote** (e.g. one TP and one SL)

**Base URL**

```
https://conditional-orders-handler.rasa.capital/api/v4/
```

Before sending TP/SL orders, you must have:

1. **MultiAccount delegation set up**

   In your MultiAccount, you must delegate at least the following functions to the [Rasa PartyB address](https://docs.symm.io/api-endpoints-and-deployments/solver-addresses-and-endpoints):

   * `requestToClosePosition(`**`0x501e891f`**`)`&#x20;
   * `requestToCancelCloseRequest(`**`0xa63b9363`**`)`

This lets Rasa submit/modify close requests on your sub-account when the TP/SL triggers.

1. **Instant trading login (Bearer token)**

   You must log in to the Rasa hedger using the **instant trading** flow (SIWE) and obtain an `access_token`.

   * See: [*Instant Trading / Login with Hedger*](https://docs.symm.io/exchange-builder-documentation/frontend-builder-technical-guidance/instant-trading).
   * That token will be sent as `Authorization: Bearer <token>` when you call the conditional orders API.
2. **An open SYMMIO position (quote)**
   * You need a valid `quote_id` from an existing open position.
   * You must know:
     * `account_address` (PartyA sub-account)
     * `symbol_id`
     * `multi_account_address`
     * Rasa PartyB address for the chain (goes in `hedger_whitelist`).

## POST api/v4/

### Request Payload

Example payload for **one TP and one SL** on the same quote:

```json
{
  "account_address": "0xB6492A3963DDa0910329787478df3Bb02C7b0DA3",
  "quote_id": 179921,
  "conditional_orders": [
    {
      "quantity": "7.2",
      "price": "1.9683",
      "conditional_price": "2.1870",
      "conditional_price_type": "last_close",
      "order_type": 1,
      "position_type": 0,
      "conditional_order_type": "take_profit",
      "leverage": 1
    },
    {
      "quantity": "7.2",
      "price": "1.7808",
      "conditional_price": "1.9787",
      "conditional_price_type": "last_close",
      "order_type": 1,
      "position_type": 0,
      "conditional_order_type": "stop_loss",
      "leverage": 1
    }
  ],
  "symbol_id": 4,
  "multi_account_address": "0x95605c64356572eb5C076Cb9c027c88b527A2059",
  "hedger_whitelist": [
    "0x9206D9d8F7F1B212A4183827D20De32AF3A23c59"
  ]
}
```

***

### Request Fields:

* **`account_address:`** \
  PartyA sub-account address for this position (the MultiAccount child that owns the quote).
* **`quote_id:`** The SYMMIO quote ID you want to attach TP/SL to (must be OPEN).
* **`symbol_id:`** The SYMMIO symbol ID (e.g. XRP = `4` in your example).
* **`multi_account_address:`**  The MultiAccount contract address deployed by a frontend builder.
* **`hedger_whitelist`**\
  Array of PartyB addresses allowed to execute these conditional orders.\
  For Rasa on Base, this is their PartyB address:

  ```json
  "hedger_whitelist": ["0x9206D9d8F7F1B212A4183827D20De32AF3A23c59"]
  ```
* **`conditional_orders`**\
  Array of TP/SL instructions for this quote. You can send one or multiple (e.g. one TP + one SL).

#### `conditional_orders[]` object

Each object describes a single TP or SL:

* **`quantity` (string) —** How much of the position should be closed when this order triggers.
* **`price` (string)** – This is the **minimum price you’re willing to accept when closing**, after slippage.

  * For **LONGS**, a common pattern is:

    ```
    price = conditional_price * (1 - slippage)
    ```

    e.g. for 10% slippage: `price = conditional_price * 0.9`
  * For **SHORTS**, the direction is inverted:

    ```
    price = conditional_price * (1 + slippage)
    ```

    e.g. for 10% slippage: `price = conditional_price * 1.1`

  The solver will only close the trade if it can get at least this price (for longs) or at most this price (for shorts).
* **`conditional_price` (string)** – The price level at which this TP/SL should  attempt to close.
* **`conditional_price_type` (string) —** How the trigger price is interpreted:
  * `"last_close"` – use the last close price feed for that symbol.
  * `"market`" — uses the market price for that symbol
* **`order_type` (number) —** Order type used when closing: `1` for market orders, `2` for limit.
* **`position_type` (number) —** The direction of the position: `0` – Long, `1` – Short.

  This is important because TP and SL logic, as well as slippage direction, depend on whether the position is long or short.
* **`conditional_order_type` (string)**
  * `"take_profit"` – close when price moves **in your favor** to lock in gains.
  * `"stop_loss"` – close when price moves **against you** to cap losses.
* **`leverage` (number)** The leverage used for the position.

### Authentication & Headers

Before calling the conditional-orders API, you must **log in** to Rasa using the [instant-trading login](https://docs.symm.io/exchange-builder-documentation/frontend-builder-technical-guidance/instant-trading/instant-login-eoa) flow and obtain an `access_token`.

When sending the request:

```http
POST https://conditional-orders-handler.rasa.capital/api/v4/
Content-Type: application/json
Accept: application/json
App-Name: Cloverfield            # App Name
Authorization: Bearer <access_token>
```

### Flow Summary

1. **Delegate permissions** via MultiAccount: Allow Rasa’s PartyB address to call `requestToClosePosition` and `requestToCancelCloseRequest` for your sub-account.
2. **Login to Rasa** via the instant-trading flow and get a Bearer token.
3. **Build your conditional orders payload**
4. **POST the payload** with the `Authorization: Bearer <token>` header.
5. When the market reaches your `conditional_price` and execution is possible above/below your `price` threshold.

***

## GET /api/v4/{quote\_id}/

You can query which TP/SL (or other conditional) orders are currently registered for a quote.

**Endpoint**

```http
GET https://conditional-orders-handler.rasa.capital/api/v4/{quote_id}/
Authorization: Bearer <access_token>
Accept: application/json
```

**Path params**

* `quote_id` – the SYMMIO quote ID you want to inspect.

**Example response**

```json
[
  {
    "quote_id": 179967,
    "party_a_address": "0xB6492A3963DDa0910329787478df3Bb02C7b0DA3",
    "symbol_id": 4,
    "conditional_order_type": "take_profit",
    "quantity": 7.2,
    "price": 2.2513,
    "conditional_order_price": 2.0466,
    "order_type": 1,
    "state": "new",
    "action_price_type": "last_close",
    "position_type": 1
  }
]
```

***

## DELETE /api/v4/{quote\_id}/{conditional\_order\_type}

You can remove an existing TP, SL, or other conditional order for a quote.

**Endpoint**

```http
DELETE https://conditional-orders-handler.rasa.capital/api/v4/{quote_id}/{conditional_order_type}
Authorization: Bearer <access_token>
Accept: application/json
```

**Path params**

* `quote_id` – the quote ID of the conditional order you want to cancel.
* `conditional_order_type` – one of:
  * `take_profit`
  * `stop_loss`

This will cancel **that specific type** of conditional order for the quote.\
For example:

* Cancel only the stop loss for quote `179921`:

```http
DELETE https://conditional-orders-handler.rasa.capital/api/v4/179921/stop_loss
Authorization: Bearer <access_token>
```

* Cancel the take profit:

```http
DELETE https://conditional-orders-handler.rasa.capital/api/v4/179921/take_profit
Authorization: Bearer <access_token>
```
