async def parse_tx(self, tx_sig: str) -> ParsedTx: tx_receipt = self._solana_client_manager.get_sol_tx_info( tx_sig, 5, "base64") self.msg(tx_receipt) encoded_data = tx_receipt["result"].get("transaction")[0] decoded_data = base64.b64decode(encoded_data) decoded_data_hex = decoded_data.hex() tx = Transaction.deserialize(bytes.fromhex(decoded_data_hex)) tx_metadata = {} # Append each parsed transaction to parsed metadata tx_instructions = [] for instruction in tx.instructions: parsed_instruction = self.anchor_parser.parse_instruction( instruction) if self.is_valid_instruction(parsed_instruction): tx_instructions.append(parsed_instruction) tx_metadata["instructions"] = tx_instructions """ For example: Embed instruction specific information in tx_metadata """ return { "tx_sig": tx_sig, "tx_metadata": tx_metadata, "result": tx_receipt["result"], }
async def parse_tx(tx_hash: str) -> List[Dict]: parsed_instructions = [] tx_info = await fetch_tx_receipt(tx_hash) if tx_info is not None: idl = get_idl() instruction_coder = InstructionCoder(idl) encoded_data = tx_info.get("transaction")[0] encoded_data_hex = base64.b64decode(encoded_data).hex() res = Transaction.deserialize(bytes.fromhex(encoded_data_hex)) for instruction in res.instructions: ix_data = parse_instruction(instruction=instruction, instruction_coder=instruction_coder) parsed_instructions.append(ix_data) return parsed_instructions
from solana._layouts.system_instructions import SYSTEM_INSTRUCTIONS_LAYOUT, InstructionType as SOLInstructionType from spl.token._layouts import INSTRUCTIONS_LAYOUT, InstructionType as SPLInstructionType import base64 # solana_client = Client("https://api.mainnet-beta.solana.com") solana_client = Client("https://devnet.solana.com") address = "" transactions = solana_client.get_confirmed_signature_for_address2(address)["result"] for tx in transactions: tx_result = solana_client.get_confirmed_transaction(tx_sig=tx["signature"], encoding="base64") raw_tx_str = tx_result['result']['transaction'][0] raw_tx_base64_bytes = raw_tx_str.encode('ascii') raw_tx_bytes = base64.b64decode(raw_tx_base64_bytes) des_tx: Transaction = Transaction.deserialize(raw_tx_bytes) tx_instruction: TransactionInstruction = des_tx.instructions.pop() # program id will be a bunch of ones if it's a transaction involving SOL if tx_instruction.program_id.__str__() == "11111111111111111111111111111111": if SYSTEM_INSTRUCTIONS_LAYOUT.parse(tx_instruction.data).instruction_type == SOLInstructionType.Transfer: transfer_params: SOLTransferParams = sol_decode_transfer(tx_instruction) print(tx["slot"]) # blockheight print(f'from:{transfer_params.from_pubkey}') # from print(f'to:{transfer_params.to_pubkey}') # to print(f'amount:{transfer_params.lamports * .000000001}') # amount # program id will be Token... if it's a transaction involving tokens if tx_instruction.program_id.__str__() == "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA": if INSTRUCTIONS_LAYOUT.parse(tx_instruction.data).instruction_type == SPLInstructionType.Transfer: transfer_params: SPLTransferParams = spl_decode_transfer(tx_instruction) print(tx["slot"]) # blockheight print(f'from:{transfer_params.source}') # from