Superbridge
Start typing to search...

Reference

Type Guards

Type guard helpers for working with discriminated unions

The SDK includes type guard helpers for working with the various discriminated unions returned by the API.

import { guards } from '@superbridge/sdk';

Quote Guards

Each entry in a RouteResponse.results[] has a result field that is either a quote or a quote error. Use guards.quotes to narrow it.

for (const route of routes.results) {
  if (guards.quotes.isQuote(route.result)) {
    // QuoteDto5 - has initiatingTransaction, fees, etc.
  }

  if (guards.quotes.isError(route.result)) {
    // QuoteError - discriminated by `type`
  }
}

Once narrowed to an error, discriminate on type directly — TypeScript will narrow to the specific error DTO.

for (const route of routes.results) {
  if (!guards.quotes.isError(route.result)) continue;

  const error = route.result;

  if (error.type === 'AmountTooLarge') {
    // AmountTooLargeQuoteErrorDto - has maximum
  }

  if (error.type === 'AmountTooSmall') {
    // AmountTooSmallQuoteErrorDto - has minimum
  }

  if (error.type === 'PriceImpactTooHigh') {
    // PriceImpactTooHighQuoteErrorDto - has priceImpactPercent, maximumPriceImpactPercent
  }

  if (error.type === 'GenericError') {
    // GenericQuoteErrorDto - has error
  }

  // 'Paused' | 'Disabled' | 'EscapeHatchNotSupported' | 'ERC777MintToSmartContract'
}

Route Errors

When a request fails (non-2xx response), the SDK throws a SuperbridgeApiError carrying the parsed response body on .data. For /routes, that body is a RouteError — a discriminated union you can narrow by type.

import { SuperbridgeApiError } from '@superbridge/sdk';
import type { RouteError } from '@superbridge/sdk';

try {
  const routes = await sb.getRoutes({ /* ... */ });
} catch (err) {
  if (!(err instanceof SuperbridgeApiError) || err.status !== 400) throw err;

  const error = err.data as RouteError;

  if (error.type === 'UsdAmountTooLarge') {
    // UsdAmountTooLargeRouteErrorDto - has amountUsd, maximumUsd
  }

  if (error.type === 'Paused') {
    // PausedRouteErrorDto
  }

  if (error.type === 'Disabled') {
    // DisabledRouteErrorDto
  }
}

Transaction Guards

Narrow the initiatingTransaction on a quote to a specific chain type.

const { initiatingTransaction } = quote;

if (guards.tx.isEvm(initiatingTransaction)) {
  // InitiatingTransactionEvmDto5 - has to, data, value, chainId
}

if (guards.tx.isEvmGasless(initiatingTransaction)) {
  // InitiatingTransactionEvmGaslessDto5
}

if (guards.tx.isSvm(initiatingTransaction)) {
  // InitiatingTransactionSvmDto5
}

if (guards.tx.isStarknet(initiatingTransaction)) {
  // InitiatingTransactionStarkDto5
}

Step Guards

Narrow steps on a SteppedBridgeDto to their specific types and statuses.

for (const step of bridge.steps) {
  // Narrow by step kind
  if (guards.steps.isTransactionStep(step)) {
    // TransactionStep
  }

  if (guards.steps.isWaitStep(step)) {
    // WaitStep
  }

  if (guards.steps.isInfoStep(step)) {
    // InfoStepDto
  }

  // Narrow transaction steps by status
  if (guards.steps.isTransactionStepReady(step)) {
    // TransactionStepReadyDto - has initiatingTransaction
  }

  if (guards.steps.isTransactionStepDone(step)) {
    // TransactionStepDoneDto - has confirmation
  }

  if (guards.steps.isTransactionStepNotReady(step)) {
    // TransactionStepNotReadyDto - has estimatedGas
  }

  if (guards.steps.isTransactionStepAuto(step)) {
    // TransactionStepAutoDto - executed automatically
  }
}

Wait steps can also be narrowed:

for (const step of bridge.steps) {
  if (!guards.steps.isWaitStep(step)) continue;

  if (guards.steps.isWaitStepNotStarted(step)) {
    // WaitStepNotStartedDto
  }

  if (guards.steps.isWaitStepInProgress(step)) {
    // WaitStepInProgressDto
  }

  if (guards.steps.isWaitStepDone(step)) {
    // WaitStepDoneDto
  }
}

Gas Estimate Guards

Narrow gas estimates to their chain-specific types.

if (guards.gasEstimate.isEvm(gasEstimate)) {
  // EvmGasEstimateDto - has gasLimit
}

if (guards.gasEstimate.isSvm(gasEstimate)) {
  // SvmGasEstimateDto - has computeUnitLimit
}