|
|
@@ -8,6 +8,7 @@ from web3.middleware import ExtraDataToPOAMiddleware # For PoA networks like Goe
|
|
|
from eth_account import Account
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
+
|
|
|
# 配置日志
|
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
|
|
@@ -156,7 +157,14 @@ class EthClient:
|
|
|
# EIP-1559 之前的gas price (注意: sepolia等测试网可能仍然需要传统的gasPrice)
|
|
|
tx['gasPrice'] = self.w3.eth.gas_price
|
|
|
|
|
|
+ '''
|
|
|
# TODO 这里后面再研究了,会报错。
|
|
|
+ 在使用 web3.py 手动构建和签名交易时,需要使用支持 EIP-1559 交易类型的签名函数。
|
|
|
+ 通常,eth_account 库及其 sign_transaction 方法是支持的,只要您提供的交易字典包含了正确的 EIP-1559 字段 (chainId, nonce, to, value, gas, maxFeePerGas, maxPriorityFeePerGas 等)。
|
|
|
+ 您构建的交易字典可能混合了传统 Gas 字段 (gasPrice) 和 EIP-1559 字段 (maxFeePerGas, maxPriorityFeePerGas)。一个交易只能使用其中一种方式来指定 Gas 费用。
|
|
|
+ 解决方案: 确保您的交易字典中只有 EIP-1559 相关的 Gas 字段(maxFeePerGas, maxPriorityFeePerGas 和 gas),或者只有传统 Gas 字段(gasPrice 和 gas)。
|
|
|
+ 不要同时包含 gasPrice 和 maxFeePerGas/maxPriorityFeePerGas。
|
|
|
+ '''
|
|
|
# # 对于支持 EIP-1559 的网络,应该使用:
|
|
|
# if 'maxPriorityFeePerGas' not in tx and 'maxFeePerGas' not in tx:
|
|
|
# latest_block = self.w3.eth.get_block('latest')
|
|
|
@@ -266,7 +274,7 @@ class EthClient:
|
|
|
tx_data['gasPrice'] = gas_price
|
|
|
|
|
|
logging.info(f"Preparing to transfer {amount_readable} of token {token_address} to {to_address}...")
|
|
|
- return self._sign_and_send_transaction(tx_data, 1.2, 1.5)
|
|
|
+ return self._sign_and_send_transaction(tx_data, 1.2, 2)
|
|
|
|
|
|
def approve_erc20(self, token_address: str, spender_address: str, amount_readable: float,
|
|
|
gas_limit: int = None, gas_price_gwei: float = None) -> str:
|
|
|
@@ -404,9 +412,9 @@ if __name__ == "__main__":
|
|
|
if my_token_balance >= Decimal(str(amount_to_transfer)):
|
|
|
logging.info(f"\nAttempting to transfer {amount_to_transfer} {token_symbol}...")
|
|
|
erc20_tx_hash = client.transfer_erc20(token_address, TEST_RECIPIENT_ADDRESS, amount_to_transfer)
|
|
|
- logging.info(f"{token_symbol} transfer transaction sent! Hash: {erc20_tx_hash}")
|
|
|
+ logging.info(f"{token_symbol} transfer transaction sent! Block: {client.w3.eth.block_number} Hash: {erc20_tx_hash}")
|
|
|
receipt = client.wait_for_transaction_receipt(erc20_tx_hash)
|
|
|
- logging.info(f"{token_symbol} transfer transaction confirmed! Status: {'Success' if receipt.status == 1 else 'Failed'}")
|
|
|
+ logging.info(f"{token_symbol} transfer transaction confirmed! Block: {client.w3.eth.block_number} Status: {'Success' if receipt.status == 1 else 'Failed'}")
|
|
|
logging.info(f"My new {token_symbol} Balance: {client.get_erc20_balance(token_address)} {token_symbol}")
|
|
|
else:
|
|
|
logging.info(f"Insufficient {token_symbol} balance to transfer {amount_to_transfer} {token_symbol}.")
|