-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: 配置驱动的链服务架构 #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
refactor: 配置驱动的链服务架构 #164
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Add AddressBalanceActivity for querying any address balance - Add AddressTransactionsActivity with explorer links - Add menu button (⋮) to WalletCardCarousel for quick access - Add useAddressBalanceQuery hook for balance queries - Add i18n translations for zh-CN and en This helps diagnose customer issues by allowing balance queries for any address without importing the wallet.
BREAKING CHANGE: 链适配器现在只存储 chainId,通过 chainConfigService 获取配置
主要变更:
1. default-chains.json 添加版本号,结构改为 { version, chains }
2. 修正 EVM/Tron 链的 api.url 为公共 RPC 端点
3. 新增 chainConfigService 作为配置查询单一入口
4. 重构所有 chain-adapter 只存 chainId,运行时动态获取配置
5. 添加配置版本比较和强制合并逻辑
6. IAdapterRegistry 接口改为 registerChain(chainId, type)
配置变更:
- ethereum: https://ethereum-rpc.publicnode.com
- binance: https://bsc-rpc.publicnode.com
- tron: https://api.trongrid.io
- 新增 ChainConfigMigrationError 错误类型 - 检测旧版数据(version == null 且 bundled >= 2.0.0)时抛出错误 - store 中添加 migrationRequired 状态 - 新增 MigrationRequiredView 组件显示迁移引导 - WalletTab 在检测到迁移需求时显示引导界面 - 添加 i18n 翻译 (zh-CN/en)
- 确保链配置初始化完成后再渲染子组件 - 初始化过程中显示 loading - 迁移检测在初始化完成后生效,避免白屏
使用 window.location.href 直接导航,避免在 Stackflow context 外部使用 hook
确保 MigrationRequiredView 能使用 useTranslation
- 新增 lib/safe-parse.ts 通用验证工具 - 新增 wallet-storage/schema.ts zod schema 定义 - wallet-storage/service.ts 所有读取方法使用 schema 验证 - 无效数据会被过滤,不会导致白屏
- WALLET_STORAGE_VERSION 升级到 2 - 新增 WalletStorageMigrationError 错误类 - wallet-storage/service.ts 初始化时检测版本不兼容 - wallet store 捕获错误,设置 migrationRequired 状态 - AppInitializer 同时检查链配置和钱包的迁移状态
由于组件在 Stackflow context 外渲染,无法使用路由导航
链配置版本检测:只有当 storedVersion 为 null 且有存储数据时才触发迁移 全新安装(无存储数据)不触发迁移
- 新增 schema.ts 定义 AddressAssetsResponseSchema - asset-service.ts 使用 safeParse 验证 API 响应 - 新增单元测试验证 Schema 正确性 - 无效数据返回空余额而非崩溃
- 构造函数改为接收 chainId: string - 使用 chainConfigService.get() 获取类型安全的 ChainConfig - 添加单元测试验证 symbol 不为 undefined - 遵循 unknown → safe-type 边界验证原则
- chain-config/index.ts 导出 chainConfigService - asset-service.ts 使用 getConfig() 而非 get() - 更新测试 mock
…or/chain-config-service
- 合并 #163 地址查询功能 - 新增 useAddressTransactionsQuery hook - AddressTransactionsPage 使用 adapter.transaction.getTransactionHistory - 保留浏览器链接作为补充查询方式 - 添加交易列表展示组件
- EVM/Tron: false (需要浏览器查询) - Bioforest/Bitcoin/Bip39: true - AddressTransactionsPage 通过 adapter 属性判断,不再硬编码类型
- 新增 API 配置格式: Record<providerType, url | [url, config]> - 添加 chainConfigService 便捷方法: getApi, getApiByType, getApiByPattern, getRpcUrl, getBiowalletApi, getEtherscanApi, getMempoolApi - 更新 default-chains.json 使用新格式 - 各 Adapter 使用 chainConfigService 获取 API 配置 Provider 类型: - biowallet-v1: BioWallet API (带 path 配置) - ethereum-rpc, bsc-rpc, tron-rpc: 各链 RPC - etherscan-v2, bscscan-v2: Etherscan API - mempool-v1: Bitcoin Mempool API
- 新增 ApiProvider 接口,定义可选的能力方法
- 新增 ChainProvider 聚合类,通过 getMethod() 动态代理
- 实现各 Provider: EtherscanProvider, EvmRpcProvider, BiowalletProvider, TronRpcProvider, MempoolProvider
- 移除 ITransactionService.supportsTransactionHistory 硬编码属性
- 更新 useAddressBalanceQuery, useAddressTransactionsQuery 使用 ChainProvider
- AddressTransactionsPage 通过 chainProvider.supportsTransactionHistory 判断
能力发现:通过 chainProvider.supports('method') 动态检查
- 测试 supports() 能力检查 - 测试方法代理委托 - 测试便捷属性 - 测试 getProviderByType()
Phase 1-3: 基础设施 - 扩展 ApiProvider 接口,添加交易和身份能力 - 实现 WrappedTransactionProvider/WrappedIdentityProvider - 更新 createChainProvider 工厂,同时创建 API + wrapped providers Phase 4-6: 业务迁移 - use-send.web3.ts: 使用 getChainProvider() 替代 getAdapterRegistry() - ecosystem/handlers/transaction.ts: 同上 - service-main.ts: 移除 setupAdapters/registerChainConfigs (ChainProvider 使用惰性初始化) - stores/wallet.ts: refreshBalance 使用 ChainProvider Phase 7: 清理 - index.ts: 标记旧 API 为 deprecated,导出新的 ChainProvider API - 移除 setupAdapters/registerChainConfigs 函数 - 更新测试文件 Breaking: getAdapterRegistry/setupAdapters/registerChainConfigs 已废弃
- wrapped-transaction-provider.test.ts (10 tests) - getNativeBalance, getTransactionHistory, getTransaction 代理 - estimateFee, buildTransaction, signTransaction, broadcastTransaction 代理 - getTransactionStatus 状态转换 - wrapped-identity-provider.test.ts (7 tests) - deriveAddress, deriveAddresses 代理 - isValidAddress, normalizeAddress 代理
- AddressTransactionsPage 单元测试 (5 tests) - 渲染测试 - 空状态测试 - 交易显示测试 - 浏览器提示测试 - API 调用参数验证 - AddressTransactionsPage Storybook story - 默认状态 - 搜索 ETH 地址 - 搜索 BFMeta 地址 - EtherscanProvider 单元测试 (8 tests) - 工厂函数测试 - API 请求测试 - 错误处理测试 - 交易状态映射测试
- 验证 Ethereum 链的 provider 创建 - 验证 BFMeta 链的 provider 创建 - 验证空配置处理 - 验证缓存机制
问题:Etherscan v2 API 需要 API Key,导致交易历史查询无数据 解决方案: - Ethereum 链使用 Blockscout API (免费,无需 API Key) - 更新 explorer 链接指向 Blockscout - 更新 createEtherscanProvider 匹配 blockscout-* 类型 Blockscout API 与 Etherscan API 格式兼容,无需修改 Provider 逻辑
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
BREAKING CHANGE
链适配器现在只存储 chainId,通过 chainConfigService 获取配置。
变更概述
1. 配置版本化
default-chains.json添加顶层版本号2.0.0{ version, chains: [...] }2. 修正 EVM/Tron RPC 端点
3. chainConfigService
新增配置查询单一入口:
4. chain-adapter 重构
constructor(chainId: string)5. IAdapterRegistry 接口变更
setChainConfigs(configs)registerChain(chainId, type)测试
迁移说明
此为破坏性变更,所有使用 ChainAdapter 的代码需要更新。