Created
September 23, 2024 12:10
-
-
Save orangesurf/2d22c614d75d23104698d3fd5ce208c4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 sys | |
| import json | |
| from datetime import datetime, timezone | |
| import requests | |
| def get_transaction_info(txid): | |
| # Check if the transaction is confirmed | |
| tx_url = f"https://mempool.space/api/tx/{txid}" | |
| try: | |
| response = requests.get(tx_url) | |
| response.raise_for_status() # Raises an HTTPError for bad responses | |
| tx_data = response.json() | |
| if tx_data['status']['confirmed']: | |
| # Transaction is confirmed, get first seen time from block API | |
| block_hash = tx_data['status']['block_hash'] | |
| block_url = f"https://mempool.space/api/v1/block/{block_hash}/tx/{txid}/audit" | |
| block_response = requests.get(block_url) | |
| block_response.raise_for_status() | |
| block_data = block_response.json() | |
| first_seen = block_data['firstSeen'] | |
| return first_seen, True | |
| else: | |
| # Transaction is unconfirmed, use transaction-times API | |
| times_url = f"https://mempool.space/api/v1/transaction-times?txId[]={txid}" | |
| times_response = requests.get(times_url) | |
| times_response.raise_for_status() | |
| times_data = times_response.json() | |
| if isinstance(times_data, dict) and txid in times_data: | |
| first_seen = times_data[txid] | |
| return first_seen, False | |
| elif isinstance(times_data, list) and len(times_data) > 0: | |
| first_seen = times_data[0][0] if isinstance(times_data[0], list) else times_data[0] | |
| return first_seen, False | |
| else: | |
| print(f"Unexpected response format from transaction-times API: {times_data}") | |
| return None, False | |
| except requests.exceptions.RequestException as e: | |
| print(f"Error occurred while fetching data: {e}") | |
| return None, None | |
| def main(): | |
| if len(sys.argv) != 2: | |
| print("Usage: python script.py <txid>") | |
| sys.exit(1) | |
| txid = sys.argv[1] | |
| first_seen, confirmed = get_transaction_info(txid) | |
| if first_seen is not None: | |
| first_seen_datetime = datetime.fromtimestamp(first_seen, tz=timezone.utc).strftime('%Y-%m-%d %H:%M:%S %Z') | |
| print(f"Transaction ID: {txid}") | |
| print(f"First Seen: {first_seen_datetime}") | |
| print(f"Confirmed: {'Yes' if confirmed else 'No'}") | |
| else: | |
| print(f"Unable to retrieve information for transaction ID: {txid}") | |
| if __name__ == "__main__": | |
| main() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example unconfirmed
python txfirstseen.py f5904c3168f0d135258fecc603f1865e94fec9d77adb93d52991d437e70a718bresponse:
Example confirmed
python txfirstseen.py 767c6b1cf55b3ea4c5ae7d93b1f7bf245a7413b3fd6b26699ccba04703ba67d7response: