# AMM

## 🔁 Auto-Route KKUB ⇄ KUB

> *"I have 5 KKUB in my wallet. Why can't I just buy with it?"* **— You can. You don't even have to think about it.**

### The Problem

On KUB Chain, users end up holding a mix of:

* **KUB** — the native gas token (18 decimals).
* **KKUB** — the ERC-20 wrapped version of KUB (same decimals, 1:1 redeemable).

KKUB shows up from:

* AMM swaps on other venues
* Cross-chain bridges
* Liquidity positions you've exited
* Accidental wraps

Without smart routing, a user with 3 KUB + 5 KKUB sees **"insufficient balance"** when they try to buy 4 KUB worth of a meme token. That's bad UX.

### The Durian Solution

On every KUB-quoted pair — whether bonding curve or post-graduation AMM — the Durian frontend treats **KUB + KKUB as a single spendable balance**.

#### What You See

```
┌───────────────────────────────────────┐
│  Your balance:   8.000 KUB  +KKUB     │
│                  ─────────────        │
│                  native: 3.0 KUB      │
│                  wrapped: 5.0 KKUB    │
└───────────────────────────────────────┘
```

The `+KKUB` tag tells you the combined total **includes auto-unwrappable wrapped KUB**. Click Buy, and the frontend figures out the rest.

#### What Happens Under the Hood

```
┌─ User submits Buy 4 KUB ──────────────────────────────────────────┐
│                                                                   │
│  Step 1 │ Frontend reads balance                                  │
│         │   native KUB    = 3.0                                   │
│         │   KKUB          = 5.0                                   │
│         │   shortfall     = 4.0 − 3.0 = 1.0 KUB worth of KKUB     │
│                                                                   │
│  Step 2 │ Auto-unwrap 1.0 KKUB → 1.0 native KUB                   │
│         │   via CMSWAP KKUB Unwrapper proxy:                      │
│         │   0xff76DD8086428EBC4Ed1b14B0e56E95eDc46a315            │
│         │                                                         │
│         │   (one-time KKUB approval, then unwrap call)            │
│                                                                   │
│  Step 3 │ Execute buy with 4.0 native KUB                         │
│         │   market.buyToken(minOut, { value: parseEther("4") })   │
│                                                                   │
└───────────────────────────────────────────────────────────────────┘
```

All three steps submitted as wallet prompts; none of them custody your funds — **non-custodial end to end**.

#### First-Trade vs Subsequent-Trade Signatures

| Scenario                                            | Prompts                                                       |
| --------------------------------------------------- | ------------------------------------------------------------- |
| **First trade on this pair**, KKUB not yet approved | 2 signatures (approve KKUB + unwrap + buy)                    |
| **Subsequent trades**, approval still valid         | 1 signature (unwrap + buy — or just buy if enough native KUB) |
| **Only native KUB, no KKUB**                        | 1 signature (buy)                                             |

> 🔧 **Gas optimization:** if your native KUB alone covers the trade, the frontend skips the unwrap entirely. You only pay unwrap gas when you actually need KKUB.

### Why Not Just Use KKUB Everywhere?

The bonding curve and sealed AMM are designed around **native KUB** because:

1. **Gas efficiency.** Native-value calls bypass ERC-20 transfer accounting.
2. **Graduation seeding.** The AMM opens with native KUB in reserve, matching the curve's closing price. No conversion step.
3. **Refund logic.** If a buy overshoots graduation, the excess is refunded as native KUB in the same transaction — impossible cleanly with ERC-20.

So KKUB support stays **at the frontend layer**, transparently bridging the ERC-20 world to the native-value contract interface.

### The Unwrapper (Not Our Code)

The CMSWAP KKUB Unwrapper is **a third-party contract**, not a Durianfun deployment:

| Field           | Value                                                |
| --------------- | ---------------------------------------------------- |
| Address         | `0xff76DD8086428EBC4Ed1b14B0e56E95eDc46a315`         |
| Source          | CMSWAP (independently deployed, verified bytecode)   |
| Function        | Burn KKUB from your balance → send native KUB to you |
| Durian exposure | Zero. Durian never holds your KKUB.                  |

The frontend simply routes your wallet through it when needed. If CMSWAP ever went away, the frontend could swap to any other KKUB wrap/unwrap contract without redeploying Durianfun contracts.

### Whitelisted Base Assets

All four native/wrapped base assets are supported by the same auto-routing logic:

| Asset                  | Address                                      | Decimals |
| ---------------------- | -------------------------------------------- | -------- |
| **KUB** (native)       | `0x0000…0000`                                | 18       |
| **KKUB** (Wrapped)     | `0x67eBD850304c70d983B2d1b93ea79c7CD6c3F6b5` | 18       |
| **kUSDC** (Bitkub-peg) | `0x77071ad51ca93fc90e77BCdECE5aa6F1B40fcb21` | 18       |
| **kUSDT** (Bitkub-peg) | `0x7d984C24d2499D840eB3b7016077164e15E5faA6` | 18       |

> ⚠️ **Decimals gotcha:** On KUB mainnet, both stables are **18-decimal** (not 6 like on other chains). Always read `token.decimals()` programmatically.

### TL;DR

You don't think about KKUB. Durian does.


---

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

```
GET https://durianandfun.gitbook.io/durianfun/durian-spot/amm.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.
