1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#![cfg(feature = "display")]

use {crate::client::RpcClientResponse, solana_cli_output::display::writeln_transaction, std::fmt};

impl fmt::Display for RpcClientResponse {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            RpcClientResponse::Signature(signature) => writeln!(f, "Signature: {}", signature),
            RpcClientResponse::Transaction(transaction) => {
                writeln!(f, "Transaction:")?;
                writeln_transaction(f, &transaction.clone().into(), None, "  ", None, None)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use {
        super::*,
        solana_sdk::{
            hash::Hash,
            pubkey::Pubkey,
            signature::{Signature, Signer, SIGNATURE_BYTES},
            signer::keypair::Keypair,
            system_instruction,
            transaction::Transaction,
        },
    };

    #[test]
    fn display_signature() {
        let signature_bytes = [202u8; SIGNATURE_BYTES];
        let signature = RpcClientResponse::Signature(Signature::new(&signature_bytes));
        println!("{}", signature);
    }

    #[test]
    fn display_transaction() {
        let payer = Keypair::new();
        let transaction = Transaction::new_signed_with_payer(
            &[system_instruction::transfer(
                &payer.pubkey(),
                &Pubkey::new_unique(),
                10,
            )],
            Some(&payer.pubkey()),
            &[&payer],
            Hash::default(),
        );
        let transaction = RpcClientResponse::Transaction(transaction);
        println!("{}", transaction);
    }
}