index.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const TronWeb = require('tronweb');
  2. async function main() {
  3. try {
  4. const [
  5. ,,
  6. privateKey,
  7. toAddress,
  8. contractAddress,
  9. fullNodeUrl,
  10. amount
  11. ] = process.argv;
  12. if (!privateKey || !toAddress || !contractAddress || !fullNodeUrl || !amount) {
  13. console.log(JSON.stringify({
  14. success: false,
  15. error: '参数不足'
  16. }));
  17. return;
  18. }
  19. const tronWeb = new TronWeb({
  20. fullHost: fullNodeUrl,
  21. privateKey: privateKey
  22. });
  23. const contract = await tronWeb.contract().at(contractAddress);
  24. // USDT 是 6 位精度
  25. const decimals = 6;
  26. const sendAmount = tronWeb.toBigNumber(amount)
  27. .times(tronWeb.toBigNumber(10).pow(decimals))
  28. .toString();
  29. const tx = await contract.transfer(
  30. toAddress,
  31. sendAmount
  32. ).send({
  33. feeLimit: 100_000_000
  34. });
  35. // 成功返回
  36. console.log(JSON.stringify({
  37. success: true,
  38. txid: tx
  39. }));
  40. } catch (e) {
  41. // 失败返回
  42. console.log(JSON.stringify({
  43. success: false,
  44. error: e.message
  45. }));
  46. }
  47. }
  48. main();