Skip to main content

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 ID
  • tokenAddress (string, required) - The token contract address
Response Fields:
  • address (string) - The token contract address
  • name (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
Response Fields:
  • price (number) - Current USD price of the token
  • pricePoints (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 ID
  • tokenAddress (string, required) - The token contract address
Response Fields:
  • address (string) - Holder wallet address
  • balance (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 ID
  • userAddress (string, required) - User wallet address
  • tokenAddress (string, required) - Token contract address
Response Fields:
  • 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 ID
  • simulationsInput (SimulationInput[], required) - Array of transactions to simulate
Response Fields:
  • link (string) - Tenderly dashboard link to simulation results
  • status (boolean) - Whether simulation succeeded
  • gasUsed (string) - Total gas consumed by the simulation
  • cumulativeBalancesDiff (Record<string, Record<string, string>>) - Balance changes by address and token
  • stateDiff (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 identifier
  • value (string, required) - Serialized value to cache
  • ttl (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 identifier
  • parameters (Record<string, unknown>) - Query parameters
  • performance (‘medium’ | ‘large’) - Execution tier
Response Fields:
  • execution_id (string) - Unique execution identifier
  • rows (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

The factories.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

  1. Always use factory functions in production for proper configuration
  2. Leverage caching to reduce external API calls and improve performance
  3. Implement fallbacks for critical data fetching operations
  4. Use dependency injection for testability and flexibility
  5. Cache null values separately to avoid repeated failed lookups
  6. Set appropriate TTLs based on data volatility (24h for token info, 2min for prices)
  7. Handle errors gracefully by returning null instead of throwing
Last modified on March 4, 2026