spl_governance_test_sdk/
tools.rs

1use {
2    solana_program::{instruction::InstructionError, program_error::ProgramError},
3    solana_sdk::{signature::Keypair, transaction::TransactionError, transport::TransportError},
4    std::convert::TryFrom,
5};
6
7/// TODO: Add to Solana SDK
8/// Instruction errors not mapped in the sdk
9pub enum ProgramInstructionError {
10    /// Incorrect authority provided
11    IncorrectAuthority = 600,
12
13    /// Cross-program invocation with unauthorized signer or writable account
14    PrivilegeEscalation,
15}
16
17impl From<ProgramInstructionError> for ProgramError {
18    fn from(e: ProgramInstructionError) -> Self {
19        ProgramError::Custom(e as u32)
20    }
21}
22
23pub fn map_transaction_error(transport_error: TransportError) -> ProgramError {
24    match transport_error {
25        TransportError::TransactionError(TransactionError::InstructionError(
26            _,
27            InstructionError::Custom(error_index),
28        )) => ProgramError::Custom(error_index),
29        TransportError::TransactionError(TransactionError::InstructionError(
30            _,
31            instruction_error,
32        )) => ProgramError::try_from(instruction_error).unwrap_or_else(|ie| match ie {
33            InstructionError::IncorrectAuthority => {
34                ProgramInstructionError::IncorrectAuthority.into()
35            }
36            InstructionError::PrivilegeEscalation => {
37                ProgramInstructionError::PrivilegeEscalation.into()
38            }
39            _ => panic!("TEST-INSTRUCTION-ERROR {:?}", ie),
40        }),
41
42        _ => panic!("TEST-TRANSPORT-ERROR: {:?}", transport_error),
43    }
44}
45
46pub fn clone_keypair(source: &Keypair) -> Keypair {
47    Keypair::from_bytes(&source.to_bytes()).unwrap()
48}
49
50/// NOP (No Operation) Override function
51#[allow(non_snake_case)]
52pub fn NopOverride<T>(_: &mut T) {}