This commit is contained in:
aaron 2026-01-24 22:31:49 +08:00
parent 2ff09f91b0
commit 5eb78a4d81
2 changed files with 30 additions and 5 deletions

22
app.js
View File

@ -246,7 +246,9 @@ class BatchTransferApp {
async updateGasPrice() { async updateGasPrice() {
try { try {
const gasPrice = await window.BatchTransfer.getGasPrice(); const gasPrice = await window.BatchTransfer.getGasPrice();
console.log('获取到的Gas价格 (Gwei):', gasPrice);
this.gasPrice.value = window.TransferUtils.formatNumber(parseFloat(gasPrice), 2); this.gasPrice.value = window.TransferUtils.formatNumber(parseFloat(gasPrice), 2);
console.log('设置的Gas价格值:', this.gasPrice.value);
this.updateSummary(); this.updateSummary();
} catch (error) { } catch (error) {
console.error('获取Gas价格失败:', error); console.error('获取Gas价格失败:', error);
@ -461,10 +463,15 @@ class BatchTransferApp {
this.totalAmount.textContent = window.TransferUtils.formatNumber(totalAmount, 4); this.totalAmount.textContent = window.TransferUtils.formatNumber(totalAmount, 4);
// 计算Gas费用 // 计算Gas费用
if (window.BatchTransfer.provider && this.gasPrice.value) { if (window.BatchTransfer.provider && this.gasPrice.value && parseFloat(this.gasPrice.value) > 0) {
const gasPrice = parseFloat(this.gasPrice.value); const gasPrice = parseFloat(this.gasPrice.value);
const gasLimit = parseInt(this.gasLimit.value); const gasLimit = parseInt(this.gasLimit.value);
console.log('开始计算Gas费用:');
console.log('- 转账数量:', this.transferData.length);
console.log('- Gas价格 (Gwei):', gasPrice);
console.log('- Gas限制:', gasLimit);
try { try {
const cost = await window.BatchTransfer.calculateTotalCost( const cost = await window.BatchTransfer.calculateTotalCost(
this.transferData, this.transferData,
@ -473,11 +480,20 @@ class BatchTransferApp {
this.isTokenTransfer this.isTokenTransfer
); );
this.estimatedGas.textContent = window.TransferUtils.formatNumber(cost.totalGasCost, 4); console.log('计算结果:');
this.totalCost.textContent = window.TransferUtils.formatNumber(cost.totalCost, 4); console.log('- 总Gas费用 (BNB):', cost.totalGasCost);
console.log('- 总成本 (BNB):', cost.totalCost);
this.estimatedGas.textContent = window.TransferUtils.formatNumber(cost.totalGasCost, 8);
this.totalCost.textContent = window.TransferUtils.formatNumber(cost.totalCost, 8);
} catch (error) { } catch (error) {
console.error('计算Gas费用失败:', error); console.error('计算Gas费用失败:', error);
} }
} else {
console.log('无法计算Gas费用:');
console.log('- Provider存在:', !!window.BatchTransfer.provider);
console.log('- Gas价格值:', this.gasPrice.value);
console.log('- Gas价格>0:', parseFloat(this.gasPrice.value) > 0);
} }
// 启用开始按钮 // 启用开始按钮

View File

@ -35,8 +35,17 @@ class TransferUtils {
} }
static formatNumber(num, decimals = 4) { static formatNumber(num, decimals = 4) {
if (isNaN(num)) return '0'; // 处理各种输入类型
return parseFloat(num.toFixed(decimals)).toString(); if (num === null || num === undefined) return '0';
// 转换为数字
const numValue = typeof num === 'string' ? parseFloat(num) : Number(num);
// 检查是否为有效数字
if (isNaN(numValue)) return '0';
// 格式化并返回
return parseFloat(numValue.toFixed(decimals)).toString();
} }
static generateLogMessage(type, message) { static generateLogMessage(type, message) {