fix(service-party-app): 修复Kava API健康检查逻辑

问题: Kava API检测失败,原因是使用测试地址查询余额的方式不可靠

解决方案:
1. 添加 healthCheck() 方法到 KavaTxService,查询最新区块
2. 添加 kava:healthCheck IPC 处理器
3. 更新 appStore 使用 healthCheck API 而非 getBalance

修改的文件:
- kava-tx-service.ts: 添加 healthCheck() 方法
- main.ts: 添加 kava:healthCheck IPC 处理器
- preload.ts: 暴露 healthCheck API
- appStore.ts: 使用 healthCheck 检测 Kava API
- electron.d.ts: 添加 KavaHealthCheckResult 类型

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2025-12-28 21:01:27 -08:00
parent a2508ab0fd
commit 81c8db9d50
5 changed files with 67 additions and 7 deletions

View File

@ -536,6 +536,16 @@ function setupIpcHandlers() {
}
});
// Kava 健康检查
ipcMain.handle('kava:healthCheck', async () => {
try {
const result = await kavaTxService?.healthCheck();
return { success: result?.ok ?? false, data: result };
} catch (error) {
return { success: false, error: (error as Error).message };
}
});
// ===========================================================================
// 对话框相关
// ===========================================================================

View File

@ -500,6 +500,43 @@ export class KavaTxService {
disconnect(): void {
// REST API 无需断开连接
}
/**
* - API
*/
async healthCheck(): Promise<{ ok: boolean; latency?: number; error?: string }> {
const startTime = Date.now();
try {
// 查询链的最新区块,这是最轻量级的检查
const response = await this.request<{
block: {
header: {
height: string;
chain_id: string;
};
};
}>('/cosmos/base/tendermint/v1beta1/blocks/latest');
const latency = Date.now() - startTime;
if (response?.block?.header?.height) {
return {
ok: true,
latency,
};
}
return {
ok: false,
error: 'Invalid response format',
};
} catch (error) {
return {
ok: false,
error: error instanceof Error ? error.message : String(error),
};
}
}
}
// 导出默认服务实例

View File

@ -182,6 +182,9 @@ contextBridge.exposeInMainWorld('electronAPI', {
rpcEndpoint?: string;
chainId?: string;
}) => ipcRenderer.invoke('kava:updateConfig', { config }),
// 健康检查
healthCheck: () => ipcRenderer.invoke('kava:healthCheck'),
},
// ===========================================================================

View File

@ -139,21 +139,20 @@ export const useAppStore = create<AppState>((set, get) => ({
setKavaApiStatus({ status: 'checking', message: '正在检测...' });
try {
if (window.electronAPI) {
// 使用一个已知的测试地址查询余额来验证 API
const testAddress = 'kava1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqnxwpql';
const result = await window.electronAPI.kava.getBalance(testAddress);
if (result.success || result.error?.includes('account')) {
// 即使账户不存在API 能响应也说明连接正常
// 使用健康检查 API (查询最新区块)
const result = await window.electronAPI.kava.healthCheck();
if (result.success && result.data?.ok) {
const config = await window.electronAPI.kava.getConfig();
const latencyMsg = result.data.latency ? ` (${result.data.latency}ms)` : '';
setKavaApiStatus({
status: 'connected',
message: config?.lcdEndpoint || 'Kava Mainnet',
message: (config?.lcdEndpoint || 'Kava Mainnet') + latencyMsg,
lastChecked: new Date(),
});
} else {
setKavaApiStatus({
status: 'error',
message: result.error || 'API 响应异常',
message: result.data?.error || result.error || 'API 响应异常',
lastChecked: new Date(),
});
}

View File

@ -331,6 +331,16 @@ interface GetKavaTxStatusResult {
error?: string;
}
interface KavaHealthCheckResult {
success: boolean;
data?: {
ok: boolean;
latency?: number;
error?: string;
};
error?: string;
}
// ===========================================================================
// Electron API 接口
// ===========================================================================
@ -403,6 +413,7 @@ interface ElectronAPI {
getTxStatus: (txHash: string) => Promise<GetKavaTxStatusResult>;
getConfig: () => Promise<KavaConfig>;
updateConfig: (config: Partial<KavaConfig>) => Promise<{ success: boolean; error?: string }>;
healthCheck: () => Promise<KavaHealthCheckResult>;
};
// 对话框相关