Created
March 18, 2024 07:51
-
-
Save llk23r/86461f21dea5d51b25920b13202abcf9 to your computer and use it in GitHub Desktop.
Get status of an ethereum transaction from Etherscan.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import re | |
def get_transaction_details(tx_id): | |
""" | |
Fetches transaction details from Etherscan and extracts status, from, and to fields. | |
Args: | |
tx_id: The transaction ID (hash) to fetch details for. | |
Returns: | |
A dictionary containing the extracted details, or None if an error occurs. | |
""" | |
url = f"https://etherscan.io/tx/{tx_id}" | |
# Set headers (you can adjust these if needed) | |
headers = { | |
'accept': | |
'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7', | |
'accept-language': | |
'en-GB,en-US;q=0.9,en;q=0.8', | |
'authority': | |
'etherscan.io', | |
'cache-control': | |
'no-cache', | |
'pragma': | |
'no-cache', | |
'sec-ch-ua': | |
'"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"', | |
'sec-ch-ua-mobile': | |
'?0', | |
'sec-ch-ua-platform': | |
'"macOS"', | |
'sec-fetch-dest': | |
'document', | |
'sec-fetch-mode': | |
'navigate', | |
'sec-fetch-site': | |
'same-origin', | |
'sec-fetch-user': | |
'?1', | |
'upgrade-insecure-requests': | |
'1', | |
'user-agent': | |
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36' | |
} | |
try: | |
response = requests.get(url, headers=headers) | |
response.raise_for_status( | |
) # Raise an exception for error status codes | |
html_content = response.text | |
# Extract status | |
status_match = re.search( | |
r'<div class="row align-items-center mb-4"><div class="col-auto col-md-3 text-dt"><i class="far fa-question-circle me-1".*?></i>Status:</div><div class="col col-md-9"><span class="badge bg-success.*?>(.*?)</span></div></div>', | |
html_content, re.DOTALL) | |
status = status_match.group(1).strip() if status_match else None | |
# Remove HTML tag for the status icon | |
status = re.sub(r'<[^>]+>', '', status) | |
final_status = status.split(' ') | |
status = final_status[-1] | |
return {'status': status} | |
except requests.exceptions.RequestException as e: | |
print(f"Error fetching transaction details: {e}") | |
return None | |
tx_id = "0x2ce13a964a9cd6969b45e65c29660304c102ab67dd63ade378d45e231ce6d337" | |
details = get_transaction_details(tx_id) | |
if details: | |
print("Status:", details['status']) | |
else: | |
print("Failed to retrieve transaction status.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment