transaction.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # -*- coding: utf-8 -*-
  2. from web3 import Web3
  3. import random
  4. import time
  5. import os
  6. # Monad 测试网 RPC URL
  7. provider_url = "https://testnet-rpc.monad.xyz"
  8. w3 = Web3(Web3.HTTPProvider(provider_url))
  9. # 合约地址、合约方法哈希值及描述
  10. contract_data = [
  11. {
  12. "address": "0x2c9c959516e9aaedb2c748224a41249202ca8be7",
  13. "data_hash": "0xd5575982",
  14. "description": "Magmastaking"
  15. },
  16. {
  17. "address": "0x760AfE86e5de5fa0Ee542fc7B7B713e1c5425701",
  18. "data_hash": "0xd5575982",
  19. "description": "ApeBond"
  20. },
  21. {
  22. "address": "0x760AfE86e5de5fa0Ee542fc7B7B713e1c5425701",
  23. "data_hash": "0xd0e30db0",
  24. "description": "OctoSwap - WMON"
  25. }
  26. ]
  27. # 读取私钥文件
  28. file_path = os.path.join(os.path.dirname(__file__), 'AccountList.txt')
  29. def get_random_amount():
  30. # 生成随机交易金额 0.00000000****(最后4位随机数)
  31. random_tail = random.randint(1000, 9999)
  32. return Web3.to_wei(f"0.00000000{random_tail}", "ether")
  33. def get_random_contract_data():
  34. # 随机获取合约地址、合约方法哈希值及描述
  35. if not contract_data:
  36. print("The contract data array is empty, the contract cannot be selected, and the program exits.")
  37. exit(1)
  38. return random.choice(contract_data)
  39. def check_wallet_balance(balance, minimum_balance):
  40. # 如果钱包余额小于等于最小余额
  41. if balance <= Web3.to_wei(minimum_balance, "ether"):
  42. print("The wallet balance is invalid.")
  43. exit(1)
  44. async def execute_contract(account, minimum_balance):
  45. try:
  46. # 随机选择一个合约地址、合约方法哈希值及描述
  47. contract_info = get_random_contract_data()
  48. address = contract_info["address"]
  49. data_hash = contract_info["data_hash"]
  50. description = contract_info["description"]
  51. print(f"Selected contract: {description}")
  52. print(f"Selected contract address: {address}")
  53. print(f"Selected contract dataHash: {data_hash}")
  54. # 检查原生代币余额
  55. balance = w3.eth.get_balance(account.address)
  56. before_balance = Web3.from_wei(balance, "ether")
  57. print(f"Native balance: {before_balance}")
  58. # 检查钱包余额,不能小于 0.2
  59. check_wallet_balance(balance, minimum_balance)
  60. # 质押金额
  61. amount_to_stake = get_random_amount()
  62. print(f"Amount to contract: {Web3.from_wei(amount_to_stake, 'ether')}")
  63. if balance < amount_to_stake:
  64. raise Exception("Insufficient balance for contract execution.")
  65. # 构建交易
  66. nonce = w3.eth.get_transaction_count(account.address)
  67. tx = {
  68. "to": Web3.to_checksum_address(address),
  69. "value": amount_to_stake,
  70. "data": data_hash,
  71. "gas": 100000,
  72. "gasPrice": Web3.to_wei(50, "gwei"),
  73. "nonce": nonce,
  74. "chainId": w3.eth.chain_id
  75. }
  76. # 签名并发送交易
  77. signed_tx = w3.eth.account.sign_transaction(tx, account.key)
  78. tx_hash = w3.eth.send_raw_transaction(signed_tx.raw_transaction)
  79. print(f"Contract Execution hash: {tx_hash.hex()}")
  80. # 等待交易确认
  81. receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
  82. print("Contract Execution status: ok")
  83. # 检查交易后的余额
  84. balance_staking = w3.eth.get_balance(account.address)
  85. after_balance = Web3.from_wei(balance_staking, "ether")
  86. print(f"Native balance after transaction: {after_balance}")
  87. print(f"The cost of this transaction: {float(before_balance) - float(after_balance)}")
  88. except Exception as e:
  89. print(f"Error executing contract: {e}")
  90. async def run_multiple_staking(private_keys):
  91. last_times = 100
  92. min_delay = 10
  93. max_delay = 20
  94. minimum_balance = 3
  95. for private_key in private_keys:
  96. account = w3.eth.account.from_key(private_key.strip())
  97. print(f"Processing wallet with address: {account.address}")
  98. for i in range(last_times):
  99. if last_times > 1:
  100. print(f"Starting Contract attempt {i + 1}")
  101. await execute_contract(account, minimum_balance)
  102. print(f"Completed Contract attempt {i + 1}")
  103. if i < last_times - 1:
  104. delay = random.uniform(min_delay, max_delay)
  105. print(f"Waiting for {delay} seconds...")
  106. print('------------------------------------------------------------')
  107. time.sleep(random.uniform(min_delay, max_delay))
  108. async def main():
  109. try:
  110. with open(file_path, 'r') as file:
  111. private_keys = [line.strip() for line in file if line.strip()]
  112. await run_multiple_staking(private_keys)
  113. except Exception as err:
  114. print(f'读取文件时发生错误: {err}')
  115. exit(1)
  116. if __name__ == "__main__":
  117. import asyncio
  118. asyncio.run(main())