// SPDX-FileCopyrightText: © 2024 LEDGER SAS // SPDX-License-Identifier: Apache-2.0 import type { FeatureConfig } from './features/types' import { MissingCoinConfig } from './errors' type ConfigStatus = | { type: 'active' features?: FeatureConfig[] } | { type: 'under_maintenance' message?: string } | { type: 'migration' chain: string from: string to: string link: string } | { type: 'feature_unavailable' link: string feature: | 'history' | 'swap' | 'token_history' | 'send_and_receive' | 'send' | 'receive' | 'sending_tokens' | 'receiving_tokens' | 'staking' | 'claiming_staking_rewards' } | { type: 'will_be_deprecated' deprecated_date: string link: string } | { type: 'deprecated' } type Banner = { isDisplay: boolean bannerText: string bannerLink?: string bannerLinkText?: string } export type CurrencyConfig = { status: ConfigStatus customBanner?: Banner checkRegionRestriction?: boolean [key: string]: unknown } export type CoinConfig = (currencyId?: string) => T /** * A logging sink. Coin modules log through this so the concrete logger is injected by the caller via * the {@link Context} (e.g. `@ledgerhq/logs`' `log`) rather than hard-wired in the framework. */ export type Logger = (...args: unknown[]) => void /** * Instance-scoped dependencies threaded to the low layers of a coin module (ADR-019). * * Rather than relying on module-level singletons, everything stateful a coin module needs is * provided explicitly through this context: the coin configuration accessor and a logger. * * The free-form `Record` allows coin-specific specializations to carry extra fields (see ADR-042). * * Note: an HTTP client is intentionally left out of scope for now and will be added later. */ export type Context = { /** Resolves the coin configuration (replaces the former `getCoinConfig`). */ config: (currencyId?: string) => Promise /** The logger to use for all logging in the coin module. */ logger: Logger } & Record function buildCoinConfig() { let coinConfig: CoinConfig | undefined const setCoinConfig = (config: CoinConfig): void => { coinConfig = config } const getCoinConfig = (currencyId?: string): T => { if (!coinConfig) { throw new MissingCoinConfig() } return coinConfig(currencyId) } return { setCoinConfig, getCoinConfig, } } export default buildCoinConfig