rush_ecs_svm/pda/
world_pda.rs

1use solana_program::pubkey::Pubkey;
2
3/// Finds the [`WorldPDA`] PDA with canonical bump
4///
5/// - Used for validating World seeds
6/// - Used for identifying specific World account onchain
7pub struct WorldPDA {}
8
9impl WorldPDA {
10    /// Tag seed for differentiating state
11    pub const TAG: &'static str = "World";
12
13    /// Find PDA for World State
14    ///
15    /// Also searches for canonical Bump Seed,
16    /// hence is very expensive.
17    ///
18    /// If Bump Seed is available,
19    /// use [`Self::create_pda`] instead
20    ///
21    /// Returns (PDA, Bump Seed)
22    pub fn find_pda(program_id: &Pubkey, name: &str, description: &str) -> (Pubkey, u8) {
23        Pubkey::find_program_address(
24            &[
25                Self::TAG.as_bytes(),
26                name.as_bytes(),
27                description.as_bytes(),
28            ],
29            program_id,
30        )
31    }
32
33    /// Create PDA for World State
34    ///
35    /// Doesn't search for canonical Bump Seed.
36    ///
37    /// Cheaper and encouraged to use if Bump
38    /// Seed is available.
39    ///
40    /// Returns PDA
41    pub fn create_pda(
42        program_id: &Pubkey,
43        name: &str,
44        description: &str,
45        world_authority: &Pubkey,
46        bump_seed: u8,
47    ) -> Pubkey {
48        // expects a valid set of seeds
49        Pubkey::create_program_address(
50            &[
51                Self::TAG.as_bytes(),
52                name.as_bytes(),
53                description.as_bytes(),
54                world_authority.as_ref(),
55                &[bump_seed],
56            ],
57            program_id,
58        )
59        .expect("Invalid seeds")
60    }
61}