Documentation Index
Fetch the complete documentation index at: https://docs.cow.bleu.builders/llms.txt
Use this file to discover all available pages before exploring further.
Repositories
The repositories library provides a flexible data access layer with support for multiple data sources, caching, and fallback strategies. All repositories follow a consistent pattern using TypeScript interfaces and InversifyJS for dependency injection.Architecture
The repository layer follows these key patterns:- Interface-based design: Each repository defines a clear interface contract
- Fallback strategy: Multiple implementations can be chained for resilience
- Caching layer: Transparent caching with configurable TTL
- Dependency injection: Uses InversifyJS with Symbol-based identifiers
Core Repositories
ERC20 Repository
Retrieves ERC20 token information (name, symbol, decimals) from various sources. Parameters:chainId(SupportedChainId, required) - The blockchain network IDtokenAddress(string, required) - The token contract address
address(string) - The token contract addressname(string) - Token name (e.g., “USD Coin”)symbol(string) - Token symbol (e.g., “USDC”)decimals(number) - Token decimals (e.g., 6 for USDC)
Implementations
- Erc20RepositoryViem: Queries blockchain via Viem RPC client
- Erc20RepositoryNative: Returns native token information
- Erc20RepositoryFallback: Chains multiple repositories
- Erc20RepositoryCache: Adds caching layer with 24-hour TTL
USD Repository
Fetches USD price data for tokens across different time strategies. Parameters:chainIdOrSlug(string, required) - Chain ID or slug identifier (e.g., “mainnet”, “gnosis”)tokenAddress(string) - Token contract address (optional for native tokens)priceStrategy(‘5m’ | ‘hourly’ | ‘daily’) - Time interval for historical prices
price(number) - Current USD price of the tokenpricePoints(PricePoint[]) - Historical price data with timestamps
Price Point Structure
Implementations
- UsdRepositoryCoingecko: Fetches prices from CoinGecko API
- UsdRepositoryCow: Uses CoW Protocol API for price data
- UsdRepositoryFallback: Falls back from Coingecko to CoW API
- UsdRepositoryCache: Caches with 2-minute TTL for values, 30-minute for null results
Token Holder Repository
Retrieves top token holders for a given token. Parameters:chainId(SupportedChainId, required) - The blockchain network IDtokenAddress(string, required) - The token contract address
address(string) - Holder wallet addressbalance(string) - Token balance as a string (to handle large numbers)
Implementations
- TokenHolderRepositoryMoralis: Primary source via Moralis API
- TokenHolderRepositoryEthplorer: Fallback via Ethplorer API
- TokenHolderRepositoryFallback: Chains Moralis -> Ethplorer
- TokenHolderRepositoryCache: 2-minute cache for successful results
User Balance Repository
Fetches user token balances on-chain. Parameters:chainId(SupportedChainId, required) - The blockchain network IDuserAddress(string, required) - User wallet addresstokenAddress(string, required) - Token contract address
balance(bigint) - Token balance in base units
Implementations
- UserBalanceRepositoryViem: Queries via Viem RPC client
- UserBalanceRepositoryCache: 1-second cache for balance queries
Simulation Repository
Simulates transaction bundles using Tenderly. Parameters:chainId(SupportedChainId, required) - The blockchain network IDsimulationsInput(SimulationInput[], required) - Array of transactions to simulate
link(string) - Tenderly dashboard link to simulation resultsstatus(boolean) - Whether simulation succeededgasUsed(string) - Total gas consumed by the simulationcumulativeBalancesDiff(Record<string, Record<string, string>>) - Balance changes by address and tokenstateDiff(StateDiff[]) - State changes from the simulation
Implementation
- SimulationRepositoryTenderly: Integrates with Tenderly simulation API
Cache Repository
Core caching abstraction used by other repositories. Parameters:key(string, required) - Cache key identifiervalue(string, required) - Serialized value to cachettl(number, required) - Time-to-live in seconds
Implementations
- CacheRepositoryRedis: Production caching with Redis
- CacheRepositoryMemory: In-memory cache for development
Database Repositories
Indexer State Repository
Manages blockchain indexer state for tracking processed blocks.- IndexerStateRepositoryPostgres: PostgreSQL-backed implementation
- IndexerStateRepositoryOrm: TypeORM-based alternative
OnChain Placed Orders Repository
Stores and retrieves on-chain order data.- OnChainPlacedOrdersRepositoryPostgres: PostgreSQL storage
Expired Orders Repository
Tracks expired orders for cleanup and monitoring.- ExpiredOrdersRepositoryPostgres: PostgreSQL storage
Orders App Data Repository
Stores application-specific order metadata.- OrdersAppDataRepositoryPostgres: PostgreSQL storage
Integration Repositories
Affiliates Repository
Fetches affiliate program configuration.- AffiliatesRepositoryCms: Retrieves from CMS API
Dune Repository
Interacts with Dune Analytics for querying blockchain data. Parameters:queryId(number, required) - Dune query identifierparameters(Record<string, unknown>) - Query parametersperformance(‘medium’ | ‘large’) - Execution tier
execution_id(string) - Unique execution identifierrows(T[]) - Query result rows
Push Notifications Repositories
Manages push notification subscriptions and delivery.- PushSubscriptionsRepositoryCms: Stores subscriptions in CMS
- PushNotificationsRepositoryRabbit: Publishes via RabbitMQ
Token Balances Repository
Bulk token balance fetching for multiple tokens.- TokenBalancesRepositoryAlchemy: Uses Alchemy API
- TokenBalancesRepositoryMoralis: Alternative via Moralis
Factory Functions
Thefactories.ts module provides factory functions for creating pre-configured repository instances:
Fallback Strategy Pattern
Many repositories implement a fallback pattern for resilience:Cache Strategy Pattern
Caching repositories wrap underlying implementations:Dependency Injection
Repositories use InversifyJS symbols for injection:Data Sources
The repositories library integrates with multiple external data sources:- Blockchain RPC: Via Viem clients for on-chain data
- CoinGecko: Cryptocurrency price data
- CoW Protocol API: Order book and price data
- Moralis: Token holder and balance data
- Ethplorer: Token analytics fallback
- Alchemy: Token balances and metadata
- Tenderly: Transaction simulation
- Dune Analytics: Blockchain analytics queries
- PostgreSQL: Persistent order and indexer state
- Redis: High-performance caching
- RabbitMQ: Message queue for notifications
- CMS: Content and configuration management
Utilities
Cache Key Generation
Database Connection
Viem Clients
Best Practices
- Always use factory functions in production for proper configuration
- Leverage caching to reduce external API calls and improve performance
- Implement fallbacks for critical data fetching operations
- Use dependency injection for testability and flexibility
- Cache null values separately to avoid repeated failed lookups
- Set appropriate TTLs based on data volatility (24h for token info, 2min for prices)
- Handle errors gracefully by returning null instead of throwing