// GetNamespaces already scopes free namespaces to the current access server. // Compare integer lamports, never floating-point SOL prices. import { getIntlLocale } from '../i18n/format.js'; export function registrationNamespaceOptions(namespaces, flowType) { const type = flowType === 'free' ? 2 : flowType === 'paid' ? 1 : 0; if (!type || !Array.isArray(namespaces)) return []; return namespaces.filter(ns => Number(ns?.type) === type && /^[a-z0-9_]+$/.test(ns.name)) .sort((a, b) => { if (type === 1) { const pa = registrationFee(a), pb = registrationFee(b); if (pa === null && pb !== null) return 1; if (pb === null && pa !== null) return -1; if (pa !== null && pb !== null && pa !== pb) return pa < pb ? -1 : 1; } return a.name.localeCompare(b.name, 'en'); }); } export function registrationFee(ns) { if (!ns || Number(ns.currencyCode ?? 1) !== 1) return null; const value = ns.registrationFeeLamports; if (typeof value === 'number' && !Number.isSafeInteger(value)) return null; if (!/^\d+$/.test(String(value ?? ''))) return null; return BigInt(value); } export function chooseRegistrationNamespace(options, preferredName) { return options.find(ns => ns.name === preferredName) || options[0]; } // Exact 9-decimal SOL formatting; never round a nonzero fee to zero. export function formatRegistrationFee(ns, locale = getIntlLocale()) { const fee = registrationFee(ns); if (fee === null) return null; const scale = 1_000_000_000n; const integer = new Intl.NumberFormat(locale).format(fee / scale); const fractional = String(fee % scale).padStart(9, '0').replace(/0+$/, ''); const decimal = new Intl.NumberFormat(locale).formatToParts(1.1).find(p => p.type === 'decimal').value; return `${integer}${fractional ? decimal + fractional : ''} SOL`; }