light_token_interface/state/mint/
borsh.rs

1use borsh::{BorshDeserialize, BorshSerialize};
2use light_compressed_account::Pubkey;
3
4use super::compressed_mint::BaseMint;
5
6// Manual implementation of BorshSerialize for SPL compatibility
7impl BorshSerialize for BaseMint {
8    fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
9        // Write mint_authority as COption (4 bytes + 32 bytes)
10        if let Some(authority) = self.mint_authority {
11            writer.write_all(&[1, 0, 0, 0])?; // COption Some discriminator
12            writer.write_all(&authority.to_bytes())?;
13        } else {
14            writer.write_all(&[0; 36])?; // COption None (4 bytes) + empty pubkey (32 bytes)
15        }
16
17        // Write supply (8 bytes)
18        writer.write_all(&self.supply.to_le_bytes())?;
19
20        // Write decimals (1 byte)
21        writer.write_all(&[self.decimals])?;
22
23        // Write is_initialized (1 byte)
24        writer.write_all(&[if self.is_initialized { 1 } else { 0 }])?;
25
26        // Write freeze_authority as COption (4 bytes + 32 bytes)
27        if let Some(authority) = self.freeze_authority {
28            writer.write_all(&[1, 0, 0, 0])?; // COption Some discriminator
29            writer.write_all(&authority.to_bytes())?;
30        } else {
31            writer.write_all(&[0; 36])?; // COption None (4 bytes) + empty pubkey (32 bytes)
32        }
33
34        Ok(())
35    }
36}
37
38// Manual implementation of BorshDeserialize for SPL compatibility
39impl BorshDeserialize for BaseMint {
40    fn deserialize_reader<R: std::io::Read>(buf: &mut R) -> std::io::Result<Self> {
41        // Read mint_authority COption
42        let mut discriminator = [0u8; 4];
43        buf.read_exact(&mut discriminator)?;
44        let mut pubkey_bytes = [0u8; 32];
45        buf.read_exact(&mut pubkey_bytes)?;
46        let mint_authority = if u32::from_le_bytes(discriminator) == 1 {
47            Some(Pubkey::from(pubkey_bytes))
48        } else {
49            None
50        };
51
52        // Read supply
53        let mut supply_bytes = [0u8; 8];
54        buf.read_exact(&mut supply_bytes)?;
55        let supply = u64::from_le_bytes(supply_bytes);
56
57        // Read decimals
58        let mut decimals = [0u8; 1];
59        buf.read_exact(&mut decimals)?;
60        let decimals = decimals[0];
61
62        // Read is_initialized
63        let mut is_initialized = [0u8; 1];
64        buf.read_exact(&mut is_initialized)?;
65        let is_initialized = is_initialized[0] != 0;
66
67        // Read freeze_authority COption
68        let mut discriminator = [0u8; 4];
69        buf.read_exact(&mut discriminator)?;
70        let mut pubkey_bytes = [0u8; 32];
71        buf.read_exact(&mut pubkey_bytes)?;
72        let freeze_authority = if u32::from_le_bytes(discriminator) == 1 {
73            Some(Pubkey::from(pubkey_bytes))
74        } else {
75            None
76        };
77
78        Ok(Self {
79            mint_authority,
80            supply,
81            decimals,
82            is_initialized,
83            freeze_authority,
84        })
85    }
86}