Skip to content

Manager initialization

Blockchain Manager (BlockchainManager)

The BlockchainManager is an interface (defined in src/api/common.ts) that standardizes interactions with a blockchain. Key responsibilities include:

  • Managing native and custom tokens (getToken, getTokens).
  • Deriving wallet addresses from public keys (getWalletAddress).
  • Validating addresses (isValidAddress).
  • Providing access to the native coin of the blockchain.

Each supported blockchain has a corresponding manager class (e.g., EvmManager, XrplManager, SolanaManager) that implements this interface.

Manager Factory (createManager)

The createManager function (in src/api/factory.ts) is a factory that takes a manifest file and returns the appropriate BlockchainManager instance. It reads the type property from the manifest (e.g., "evm", "xrpl") to determine which manager class to instantiate.

This approach decouples the application from the specific implementation details of each blockchain.

Initialization (initialManager)

The initialManager function in src/api/ApiHandler.ts is the main entry point for accessing a blockchain manager. It acts as a singleton provider for each manager, ensuring that only one instance of each manager is created and reused throughout the application's lifecycle.

It uses a Map to associate a network name (like "Ethereum" or "XRP") with its initialization function (e.g., initialEvmManager, initialXrplManager).

Example: How a Manager is Initialized

typescript
// file: src/api/ApiHandler.ts

// A map to hold the initialization functions for each manager
const managerMap: Record<string, (isForced: boolean) => BlockchainManager | string> = {
    'Ethereum': initialEvmManager,
    'Tron': initialTronManager,
    'BNB Chain': initialBnbManager,
    'Ton': initialTonManager,
    'Bitcoin': initialBitcoinManager,
    'Solana': initialSolanaManager,
    'Arbitrum': initialArbitrumManager,
    'Base': initialBaseManager,
    'Polygon': initialPolygonManager,
    'Avalanche': initialAvalancheManager,
    'Xrp': initialXrplManager,
    'Coti': initialCotiManager,
};

// The main function to get a manager instance
export const initialManager = (
  isForced = false,
  rpcProvider: RpcNamesType | string = store.state.settings.rpcProvider?.name
): BlockchainManager | string => {
  
  // ... (prefix logic)

  const managerFn = managerMap[rpcProvider as string];
  if (typeof managerFn === 'function') {
    return managerFn(isForced);
  } else {
    // Default fallback
    return initialVenomWalletManager(isForced);
  }
}