index.js 1.6 KB

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