> For the complete documentation index, see [llms.txt](https://docs.orriss.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.orriss.xyz/bin-pricing/swap-math.md). Agents can also search and read this documentation through its [MCP server](https://docs.orriss.xyz/~gitbook/mcp).

# Swap execution

Calculate exact-input trades across bins, including output limits, fees and atomic-unit rounding.

An Orriss swap specifies an exact input, a minimum output and an expected reference price. The program walks the available bins in order and calculates one fill at a time. Settlement requires the entire input to be spent and the sum of outputs to meet the minimum. If either condition fails, the swap's transfers and reserve changes are rolled back.

## Quote a single fill

Use integer token atoms throughout. Let `g` be the available gross input, `f` the fee in basis points, `B = 10,000` and `L = 1,000,000,000` lamports per SOL. The bin price `p` is in quote atoms per whole SOL. The fee rate is 20 bps for SOL/USDC and 60 bps for other supported quote mints.

First calculate the spendable input after fees.

`n = floor(g × (B − f) / B)`

| Direction                    | Output for net input n | Output unit |
| ---------------------------- | ---------------------- | ----------- |
| Buy SOL with the quote token | `floor(n × L / p)`     | Lamports    |
| Sell SOL for the quote token | `floor(n × p / L)`     | Quote atoms |

If the calculated output is positive and strictly below the available reserve, the fill spends all `g`. The fee is `g − n`. A zero-output calculation skips the bin.

If the calculated output reaches or exceeds the bin's available output `A`, the pool delivers exactly `A`. It computes the smallest input that pays for it. Write the conversion in the table as `floor(n × N / D)`. To buy SOL, use `N = L` and `D = p`. To sell SOL, use `N = p` and `D = L`.

`netRequired = ceil(A × D / N)`

`grossSpent = ceil(netRequired × B / (B − f))`

The program charges the fee on the gross amount actually spent in that bin and carries the remaining input to the next bin. Rounding applies separately to each fill.

## Example · Buy SOL across two bins

Assume a reference of 100 USDC/SOL and an active index of 10. Bin 10 holds 1 SOL, and bin 11 holds 2 SOL. A trader submits **150.000000 USDC**. The following amounts use an illustrative pool state.

| Bin       | Midpoint (USDC/SOL) | Gross input (USDC) | Fee (USDC)   | Output (SOL)    |
| --------- | ------------------- | ------------------ | ------------ | --------------- |
| 10        | 100.50              | 100.701403         | 0.201403     | 1.000000000     |
| 11        | 101.50              | 49.298597          | 0.098598     | 0.484729054     |
| **Total** |                     | **150.000000**     | **0.300001** | **1.484729054** |

1. At 100.50 USDC/SOL, removing exactly 1 SOL requires 100.500000 USDC of net input. Rounding the gross cost up gives 100.701403 USDC.
2. The remaining 49.298597 USDC reaches bin 11. After the fee, 49.199999 USDC buys 0.484729054 SOL at 101.50 USDC/SOL, rounded down to lamports.
3. The resulting average price, including the pool fee, is `150 / 1.484729054 ≈ 101.028534` USDC/SOL. The pool records bin 11 as active.

At an output slippage tolerance of 50 bps, the quoted minimum would be `floor(1,484,729,054 × 9,950 / 10,000)` = **1,477,305,408 lamports**. Execution also checks that the reference price is within 50 bps of the expected reference supplied by the client. These two checks protect different quantities. See [Reference price](https://docs.orriss.xyz/bin-pricing/reference-price.md).

## Example · Sell SOL into a bid

Start from a separate pool state with the same 100 USDC/SOL reference. Bin 10 is active and has zero USDC. Bin 9 holds at least 100 USDC at a midpoint of 99.50 USDC/SOL. A trader sells **1.000000000 SOL**.

The search skips bin 10 and finds quote inventory in bin 9. The pool deducts a 0.002000000 SOL fee, leaving 0.998000000 SOL for conversion. At 99.50 USDC/SOL, the trader receives **99.301000 USDC**. The active index becomes 9. In this direction the fee is collected in SOL. Its distribution follows the same rules as a quote-token fee. See [LP shares and fees](https://docs.orriss.xyz/bin-pricing/lp-accounting.md).


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.orriss.xyz/bin-pricing/swap-math.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
