import { ADDITIONAL_MARKET_FEE } from '../../shared/market.ts'; import { validateXmrAddress } from './xmr.ts'; export type MarketPolicy = { marketEnabled?: boolean; tradingEnabled?: boolean; hostFeeAddress?: string }; export function operatorMarketPolicy(env: NodeJS.ProcessEnv = process.env): Required { const flag = (name: string, fallback: boolean) => { const value = env[name] ?? (fallback ? '1' : '0'); if (value !== '0' && value !== '1') throw new Error(`${name} must be 0 or 1`); return value === '1'; }; const hostFeeAddress = env.WIKIDECK_MARKET_HOST_FEE_ADDRESS ?? ADDITIONAL_MARKET_FEE.address; if (!/^[1-9A-HJ-NP-Za-km-z]{95}$/.test(hostFeeAddress)) throw new Error('Invalid WIKIDECK_MARKET_HOST_FEE_ADDRESS'); return { marketEnabled: flag('WIKIDECK_MARKET_ENABLED', true), tradingEnabled: flag('WIKIDECK_MARKET_TRADING_ENABLED', true), hostFeeAddress }; } export function resolvedMarketPolicy(policy: MarketPolicy): Required { return { marketEnabled: policy.marketEnabled ?? true, tradingEnabled: policy.tradingEnabled ?? true, hostFeeAddress: policy.hostFeeAddress ?? ADDITIONAL_MARKET_FEE.address }; } export async function validateMarketPolicy(policy: MarketPolicy): Promise { await validateXmrAddress(resolvedMarketPolicy(policy).hostFeeAddress); } /** Pausing new business never hides liabilities or blocks safe resolution/withdrawal requests. */ export function permitsMarketAction(policy: MarketPolicy, action: string): boolean { return !['list', 'bid', 'buy', 'reserve', 'mock-credit'].includes(action) || (resolvedMarketPolicy(policy).marketEnabled && resolvedMarketPolicy(policy).tradingEnabled); }