WDK logoWDK documentation

0x Swidge API Reference

API reference for @0x/wdk-protocol-swidge-0x 0.1.0.

Community modules are developed and maintained independently by third-party contributors.

Tether and the WDK Team do not endorse or assume responsibility for their code, security, or maintenance. Use your own judgment and proceed at your own risk.

Version 0.1.0 declares ZeroExProtocol as the default and named export of @0x/wdk-protocol-swidge-0x.

import ZeroExProtocol, {
  ZeroExFeeLimitExceededError,
  ZeroExValidationError
} from '@0x/wdk-protocol-swidge-0x'

The import specifier above resolves from @0x/wdk-protocol-swidge-0x@0.1.0.

Class: ZeroExProtocol

ZeroExProtocol extends SwidgeProtocol from @tetherto/wdk-wallet/protocols.

Constructor

new ZeroExProtocol(
  account: IWalletAccount | IWalletAccountReadOnly | undefined,
  config: ZeroExProtocolConfig
)

apiKey and chainId are required. A writable account implementing sendTransaction() is required for swidge().

The generated declaration currently marks config optional even though construction without both required fields throws at runtime. Pass config explicitly.

quoteSwidge(options)

quoteSwidge(options: SwidgeOptions): Promise<SwidgeQuote>

Calls the 0x AllowanceHolder /price endpoint and returns an indicative quote. It accepts exact-input and exact-output requests.

The method can run without an account. When a bound account's address resolves, it is sent as the 0x taker. If no account address is available, options.recipient is used as the taker when provided.

swidge(options, config?)

swidge(
  options: SwidgeOptions,
  config?: SwidgeProtocolConfig
): Promise<SwidgeResult>

Calls the 0x AllowanceHolder /quote endpoint, evaluates configured fee limits, performs an ERC-20 approval when required, and submits the transaction returned by 0x.

config supports per-call maxNetworkFeeBps and maxProtocolFeeBps overrides.

getSwidgeStatus(id, options?)

getSwidgeStatus(
  id: string,
  options?: SwidgeStatusOptions
): Promise<SwidgeStatusResult>

Accepts:

  • The '<chainId>:<transactionHash>' id returned by swidge().
  • A bare transaction hash with options.fromChain.
  • A bare transaction hash without options.fromChain, which falls back to the configured chainId.

The method maps a successful receipt to completed, a reverted receipt to failed, and an unresolved lookup to pending. If getTransactionByHash() exists and returns null, it throws ZeroExUnknownTransactionError.

getSupportedChains()

getSupportedChains(): Promise<SwidgeSupportedChain[]>

Returns a static list defined by the 0.1.0 package source:

ChainChain idNative token
Abstract2741ETH
Arbitrum One42161ETH
Avalanche C-Chain43114AVAX
Base8453ETH
Berachain80094BERA
BNB Smart Chain56BNB
Ethereum1ETH
HyperEVM999HYPE
Ink57073ETH
Linea59144ETH
Mantle5000MNT
Monad143MON
OP Mainnet10ETH
Plasma9745ETH
Polygon137POL
Scroll534352ETH
Sonic146S
Tempo4217ETH
Unichain130ETH
World Chain480ETH

This method does not call 0x or prove that a requested token pair has live liquidity.

getSupportedTokens()

getSupportedTokens(): Promise<never>

Throws NotImplementedError. Pass chain-specific ERC-20 contract addresses or a documented native-token alias to quoteSwidge() and swidge().

SwidgeOptions behavior

FieldTypeVersion 0.1.0 behavior
fromTokenstringRequired. Sell-token contract address, or a native-token alias if selling the chain's native token.
toTokenstringRequired. Buy-token contract address, or a native-token alias if buying the chain's native token.
fromTokenAmountnumber | bigintExact sell amount. Pass either this field or toTokenAmount.
toTokenAmountnumber | bigintExact buy amount. Pass either this field or fromTokenAmount.
toChainstring | numberOptional. When provided, it must numerically equal configured chainId.
recipientstringOptional output recipient for execution. For an account-free indicative quote, it is also used as the taker.
slippagenumberOptional decimal slippage. Overrides defaultSlippage.
refundAddressstringNot consumed in version 0.1.0.
minAmountOutnumber | bigintNot consumed in version 0.1.0.

