如何利用python脚本快速mint以太坊铭文?
  
    
  
      2023-06-28 07:38
  
    
      Words count:
    
    
      1156
    
  
  上一篇文章已经介绍了怎么查重,并且把未打的指定格式的铭文自动生成了一个新的csv文件,那我们获取到了对应的十六进制字符串之后,如果要再一行一行去小狐狸钱包发送交易,这也太慢了也不方便。
这里同样我们可以用python的web3库实现批量自动发送交易,节省大量操作时间。...
    上一篇文章已经介绍了怎么查重,并且把未打的指定格式的铭文自动生成了一个新的csv文件,那我们获取到了对应的十六进制字符串之后,如果要再一行一行去小狐狸钱包发送交易,这也太慢了也不方便。
这里同样我们可以用python的web3库实现批量自动发送交易,节省大量操作时间。
这个代码需要本地有.env文件,用于存放自己的钱包地址和私钥,需要设置两个变量public_address和private_key。以及需要一个rpc地址,代码会读取之前csv文件的第三列的十六进制字符串,然后在交易的data部分转账的时候发送这部分字符串。
代码如下,有兴趣的可以自己去尝试跑一跑,有疑问可以在twitter上问:
import json
from web3 import Web3
import pandas as pd
import time
import os
from dotenv import load_dotenv
from web3.types import HexBytes
# 定义处理HexBytes对象的函数
def hexbytes_to_string(value):
    if isinstance(value, HexBytes):
        return value.hex()
# 连接RPC
http_rpc = '<https://eth-mainnet.g.alchemy.com/v2/xxx>' #填写alchemy的rpc地址
w3 = Web3(Web3.HTTPProvider(http_rpc))
print('Connected', w3.isConnected(), http_rpc)
# 你的私钥和公钥,代码目录放置.env文件,用于存放钱包和私钥
load_dotenv()
private_key = os.getenv('PRIVATE_KEY') 
public_address = os.getenv('PUBLIC_KEY')
print(public_address)
print(private_key)
# 读取CSV文件的第三列,任意x行
df = pd.read_csv('0626_result_twitter.csv').tail(50)
#读取指定行,读取第10行到第50行(包括第10行和第50行)
#df = df.iloc[9:50]
# 获取初始nonce值
next_nonce = w3.eth.getTransactionCount(public_address)
for index, row in df.iterrows():
    print("\\n=================== New Transaction ===================\\n")
    hex_data = row['hex_string']
    # 转换为bytes
    input_data = bytes.fromhex(hex_data)
    print(f"Gas Price: {w3.eth.gasPrice}")
    print(f"Input Data: {input_data}")
    # 创建并签署交易
    transaction = {
        'from': public_address,
        'to': public_address,
        'value': 0,  # 转账数量
        'gas': 30000,  # Gas limit
        'gasPrice': w3.eth.gasPrice,  # Gas price
        'nonce': next_nonce,  # 使用预存的next_nonce值
        'data': input_data  # Input data
    }
    signed_tx = w3.eth.account.signTransaction(transaction, private_key)
    # 发送交易
    tx_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
    print(f'\\nTransaction hash: {tx_hash.hex()}\\n')
    # 等待交易完成并检查状态
    print("Waiting for transaction to be mined...")
    receipt = w3.eth.waitForTransactionReceipt(tx_hash)
    # 获取交易的完整数据
    tx_data = w3.eth.getTransaction(tx_hash)
    input_data = tx_data['input']
    # 转换16进制的input_data到ASCII
    input_data_ascii = bytes.fromhex(input_data[2:]).decode('utf-8')  # [2:]用于去除前缀"0x"
    # 在打印收据和input_data时使用我们定义的函数来处理HexBytes对象
    print(f"\\nReceipt:\\n{json.dumps(dict(receipt), indent=4, default=hexbytes_to_string)}")
    print(f"\\nInput Data: {input_data_ascii}")
    if receipt['status'] == 1:
        print('\\nTransaction was successful.')
        # 只有在交易成功后,才更新next_nonce值以备下次使用
        next_nonce += 1
    else:
        print('\\nTransaction failed.')
    # 可能需要添加一点延迟,以防止发送交易太快
    time.sleep(2)
  0
  times bought, 
  0
  times reward
        0.0
      
      
        Price(ETH)
      
    
        0
      
      
        Paid Times
      
    
        0.0
      
      
        Revenue(USD)
      
    
        0.0
      
      
        My Share(%)
      
    
      Comments
    
    
  