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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use switchboard_solana::anchor_lang::prelude::*;

#[derive(Default, Clone, Debug, AnchorDeserialize, AnchorSerialize)]
pub struct TransactionOptions {
    pub compute_units: Option<u32>,
    pub compute_unit_price: Option<u64>,
}
impl TransactionOptions {
    pub const DEFAULT_COMPUTE_UNITS: u32 = 1_000_000;
    pub const DEFAULT_COMPUTE_UNIT_PRICE: u64 = 1;

    pub fn get_compute_units(&self) -> u32 {
        std::cmp::max(
            std::cmp::min(
                self.compute_units.unwrap_or(Self::DEFAULT_COMPUTE_UNITS),
                1_400_000,
            ),
            200_000,
        )
    }

    pub fn get_compute_unit_price(&self) -> u64 {
        std::cmp::max(
            1,
            std::cmp::min(
                1000,
                self.compute_unit_price
                    .unwrap_or(Self::DEFAULT_COMPUTE_UNIT_PRICE),
            ),
        )
    }

    pub fn get_priority_fee_lamports(&self) -> u64 {
        // 1_000_000 compute units * 1 micro_lamports per compute unit
        // 1 micro_lamports per compute unit * 1_000_000 compute units = 1_000_000 micro_lamports
        // 1_000_000 micro_lamports / 1_000_000 micro_lamports per lamport = 1 lamport

        (u64::from(self.get_compute_units()) * self.get_compute_unit_price()) / 1_000_000
    }
}

#[derive(Clone, Debug, Default, AnchorSerialize, AnchorDeserialize, InitSpace)]
pub struct AccountMetaBorsh {
    pub pubkey: Pubkey,
    pub is_signer: bool,
    pub is_writable: bool,
}
impl From<AccountMeta> for AccountMetaBorsh {
    fn from(value: AccountMeta) -> Self {
        Self {
            pubkey: value.pubkey,
            is_signer: value.is_signer,
            is_writable: value.is_writable,
        }
    }
}

#[derive(Clone, Debug, Default, AnchorSerialize, AnchorDeserialize, InitSpace)]
pub struct Callback {
    pub program_id: Pubkey,
    #[max_len(32)]
    pub accounts: Vec<AccountMetaBorsh>,
    #[max_len(1024)]
    pub ix_data: Vec<u8>,
}

impl From<AccountMetaBorsh> for AccountMeta {
    fn from(val: AccountMetaBorsh) -> Self {
        AccountMeta {
            pubkey: val.pubkey,
            is_signer: val.is_signer,
            is_writable: val.is_writable,
        }
    }
}
impl From<&AccountMetaBorsh> for AccountMeta {
    fn from(val: &AccountMetaBorsh) -> Self {
        AccountMeta {
            pubkey: val.pubkey,
            is_signer: val.is_signer,
            is_writable: val.is_writable,
        }
    }
}