gmsol_treasury/states/
config.rs

1use anchor_lang::prelude::*;
2use gmsol_store::{states::Seed, utils::pubkey::optional_address, CoreError};
3use gmsol_utils::InitSpace;
4
5use crate::constants;
6
7/// Treasury config account.
8#[account(zero_copy)]
9#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
10pub struct Config {
11    version: u8,
12    pub(crate) bump: u8,
13    pub(crate) receiver_bump: u8,
14    #[cfg_attr(feature = "debug", debug(skip))]
15    padding_0: [u8; 13],
16    pub(crate) store: Pubkey,
17    treasury_vault_config: Pubkey,
18    gt_factor: u128,
19    buyback_factor: u128,
20    #[cfg_attr(feature = "debug", debug(skip))]
21    reserved: [u8; 256],
22}
23
24impl Seed for Config {
25    const SEED: &'static [u8] = b"config";
26}
27
28impl InitSpace for Config {
29    const INIT_SPACE: usize = std::mem::size_of::<Self>();
30}
31
32impl Config {
33    pub(crate) fn init(&mut self, bump: u8, receiver_bump: u8, store: &Pubkey) {
34        self.bump = bump;
35        self.receiver_bump = receiver_bump;
36        self.store = *store;
37    }
38
39    /// Get the treasury vault config address.
40    pub fn treasury_vault_config(&self) -> Option<&Pubkey> {
41        optional_address(&self.treasury_vault_config)
42    }
43
44    /// Set the treasury vault config address.
45    pub(crate) fn set_treasury_vault_config(&mut self, mut address: Pubkey) -> Result<Pubkey> {
46        require_keys_neq!(
47            self.treasury_vault_config,
48            address,
49            CoreError::PreconditionsAreNotMet
50        );
51
52        std::mem::swap(&mut address, &mut self.treasury_vault_config);
53
54        Ok(address)
55    }
56
57    /// Get GT factor.
58    pub fn gt_factor(&self) -> u128 {
59        self.gt_factor
60    }
61
62    /// Set GT factor.
63    pub(crate) fn set_gt_factor(&mut self, mut factor: u128) -> Result<u128> {
64        require_gte!(
65            gmsol_store::constants::MARKET_USD_UNIT,
66            factor,
67            CoreError::InvalidArgument
68        );
69        require_neq!(self.gt_factor, factor, CoreError::PreconditionsAreNotMet);
70        std::mem::swap(&mut self.gt_factor, &mut factor);
71        Ok(factor)
72    }
73
74    /// Get buyback factor.
75    pub fn buyback_factor(&self) -> u128 {
76        self.buyback_factor
77    }
78
79    /// Set buyback factor.
80    pub(crate) fn set_buyback_factor(&mut self, mut factor: u128) -> Result<u128> {
81        require_gte!(
82            gmsol_store::constants::MARKET_USD_UNIT,
83            factor,
84            CoreError::InvalidArgument
85        );
86        require_neq!(
87            self.buyback_factor,
88            factor,
89            CoreError::PreconditionsAreNotMet
90        );
91        std::mem::swap(&mut self.buyback_factor, &mut factor);
92        Ok(factor)
93    }
94
95    /// Get signer.
96    pub(crate) fn signer(&self) -> ConfigSigner {
97        ConfigSigner {
98            store: self.store,
99            bump_bytes: [self.bump],
100        }
101    }
102}
103
104/// Config Signer.
105pub struct ConfigSigner {
106    store: Pubkey,
107    bump_bytes: [u8; 1],
108}
109
110impl ConfigSigner {
111    /// As signer seeds.
112    pub fn as_seeds(&self) -> [&[u8]; 3] {
113        [Config::SEED, self.store.as_ref(), &self.bump_bytes]
114    }
115}
116
117/// Receiver Signer.
118pub struct ReceiverSigner {
119    config: Pubkey,
120    bump_bytes: [u8; 1],
121}
122
123impl ReceiverSigner {
124    /// Create from config address and bump.
125    pub fn new(config: Pubkey, bump: u8) -> Self {
126        Self {
127            config,
128            bump_bytes: [bump],
129        }
130    }
131
132    /// As signer seeds.
133    pub fn as_seeds(&self) -> [&[u8]; 3] {
134        [
135            constants::RECEIVER_SEED,
136            self.config.as_ref(),
137            &self.bump_bytes,
138        ]
139    }
140}