const fullNode = "https://api.shasta.trongrid.io"; const solidityNode = "https://api.shasta.trongrid.io"; const eventServer = "https://api.shasta.trongrid.io"; const tronWeb = new TronWeb(fullNode, solidityNode, eventServer); async function sendTrx() { let privateKey = document.getElementById("privKey").value.trim(); const toAddress = document.getElementById("toAddr").value.trim(); const amount = parseFloat(document.getElementById("amount").value) * 1e6; const outputDiv = document.getElementById("sendOutput"); outputDiv.innerHTML = "⏳ Sending transaction..."; try { // Derive fromAddress from private key let fromAddress; let source = "Tron"; // (WIF → hex if needed) if (/^[5KLc9RQ][1-9A-HJ-NP-Za-km-z]{50,}$/.test(privateKey)) { // Looks like WIF (BTC / FLO style) const decoded = coinjs.wif2privkey(privateKey); if (!decoded || !decoded.privkey) { throw new Error("Invalid WIF private key"); } privateKey = decoded.privkey; // hex format now source = "BTC/FLO"; } else if (!/^[0-9a-fA-F]{64}$/.test(privateKey)) { throw new Error("Private key must be Tron hex or valid WIF"); } // Derive Tron address from private key fromAddress = tronWeb.address.fromPrivateKey(privateKey); // Build transaction const tradeobj = await tronWeb.transactionBuilder.sendTrx( toAddress, amount, fromAddress ); // Sign transaction const signedtxn = await tronWeb.trx.sign(tradeobj, privateKey); // Broadcast transaction const receipt = await tronWeb.trx.sendRawTransaction(signedtxn); console.log(receipt); if (receipt && receipt.result && receipt.result.code === "INSUFFICIENT_BALANCE") { throw new Error("Insufficient balance to send transaction."); } if (receipt && receipt.code && receipt.code === 'CONTRACT_VALIDATE_ERROR') { throw new Error("Insufficient balance to send transaction."); } if (receipt && !receipt.result) { throw new Error("Transaction failed: " + (receipt.result.message || "Unknown error")); } const status = receipt.result ? "✅ Success" : "❌ Failed"; const statusColor = receipt.result ? "green" : "red"; const txid = receipt.txid ? truncate(receipt.txid) : "N/A"; outputDiv.innerHTML = `

Transaction Sent Successfully!

Your TRX transaction has been broadcasted to the network.

Transaction Details

TRON
${ amount / 1e6 } TRX
${txid} ${ receipt.txid ? `` : "" }
`; return receipt; } catch (err) { const outputDiv = document.getElementById("sendOutput"); outputDiv.innerHTML = `

Transaction Failed

${err.message}

`; if (typeof notify === "function") notify(err.message, "error"); throw err; } } function truncate(str, len = 12) { if (!str) return ""; return str.length > len ? str.slice(0, 6) + "..." + str.slice(-6) : str; } function copyToClipboard(text) { navigator.clipboard.writeText(text).then(() => { alert("Copied: " + text); }); }