fix(android): 添加交易记录保存的错误处理

修复问题:
- saveTransactionRecord() 调用没有错误处理,保存失败会静默
- 如果保存失败,交易已广播但没有本地记录

改进:
- 添加 try-catch 捕获保存异常
- 保存失败时提示用户"交易已广播但保存记录失败"
- 添加成功日志便于调试

影响:
- 确保本地发起的交易 100% 被记录或提示失败原因

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-26 09:19:43 -08:00
parent 656f75a4d1
commit 0b22928d9a
1 changed files with 18 additions and 12 deletions

View File

@ -1471,20 +1471,26 @@ class MainViewModel @Inject constructor(
// 保存交易记录到本地数据库
val state = _transferState.value
android.util.Log.d("MainViewModel", "[BROADCAST] Saving transaction record: shareId=${state.shareId}, tokenType=${state.tokenType}")
repository.saveTransactionRecord(
shareId = state.shareId,
fromAddress = tx.from,
toAddress = tx.to,
amount = state.amount,
tokenType = state.tokenType,
txHash = hash,
gasPrice = tx.gasPrice.toString()
)
try {
repository.saveTransactionRecord(
shareId = state.shareId,
fromAddress = tx.from,
toAddress = tx.to,
amount = state.amount,
tokenType = state.tokenType,
txHash = hash,
gasPrice = tx.gasPrice.toString()
)
android.util.Log.d("MainViewModel", "[BROADCAST] Transaction record saved successfully")
// 启动后台确认交易状态
confirmTransactionInBackground(hash, rpcUrl)
// 启动后台确认交易状态
confirmTransactionInBackground(hash, rpcUrl)
_uiState.update { it.copy(isLoading = false, successMessage = "交易已广播!") }
_uiState.update { it.copy(isLoading = false, successMessage = "交易已广播!") }
} catch (e: Exception) {
android.util.Log.e("MainViewModel", "[BROADCAST] Failed to save transaction record: ${e.message}", e)
_uiState.update { it.copy(isLoading = false, error = "交易已广播但保存记录失败: ${e.message}") }
}
},
onFailure = { e ->
android.util.Log.e("MainViewModel", "[BROADCAST] FAILED: ${e.message}", e)