<!-- tempo-docs-context -->
> Source: [/docs/guide/stablecoin-dex](https://tempo.com/developers/docs/guide/stablecoin-dex.md)
> Section: [Tempo EVM](https://tempo.com/developers/docs/development.md). Build on Tempo EVM: wallets and keys, payments, transactions and fees, TIP-20 Tokens, contracts, exchange, network details, nodes, protocol specifications, and changelog.
> [Documentation index](https://tempo.com/developers/llms.txt) · [Docs skill](https://tempo.com/developers/SKILL.md)

> Tempo MCP: Use `search`, `find_pages`, `read_page`, and `code` at `https://mcp.tempo.xyz` for Tempo and related documentation.
> If an indexed page is missing or reflects an older site, read this site’s [documentation index](https://tempo.com/developers/llms.txt) and Markdown pages directly.

<span id="stablecoin-dex" />

<span id="exchange-stablecoins-on-the-tempo-dex" />

<span id="exchange" />

# Stablecoin DEX

Swap stablecoins on Tempo through the onchain orderbook, or provide liquidity by placing orders. Use [Routes](https://tempo.com/developers/docs/routes) when you need to move funds between networks.

:::info[Interactive walkthrough]
Try the [swap walkthrough](https://tempo.com/developers/docs/guide/stablecoin-dex/executing-swaps) with a test wallet, then build the swap form in your application.
:::

<span id="stablecoin-exchange-paths" />

<span id="swap-or-provide-liquidity" />

## Swap stablecoins

Choose the input and output tokens, request a quote, and set the minimum output your application accepts. This example swaps one AlphaUSD for BetaUSD, two test stablecoins on Moderato testnet. The pair needs available liquidity.

The example reuses the clients and values from [Network examples](https://tempo.com/developers/docs/network/examples). Viem imports `client`, `alphaUsd`, and `betaUsd` from `network.config.ts`. The CLI uses the `DEX`, `TOKEN` (AlphaUSD), `BETA_TOKEN`, and `RPC_URL` shell variables. Python imports from `network_config.py`.

::::steps
### Get a quote

Ask the DEX how much BetaUSD one AlphaUSD buys. The amounts are integers in base units, and these stablecoins have six decimals, so `1000000` is one AlphaUSD. `quoteSwapExactAmountIn` takes the input token, output token, and input amount.

:::code-group
```ts [Viem]
const quote = await client.dex.getSellQuote({
  tokenIn: alphaUsd,
  tokenOut: betaUsd,
  amountIn: 1_000_000n,
})
```

```bash [CLI]
QUOTE=$(cast call "$DEX" \
  'quoteSwapExactAmountIn(address,address,uint128)(uint128)' \
  "$TOKEN" "$BETA_TOKEN" 1000000 --rpc-url "$RPC_URL" --json | jq -r '.[0]')
```

```ts [Wagmi]
  const quote = await Actions.dex.getSellQuote(config, {
    tokenIn: alphaUsd,
    tokenOut: betaUsd,
    amountIn: 1_000_000n,
  })
```

```python [Python]
from network_config import ALPHA_USD, BETA_USD, dex

quote = dex.functions.quoteSwapExactAmountIn(
    ALPHA_USD, BETA_USD, 1_000_000
).call()
```
:::

<span id="approve-the-input-token" />

The DEX needs no separate `approve` before the swap. [Implicit approval](https://tempo.com/developers/docs/protocol/tip20/spec#implicit-approvals-for-listed-precompiles) lets listed protocol contracts, including the DEX, pull [TIP-20 tokens](https://tempo.com/developers/docs/protocol/tip20/overview), Tempo's stablecoin standard, from the caller. The pull still checks the balance, the token's [transfer policy](https://tempo.com/developers/docs/protocol/tip403/overview), and any [access key](https://tempo.com/developers/docs/development/account-keychain) spending limit.

### Submit the swap

Set `minAmountOut` to the lowest output you accept. It's `min_amount_out` in Python and the fourth argument in the CLI. This example accepts at most 0.5% less than the quote. A quote doesn't reserve liquidity, so the DEX reverts the swap if it can't meet `minAmountOut`.

The CLI pays the network fee in AlphaUSD with `--tempo.fee-token`, and `--interactive` prompts for the signing key. The Python script continues from the quote step.

:::code-group
```ts [Viem]
const { receipt } = await client.dex.sellSync({
  tokenIn: alphaUsd,
  tokenOut: betaUsd,
  amountIn: 1_000_000n,
  minAmountOut: (quote * 995n) / 1000n,
})
console.log(receipt.transactionHash)
```

```bash [CLI]
MIN_OUT=$((QUOTE * 995 / 1000))
cast send "$DEX" 'swapExactAmountIn(address,address,uint128,uint128)' \
  "$TOKEN" "$BETA_TOKEN" 1000000 "$MIN_OUT" \
  --rpc-url "$RPC_URL" --tempo.fee-token "$TOKEN" --interactive
```

```ts [Wagmi]
  const result = await Actions.dex.sellSync(config, {
    tokenIn: alphaUsd,
    tokenOut: betaUsd,
    amountIn: 1_000_000n,
    minAmountOut: (quote * 995n) / 1000n,
  })
  console.log(result.receipt.transactionHash)
```

```python [Python]
from pytempo.contracts import StablecoinDEX
from network_config import make_transaction, submit

call = StablecoinDEX.swap_exact_amount_in(
    token_in=ALPHA_USD, token_out=BETA_USD,
    amount_in=1_000_000, min_amount_out=quote * 995 // 1000,
)
receipt = submit(make_transaction((call,)))
print(receipt["transactionHash"].hex())
```
:::
::::

For direct HTTP submission, [sign locally and broadcast through JSON-RPC](https://tempo.com/developers/docs/network/transactions#sign-and-submit). Each stablecoin on the DEX pairs against one [quote token](https://tempo.com/developers/docs/protocol/exchange/quote-tokens), another stablecoin it chooses. That gives one path between any two stablecoins.

## Provide liquidity

Place a limit order, a buy or sell order that waits on the orderbook until someone trades at your price. A flip order is a limit order that switches between buying and selling each time it fills, so it keeps providing liquidity. The [liquidity guide](https://tempo.com/developers/docs/guide/stablecoin-dex/providing-liquidity) covers order funding, placement, querying, and cancellation with a working demo.

The [Fee AMM](https://tempo.com/developers/docs/protocol/fees/fee-amm) is a separate protocol pool, not part of the DEX. It converts the stablecoin a user pays network fees in into the token the validator prefers. Use [Provide fee liquidity](https://tempo.com/developers/docs/guide/stablecoin-dex/managing-fee-liquidity) to supply it.

<span id="understand-trading-pairs-and-balances" />

## Account and exchange balances

The DEX records its own balance for each account and token, separate from the tokens at the account's address. Order fills credit proceeds to this DEX balance. Swaps and orders spend it before pulling any shortfall from the account. Read [DEX balances](https://tempo.com/developers/docs/protocol/exchange/exchange-balance) before showing available funds or reconciling orders. The [DEX specification](https://tempo.com/developers/docs/protocol/exchange/spec) defines order, swap, and settlement behavior.
