index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const { TronWeb } = require('tronweb');
  2. // 1. 读取命令行参数
  3. const privateKey = process.argv[2];
  4. const toAddress = process.argv[3];
  5. const contractAddress = process.argv[4];
  6. const fullNodeUrl = process.argv[5] || 'https://api.trongrid.io'; // 默认主网
  7. const amount = parseFloat(process.argv[6]); // 默认转1个USDT
  8. const tronProApiKey = process.argv[7] || '';
  9. // 2. 初始化 TronWeb 实例
  10. const tronWeb = new TronWeb({
  11. fullHost: fullNodeUrl,
  12. headers: tronProApiKey ? { 'TRON-PRO-API-KEY': tronProApiKey } : undefined,
  13. });
  14. // 3. 金额(转换为 USDT 单位)
  15. const usdtAmount = amount * 1e6;
  16. async function sendTRC20() {
  17. let attempts = 0;
  18. try {
  19. while (attempts < 2) {
  20. try {
  21. const contract = await tronWeb.contract().at(contractAddress);
  22. const tx = await contract.methods.transfer(toAddress, usdtAmount).send({
  23. feeLimit: 10_000_000,
  24. }, privateKey);
  25. console.log(JSON.stringify({ success: true, txid: tx }));
  26. return;
  27. } catch (err) {
  28. attempts += 1;
  29. if (attempts < 2 && String(err.message || '').includes('429')) {
  30. await new Promise((resolve) => setTimeout(resolve, 5500));
  31. continue;
  32. }
  33. console.error(JSON.stringify({ success: false, error: err.message }));
  34. return;
  35. }
  36. }
  37. } catch (err) {
  38. console.error(JSON.stringify({ success: false, error: err.message }));
  39. }
  40. }
  41. sendTRC20();