Swap.mjs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import {ethers} from "ethers";
  2. import fs from 'fs/promises';
  3. import path from 'path';
  4. import { fileURLToPath } from 'url';
  5. // Monad 测试网 RPC URL
  6. const providerUrl = "https://testnet-rpc.monad.xyz";
  7. const provider = new ethers.JsonRpcProvider(providerUrl);
  8. // 合约地址、合约方法哈希值及描述
  9. const contractData = [
  10. {
  11. address: "0x2c9c959516e9aaedb2c748224a41249202ca8be7",
  12. dataHash: "0xd5575982",
  13. description: "Magmastaking"
  14. },
  15. {
  16. address: "0x760AfE86e5de5fa0Ee542fc7B7B713e1c5425701",
  17. dataHash: "0xd5575982",
  18. description: "ApeBond"
  19. },
  20. {
  21. address: "0x760AfE86e5de5fa0Ee542fc7B7B713e1c5425701",
  22. dataHash: "0xd0e30db0",
  23. description: "OctoSwap - WMON"
  24. }
  25. ];
  26. // 读取私钥文件
  27. const __filename = fileURLToPath(import.meta.url);
  28. const __dirname = path.dirname(__filename);
  29. const filePath = path.join(__dirname, './AccountList.txt');
  30. function sleep(min_delay, max_delay) {
  31. // 随机睡眠
  32. const delay = Math.floor(Math.random() * (max_delay * 1000 - min_delay * 1000 + 1)) + min_delay * 1000;
  33. return new Promise((resolve) => setTimeout(resolve, delay));
  34. }
  35. function getRandomAmount() {
  36. // 生成随机交易金额 0.00000000****(最后4位随机数)
  37. const randomTail = Math.floor(1000 + Math.random() * 2000); // 生成1000-9999的随机4位数
  38. return ethers.parseEther(`0.00000000${randomTail}`);
  39. }
  40. function getRandomContractData() {
  41. // 随机获取合约地址、合约方法哈希值及描述
  42. if (contractData.length === 0) {
  43. console.error("The contract data array is empty, the contract cannot be selected, and the program exits.");
  44. process.exit(1);
  45. }
  46. const randomIndex = Math.floor(Math.random() * contractData.length);
  47. return contractData[randomIndex];
  48. }
  49. function checkWalletBalance(balance) {
  50. if (balance <= ethers.parseEther("5")) {
  51. console.error("The wallet balance is invalid.");
  52. process.exit(1);
  53. }
  54. }
  55. async function executeContract(wallet) {
  56. try {
  57. // 随机选择一个合约地址、合约方法哈希值及描述
  58. const {address, dataHash, description} = getRandomContractData();
  59. console.log(`Selected contract: ${description}`);
  60. console.log("Selected contract address:", address);
  61. console.log("Selected contract dataHash:", dataHash);
  62. // 检查原生代币余额
  63. const balance = await provider.getBalance(wallet.address);
  64. const beforeBalance = ethers.formatEther(balance);
  65. console.log("Native balance:", beforeBalance);
  66. // 检查钱包余额,不能小于 0.2
  67. checkWalletBalance(balance);
  68. // 质押金额
  69. const amountToStake = getRandomAmount();
  70. console.log("Amount to contract:", ethers.formatEther(amountToStake));
  71. if (balance < amountToStake) {
  72. throw new Error("Insufficient balance for contract execution.");
  73. }
  74. // 低级调用,发送交易
  75. const tx = await wallet.sendTransaction({
  76. to: address,
  77. value: amountToStake,
  78. data: dataHash,
  79. gasLimit: 100000,
  80. gasPrice: ethers.parseUnits("50", "gwei")
  81. });
  82. console.log("Contract Execution hash:", tx.hash);
  83. // 等待交易确认
  84. const receipt = await tx.wait();
  85. console.log("Contract Execution status:", "ok");
  86. // 检查交易后的余额
  87. const balanceStaking = await provider.getBalance(wallet.address);
  88. const afterBalance = ethers.formatEther(balanceStaking);
  89. console.log("Native balance after transaction:", afterBalance);
  90. console.log("The cost of this transaction:", parseFloat(beforeBalance) - parseFloat(afterBalance));
  91. } catch (error) {
  92. console.error("Error executing contract:", error);
  93. }
  94. }
  95. async function runMultipleStaking(privateKeys) {
  96. const last_times = 1000;
  97. const min_delay = 10;
  98. const max_delay = 20;
  99. for (const privateKey of privateKeys) {
  100. const wallet = new ethers.Wallet(privateKey, provider);
  101. console.log(`Processing wallet with address: ${wallet.address}`);
  102. for (let i = 0; i < last_times; i++) {
  103. if (last_times > 1) {
  104. console.log(`Starting Contract attempt ${i + 1}`);
  105. }
  106. await executeContract(wallet);
  107. console.log(`Completed Contract attempt ${i + 1}`);
  108. if (i < last_times - 1) {
  109. // 最后一次循环不等待
  110. const delay = Math.floor(Math.random() * (max_delay * 1000 - min_delay * 1000 + 1)) + min_delay * 1000;
  111. console.log(`Waiting for ${delay / 1000} seconds...`);
  112. console.log('------------------------------------------------------------')
  113. await sleep(min_delay, max_delay);
  114. }
  115. }
  116. }
  117. }
  118. async function main() {
  119. try {
  120. const data = await fs.readFile(filePath, 'utf8');
  121. const privateKeys = data.split('\n').filter(line => line.trim() !== '');
  122. await runMultipleStaking(privateKeys);
  123. } catch (err) {
  124. console.error('读取文件时发生错误:', err);
  125. process.exit(1);
  126. }
  127. }
  128. main();