faucet.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # -*- coding: utf-8 -*-
  2. import time
  3. import random
  4. from web3 import Web3
  5. from eth_account import Account
  6. private_key_list = [
  7. "48cd0f0db84c9ca488292c9da449236d14a845e07618b6c0975cb158ccd60794"
  8. ]
  9. def claim_token(private_key):
  10. # 配置信息
  11. rpc_url = "https://rpc.sepolia.ethpandaops.io"
  12. target_chain_id = 11155111
  13. contract_address = "0x3edf60dd017ace33a0220f78741b5581c385a1ba"
  14. try:
  15. # 连接Web3
  16. web3 = Web3(Web3.HTTPProvider(rpc_url))
  17. if not web3.is_connected():
  18. print("❌ 无法连接到目标网络")
  19. return False
  20. # 校验链ID是否为Sepolia
  21. chain_id = web3.eth.chain_id
  22. if chain_id != target_chain_id:
  23. print(f"❌ 连接了错误的网络,当前链ID: {chain_id}, 期望: {target_chain_id}")
  24. return False
  25. # 创建账户对象
  26. account = Account.from_key("0x" + private_key)
  27. # 查询当前账户余额
  28. balance = web3.eth.get_balance(account.address)
  29. balance_eth = web3.from_wei(balance, 'ether')
  30. print(f"当前账户余额: {balance_eth} Token")
  31. # 构造调用数据
  32. wallet_address = account.address[2:].lower() # 去掉0x前缀
  33. wallet_param = wallet_address.zfill(64) # 补足64位(32字节)
  34. call_data = "0x6a627842" + wallet_param
  35. # 构造交易
  36. transaction = {
  37. "to": Web3.to_checksum_address(contract_address),
  38. "value": 0,
  39. "gas": 100000, # 固定Gas限制,可根据需要调整
  40. "gasPrice": web3.to_wei(20, "gwei"), # 固定Gas价格,可调整
  41. "nonce": web3.eth.get_transaction_count(account.address),
  42. "data": call_data,
  43. "chainId": target_chain_id,
  44. }
  45. print(f"📋 调用数据: {call_data}")
  46. print("📤 发送交易中...")
  47. # 签名交易
  48. signed_txn = web3.eth.account.sign_transaction(
  49. transaction, private_key)
  50. # 发送交易
  51. tx_hash = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
  52. tx_hash_hex = tx_hash.hex()
  53. print(f"📤 交易已发送: {tx_hash_hex}")
  54. print("⏳ 等待交易确认...")
  55. # 等待交易确认
  56. receipt = web3.eth.wait_for_transaction_receipt(tx_hash, timeout=300)
  57. if receipt.status == 1:
  58. print(f"✅ 代币领取成功! Gas使用: {receipt.gasUsed}")
  59. print(f"🔗 交易链接: https://sepolia.etherscan.io/tx/{tx_hash_hex}")
  60. return True
  61. else:
  62. print("❌ 交易执行失败")
  63. return False
  64. except Exception as e:
  65. error_msg = str(e).lower()
  66. if "insufficient funds" in error_msg:
  67. print("❌ ETH余额不足,无法支付Gas费用")
  68. elif "nonce too low" in error_msg:
  69. print("❌ Nonce值过低,请稍后重试")
  70. elif "replacement transaction underpriced" in error_msg:
  71. print("❌ 交易费用过低,请提高Gas价格")
  72. else:
  73. print(f"❌ 领取失败: {e}")
  74. return False
  75. if __name__ == "__main__":
  76. claim_times = 200000
  77. for private_key in private_key_list:
  78. for i in range(claim_times):
  79. print(f'开始领取第 {i+1} 次')
  80. n = 1
  81. retry_min_time = 10
  82. retry_max_time = 20
  83. retry_time = random.uniform(retry_min_time, retry_max_time)
  84. retry_max_times = 3
  85. while True:
  86. if n > retry_max_times:
  87. print(f'领取 {i+1} 次失败, 重试次数超过 {retry_max_times} 次, 跳过')
  88. break
  89. if claim_token(private_key):
  90. break
  91. print(f'领取 {i+1} 次失败, 重试第 {n} 次, 等待 {retry_time} 秒后再次领取')
  92. n += 1
  93. time.sleep(retry_time)
  94. print(f'领取第 {i+1} 次成功, 等待 {retry_time} 秒后再次领取')
  95. time.sleep(retry_time)