| … | … | @@ -1628,21 +1628,44 @@ const routes = { |
| 1628 | 1628 | const { wdk } = await initializeWDK(walletMnemonic, { testnet: network === 'testnet' }) |
| 1629 | 1629 | const account = await wdk.getAccount(baseChain, 0) |
| 1630 | 1630 | |
| 1631 | | // Quote token transfer (fee in ETH, not USDT) |
| 1632 | | const quote = await account.quoteTransfer({ |
| 1633 | | token: tokenInfo.contractAddress, |
| 1634 | | recipient: to, |
| 1635 | | amount: amountBigInt |
| 1636 | | }) |
| 1631 | let fee |
| 1632 | try { |
| 1633 | // Quote token transfer (fee in ETH, not USDT) |
| 1634 | const quote = await account.quoteTransfer({ |
| 1635 | token: tokenInfo.contractAddress, |
| 1636 | recipient: to, |
| 1637 | amount: amountBigInt |
| 1638 | }) |
| 1639 | fee = quote.fee |
| 1640 | } catch (quoteErr) { |
| 1641 | // On testnet, the token contract may not exist or be valid |
| 1642 | // Fall back to estimated gas: ~65000 gas for ERC-20 transfer |
| 1643 | if (network === 'testnet') { |
| 1644 | console.warn(`Token fee estimation failed on testnet, using estimate: ${quoteErr.message}`) |
| 1645 | // Get current gas price and estimate fee (65000 gas * gasPrice) |
| 1646 | try { |
| 1647 | const provider = account.provider || wdk.provider |
| 1648 | const feeData = await provider.getFeeData() |
| 1649 | const gasPrice = feeData.gasPrice || BigInt(20000000000) // 20 gwei fallback |
| 1650 | fee = BigInt(65000) * gasPrice |
| 1651 | } catch (priceErr) { |
| 1652 | // Complete fallback: assume 20 gwei gas price |
| 1653 | fee = BigInt(65000) * BigInt(20000000000) // ~0.0013 ETH |
| 1654 | } |
| 1655 | } else { |
| 1656 | throw quoteErr // Re-throw on mainnet - need accurate fee |
| 1657 | } |
| 1658 | } |
| 1637 | 1659 | |
| 1638 | 1660 | sendJSON(res, { |
| 1639 | | fee: quote.fee.toString(), |
| 1661 | fee: fee.toString(), |
| 1640 | 1662 | total: amountBigInt.toString(), // For tokens, total = amount (fee is separate in ETH) |
| 1641 | 1663 | formatted: { |
| 1642 | | fee: formatAmount(quote.fee, baseChain), // Fee in ETH |
| 1664 | fee: formatAmount(fee, baseChain), // Fee in ETH |
| 1643 | 1665 | amount: `${Number(amountBigInt) / Math.pow(10, tokenInfo.decimals)} ${tokenInfo.symbol}`, |
| 1644 | | total: `${Number(amountBigInt) / Math.pow(10, tokenInfo.decimals)} ${tokenInfo.symbol} + ${formatAmount(quote.fee, baseChain)} gas` |
| 1645 | | } |
| 1666 | total: `${Number(amountBigInt) / Math.pow(10, tokenInfo.decimals)} ${tokenInfo.symbol} + ${formatAmount(fee, baseChain)} gas` |
| 1667 | }, |
| 1668 | estimated: network === 'testnet' // Flag if this is an estimate |
| 1646 | 1669 | }) |
| 1647 | 1670 | } else { |
| 1648 | 1671 | // Native token fee estimation (existing code) |