Reputation: 1
I have written a script to fetch transaction details from the Solana blockchain using the solana-py
library. It includes sanitization for the transaction ID to ensure it is a valid Base58 string. Here's the code:
from solana.rpc.api import Client # type: ignore
import re
# Set up your Solana RPC endpoint
SOLANA_RPC_URL = "https://api.mainnet-beta.solana.com"
solana_client = Client(SOLANA_RPC_URL)
# Function to sanitize transaction ID
def sanitize_transaction_id(transaction_id: str) -> str:
"""
Removes invalid characters from a transaction ID, ensuring it is a valid Base58 string.
"""
return re.sub(r'[^123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]', '', transaction_id)
# Function to check a Solana transaction
def check_transaction(transaction_id: str):
try:
# Sanitize the transaction ID
sanitized_transaction_id = sanitize_transaction_id(transaction_id)
print(f"Raw Transaction ID: {transaction_id}")
print(f"Sanitized Transaction ID: {sanitized_transaction_id}")
# Ensure the sanitized transaction ID is 88 characters long
if len(sanitized_transaction_id) != 88: # Solana transaction signatures are typically 88 Base58 characters
raise ValueError("Transaction ID length is invalid.")
# Fetch the transaction details using the Solana RPC API
print("Attempting to fetch transaction details...")
transaction = solana_client.get_transaction(sanitized_transaction_id) # Pass as Base58 string
if not transaction or not transaction.get("result"):
print("Transaction not found or incomplete.")
return
print("Transaction details fetched successfully!")
print(transaction)
except ValueError as ve:
print(f"Validation Error: {ve}")
except Exception as e:
print(f"Unexpected Error: {e}")
# Example usage
if __name__ == "__main__":
# Replace with your test transaction ID
test_transaction_id = "2JNwfJbNmTFkH2YuWWErWTaNpMpbqETguMiLFWqFE6mMy8LiNrf3N9dYZc66NwowybPd1JaE1thTBT36cRx4h7ki"
check_transaction(test_transaction_id)
Upvotes: 0
Views: 55
Reputation: 8472
The error here tells you a lot of information: Unexpected Error: argument 'signature': 'str' object cannot be converted to 'Signature'
The get_transaction
function is expecting a Signature
, but you're passing in a str
. So you need to convert your string into a Signature
, which you can do by calling:
from solders.signature import Signature
signature = Signature.new_from_str(sanitized_transaction_id)
transaction = solana_client.get_transaction(signature)
Side note: not all signatures will have 88 base-58 characters! So you might want to change that to make sure it has at most 88 characters.
Upvotes: 0