Skip to main content

Shared

The shared library provides common utilities, type definitions, constants, and helper functions used across the entire CoW Protocol BFF application. This library has no dependencies on other internal libraries, making it the foundation layer.

Type Definitions

Logger

Standardized logging interface using Pino.
The logger is configured and exported as a singleton:

Constants

Chain Identifiers

Supported blockchain networks from CoW Protocol SDK:

Native Currency Address

Standardized address representing native currencies (ETH, xDAI, etc.):

Wrapped Native Token Addresses

Mapping of chain IDs to their wrapped native token addresses (WETH, wxDAI, etc.):

Chain Names

Human-readable chain names:

Explorer Network Names

Network identifiers for CoW Protocol Explorer URLs:
Supported values:
  • mainnet - Ethereum Mainnet
  • gc - Gnosis Chain
  • arb1 - Arbitrum One
  • base - Base
  • pol - Polygon
  • avax - Avalanche
  • bnb - BNB Chain
  • linea - Linea
  • plasma - Plasma
  • ink - Ink
  • sepolia - Sepolia Testnet

CoW API Network Names

Network identifiers for CoW Protocol API endpoints:

Address Utilities

toTokenAddress

Converts any token address to its canonical form, handling native currency conversion:

isSupportedChain

Type guard to check if a chain ID is supported:

toSupportedChainId

Converts a string or number to SupportedChainId, throwing if unsupported:

Formatting Utilities

getExplorerUrl

Generates CoW Explorer URL for an order:

getExplorerBaseUrl

Generates base CoW Explorer URL for a chain:

formatAmount

Formats a token amount from base units to human-readable form:

formatTokenName

Formats a token name, preferring symbol over address:

Transformers

BigInt Serialization

Helpers for JSON serialization of BigInt values:

String to BigInt Conversion

Buffer and Hex String Conversion

Miscellaneous Utilities

sleep

Async delay utility:

ensureEnvs

Validates required environment variables are set:

doForever

Runs an async function repeatedly with error handling:
The function will:
  • Catch and log errors without crashing
  • Automatically restart after errors
  • Include context in error logs

Logger Configuration

The shared library provides a pre-configured Pino logger:

Best Practices

  1. Import from @cowprotocol/shared for all utilities to avoid duplication
  2. Use type guards like isSupportedChain() for safe type narrowing
  3. Always handle BigInt serialization when sending JSON responses
  4. Validate environment variables early using ensureEnvs() at startup
  5. Use structured logging with context objects rather than string concatenation
  6. Convert native addresses using toTokenAddress() for consistency
  7. Format user-facing amounts with formatAmount() for readability
  8. Use constants instead of hardcoding addresses or chain IDs
  9. Prefer type-safe helpers over manual address manipulation
  10. Leverage async utilities like sleep() and doForever() for common patterns

Type Safety

The shared library is fully typed with TypeScript and exports all necessary types:
All constants, utilities, and transformers include full TypeScript definitions for compile-time safety.
Last modified on March 17, 2026