> For the complete documentation index, see [llms.txt](https://docs.anchored.finance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.anchored.finance/trading-api/reference/api-contract-map.md).

# API ↔ Contract Map

This page maps partner API endpoints to the contract-level effect they prepare, submit, or query. Use it when debugging or explaining why an API call changes `mUSD`, `Stock.stockBalance`, or order state.

## Overview

```mermaid
flowchart TB
    API[Trading API] --> Router[StockRouter]
    Router --> Cashier[Cashier]
    Router --> Stock[Stock]
    Stock --> Cashier
```

## Market and Portfolio

| Endpoint                                | Contract relationship                        | Effect                                                                         |
| --------------------------------------- | -------------------------------------------- | ------------------------------------------------------------------------------ |
| `GET /api/v1/config`                    | Reads on-chain Cashier/Exchange config cache | Returns fee rates, buffer state, and per-token cashier limits; no state change |
| `GET /api/v1/symbols`                   | Reads backend/token registry                 | Lists tradable stock tokens and contract addresses                             |
| `GET /api/v1/prices/{symbol}`           | Off-chain quote plus on-chain token context  | Quote display; no state change                                                 |
| `GET /api/v1/prices/{symbol}/history`   | Market data                                  | Historical display; no state change                                            |
| `GET /api/v1/underlying/{symbol}/price` | Market data                                  | Underlying quote; no state change                                              |
| `GET /api/v1/corporate-action`          | Corporate action data                        | Split/dividend data; no state change                                           |
| `GET /api/v1/users/{userId}/balance`    | Cashier + token balances + allowance         | Portfolio, buy power, deposit readiness                                        |
| `GET /api/v1/users/{userId}/positions`  | Indexed Stock state                          | Stock positions                                                                |

## Orders

| Endpoint                                    | Contract relationship                                                                                                        | Effect                                                                                                                                                      |
| ------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `POST /api/v1/orders/calldata`              | Encodes `StockRouter.placeMarketOrder`/`placeLimitOrder` — or `deferredMarketSell`/`deferredLimitSell` when `deferred: true` | Returns tx data; no state change until submitted. `deferred: true` (sell only) is gated on the deferred-sell whitelist. See [Deferred Sell](#deferred-sell) |
| `POST /api/v1/orders/send`                  | OneClickRouter forwards to StockRouter                                                                                       | Places order (incl. **deferred sell** when `deferred: true`) and auto-records tx                                                                            |
| `POST /api/v1/orders/with-deposit/calldata` | Encodes `StockRouter.depositAndMarketBuy`, `depositAndLimitBuy`, `depositStockAndMarketSell`, or `depositStockAndLimitSell`  | Returns tx data for combined deposit + order                                                                                                                |
| `POST /api/v1/orders/with-deposit/send`     | OneClickRouter forwards combined deposit + order                                                                             | Submits combined flow and auto-records tx                                                                                                                   |
| `POST /api/v1/orders/tx`                    | API/indexer mapping                                                                                                          | Links self-submitted tx to API key                                                                                                                          |
| `GET /api/v1/orders`                        | API order mapping / indexed state                                                                                            | Lists mapped orders                                                                                                                                         |
| `GET /api/v1/orders/tx/{txHash}`            | API tx mapping                                                                                                               | Finds order by tx                                                                                                                                           |
| `GET /api/v1/orders/{orderId}`              | Indexed Stock state                                                                                                          | Shows open or historical order                                                                                                                              |
| `DELETE /api/v1/orders/{orderId}/calldata`  | Encodes `StockRouter.cancelLimitOrder`                                                                                       | Returns cancel tx data                                                                                                                                      |
| `DELETE /api/v1/orders/{orderId}/send`      | OneClickRouter forwards cancel                                                                                               | Cancels via delegated execution                                                                                                                             |

## Deferred Sell

A deferred sell is **placed / listed / detailed / cancelled via the standard** [**Orders**](#orders) **endpoints** with `deferred: true` + `side: Sell` (responses carry `deferred: true`); it is not a separate placement endpoint. Only the following are deferred-specific:

| Endpoint                                    | Contract relationship                        | Effect                                                            |
| ------------------------------------------- | -------------------------------------------- | ----------------------------------------------------------------- |
| `POST /api/v1/deferred-sell/repay/calldata` | Encodes `StockRouter.repayDeferredStockDebt` | Returns repay tx data                                             |
| `POST /api/v1/deferred-sell/repay/send`     | OneClickRouter forwards repay                | Repays debt and auto-records tx (`operationType: deferred_repay`) |
| `GET /api/v1/deferred-sell/debts`           | Indexed deferred-sell debt state             | Lists outstanding debt per stock for the bound user               |

Deferred sell creates a debt equal to the filled quantity (`DeferredSellDebtIncreased`); repayment reduces it (`DeferredSellDebtRepaid`). A partner that is `restricted` is also blocked from stock withdrawals on-chain.

Repay (`repayDeferredStockDebt`) transfers the stock from the partner's **wallet** (ERC-20 `transferFrom`, so the wallet must approve `StockRouter`) and burns it — it does not consume `exchangeBalance`. Stock held in `exchangeBalance` must be withdrawn to the wallet first.

## Cash

| Endpoint                                     | Contract relationship              | Effect                             |
| -------------------------------------------- | ---------------------------------- | ---------------------------------- |
| `POST /api/v1/cash/deposits/calldata`        | Encodes `StockRouter.deposit`      | Returns deposit tx data            |
| `POST /api/v1/cash/deposits/send`            | OneClickRouter forwards deposit    | Deposits from bound user           |
| `POST /api/v1/cash/deposits/tx`              | API/indexer mapping                | Links self-submitted deposit tx    |
| `GET /api/v1/cash/deposits`                  | Indexed Cashier state              | Lists deposits                     |
| `GET /api/v1/cash/deposits/{operationId}`    | Cash operation state               | Deposit detail and status          |
| `POST /api/v1/cash/withdrawals/calldata`     | Encodes `StockRouter.withdraw`     | Returns withdrawal tx data         |
| `POST /api/v1/cash/withdrawals/send`         | OneClickRouter forwards withdrawal | Withdraws from bound user          |
| `POST /api/v1/cash/withdrawals/tx`           | API/indexer mapping                | Links self-submitted withdrawal tx |
| `GET /api/v1/cash/withdrawals`               | Indexed Cashier state              | Lists withdrawals                  |
| `GET /api/v1/cash/withdrawals/{operationId}` | Cash operation state               | Withdrawal detail and status       |

## Stock

| Endpoint                                      | Contract relationship                    | Effect                                   |
| --------------------------------------------- | ---------------------------------------- | ---------------------------------------- |
| `POST /api/v1/stock/deposits/calldata`        | Encodes `StockRouter.depositStock`       | Returns stock deposit tx data            |
| `POST /api/v1/stock/deposits/send`            | OneClickRouter forwards stock deposit    | Deposits stock from bound user           |
| `POST /api/v1/stock/deposits/tx`              | API/indexer mapping                      | Links self-submitted stock deposit tx    |
| `GET /api/v1/stock/deposits`                  | Indexed Stock state                      | Lists stock deposits                     |
| `GET /api/v1/stock/deposits/{operationId}`    | Stock operation state                    | Stock deposit detail                     |
| `POST /api/v1/stock/withdrawals/calldata`     | Encodes `StockRouter.withdrawStock`      | Returns stock withdrawal tx data         |
| `POST /api/v1/stock/withdrawals/send`         | OneClickRouter forwards stock withdrawal | Withdraws stock from bound user          |
| `POST /api/v1/stock/withdrawals/tx`           | API/indexer mapping                      | Links self-submitted stock withdrawal tx |
| `GET /api/v1/stock/withdrawals`               | Indexed Stock state                      | Lists stock withdrawals                  |
| `GET /api/v1/stock/withdrawals/{operationId}` | Stock operation state                    | Stock withdrawal detail                  |

For stock deposit and withdrawal detail endpoints, `operationId` is an API/indexer lookup identifier. It is not emitted as a native field by `Stock.depositStock` or `Stock.withdrawStock` events.

## One Click

| Endpoint                   | Contract relationship                  | Effect                 |
| -------------------------- | -------------------------------------- | ---------------------- |
| `GET /api/v1/1ct/status`   | Reads OneClickRouter delegate state    | Shows delegation state |
| `POST /api/v1/1ct/prepare` | Builds EIP-712 delegate payload        | No state change        |
| `POST /api/v1/1ct/enable`  | Calls `delegateBySig` through operator | Enables delegatee      |
| `POST /api/v1/1ct/disable` | Calls undelegate flow                  | Disables delegation    |

## Invariants for Integrators

* API calldata targets `StockRouter`; do not call Cashier or Stock directly.
* Buy orders lock `mUSD` in Cashier.
* Plain sell orders lock `Stock.stockBalance`, surfaced by the API as `exchangeBalance`.
* Order settlement updates `mUSD` and `exchangeBalance` asynchronously after execution.
* Cash operations can be instant or queued depending on Cashier buffers.
