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

# Track Zap Status

> Monitor and track complex zap transaction status using the DZap SDK

> Monitor the progress of complex multi-step zap operations, from simple swaps to cross-chain DeFi strategies.

Zap operations can involve multiple steps across different protocols and chains. The DZap SDK provides detailed status tracking to monitor each step of your zap execution and handle any issues that may arise.

## Basic Zap Status Tracking

Monitor the status of a completed zap operation:

```typescript theme={null}
import { DZapClient } from "@dzapio/sdk";

const dZap = DZapClient.getInstance();

const zapStatus = await dZap.getZapTxnStatus({
  chainId: 8453, // Chain ID where the zap was executed
  txnHash: "0x1234567890abcdef...", // Transaction hash from zap execution
});

console.log("Zap Status:", zapStatus);
```

## Zap Status Request Parameters

The `getZapTxnStatus` function accepts the following parameters:

| Parameter | Type   | Required | Description                                 |
| --------- | ------ | -------- | ------------------------------------------- |
| `chainId` | number | yes      | Chain ID where the transaction was executed |
| `txnHash` | string | yes      | Transaction hash from the zap execution     |

## Zap Status Response Structure

The `getZapTxnStatus` method returns a response that follows this structure:

```typescript theme={null}
type ZapStatus = "PENDING" | "COMPLETED" | "FAILED" | "REFUNDED";

type ZapStatusResponse = {
  status: ZapStatus; // Overall zap status
  account: string; // Wallet that initiated the zap
  recipient: string; // Address receiving the final tokens/position
  input: ZapStatusAsset[]; // Overall input asset(s) for the whole zap
  output: ZapStatusAsset[]; // Overall output asset(s) for the whole zap
  steps: ZapStatusStep[];
  timestamp: number; // When the zap started
  completedAt: number; // When the zap reached a terminal state
};

type ZapStatusAsset = {
  amount: string; // Amount
  amountUSD: string; // USD value
  asset: {
    chainId: number;
    address: string;
    symbol: string;
    decimals: number;
    type: string;
    logo: string;
    name: string;
    price: string;
    provider?: { id: string; name: string; icon: string };
    underlyingTokens?: {
      chainId: number;
      address: string;
      name?: string;
      symbol: string;
      decimals: number;
      logo?: string | null;
    }[];
  };
};

type ZapStatusStep = {
  chainId: number; // Chain where step was executed
  hash?: string; // Transaction hash for this step (may be absent before it's mined)
  status: ZapStatus;
  action: string; // Action type (swap, bridge, deposit, etc.)
  protocol: {
    id: string; // Protocol identifier
    name: string; // Protocol name
    icon: string; // Protocol icon URL
  };
  input: ZapStatusAsset[];
  output: ZapStatusAsset[];
};
```

## Working with Zap Status Results

Process and display zap status information:

```typescript theme={null}
const zapStatus = await dZap.getZapTxnStatus({
  chainId: 8453,
  txnHash: "0x1234567890abcdef...",
});

console.log(`Overall zap status: ${zapStatus.status}`);
console.log(`Number of steps: ${zapStatus.steps.length}`);

// Process each step
zapStatus.steps.forEach((step, index) => {
  console.log(`\nStep ${index + 1}:`);
  console.log(`  Action: ${step.action}`);
  console.log(`  Protocol: ${step.protocol.name}`);
  console.log(`  Status: ${step.status}`);
  console.log(`  Chain: ${step.chainId}`);
  console.log(`  Transaction: ${step.hash}`);

  // Show input assets
  step.input.forEach((input, inputIndex) => {
    console.log(
      `  Input ${inputIndex + 1}: ${input.amount} ${input.asset.symbol} ($${
        input.amountUSD
      })`
    );
  });

  // Show output assets
  step.output.forEach((output, outputIndex) => {
    console.log(
      `  Output ${outputIndex + 1}: ${output.amount} ${output.asset.symbol} ($${
        output.amountUSD
      })`
    );
  });
});
```

## Polling for Zap Completion

For zaps that include cross-chain steps, poll until the operation reaches a terminal state:

```typescript theme={null}
import { DZapClient } from "@dzapio/sdk";

const dZap = DZapClient.getInstance();

async function waitForZapCompletion(
  txnHash: string,
  chainId: number,
  options?: { intervalMs?: number; timeoutMs?: number }
) {
  const interval = options?.intervalMs ?? 10_000; // 10 seconds
  const timeout = options?.timeoutMs ?? 600_000; // 10 minutes
  const start = Date.now();

  while (Date.now() - start < timeout) {
    const status = await dZap.getZapTxnStatus({ txnHash, chainId });

    // Log step progress
    const completed = status.steps.filter((s) => s.status === "COMPLETED").length;
    console.log(
      `Zap ${status.status} - ${completed}/${status.steps.length} steps done`
    );

    switch (status.status) {
      case "COMPLETED":
        return status;
      case "FAILED":
        throw new Error("Zap failed. Check step details for the failing action.");
      case "REFUNDED":
        console.log("Zap was refunded - funds returned to sender.");
        return status;
      case "PENDING":
        break; // Keep polling
    }

    await new Promise((resolve) => setTimeout(resolve, interval));
  }

  throw new Error(`Zap timed out after ${timeout / 1000}s`);
}

// Usage after executing a zap
const zapResult = await dZap.zap({ request, signer });
if (zapResult.status === "success") {
  const finalStatus = await waitForZapCompletion(
    zapResult.txnHash!,
    request.srcChainId
  );
  console.log("Zap completed:", finalStatus);
}
```

## Best Practices

1. **Implement proper polling intervals** - Don't poll too frequently to avoid rate limits (recommended: 10s+)
2. **Handle all status types** - Prepare for successful, failed, pending, and refunded states
3. **Log step progress** - Zaps have multiple steps; showing progress improves user experience