Use token amounts in the token's smallest unit. Pass positive bigint values when possible. Although the interface also accepts number, values above Number.MAX_SAFE_INTEGER can lose precision before conversion, and the module does not reject zero or negative amounts before calling 0x.

Native-token aliases

Five identifiers resolve to the chain's native token in the v0.1.0 source:

  • native
  • eth
  • '' (an empty string)
  • 0x0000000000000000000000000000000000000000
  • 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE

The module matches these identifiers case-insensitively and rewrites each one to the 0x sentinel address before sending the request. eth and '' identify the native token on every supported chain; for example, eth means POL on Polygon, not ether.

Any other token string is forwarded to 0x unchanged. The module does not trim, validate, or checksum non-native token addresses.

ZeroExProtocolConfig

type ZeroExProtocolConfig = {
  chainId: number | string
  apiKey: string
  baseUrl?: string
  defaultSlippage?: number
  skipApproval?: boolean
  maxNetworkFeeBps?: number | bigint
  maxProtocolFeeBps?: number | bigint
}

See Configuration for validation, approval, and fee-limit caveats.

Quote and result fields

quoteSwidge() returns:

FieldTypeDescription
fromTokenAmountbigintSell amount returned by 0x.
toTokenAmountbigintBuy amount returned by 0x for exact-input, or the requested amount for exact-output.
toTokenAmountMinbigintminBuyAmount returned by 0x, or the module's fallback calculation.
feesSwidgeFee[]Network, 0x protocol, and integrator fees present in the response.
priceImpactnumber | undefined0x percentage value converted to a decimal.
expiryundefinedThe indicative /price response does not expose expiry through this module.

When 0x omits minBuyAmount for an exact-input quote, the fallback converts the buy amount to number before applying slippage. Large base-unit values can therefore lose precision.

swidge() returns:

FieldTypeDescription
idstring'<chainId>:<sourceTransactionHash>'.
hashstringSubmitted swap transaction hash.
transactionsSwidgeTransaction[]Optional approval transaction followed by the source swap transaction.
fromTokenAmountbigintFirm quote sell amount.
toTokenAmountbigintFirm quote buy amount.
toTokenAmountMinbigint | undefinedFirm quote minBuyAmount when present.
feesSwidgeFee[]Fees mapped from the firm quote.

Fee mapping

0x response fieldWDK fee typeDenomination
totalNetworkFeenetworkNative token symbol from the package's chain list
fees.zeroExFeeprotocolfeeToken returned by 0x
fees.integratorFeeaffiliatefeeToken returned by 0x

Runtime exports

The 0.1.0 package entrypoint exports:

  • ZeroExProtocol as the default and a named export.
  • ISwidgeProtocol.
  • ZeroExApiError.
  • ZeroExInsufficientLiquidityError.
  • ZeroExFeeLimitExceededError.
  • ZeroExReadOnlyError.
  • ZeroExValidationError.
  • ZeroExUnsupportedOperationError.
  • ZeroExTransactionRevertedError.
  • ZeroExTimeoutError.

The package also ships TypeScript declarations for ZeroExProtocolConfig and the inherited Swap, Bridge, and Swidge type aliases exposed by its entrypoint.

The package also declares a bare export condition that initializes bare-node-runtime/global before re-exporting the main entrypoint.

Errors

ErrorWhen thrown
ZeroExApiErrorThe 0x API returns a non-success HTTP status. Exposes status and parsed body.
ZeroExInsufficientLiquidityErrorThe 0x response reports liquidityAvailable: false.
ZeroExFeeLimitExceededErrorA present fee exceeds a configured limit, or a present fee cannot be evaluated against that limit.
ZeroExReadOnlyErrorswidge() is called without a writable account.
ZeroExValidationErrorRequired configuration is absent; both or neither amount field is supplied; a status identifier is malformed; or a required approval cannot be performed.
ZeroExUnsupportedOperationErrorA cross-chain toChain is requested.
ZeroExTransactionRevertedErrorThe automatic approval transaction receipt reports failure.
ZeroExTimeoutErrorAutomatic approval receipt polling exceeds three minutes. Exposes the broadcast approval transaction hash as hash.
ZeroExUnknownTransactionErrorgetTransactionByHash() returns null during status lookup. This class is not exported by the 0.1.0 package entrypoint.
NotImplementedErrorgetSupportedTokens() is called. Exported by @tetherto/wdk-wallet, not by this package.

On this page