somnia_exchange_trade.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # -- coding: utf-8 --
  2. from web3 import Web3
  3. import random
  4. import time
  5. import os
  6. provider_url = "https://dream-rpc.somnia.network"
  7. w3 = Web3(Web3.HTTPProvider(provider_url))
  8. contract_data = [
  9. {
  10. "address": "0xF22eF0085f6511f70b01a68F360dCc56261F768a",
  11. "data_hash": "0xd0e30db0",
  12. "description": "SomniaExchangeTrade - 1"
  13. }
  14. ]
  15. file_path = os.path.join(os.path.dirname(__file__), 'AccountList.txt')
  16. def get_random_amount():
  17. random_tail = str(random.randint(1000, 9999))
  18. random_bit = random.randint(14, 18)
  19. random_balance = f"0.{random_tail.zfill(random_bit)}"
  20. return Web3.to_wei(random_balance, "ether")
  21. def get_random_contract_data():
  22. if not contract_data:
  23. print("The contract data array is empty, the contract cannot be selected, and the program exits.")
  24. exit(1)
  25. return random.choice(contract_data)
  26. def check_wallet_balance(balance, minimum_balance):
  27. if balance <= Web3.to_wei(minimum_balance, "ether"):
  28. print("The wallet balance is invalid.")
  29. exit(1)
  30. async def execute_contract(account, minimum_balance):
  31. try:
  32. contract_info = get_random_contract_data()
  33. address = contract_info["address"]
  34. data_hash = contract_info["data_hash"]
  35. description = contract_info["description"]
  36. print(f"Selected contract: {description}")
  37. print(f"Selected contract address: {address}")
  38. print(f"Selected contract dataHash: {data_hash}")
  39. balance = w3.eth.get_balance(account.address)
  40. before_balance = Web3.from_wei(balance, "ether")
  41. print(f"Native balance: {before_balance}")
  42. check_wallet_balance(balance, minimum_balance)
  43. amount_to_stake = get_random_amount()
  44. print(f"Amount to contract: {Web3.from_wei(amount_to_stake, 'ether')}")
  45. if balance < amount_to_stake:
  46. raise Exception("Insufficient balance for contract execution.")
  47. contract_address = Web3.to_checksum_address(address)
  48. abi = [
  49. {
  50. "inputs": [],
  51. "name": "deposit",
  52. "outputs": [],
  53. "stateMutability": "payable",
  54. "type": "function"
  55. }
  56. ]
  57. contract = w3.eth.contract(address=contract_address, abi=abi)
  58. nonce = w3.eth.get_transaction_count(account.address)
  59. now_gas = Web3.from_wei(w3.eth.gas_price, 'ether')
  60. print(f"Now Gas Fee is: {now_gas}")
  61. tx = contract.functions.deposit().build_transaction({
  62. "from": account.address,
  63. "value": amount_to_stake,
  64. "gas": 100000,
  65. "gasPrice": w3.eth.gas_price,
  66. "nonce": nonce,
  67. "chainId": w3.eth.chain_id
  68. })
  69. signed_tx = w3.eth.account.sign_transaction(tx, account.key)
  70. tx_hash = w3.eth.send_raw_transaction(signed_tx.raw_transaction)
  71. print(f"Contract Execution hash: {tx_hash.hex()}")
  72. receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
  73. execute_status = receipt["status"]
  74. if execute_status == 1:
  75. print("Contract Execution status: seccessful")
  76. elif execute_status == 0:
  77. print(f"Contract Execution status: failed\n{receipt}")
  78. else:
  79. print("Contract Execution status: unknown")
  80. balance_staking = w3.eth.get_balance(account.address)
  81. after_balance = Web3.from_wei(balance_staking, "ether")
  82. print(f"Native balance after transaction: {after_balance}")
  83. print(f"The cost of this transaction: {float(before_balance) - float(after_balance)}")
  84. except Exception as e:
  85. print(f"Error executing contract: {e}")
  86. async def run_multiple_staking(private_keys):
  87. last_times = 1
  88. min_delay = 10
  89. max_delay = 20
  90. minimum_balance = 0.2
  91. for private_key in private_keys:
  92. account = w3.eth.account.from_key(private_key.strip())
  93. print(f"Processing wallet with address: {account.address}")
  94. for i in range(last_times):
  95. if last_times > 1:
  96. print(f"Starting Contract attempt {i + 1}")
  97. await execute_contract(account, minimum_balance)
  98. print(f"Completed Contract attempt {i + 1}")
  99. if i < last_times - 1:
  100. delay = random.uniform(min_delay, max_delay)
  101. print(f"Waiting for {delay} seconds...")
  102. print('------------------------------------------------------------')
  103. time.sleep(random.uniform(min_delay, max_delay))
  104. async def main():
  105. try:
  106. with open(file_path, 'r') as file:
  107. private_keys = [line.strip() for line in file if line.strip()]
  108. await run_multiple_staking(private_keys)
  109. except Exception as err:
  110. print(f'读取文件时发生错误: {err}')
  111. exit(1)
  112. if __name__ == "__main__":
  113. import asyncio
  114. asyncio.run(main())