tape_api/
pda.rs

1use steel::*;
2use crate::consts::*;
3
4pub fn archive_pda() -> (Pubkey, u8) {
5    Pubkey::find_program_address(&[ARCHIVE], &crate::id())
6}
7
8pub fn epoch_pda() -> (Pubkey, u8) {
9    Pubkey::find_program_address(&[EPOCH], &crate::id())
10}
11
12pub fn treasury_pda() -> (Pubkey, u8) {
13    Pubkey::find_program_address(&[TREASURY], &crate::id())
14}
15
16pub fn treasury_ata() -> (Pubkey, u8) {
17    let (treasury_pda, _bump) = treasury_pda();
18    let (mint_pda, _bump) = mint_pda();
19
20    Pubkey::find_program_address(
21        &[
22            treasury_pda.as_ref(), 
23            spl_token::ID.as_ref(),
24            mint_pda.as_ref()
25        ],
26        &spl_associated_token_account::ID,
27    )
28}
29
30pub fn mint_pda() -> (Pubkey, u8) {
31    Pubkey::find_program_address(&[MINT, MINT_SEED], &crate::id())
32}
33
34pub fn metadata_pda(mint: Pubkey) -> (Pubkey, u8) {
35    Pubkey::find_program_address(
36        &[ METADATA, mpl_token_metadata::ID.as_ref(), mint.as_ref() ],
37        &mpl_token_metadata::ID,
38    )
39}
40
41pub fn spool_pda(id: u8) -> (Pubkey, u8) {
42    Pubkey::find_program_address(&[SPOOL, &[id]], &crate::id())
43}
44
45pub fn tape_pda(authority: Pubkey, name: &[u8; MAX_NAME_LEN]) -> (Pubkey, u8) {
46    Pubkey::find_program_address(&[TAPE, authority.as_ref(), name.as_ref()], &crate::id())
47}
48
49pub fn writer_pda(tape: Pubkey) -> (Pubkey, u8) {
50    Pubkey::find_program_address(&[WRITER, tape.as_ref()], &crate::id())
51}
52
53pub fn miner_pda(authority: Pubkey, name: [u8; MAX_NAME_LEN]) -> (Pubkey, u8) {
54    Pubkey::find_program_address(&[MINER, authority.as_ref(), name.as_ref()], &crate::id())
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_pda_against_consts() {
63        let (pda, bump) = archive_pda();
64        assert_eq!(bump, ARCHIVE_BUMP);
65        assert_eq!(pda, ARCHIVE_ADDRESS);
66
67        let (pda, bump) = epoch_pda();
68        assert_eq!(bump, EPOCH_BUMP);
69        assert_eq!(pda, EPOCH_ADDRESS);
70
71        let (pda, bump) = mint_pda();
72        assert_eq!(bump, MINT_BUMP);
73        assert_eq!(pda, MINT_ADDRESS);
74
75        let (pda, bump) = treasury_pda();
76        assert_eq!(bump, TREASURY_BUMP);
77        assert_eq!(pda, TREASURY_ADDRESS);
78
79        let (pda, _bump) = treasury_ata();
80        assert_eq!(pda, TREASURY_ATA);
81    }
82}