> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dzap.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Swap

> Same-chain token swaps via DZap Core.

A complete same-chain swap, end to end. Source: USDC on Arbitrum → WETH on Arbitrum.

## SDK

<Steps>
  <Step title="Install + initialize">
    <Snippet file="install-sdk.mdx" />

    ```ts theme={null}
    import { DZapClient, ApprovalModes } from '@dzapio/sdk';
    import { createWalletClient, http } from 'viem';
    import { arbitrum } from 'viem/chains';

    const dzap = DZapClient.getInstance();

    const walletClient = createWalletClient({
      account: '0xYourAddress',
      chain: arbitrum,
      transport: http(),
    });
    ```
  </Step>

  <Step title="Quote">
    ```ts theme={null}
    const quotes = await dzap.getTradeQuotes({
      fromChain: 42161,
      account: '0xYourAddress',
      data: [{
        amount: '1000000',                                          // 1 USDC
        srcToken: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
        destToken: '0x82aF49447D8a07e3bd95BD0d56f35241523fBab1',
        slippage: 1,
      }],
    });
    ```
  </Step>

  <Step title="Approve">
    ```ts theme={null}
    const allowance = await dzap.getAllowance({
      chainId: 42161,
      sender: '0xYourAddress',
      tokens: [{ address: '0xaf88...', amount: 1_000_000n }],
      service: 'swap',
      mode: ApprovalModes.AutoPermit,
    });

    if (allowance.data['0xaf88...'].allowance < 1_000_000n) {
      await dzap.approve({
        chainId: 42161,
        signer: walletClient,
        sender: '0xYourAddress',
        tokens: [{ address: '0xaf88...', amount: 1_000_000n }],
        service: 'swap',
        mode: ApprovalModes.AutoPermit,
      });
    }
    ```
  </Step>

  <Step title="Execute">
    ```ts theme={null}
    const result = await dzap.trade({
      request: {
        fromChain: 42161,
        sender: '0xYourAddress',
        refundee: '0xYourAddress',
        data: [{
          amount: '1000000',
          srcToken: '0xaf88...',
          destToken: '0x82aF...',
          protocol: quotes[0].protocol,
          recipient: '0xYourAddress',
          slippage: 1,
        }],
      },
      signer: walletClient,
    });

    console.log(result.txnHash);
    ```
  </Step>

  <Step title="Track">
    ```ts theme={null}
    const status = await dzap.getTradeTxnStatus({
      txHash: result.txnHash,
      chainId: '42161',
    });
    ```
  </Step>
</Steps>

## API equivalent

```bash theme={null}
curl -X POST https://api.dzap.io/v1/quotes \
  -H "Content-Type: application/json" \
  -d '{
    "fromChain": 42161,
    "account": "0xYourAddress",
    "data": [{
      "amount": "1000000",
      "srcToken": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
      "destToken": "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
      "slippage": 1
    }]
  }'
```

Then `POST /v1/buildTx` and submit the returned `data` to the chain.

## Common gotchas

* **Native token sentinel** — use `0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE` for ETH/native, not the zero address.
* **Decimals matter** — `amount` is wei. USDC is 6 decimals (`1000000` = 1 USDC), WETH is 18.
* **Slippage units** — `1` = 1%, not 100bps. Default is 1.
* **Approvals once** — for `AutoPermit` flows the on-chain approval to Permit2 is one-time.

See [Approvals](/sdk/approvals) for the full approval-mode breakdown.
