Files
assetx/webapp/lib/contracts/registry.ts
default 2ee4553b71 init: 初始化 AssetX 项目仓库
包含 webapp(Next.js 用户端)、webapp-back(Go 后端)、
antdesign(管理后台)、landingpage(营销落地页)、
数据库 SQL 和配置文件。
2026-03-27 11:26:43 +00:00

31 lines
1.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Dynamic contract address registry.
* Populated from /api/contracts at runtime; falls back to static config.
* Module-level store — safe because addresses are the same across all renders.
*/
const dynamicRegistry = new Map<string, `0x${string}`>()
export function setContractAddressDynamic(name: string, chainId: number, address: string) {
if (address && address.startsWith('0x')) {
dynamicRegistry.set(`${name}:${chainId}`, address as `0x${string}`)
}
}
export function getDynamicOverride(name: string, chainId: number): `0x${string}` | undefined {
return dynamicRegistry.get(`${name}:${chainId}`)
}
/**
* 按名称搜索,忽略 chainId当 token 的 chainId=0 即 DB 未配置时使用)
* 返回第一个匹配的 { address, chainId }
*/
export function getDynamicOverrideByName(name: string): { address: `0x${string}`; chainId: number } | undefined {
for (const [key, value] of dynamicRegistry) {
if (key.startsWith(`${name}:`)) {
const chainId = parseInt(key.split(':')[1], 10)
return { address: value, chainId }
}
}
return undefined
}