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

# Trade

> Same-chain swaps and cross-chain bridges.

The trade methods cover same-chain swaps and cross-chain bridges. Same shape for both — set `toChain` to bridge.

## getTradeQuotes

```ts theme={null}
const quotes = await dzap.getTradeQuotes({
  fromChain: 42161,
  account: '0xUser',
  data: [{
    srcToken: '0xaf88...',
    destToken: '0x82aF...',
    amount: '1000000',
    slippage: 1,
    // toChain: 8453,                  // omit for same-chain
  }],
});
```

Returns ranked routes for each item in `data`.

## buildTradeTxn

Compiles a chosen quote into transaction calldata.

```ts theme={null}
const tx = await dzap.buildTradeTxn({
  fromChain: 42161,
  sender: '0xUser',
  refundee: '0xUser',
  data: [{
    srcToken: '0xaf88...',
    destToken: '0x82aF...',
    amount: '1000000',
    protocol: quotes[0].protocol,    // from getTradeQuotes
    recipient: '0xUser',
    slippage: 1,
  }],
});
// tx => { to, data, value, gasLimit }
```

## trade

Build + sign + send in one call. Use this when you have the user's signer at hand.

```ts theme={null}
const result = await dzap.trade({
  request: {
    fromChain: 42161,
    sender: '0xUser',
    refundee: '0xUser',
    data: [/* same shape as buildTradeTxn */],
  },
  signer: walletClient,                // viem WalletClient or ethers Signer
});

console.log(result.txnHash);
```

> **Approval required.** If `getAllowance()` reports allowance \< amount for the chosen token, call `approve()` first. See [Approvals](/sdk/approvals).

## tradeGasless

Gasless trades let users swap without holding native gas tokens. DZap pays the transaction fee in gas and takes an equivalent fee in the source token.

### How it works

1. User approves tokens (standard approval or Permit2)
2. DZap executes the transaction on behalf of the user, paying gas fees
3. DZap deducts the gas cost from the output, converted to the source token at current rates

For trades with multiple source tokens, DZap checks USD ratios and takes fees proportionally.

### Integration

Gasless trades follow the same pattern as regular trades with these changes:

#### 1. Approval or Permit2

Check and set up approvals using `getAllowance`. See [Approvals](/sdk/approvals) for details.

```ts theme={null}
import { ApprovalModes } from '@dzapio/sdk';

const { status, data } = await dzap.getAllowance({
  chainId: 42161,
  sender: '0xUser',
  tokens: [{ address: '0xaf88...', amount: 1_000_000n }],
  service: 'swap',
  mode: ApprovalModes.Default,
});
```

#### 2. Get quote with gasless flag

```ts theme={null}
const quotes = await dzap.getTradeQuotes({
  gasless: true,
  fromChain: 42161,
  account: '0xUser',
  data: [{
    srcToken: '0xaf88...',
    destToken: '0x82aF...',
    amount: '1000000',
    slippage: 1,
    // toChain: 8453,                  // omit for same-chain
  }],
});
```

#### 3. Build and execute

Use `tradeGasless` to build and execute in one call. If using Permit2 and you have already given token approval to Permit2, set `hasPermit2ApprovalForAllTokens: true`.

```ts theme={null}
const result = await dzap.tradeGasless({
  request: {
    integratorId: 'dzap',
    fromChain: 42161,
    sender: '0x...',
    refundee: '0x...',
    gasless: true,
    hasPermit2ApprovalForAllTokens: true,  // only if Permit2 approval exists
    data: [{
      amount: '1000000',
      srcToken: '0xA0b8...eB48',
      destToken: '0xC02a...6Cc2',
      srcDecimals: 6,
      destDecimals: 18,
      toChain: 42161,
      selectedRoute: 'uniswap',
      recipient: '0x...',
      slippage: 1
    }]
  },
  signer: walletClient,
});
```

If you have pre-built transaction data, pass it directly:

```ts theme={null}
const result = await dzap.tradeGasless({
  request: { /* same as above */ },
  signer: walletClient,
  txnData: preBuiltTxData,  // optional: skips build step if provided
});
```

## getTradeTxnStatus

```ts theme={null}
// Single
const s = await dzap.getTradeTxnStatus({ txHash: '0xabc', chainId: '42161' });

// Batch
const map = await dzap.getTradeTxnStatus({ txIds: '42161-0xabc,8453-0xdef' });
```

For bridges, poll until `s.status === 'completed'`.
