UNPKG

@malda-protocol/protocol-config

Version:

Centralized contract addresses, constants, and token configurations for Malda Protocol

171 lines (170 loc) 6.15 kB
import { PRODUCTION_CHAINS, TESTNET_CHAINS } from './types.js'; /** * Check if a market (token) is supported on a specific chain * @param token The token to check * @param chainId The chain ID to check * @returns boolean indicating if the market is available on the specified chain */ export function isMarketSupportedOnChain(token, chainId) { // If token has no specific chain restrictions, it's available everywhere if (!token.supportedChainIds) return true; return token.supportedChainIds.includes(chainId); } /** * Check if a market (token) is supported on a specific chain (using chain object) * @param token The token to check * @param chain The chain object to check * @returns boolean indicating if the market is available on the specified chain */ export function isMarketSupportedOnChainObject(token, chain) { return isMarketSupportedOnChain(token, chain.id); } /** * Get all markets available on a specific chain * @param tokens Array of tokens to filter * @param chainId The chain ID to filter by * @returns Array of tokens available on the specified chain */ export function getMarketsForChain(tokens, chainId) { return tokens.filter((token) => isMarketSupportedOnChain(token, chainId)); } /** * Get all markets available on a specific chain (using chain object) * @param tokens Array of tokens to filter * @param chain The chain object to filter by * @returns Array of tokens available on the specified chain */ export function getMarketsForChainObject(tokens, chain) { return getMarketsForChain(tokens, chain.id); } /** * Get all supported chains for a specific market/token * @param token The token to get supported chains for * @param allChains Array of all possible chain objects * @param config Protocol configuration to check asset details * @returns Array of chain objects where this market is available */ export function getSupportedChainsForMarket(token, allChains, config) { if (!token) return [...allChains]; // If token has explicit supportedChainIds, use those if (token.supportedChainIds) { return allChains.filter((chain) => token.supportedChainIds?.includes(chain.id)); } // Otherwise, find the asset in the CONFIG to check its supportedChainIds const asset = config.assets.find((a) => a.symbol === token.symbol); if (!asset) return [...allChains]; // Fallback to all chains if asset not found // If asset has explicit supportedChainIds, use those if (asset.supportedChainIds) { return allChains.filter((chain) => asset.supportedChainIds?.includes(chain.id)); } // If neither token nor asset have supportedChainIds, then it's available on all chains return [...allChains]; } /** * Transform protocol assets into simplified token format * @param config Protocol configuration * @returns Array of tokens derived from the config assets */ export function createTokensFromConfig(config) { return config.assets .filter((asset) => { // If supportedChainIds is not specified, asset is available on all chains if (!asset.supportedChainIds) return true; // Otherwise check if the host chain is supported return asset.supportedChainIds.includes(config.hostChainId); }) .map((asset) => ({ name: asset.name, symbol: asset.symbol, decimals: asset.decimals, address: asset.mToken, underlyingAddresses: asset.addresses, ...(asset.supportedChainIds ? { supportedChainIds: asset.supportedChainIds } : {}), })); } /** * Get underlying token address for a specific chain * @param token The token to get the underlying address for * @param chainId The chain ID * @returns The underlying token address or undefined if not available */ export function getUnderlyingAddress(token, chainId) { return token.underlyingAddresses[chainId]; } /** * Get underlying token address for a specific chain (using chain object) * @param token The token to get the underlying address for * @param chain The chain object * @returns The underlying token address or undefined if not available */ export function getUnderlyingAddressForChain(token, chain) { return getUnderlyingAddress(token, chain.id); } /** * Find a chain object by its ID * @param chainId The chain ID to find * @param chains Array of chain objects to search in * @returns The chain object or undefined if not found */ export function findChainById(chainId, chains) { return chains.find((chain) => chain.id === chainId); } /** * Get underlying token address for a specific wagmi chain * @param token The token to get the underlying address for * @param chain Wagmi Chain object * @returns The underlying token address or undefined if not available */ export function getUnderlyingAddressForWagmiChain(token, chain) { return token.underlyingAddresses[chain.id]; } /** * Get the native currency symbol for a wagmi chain * @param chain Wagmi Chain object * @returns The native currency symbol (e.g., 'ETH') */ export function getNativeCurrencySymbol(chain) { return chain.nativeCurrency.symbol; } /** * Get the first RPC URL for a wagmi chain * @param chain Wagmi Chain object * @returns The first RPC URL or undefined if none available */ export function getRpcUrl(chain) { return chain.rpcUrls.default?.http[0]; } /** * Get the block explorer URL for a wagmi chain * @param chain Wagmi Chain object * @returns The block explorer URL or undefined if none available */ export function getBlockExplorerUrl(chain) { return chain.blockExplorers?.default?.url; } /** * Check if a wagmi chain is a testnet * @param chain Wagmi Chain object * @returns boolean indicating if the chain is a testnet */ export function isTestnetChain(chain) { return chain.testnet === true; } /** * Get production (mainnet) chains * @returns Array of production chains only */ export function getProductionChains() { return PRODUCTION_CHAINS; } /** * Get testnet chains * @returns Array of testnet chains only */ export function getTestnetChains() { return TESTNET_CHAINS; }