Skip to main content

pinocchio_token/
lib.rs

1#![no_std]
2
3#[cfg(feature = "alloc")]
4extern crate alloc;
5use {
6    solana_address::Address,
7    solana_program_error::{ProgramError, ProgramResult},
8};
9
10pub mod instructions;
11pub mod state;
12
13solana_address::declare_id!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
14
15/// The address of the SPL Token-2022 program.
16const TOKEN_2022: Address = Address::from_str_const("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb");
17
18/// A trait for token programs that can be used in a CPI with a statically known
19/// program address.
20pub trait TokenInterface {
21    const ID: Address;
22
23    /// Returns `Ok(())` when `address` is accepted for cross-program
24    /// invocations.
25    ///
26    /// Instructions may accept addresses other than `Self::ID` when a
27    /// compatible program can process the same instruction layout.
28    #[inline(always)]
29    fn verify(address: &Address) -> ProgramResult {
30        if address != &Self::ID {
31            return Err(incorrect_program_id());
32        }
33
34        Ok(())
35    }
36}
37
38/// Struct to represent the SPL Token program.
39///
40/// This struct implements the `TokenProgram` trait, which statically provides
41/// the SPL Token address for instruction building.
42pub struct TokenProgram;
43
44impl TokenInterface for TokenProgram {
45    const ID: Address = crate::ID;
46
47    /// Returns `Ok(())` when `address` is accepted for cross-program
48    /// invocations.
49    ///
50    /// This implementation accepts both SPL Token and the SPL Token-2022
51    /// programs.
52    #[inline(always)]
53    fn verify(address: &Address) -> ProgramResult {
54        if address != &Self::ID && address != &TOKEN_2022 {
55            return Err(incorrect_program_id());
56        }
57
58        Ok(())
59    }
60}
61
62/// Cold helper for constructing `ProgramError::IncorrectProgramId` outside the
63/// hot path.
64#[doc(hidden)]
65#[cold]
66fn incorrect_program_id() -> ProgramError {
67    ProgramError::IncorrectProgramId
68}