devol_accounts_kit/accounts/mints/
mint.rs

1use solana_program::pubkey::Pubkey;
2
3pub const MINT_ADDRESS_OFFSET: usize = 0;
4pub const MINT_PROGRAM_ADDRESS_OFFSET: usize = 32;
5pub const MINT_LOG_ADDRESS_OFFSET: usize = 64;
6pub const MINT_OPS_COUNTER_OFFSET: usize = 96;
7pub const MINT_TICKER_OFFSET: usize = 104;
8pub const MINT_OWN_OFFSET: usize = 112;
9pub const MINT_CLIENT_OFFSET: usize = 120;
10pub const MINT_SIZE: usize = 128;
11
12#[derive(Clone, Copy)]
13#[repr(C)]
14pub struct Mint {
15    // 32 bytes, MINT_ADDRESS_OFFSET
16    pub address: Pubkey,
17    // 32 bytes, MINT_PROGRAM_ADDRESS_OFFSET
18    pub program_address: Pubkey,
19    // 32 bytes, MINT_LOG_ADDRESS_OFFSET
20    pub log_address: Pubkey,
21    // 8 bytes, MINT_OPS_COUNTER_OFFSET
22    pub ops_counter: [u8; 8],
23    // 8 bytes, MINT_TICKER_OFFSET
24    pub ticker: [u8; 8],
25    // 8 bytes, MINT_OWN_OFFSET
26    pub own: [u8; 8],
27    // 8 bytes, MINT_CLIENT_OFFSET
28    pub client: [u8; 8],
29}
30
31impl Mint {
32    #[inline(always)]
33    pub fn get_ops_counter(&self) -> i64 { i64::from_ne_bytes(self.ops_counter) }
34    #[inline(always)]
35    pub fn set_ops_counter(&mut self, value: i64) { self.ops_counter = value.to_ne_bytes() }
36    #[inline(always)]
37    pub fn get_ticker(&self) -> i64 { i64::from_ne_bytes(self.ticker) }
38    #[inline(always)]
39    pub fn set_ticker(&mut self, value: i64) {
40        self.ticker = value.to_ne_bytes() }
41    #[inline(always)]
42    pub fn get_own(&self) -> i64 { i64::from_ne_bytes(self.own) }
43    #[inline(always)]
44    pub fn set_own(&mut self, value: i64) { self.own = value.to_ne_bytes() }
45    #[inline(always)]
46    pub fn get_client(&self) -> i64 { i64::from_ne_bytes(self.client) }
47    #[inline(always)]
48    pub fn set_client(&mut self, value: i64) { self.client = value.to_ne_bytes() }
49}
50
51#[cfg(test)]
52impl Default for Mint {
53    fn default() -> Self {
54        Self {
55            address: Pubkey::default(),
56            program_address: Pubkey::default(),
57            log_address: Pubkey::default(),
58            ops_counter: [0; 8],
59            ticker: [0; 8],
60            own: [0; 8],
61            client: [0; 8],
62        }
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69    use std::mem;
70
71    #[test]
72    fn test_root_account_offsets() {
73        let account = Mint::default();
74
75        let base_ptr = &account as *const _ as usize;
76
77        // checking fields size and offset
78        assert_eq!(&account.address as *const _ as usize - base_ptr, MINT_ADDRESS_OFFSET);
79        assert_eq!(&account.program_address as *const _ as usize - base_ptr, MINT_PROGRAM_ADDRESS_OFFSET);
80        assert_eq!(&account.log_address as *const _ as usize - base_ptr, MINT_LOG_ADDRESS_OFFSET);
81        assert_eq!(&account.ops_counter as *const _ as usize - base_ptr, MINT_OPS_COUNTER_OFFSET);
82        assert_eq!(&account.ticker as *const _ as usize - base_ptr, MINT_TICKER_OFFSET);
83        assert_eq!(&account.own as *const _ as usize - base_ptr, MINT_OWN_OFFSET);
84        assert_eq!(&account.client as *const _ as usize - base_ptr, MINT_CLIENT_OFFSET);
85
86        // checking total size
87        assert_eq!(mem::size_of::<Mint>(), MINT_SIZE);
88    }
89}