1

I have a code

fn main() { let rpc_url = String::from("https://api.mainnet-beta.solana.com/"); let client = RpcClient::new(rpc_url);

let pk = Pubkey::from_str("adresshere").unwrap();

let signatures = client.get_signatures_for_address(&pk).unwrap();

if let Some(last_signature) = signatures.first() {

    let signature = Signature::from_str(&last_signature.signature).unwrap();
    let trx = client.get_transaction(
        &signature,
        UiTransactionEncoding::JsonParsed
    ).unwrap();

    match trx.transaction.transaction {
        EncodedTransaction::Json(json) => {
            match json.message {
                UiMessage::Parsed(message) => {
                    //println!("{:?}", &message.instructions[0]);
                    match &message.instructions[0] {
                        UiInstruction::Parsed(inst) => {
                            match inst {
                                UiParsedInstruction::Parsed(d) => {
                                    println!("{:?}", d);
                                }
                                _ => panic!()
                            }
                        }
                        _ => panic!()
                    }
                }
                _ => panic!()
            }
        }
        _ => panic!()
    }

} else {
    println!("Not found");
}

}

But I'm having trouble getting the program_id. I walk JSON response and using struct and enum-methods from solana_transaction_status I get the answers Please help

1
  • I faced with the same problem, is there any more elegant way to solve the problem?
    – yuan wang
    Commented Jul 4 at 10:10

1 Answer 1

2

You're very close! Once you get to UiParsedInstruction::Parsed(d), the program_id is right there, so you can do:

                                UiParsedInstruction::Parsed(d) => {
                                    println!("{:?}", d.program_id);
                                }

The source code for ParsedInstruction exists at https://github.com/anza-xyz/agave/blob/b0cfffb290051d8c8a42923eca1459e3bba4cef2/transaction-status/src/parse_instruction.rs#L82

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.