> For the complete documentation index, see [llms.txt](https://docs.jaawle.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.jaawle.xyz/protocol/worked-examples.md).

# Worked examples

The rules of the [trading engine](/protocol/trading-engine.md), [liquidation](/risk/liquidation-and-insurance.md), [pools](/protocol/pools-and-nav.md) and [dividends](/protocol/dividends.md) applied to five cases, with the local parameters. Amounts are rounded to the cent at each step; prices to the cent; the Regular session unless stated. Every fee and borrow amount is split 10 % to the insurance vault, 10 % to the treasury, 80 % to the pool.

## A. Long AAPL, 5x

| step                           | computation                               | result                                          |
| ------------------------------ | ----------------------------------------- | ----------------------------------------------- |
| post collateral                |                                           | 1 000.00 USDC                                   |
| open $5 000 long at ask 316.40 | tokens = 5 000 / 316.40                   | 15.8028 shares                                  |
| open fee                       | 5 000 x 0.05 %                            | 2.50 (0.25 insurance, 0.25 treasury, 2.00 pool) |
| collateral after fee           | 1 000 − 2.50                              | 997.50                                          |
| leverage check                 | 5 000 <= 997.50 x 10                      | passes (5.01x)                                  |
| pool side                      | `reserveInventory(15.8028)`               | 15.8028 shares reserved from inventory          |
| liquidation mark, Regular      | (5 000 + 25.00 + 2.50 − 997.50) / 15.8028 | 255.02 (−19.4 %)                                |
| liquidation mark, Extended     | (5 000 + 37.50 + 2.50 − 997.50) / 15.8028 | 255.81                                          |

Seven days later the bid is 330.00. Utilisation averaged 20 % and longs were 60 % of the market's open interest (skew 0.2), so the long side paid funding.

| step                          | computation                     | result                                            |
| ----------------------------- | ------------------------------- | ------------------------------------------------- |
| P\&L at the bid               | 15.8028 x 330.00 − 5 000        | +214.92                                           |
| borrow                        | 5 000 x 87.6 % x 20 % x 7 / 365 | 16.80                                             |
| funding                       | 5 000 x 10 % x 0.2 x 7 / 365    | 1.92                                              |
| borrow leg (borrow + funding) | 16.80 + 1.92                    | 18.72 (1.87 insurance, 1.87 treasury, 14.98 pool) |
| close fee                     | 5 000 x 0.05 %                  | 2.50 (0.25 / 0.25 / 2.00)                         |
| payout                        | 997.50 + 214.92 − 18.72 − 2.50  | 1 191.20                                          |

A +4.30 % move in the stock returned +19.1 % on the collateral posted. The pool paid 214.92 of P\&L and kept 18.98 in fees and borrow (2.00 + 2.00 + 14.98); the insurance vault received 2.37 and the treasury 2.37.

## B. Short ETH, 10x on a synthetic pool

| step                              | computation                                | result                                   |
| --------------------------------- | ------------------------------------------ | ---------------------------------------- |
| post collateral                   |                                            | 500.00 USDC                              |
| open $5 000 short at bid 2 480.00 | tokens = 5 000 / 2 480                     | 2.016129 ETH                             |
| open fee, collateral after        | 2.50; 500 − 2.50                           | 497.50                                   |
| leverage check                    | 5 000 <= 497.50 x 20                       | passes (10.05x)                          |
| pool side                         | `increaseShortOi(5 000)`                   | needs room under 80 % of the pool's USDC |
| liquidation mark                  | (497.50 + 5 000 − 25.00 − 2.50) / 2.016129 | 2 713.12 (+9.4 %)                        |

Three days later the ask is 2 400.00. Utilisation averaged 15 % and shorts were the lighter side, so no funding was owed.

| step            | computation                     | result  |
| --------------- | ------------------------------- | ------- |
| P\&L at the ask | 5 000 − 2.016129 x 2 400.00     | +161.29 |
| borrow          | 5 000 x 87.6 % x 15 % x 3 / 365 | 5.40    |
| close fee       | 5 000 x 0.05 %                  | 2.50    |
| payout          | 497.50 + 161.29 − 5.40 − 2.50   | 650.89  |

No dividends and no session rules on a crypto pool; the position could have been opened or closed at any hour.

## C. The long in A is liquidated

The stock falls to a mid of 254.80 the same day (borrow and funding negligible).

| step                               | computation                                    | result                    |
| ---------------------------------- | ---------------------------------------------- | ------------------------- |
| unrealised P\&L at the mid         | 15.8028 x 254.80 − 5 000                       | −973.45                   |
| equity                             | 997.50 − 973.45 − 2.50                         | 21.55                     |
| maintenance                        | 5 000 x 0.50 %                                 | 25.00: liquidatable       |
| who                                | the keeper at once; anyone 30 s after a `flag` |                           |
| chunk                              | 5 000 < 50 000                                 | full liquidation          |
| close at bid 254.70                | 15.8028 x 254.70 − 5 000                       | −975.03                   |
| close fee                          | 5 000 x 0.05 %                                 | 2.50 (0.25 / 0.25 / 2.00) |
| collateral left                    | 997.50 − 975.03 − 2.50                         | 19.97                     |
| penalty                            | min(1.5 % x 5 000 = 75.00, 19.97)              | 19.97                     |
| liquidator / insurance             | 30 % / 70 % of 19.97                           | 5.99 / 13.98              |
| rest of the collateral to the pool | 19.97 − 19.97                                  | 0.00                      |
| pool receives                      | 975.03 loss + 2.00 fee                         | 977.03                    |
| trader receives                    |                                                | 0.00                      |

The insurance vault received 14.23 in total (0.25 from the fee, 13.98 from the penalty) and the treasury 0.25.

**A gap instead.** Suppose the stock closed at 316 on Friday and the position was healthy, then opened Monday with a bid of 240.00. Nothing can execute while the market is Closed; the first live print arrives in the Extended session at 04:00 ET, where the close fee is doubled.

| step                        | computation                                      | result                    |
| --------------------------- | ------------------------------------------------ | ------------------------- |
| P\&L at the bid             | 15.8028 x 240.00 − 5 000                         | −1 207.33                 |
| close fee, Extended         | 5 000 x 0.05 % x 2                               | 5.00 (0.50 / 0.50 / 4.00) |
| collateral after settlement | 997.50 − 1 207.33 − 5.00                         | −214.83: bad debt         |
| insurance cover             | min(214.83, $50 000 per tx, daily room, balance) | 214.83                    |
| pool receives               | 1 207.33 loss + 4.00 fee                         | 1 211.33                  |
| penalty                     | nothing remains                                  | 0.00                      |
| trader loses                | the collateral, nothing more                     | 997.50                    |

Had the vault been unable to pay, the uncovered part would have been booked in `badDebtUsd` as a loss of the pool.

## D. An LP deposit

AAPL pool: NAV $367 746, LP token price 1.2295, stock weight 59.7 % (target 60 %). An LP requests a 10 000 USDC deposit. For a USDC request the pool looks at the cash side, target 40 %:

| step               | computation                                          | result                   |
| ------------------ | ---------------------------------------------------- | ------------------------ |
| cash weight before | 100 % − 59.7 %                                       | 4 030 bps, drift 30 bps  |
| cash weight after  | (4 030 x 367 746 + 10 000 x 10 000) / 377 746        | 4 188 bps, drift 188 bps |
| direction          | 188 > 30                                             | away from target: taxed  |
| tax                | min(50, 50 x (30 + 188) / 2 / 4 000) = 5 450 / 4 000 | 1 bps (integer division) |
| fee                | 30 + 1 bps of 10 000                                 | 31.00                    |
| LP tokens minted   | (10 000 − 31.00) / 1.2295                            | 8 108.17                 |

The tokens are minted when the keeper executes the request (within minutes, or at the first live print if the market was closed; the request lives 3 days), at the NAV of that moment. The 31.00 stays in the pool for the other LPs.

**Staking into a fresh pool.** The same LP holds $10 000 of AAPL dShares and stakes them (`DepositStock`) into a pool that has just been seeded with USDC only, so its stock weight is 0 % against a 60 % target. The request moves the pool toward the target; the rebate is `50 x 6 000 / 6 000 = 50 bps`, which exceeds the 30 bps base, so the fee is 0. The shares are valued at the bid and the LP receives the full $10 000 in LP tokens at NAV. The request executes in the Regular session only.

## E. A dividend

AAPL pays $0.26 per share. The pool holds 694.17 shares and receives 180.48 in cash (694.17 x 0.26). At sweep time longs hold 15.8028 shares of exposure (the position in A) and shorts none.

| step                                      | computation          | result                              |
| ----------------------------------------- | -------------------- | ----------------------------------- |
| cash arrived                              | 694.17 x 0.26        | 180.48                              |
| pass-through owed                         | (15.8028 − 0) x 0.26 | 4.11                                |
| to the treasury                           | (180.48 − 4.11) x 0  | 0.00                                |
| booked into the pool's cash               |                      | 180.48                              |
| liability to the long, via `poolExposure` |                      | 4.11                                |
| net added to NAV by the sweep             | 180.48 − 4.11        | 176.38 (from the unrounded figures) |

The long's position now carries a dividend credit of 4.11, paid into its collateral when it closes; a short of the same size would carry a 4.11 debit. The shares the pool holds are already ex-dividend, so over the whole event the LPs' stock exposure lost 180.48 and their cash gained 180.48 less the 4.11 they pass to the long: they keep the dividend on the 678.37 shares that are not lent to longs. The platform receives nothing.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.jaawle.xyz/protocol/worked-examples.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
