Skip to main content

shadow_drive_sdk/models/
storage_acct.rs

1use std::str::FromStr;
2
3use anchor_lang::prelude::Pubkey;
4use serde::{Deserialize, Deserializer};
5#[derive(Clone, Debug, Deserialize)]
6pub struct StorageAccount {
7    #[serde(deserialize_with = "deserialize_pubkey")]
8    pub storage_account: Pubkey,
9
10    /// Number of bytes of storage associated with this account
11    pub reserved_bytes: u64,
12
13    /// Bytes in use
14    pub current_usage: u64,
15
16    /// Boolean to track whether storage account (and all child File accounts) are immutable
17    pub immutable: bool,
18
19    /// Delete flag
20    pub to_be_deleted: bool,
21
22    /// Delete request epoch
23    pub delete_request_epoch: u32,
24
25    /// Primary owner of StorageAccount (immutable)
26    #[serde(alias = "owner1", deserialize_with = "deserialize_pubkey")]
27    pub owner_1: Pubkey,
28
29    /// Optional owner 2
30    #[serde(alias = "owner2", deserialize_with = "deserialize_pubkey")]
31    pub owner_2: Pubkey,
32
33    /// Counter at time of initialization
34    pub account_counter_seed: u32,
35
36    /// Time of storage account creation
37    pub creation_time: u32,
38
39    /// Time of storage account creation
40    pub creation_epoch: u32,
41
42    /// The last epoch through which the user paid
43    pub last_fee_epoch: u32,
44
45    /// Some unique identifier that the user provides.
46    /// Serves as a seed for storage account PDA.
47    pub identifier: String,
48}
49
50// Copied from shadow-drive-user-staking crate to add JSON deserialization
51#[derive(Clone, Debug, Deserialize)]
52pub struct StorageAccountV2 {
53    #[serde(deserialize_with = "deserialize_pubkey")]
54    pub storage_account: Pubkey,
55
56    /// Number of bytes of storage associated with this account
57    pub reserved_bytes: u64,
58
59    /// Bytes in use
60    pub current_usage: u64,
61
62    /// Boolean to track whether storage account (and all child File accounts) are immutable
63    pub immutable: bool,
64
65    /// Delete flag
66    pub to_be_deleted: bool,
67
68    /// Delete request epoch
69    pub delete_request_epoch: u32,
70
71    /// Primary owner of StorageAccount (immutable)
72    #[serde(alias = "owner1", deserialize_with = "deserialize_pubkey")]
73    pub owner_1: Pubkey,
74
75    /// Counter at time of initialization
76    pub account_counter_seed: u32,
77
78    /// Time of storage account creation
79    pub creation_time: u32,
80
81    /// Time of storage account creation
82    pub creation_epoch: u32,
83
84    /// The last epoch through which the user paid
85    pub last_fee_epoch: u32,
86
87    /// Some unique identifier that the user provides.
88    /// Serves as a seed for storage account PDA.
89    pub identifier: String,
90}
91
92fn deserialize_pubkey<'de, D>(deserializer: D) -> Result<Pubkey, D::Error>
93where
94    D: Deserializer<'de>,
95{
96    let s = String::deserialize(deserializer)?;
97    Pubkey::from_str(&s).map_err(serde::de::Error::custom)
98}
99
100#[derive(Debug, Deserialize)]
101#[serde(tag = "version")]
102pub enum StorageAcct {
103    V1(StorageAccount),
104    V2(StorageAccountV2),
105}
106
107impl StorageAcct {
108    pub fn is_immutable(&self) -> bool {
109        match self {
110            StorageAcct::V1(acct) => acct.immutable,
111            StorageAcct::V2(acct) => acct.immutable,
112        }
113    }
114    pub fn storage(&self) -> u64 {
115        match self {
116            StorageAcct::V1(acct) => acct.reserved_bytes,
117            StorageAcct::V2(acct) => acct.reserved_bytes,
118        }
119    }
120
121    pub fn to_be_deleted(&self) -> bool {
122        match self {
123            StorageAcct::V1(acct) => acct.to_be_deleted,
124            StorageAcct::V2(acct) => acct.to_be_deleted,
125        }
126    }
127
128    pub fn is_owner(&self, account: Pubkey) -> bool {
129        match self {
130            StorageAcct::V1(acct) => (acct.owner_1 == account) | (acct.owner_2 == account),
131            StorageAcct::V2(acct) => acct.owner_1 == account,
132        }
133    }
134}