Why do I always get ContractLogicErrors when trying to simulate a swap pending transaction?

  Kiến thức lập trình

Question:

I’m trying to simulate a pending swap transaction on Ethereum using Web3.py and a Geth node, but I keep encountering ContractLogicError. I’ve tried different methods, including debug_traceTransaction, debug_traceCall and eth_call, but none of them seem to work for pending transactions. Here are the details of what I’ve tried:

Context:

  • Environment: Python, Web3.py, Geth node
  • Transaction Type: Swap pending transaction
  • Methods Tried: debug_traceTransaction, eth_call

Code Samples:

Method 1: debug_traceTransaction

This method doesn’t work for pending transactions.

state_diff = node.manager.request_blocking("debug_traceTransaction", [tx['hash'].hex(), {"tracer": "callTracer"}])

Method 2: eth_call

I simplified the transaction data to include only essential fields, but still encounter ContractLogicError.

from web3 import Web3
from web3.exceptions import ContractLogicError

# Connect to your Ethereum node
def connect():
    return Web3(Web3.HTTPProvider('http://localhost:8545'))

web3 = connect()

# Fetch the transaction details using the transaction hash
tx_hash = "0x35a2d684e71d7d0f9db027b68bfeb857bad8f078319e9845d47ad7ae412fa5b7"
tx = web3.eth.get_transaction(tx_hash)

# Prepare the simplified transaction data
tx_data = {
        'to': Web3.to_checksum_address(tx['to']) if tx['to'] else None,
        'from': Web3.to_checksum_address(tx['from']),
        'data': tx['input'].hex(),
        'value': hex(tx['value']),
        'gas': hex(tx['gas']),
        'nonce': hex(tx['nonce']),
        'type': hex(tx['type']),
        'accessList': tx['accessList'],
        'chainId': hex(tx['chainId'])
    }


# Conditionally add fee parameters
if tx['type'] == 2:
    tx_data['maxFeePerGas'] = hex(tx['maxFeePerGas'])
    tx_data['maxPriorityFeePerGas'] = hex(tx['maxPriorityFeePerGas'])
else:
    tx_data['gasPrice'] = hex(tx['gasPrice'])


# Simulate the transaction using eth_call with the pending state
try:
    simulation_result = web3.eth.call(tx_data, 'pending')
    print("Simulation Result:", simulation_result)
except ContractLogicError as e:
    print(f"Contract Logic Error: {e}")
except ValueError as e:
    print(f"ValueError: {e}")

Error Message:

ContractLogicError: execution reverted: S13

Questions:

  1. Why do I keep encountering ContractLogicError when trying to simulate this pending transaction?
  2. Is there a better way to simulate or trace pending transactions on Ethereum?
  3. How can I diagnose and fix the issues causing the contract to revert during simulation?

Any guidance or suggestions on how to properly simulate pending transactions without running into these errors would be greatly appreciated. Thank you!

LEAVE A COMMENT