near_api_types/
storage.rs

1use crate::NearToken;
2use serde::de::{Deserialize, Deserializer};
3
4/// A type that represents the storage balance. Please note that this type is not part of the NEP-145 standard.
5/// This type provides a more detailed view of the storage balance on the contract.
6///
7/// [StorageBalanceInternal] is a [NEP-145](https://github.com/near/NEPs/blob/master/neps/nep-0145.md) standard type.
8/// This type is used internally to parse the storage balance from the contract and
9/// to convert it into the [StorageBalance] type.
10///
11/// As a storing data on-chain requires storage staking, the contracts require users to deposit NEAR to store user-rel.
12#[derive(Debug, Clone)]
13pub struct StorageBalance {
14    /// The available balance that might be used for storage.
15    ///
16    /// The user can withdraw this balance from the contract.
17    pub available: NearToken,
18    /// The total user balance on the contract for storage.
19    ///
20    /// This is a sum of the `available` and `locked` balances.
21    pub total: NearToken,
22
23    /// The storage deposit that is locked for the account
24    ///
25    /// The user can unlock some funds by removing the data from the contract.
26    /// Though, it's contract-specific on how much can be unlocked.
27    pub locked: NearToken,
28}
29
30/// Used internally to parse the storage balance from the contract and
31/// to convert it into the [StorageBalance] type.
32///
33/// This type is a part of the [NEP-145](https://github.com/near/NEPs/blob/master/neps/nep-0145.md) standard.
34#[derive(Debug, Clone, serde::Deserialize)]
35pub struct StorageBalanceInternal {
36    #[serde(deserialize_with = "parse_u128_string")]
37    pub available: NearToken,
38    #[serde(deserialize_with = "parse_u128_string")]
39    pub total: NearToken,
40}
41
42fn parse_u128_string<'de, D>(deserializer: D) -> Result<NearToken, D::Error>
43where
44    D: Deserializer<'de>,
45{
46    <std::string::String as Deserialize>::deserialize(deserializer)?
47        .parse::<u128>()
48        .map(NearToken::from_yoctonear)
49        .map_err(serde::de::Error::custom)
50}