:2026-03-20 13:12 点击:3
Python实现以太坊转账:从入门到实践**
以太坊作为全球领先的智能合约平台,其原生代币ETH的转账功能是区块链应用中最基础也最重要的操作之一,利用Python这一简洁而强大的编程语言,我们可以轻松实现以太坊的自动化转账,本文将详细介绍如何使用Python与以太坊交互,完成ETH转账的全过程,涵盖环境搭建、核心库使用、代码实现及注意事项。
在开始编写Python代码之前,我们需要确保开发环境已经准备就绪。
Python环境:确保你的系统已安装Python(推荐3.6及以上版本),如果没有,可以从Python官网下载并安装。
以太坊节点:Python脚本需要与以太坊网络交互,你有两种主要选择:
安装Python库:我们将使用web3.py库,这是与以太坊交互最流行的Python库之一,它提供了丰富的API来调用以太坊节点的功能,为了处理私钥和地址,我们可能会用到eth-account。
通过pip安装这些库:
pip install web3 pip install eth-account
在进行转账之前,我们需要理解几个核心概念:
0x开头,40位十六进制字符。重要提示:在实际应用中,切勿将私钥硬编码在代码中,尤其是将代码上传到公共代码仓库(如GitHub),更安全的做法是使用环境变量、配置文件(妥善保管)或硬件钱包来管理私钥。
下面,我们以连接到Infura公共节点为例,展示如何使用Python进行ETH转账。
from web3 import Web3
import os
infura_url = "https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID"
# 初始化Web3实例
w3 = Web3(Web3.HTTPProvider(infura_url))
# 检查连接是否成功
print(f"Connected to Ethereum: {w3.is_connected()}")
# 替换为你的发送方私钥(仅用于演示,实际请妥善保管!)
sender_private_key = "0x_YOUR_SENDER_PRIVATE_KEY"
# 从私钥获取账户对象
sender_account = w3.eth.account.from_key(sender_private_key)
sender_address = sender_account.address
print(f"Sender Address: {sender_address}")
# 接收方地址(确保是以太坊主网地址,且格式正确)
receiver_address = "0x_YOUR_RECEIVER_ADDRESS"
# 转账金额(以ETH为单位)
amount_eth = 0.01
# 将ETH转换为Wei(以太坊的最小单位,1 ETH = 10^18 Wei)
amount_wei = w3.to_wei(amount_eth, 'ether')
print(f"Receiver Address: {receiver_address}")
print(f"Amount to send: {amount_eth} ETH ({amount_wei} Wei)")
以太坊交易包含多个字段,我们需要构建一个交易字典:
# 获取当前nonce(确保交易唯一性)
nonce = w3.eth.get_transaction_count(sender_address)
# 构建交易
transaction = {
'nonce': nonce,
'to': receiver_address,
'value': amount_wei,
'gas': 21000, # 转账ETH的gas limit通常是21000
'gasPrice': w3.eth.gas_price, # 获取当前建议的gas price
'chainId': 1 # 以太坊主网chainId为1,测试网为3(Ropsten)、4(Rinkeby)等
}
使用发送方的私钥对交易进行签名:
# 签名交易 signed_txn = w3.eth.account.sign_transaction(transaction, sender_private_key)
# 发送签名后的交易
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
# 等待交易被打包(可选,可以轮询交易状态)
print(f"Transaction sent! Hash: {tx_hash.hex()}")
# 等待交易确认
receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print(f"Transaction receipt: {receipt}")
如果一切顺利,交易将被成功打包,你可以在以太坊浏览器中通过交易哈希查看详情。
from web3 import Web3
import os
def send_eth_transaction():
# 1. 初始化Web3
infura_url = "https
://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID"
w3 = Web3(Web3.HTTPProvider(infura_url))
if not w3.is_connected():
print("Failed to connect to Ethereum network!")
return
# 2. 设置发送方账户和私钥(实际应用中应从安全的地方获取)
sender_private_key = "0x_YOUR_SENDER_PRIVATE_KEY" # 替换为你的私钥
sender_account = w3.eth.account.from_key(sender_private_key)
sender_address = sender_account.address
print(f"Sender Address: {sender_address}")
# 3. 设置接收方地址和金额
receiver_address = "0x_YOUR_RECEIVER_ADDRESS" # 替换为接收方地址
amount_eth = 0.01 # 转账0.01 ETH
amount_wei = w3.to_wei(amount_eth, 'ether')
print(f"Receiver Address: {receiver_address}")
print(f"Amount to send: {amount_eth} ETH ({amount_wei} Wei)")
# 4. 构建交易
nonce = w3.eth.get_transaction_count(sender_address)
gas_price = w3.eth.gas_price
print(f"Current Gas Price: {gas_price} Wei")
transaction = {
'nonce': nonce,
'to': receiver_address,
'value': amount_wei,
'gas': 21000,
'gasPrice': gas_price,
'chainId': 1 # 主网
}
# 5. 签名并发送交易
try:
signed_txn = w3.eth.account.sign_transaction(transaction, sender_private_key)
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
print(f"Transaction sent! Hash: {tx_hash.hex()}")
# 6. 等待交易确认
receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print(f"Transaction receipt: {receipt}")
if receipt['status'] == 1:
print("Transaction successful!")
else:
print("Transaction failed!")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
send_eth_transaction()
gasPrice,在高拥堵时期,Gas费用会很高,可以使用etherscan.io或类似工具查看当前Gas价格建议。chainId与你连接的以太坊网络一致(主网1,Ropsten测试网3,Rinkeby测试网4,Goerli测试网5等),错误的选择会导致交易失败或资金损失。本文由用户投稿上传,若侵权请提供版权资料并联系删除!