import type { Asset, Chain } from './types'; import { DEFAULT_CONFIRMATIONS } from './statemachine'; export interface ChainNetworkParams { chain: Chain; asset: Asset; tokenContract: string; tokenDecimals: number; /** JSON-RPC endpoint (Base). */ rpcUrl?: string; /** TronGrid base URL (Tron). */ apiUrl?: string; confirmations: number; explorerTx: (txid: string) => string; explorerAddress: (address: string) => string; } export interface ChainParamsInput { testnet: boolean; env?: Record; } /** * Network parameters for both chains, with TESTNET switching everything to * Base Sepolia / Tron Nile, and every value overridable from .env. */ export function chainNetworkParams({ testnet, env = {} }: ChainParamsInput): Record { // Blank env values (e.g. VAR= in .env / compose defaults) mean "unset". const set = (v: string | undefined) => (v && v.trim() !== '' ? v.trim() : undefined); const base: ChainNetworkParams = { chain: 'base', asset: 'USDC', tokenDecimals: 6, rpcUrl: set(env.BASE_RPC_URL) ?? (testnet ? 'https://sepolia.base.org' : 'https://mainnet.base.org'), tokenContract: set(env.BASE_USDC_CONTRACT) ?? (testnet ? '0x036CbD53842c5426634e7929541eC2318f3dCF7e' // USDC on Base Sepolia : '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913'), // USDC on Base mainnet confirmations: intFromEnv(env.BASE_CONFIRMATIONS) ?? DEFAULT_CONFIRMATIONS.base, explorerTx: (txid) => `https://${testnet ? 'sepolia.' : ''}basescan.org/tx/${txid}`, explorerAddress: (address) => `https://${testnet ? 'sepolia.' : ''}basescan.org/address/${address}`, }; const tron: ChainNetworkParams = { chain: 'tron', asset: 'USDT', tokenDecimals: 6, apiUrl: set(env.TRONGRID_URL) ?? (testnet ? 'https://nile.trongrid.io' : 'https://api.trongrid.io'), tokenContract: set(env.TRON_USDT_CONTRACT) ?? (testnet ? 'TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf' // USDT on Nile testnet — verify/override in .env at deploy time : 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'), // USDT on Tron mainnet confirmations: intFromEnv(env.TRON_CONFIRMATIONS) ?? DEFAULT_CONFIRMATIONS.tron, explorerTx: (txid) => `https://${testnet ? 'nile.' : ''}tronscan.org/#/transaction/${txid}`, explorerAddress: (address) => `https://${testnet ? 'nile.' : ''}tronscan.org/#/address/${address}`, }; return { base, tron }; } function intFromEnv(value: string | undefined): number | undefined { if (!value) return undefined; const n = Number.parseInt(value, 10); return Number.isFinite(n) && n > 0 ? n : undefined; }