<!-- tempo-docs-context -->
> Source: [/docs/guide/private-zones/swap-across-zones](https://tempo.com/developers/docs/guide/private-zones/swap-across-zones.md)
> Section: [Zones](https://tempo.com/developers/docs/zones.md). Connect to a Zone and work with private balances, deposits, and withdrawals. Zones is live on Mainnet; see the introduction to set up a Zone. Earlier testnet sandbox guides describe a separate integration.
> Earlier testnet sandbox: use [Zones](https://tempo.com/developers/docs/zones.md) for current Mainnet deployments.
> [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.

# Swap Across Zones

Swap `OUSD` in Zone A for `betaUSD` in Zone B through the Stablecoin DEX.

## Overview

A cross-Zone swap trades `OUSD` test tokens in **Zone A** for `betaUSD` in **Zone B**. It uses the [testnet sandbox](https://tempo.com/developers/docs/guide/private-zones#testnet-compatibility), which differs from current deployments. The swap settles on Tempo before Zone B credits the balance.

The route uses the sandbox's [SwapAndDepositRouter deployment](https://tempo.com/developers/docs/guide/private-zones#sandbox-router-deployment) on Tempo. It withdraws from `Zone A` and swaps on the Stablecoin DEX. Then it deposits the output token into `Zone B`.

Zone A withdraws OUSD. The router swaps it for betaUSD on the Stablecoin DEX and deposits into Zone B in one public transaction. Zone B imports the deposit and credits betaUSD later.

<a id="swapping-pathusd-from-zone-a-into-betausd-on-zone-b" />

<span id="swapping-ousd-from-zone-a-into-betausd-on-zone-b" />

## Swap OUSD for betaUSD

The demo runs the full route:

1. Withdraws `OUSD` from `Zone A`.
2. Routes it through Tempo and swaps it on the Stablecoin DEX.
3. Deposits the output token into `Zone B` through its portal (`ZonePortal`).
4. Confirms the credited `betaUSD` balance in `Zone B`.

**Interactive demo: Swap across zones**

1. Swap across zones

## Code Example

This snippet assumes a signed-in `rootClient` on Tempo and a `zoneAClient`. It also assumes the shared token and portal constants used throughout the Zone guides. The router address and callback encoding are specific to Moderato's Zone A and Zone B sandbox.

This code covers the routed swap submission. Use the demo to watch the output deposit settle into Zone B.

```ts
import { encodeAbiParameters, parseUnits } from 'viem'

const swapAndDepositRouter = '0xf9b794e0dca9bc12ac90067df792d7aad33436e4' as const
const swapAmount = parseUnits('25', 6)

await zoneAClient.zone.signAuthorizationToken({ zoneId: 6 })

const quotedBetaOut = await rootClient.dex.getSellQuote({
  amountIn: swapAmount,
  tokenIn: ousd,
  tokenOut: betaUsd,
})

const minimumBetaOut = quotedBetaOut - quotedBetaOut / 100n

const callbackData = encodeAbiParameters(
  [
    { type: 'bool' },
    { type: 'address' },
    { type: 'address' },
    { type: 'address' },
    { type: 'bytes32' },
    { type: 'uint128' },
  ],
  [false, betaUsd, ZONE_B.portalAddress, rootClient.account.address, zeroBytes32, minimumBetaOut],
)

const { receipt } = await zoneAClient.zone.requestWithdrawalSync({
  account: rootClient.account,
  amount: swapAmount,
  data: callbackData,
  feeToken: ousd,
  fallbackRecipient: rootClient.account.address,
  gas: routerCallbackGasLimit,
  to: swapAndDepositRouter,
  token: ousd,
})

// This receipt confirms source submission. Verify L1 processing and destination credit next.
console.log(receipt.blockNumber)
```

<span id="how-routed-zone-swaps-settle" />

<span id="how-the-swap-settles" />

## Swap Settlement

The swap is asynchronous because the trade temporarily leaves the Zone.

The source token exits through the outbox (`ZoneOutbox`) and transfers to `SwapAndDepositRouter` on Tempo. The router optionally swaps it on the Stablecoin DEX. It then deposits the output token through the destination portal. That routed deposit pays the normal [deposit fee](https://tempo.com/developers/docs/protocol/zones/execution#deposit-fees), so the destination Zone receives the post-fee output.

The swap completes when the destination Zone credits the recipient. Follow the [routed-result checks](https://tempo.com/developers/docs/guide/private-zones#check-the-full-routed-result) to tell destination credit apart from a callback failure and bounce-back.

:::warning
If the routed withdrawal fails on Tempo, the amount [bounces back](https://tempo.com/developers/docs/protocol/zones/bridging#withdrawal-failure-and-bounce-back) to the withdrawal's `fallbackRecipient` inside the source Zone. Failures include a failed swap or transfer, a reverting router callback, or a target deposit that can't complete. The sequencer still collects the fee, so a failed routed swap still costs the sender fees.
:::

## Related

[Send Across Zones](https://tempo.com/developers/docs/guide/private-zones/send-tokens-across-zones): Use for moving OUSD from Zone A into Zone B without a swap.

[Withdraw OUSD](https://tempo.com/developers/docs/guide/private-zones/withdraw-from-a-zone): Use for moving OUSD from Zone A back to your public balance.

[Withdraw From a Zone](https://tempo.com/developers/docs/zones/withdraw): Use for withdrawals and contract callbacks on a current Zone deployment.

[Testnet Sandbox](https://tempo.com/developers/docs/guide/private-zones): Covers the sandbox router deployment and the routed-result checks.
