# trading

basefun routes trades to one of two places depending on graduation state:

* **Pre-graduation** → `BondingCurveV2.buy()` / `BondingCurveV2.sell()`.
* **Post-graduation** → Uniswap V2 router with appropriate swap variant.

The UI switches automatically. You don't pick.

## Buy flow (pre-graduation)

1. **First time only**: approve USDC to the curve (`USDC.approve(curve, MAX)`).
2. Front-end reads the live AMM state (`getReserveUSDC`, `tokensSoldFromCurve`) and replicates the contract's `_quoteBuy` formula to compute expected tokens out.
3. Apply slippage tolerance (default 1%) → `minTokensOut`.
4. Call `curve.buy(usdcAmount, minTokensOut)`.

Inside the contract:

```
usdc.transferFrom(buyer, curve, usdcAmount)
   → 0.5% to treasury, 0.5% to creatorFeeRecipient  (both inline, same tx)
   → net 99% goes through LT.mint() → keeper EOA → opens/increases Avantis perp
tokensOut from constant-product curve
   → token.transfer(buyer, tokensOut)
```

No second tx. No claim step. Fees are settled the moment the trade lands.

## Sell flow (pre-graduation)

1. **First time only**: approve token to the curve.
2. Front-end computes expected USDC out via the same AMM formula (`_quoteSell`) and applies slippage.
3. Call `curve.sell(tokenAmount, minUSDCOut)`.

Inside:

```
token.transferFrom(seller, curve, tokenAmount)
   → LT.redeem() pulls USDC back from keeper EOA
   → 0.5% to treasury, 0.5% to creator
   → 99% net to seller
```

The keeper needs to have given the LT a `USDC.approve(LT, MAX)`. This is a one-time per-LT setup chore the protocol owners take care of when a new pair/leverage combo first appears.

## Buy/sell flow (post-graduation)

After graduation the curve is permanently retired (`graduated = true`; all writes revert). The UI routes through the Uniswap V2 router:

* **V1 tokens (FactoryV2)** → `swapExactTokensForTokens` (plain).
* **V2 tokens (FactoryV3)** → `swapExactTokensForTokensSupportingFeeOnTransferTokens` (required because of the 1% FOT skim).

You still approve once per token (USDC for buys, token for sells). minOut is computed from `router.getAmountsOut` with your slippage, **after** shaving 1% for FOT on V2 tokens.

## Slippage tolerance

| Default      | 1%                                                                        |
| ------------ | ------------------------------------------------------------------------- |
| Configurable | 0.1% — 50%                                                                |
| Pre-bond     | applied to AMM `tokensOut`                                                |
| Post-bond    | applied to Uni V2 `amountsOut`, after the 1% FOT correction for V2 tokens |

If a swap is reverting with `SlippageExceeded`, bump slippage to 3–5%. Most reverts come from spot price moving between simulation and execution, especially in fast-moving curves close to graduation.

## "Likely to fail" in your wallet

If MetaMask / Rabby shows "this transaction is likely to fail" right after an approve:

* Wait \~5 seconds and retry. Your wallet has its own RPC and may not have seen your approve yet.
* If it persists, your USDC allowance to the curve may be 0 — re-do the approve.
* If still failing on a sell, the LT for that token may not have its USDC allowance set from the keeper. Mention the token in the Telegram and we'll fix it on-chain.

## Costs to budget

| Cost                  | Amount                           | Notes                                                      |
| --------------------- | -------------------------------- | ---------------------------------------------------------- |
| Gas                   | \~$0.05–$0.15 per swap           | Base is cheap; approves cost \~$0.05.                      |
| Swap fee              | 1%                               | Pre-bond all in USDC, post-bond all in token (FOT).        |
| Avantis execution fee | \~$0.0002 ETH per LT mint/redeem | Built into the contract; the keeper pays this.             |
| Avantis trading fee   | Per Avantis schedule             | Embedded in the perp's NAV; not charged separately to you. |


---

# Agent Instructions: 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:

```
GET https://basefun.gitbook.io/basefun-docs/trading.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
