| … | … | @@ -1756,11 +1756,28 @@ const routes = { |
| 1756 | 1756 | } |
| 1757 | 1757 | const amountBigInt = amountValidation.amount |
| 1758 | 1758 | |
| 1759 | // Get current address index (not hardcoded 0) |
| 1760 | const currentAddr = await getCurrentAddress(baseChain) |
| 1761 | const accountIndex = currentAddr ? currentAddr.index : 0 |
| 1762 | |
| 1759 | 1763 | const { wdk } = await initializeWDK(walletMnemonic, { testnet: network === 'testnet' }) |
| 1760 | | const account = await wdk.getAccount(baseChain, 0) |
| 1764 | const account = await wdk.getAccount(baseChain, accountIndex) |
| 1765 | |
| 1766 | // Check token balance (with testnet fallback) |
| 1767 | let tokenBalance |
| 1768 | try { |
| 1769 | tokenBalance = await account.getTokenBalance(tokenInfo.contractAddress) |
| 1770 | } catch (balanceErr) { |
| 1771 | if (network === 'testnet') { |
| 1772 | console.warn(`Token balance check failed on testnet (contract may not exist): ${balanceErr.message}`) |
| 1773 | // On testnet, skip balance check if contract doesn't exist |
| 1774 | // User is responsible for ensuring they have tokens |
| 1775 | tokenBalance = amountBigInt + BigInt(1) // Allow the transfer to proceed |
| 1776 | } else { |
| 1777 | return sendError(res, `Cannot check ${tokenInfo.symbol} balance: ${balanceErr.message}`, 400) |
| 1778 | } |
| 1779 | } |
| 1761 | 1780 | |
| 1762 | | // Check token balance |
| 1763 | | const tokenBalance = await account.getTokenBalance(tokenInfo.contractAddress) |
| 1764 | 1781 | if (amountBigInt > tokenBalance) { |
| 1765 | 1782 | return sendError(res, `Insufficient ${tokenInfo.symbol} balance`, 400) |
| 1766 | 1783 | } |
| … | … | @@ -1768,7 +1785,7 @@ const routes = { |
| 1768 | 1785 | // Check native balance for gas (ETH needed for gas fees) |
| 1769 | 1786 | const ethBalance = await account.getBalance() |
| 1770 | 1787 | |
| 1771 | | // Get fee estimate for token transfer |
| 1788 | // Get fee estimate for token transfer (with testnet fallback) |
| 1772 | 1789 | let fee |
| 1773 | 1790 | try { |
| 1774 | 1791 | const quote = await account.quoteTransfer({ |
| … | … | @@ -1777,9 +1794,18 @@ const routes = { |
| 1777 | 1794 | amount: amountBigInt |
| 1778 | 1795 | }) |
| 1779 | 1796 | fee = quote.fee |
| 1780 | | } catch (err) { |
| 1781 | | console.error('Fee estimation failed:', err) |
| 1782 | | return sendError(res, `Cannot estimate fee: ${err.message}`, 400) |
| 1797 | } catch (quoteErr) { |
| 1798 | if (network === 'testnet') { |
| 1799 | // Testnet contracts may not exist - use estimate |
| 1800 | console.warn(`Token fee estimation failed on testnet, using estimate: ${quoteErr.message}`) |
| 1801 | const provider = account.provider || (await wdk.getProvider(baseChain)) |
| 1802 | const feeData = await provider.getFeeData() |
| 1803 | const gasPrice = feeData.gasPrice || BigInt(20000000000) // 20 gwei fallback |
| 1804 | fee = BigInt(65000) * gasPrice // ERC-20 transfer ~65k gas |
| 1805 | } else { |
| 1806 | console.error('Fee estimation failed:', quoteErr) |
| 1807 | return sendError(res, `Cannot estimate fee: ${quoteErr.message}`, 400) |
| 1808 | } |
| 1783 | 1809 | } |
| 1784 | 1810 | |
| 1785 | 1811 | // Check sufficient ETH for gas |
| … | … | @@ -1788,11 +1814,20 @@ const routes = { |
| 1788 | 1814 | } |
| 1789 | 1815 | |
| 1790 | 1816 | // Transfer token |
| 1791 | | const result = await account.transfer({ |
| 1792 | | token: tokenInfo.contractAddress, |
| 1793 | | recipient: to, |
| 1794 | | amount: amountBigInt |
| 1795 | | }) |
| 1817 | let result |
| 1818 | try { |
| 1819 | result = await account.transfer({ |
| 1820 | token: tokenInfo.contractAddress, |
| 1821 | recipient: to, |
| 1822 | amount: amountBigInt |
| 1823 | }) |
| 1824 | } catch (transferErr) { |
| 1825 | console.error('Token transfer failed:', transferErr) |
| 1826 | if (network === 'testnet') { |
| 1827 | return sendError(res, `Testnet ${tokenInfo.symbol} transfer failed - the testnet token contract may not exist. Error: ${transferErr.message}`, 400) |
| 1828 | } |
| 1829 | return sendError(res, `Token transfer failed: ${transferErr.message}`, 500) |
| 1830 | } |
| 1796 | 1831 | |
| 1797 | 1832 | console.log(`✅ Token transfer sent: ${result.hash}`) |
| 1798 | 1833 | console.log(` Token: ${tokenInfo.symbol}`) |