<!-- tempo-docs-context -->
> Source: [/docs/protocol/exchange/exchange-balance](https://tempo.xyz/developers/docs/protocol/exchange/exchange-balance.md)
> Section: [Tempo EVM](https://tempo.xyz/developers/docs/development.md). Build on Tempo EVM: wallets and keys, transactions and fees, TIP-20 Tokens, contracts, exchange, network details, nodes, protocol specifications, and changelog.
> [Documentation index](https://tempo.xyz/developers/llms.txt) · [Docs skill](https://tempo.xyz/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.xyz/developers/llms.txt) and Markdown pages directly.

# Understanding DEX balances on Tempo

The Stablecoin DEX records a token balance for each trading account. This DEX balance is separate from the tokens held at the account's address. Order fills credit proceeds to the DEX balance; withdraw those tokens when you need them in the account.

<span id="why-dex-balances" />

Use the DEX balance for repeated trades without transferring tokens into the contract for each operation. See [account balances](https://tempo.xyz/developers/docs/accounts/balances) to read holdings outside the DEX.

## Checking Your Balance

Use the DEX contract to view your balance of any token held on the DEX:

```solidity
function balanceOf(
    address user,
    address token
) external view returns (uint128)
```

**Example:**

```solidity
uint128 balance = exchange.balanceOf(msg.sender, USDG_ADDRESS);
```

## Using Your DEX Balance

Each transaction that you authorize will use your DEX balance before using funds you approve from your wallet. When you execute a swap or place an order, the DEX contract automatically:

1. Checks if you have sufficient balance in the DEX
2. If insufficient, transfers the needed amount from your wallet to your DEX balance
3. Uses your DEX balance for the operation

## Withdrawing from the DEX

Transfer tokens from your DEX balance back to your wallet:

```solidity
function withdraw(
    address token,
    uint128 amount
) external
```

**Parameters:**

* `token` - The token address to withdraw
* `amount` - The amount to withdraw

**Example:**

```solidity
// Withdraw 1000 USDG from exchange to your wallet
exchange.withdraw(USDG_ADDRESS, 1000e6);
```

:::warning
The withdraw function will revert if you attempt to withdraw more than your available balance on the DEX.
:::

## How Balances Work

### When Swapping

* **Before swap**: The DEX checks your balance, transfers from wallet if needed
* **After swap**: Output tokens are transferred directly to your wallet (not kept on the DEX)

### When Placing Orders

* **On placement**: Required tokens are debited from your DEX balance (or transferred from wallet if insufficient)
* **When filled**: Proceeds are credited to your DEX balance
* **On cancellation**: Unfilled portion is refunded to your DEX balance
