Skip to main content

aptos_sdk/types/
resources.rs

1//! Common resource types from the Aptos framework.
2//!
3//! These types represent the most commonly accessed on-chain resources.
4
5use crate::types::AccountAddress;
6use crate::types::events::EventHandle;
7use serde::{Deserialize, Serialize};
8
9/// The account resource stored at every account address.
10///
11/// This resource contains basic account information including
12/// the sequence number used for replay protection.
13#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
14pub struct AccountResource {
15    /// The sequence number of the next transaction to be submitted.
16    pub sequence_number: u64,
17    /// The authentication key (derived from public key).
18    pub authentication_key: String,
19}
20
21impl AccountResource {
22    /// The type string for this resource.
23    pub const TYPE: &'static str = "0x1::account::Account";
24}
25
26/// A coin store resource that holds a specific coin type.
27///
28/// This is the generic structure; use `CoinStore<AptosCoin>` for APT.
29#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
30pub struct CoinStoreResource {
31    /// The current balance.
32    pub coin: CoinInfo,
33    /// Whether deposits are frozen.
34    pub frozen: bool,
35    /// Event handle for deposit events.
36    pub deposit_events: EventHandle,
37    /// Event handle for withdraw events.
38    pub withdraw_events: EventHandle,
39}
40
41/// Coin information containing the balance.
42#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
43pub struct CoinInfo {
44    /// The coin value/balance.
45    pub value: u64,
46}
47
48impl CoinStoreResource {
49    /// Returns the coin balance.
50    pub fn balance(&self) -> u64 {
51        self.coin.value
52    }
53}
54
55/// The APT coin store type string.
56#[allow(dead_code)]
57pub const APT_COIN_STORE_TYPE: &str = "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>";
58
59/// Fungible asset balance (for the new fungible asset standard).
60#[allow(dead_code)] // Public API for users
61#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
62pub struct FungibleAssetBalance {
63    /// The balance amount.
64    pub balance: u64,
65    /// Whether the balance is frozen.
66    pub frozen: bool,
67}
68
69/// Fungible asset metadata.
70#[allow(dead_code)] // Public API for users
71#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
72pub struct FungibleAssetMetadata {
73    /// The name of the asset.
74    pub name: String,
75    /// The symbol of the asset.
76    pub symbol: String,
77    /// The number of decimals.
78    pub decimals: u8,
79}
80
81/// Collection data for NFTs (v2).
82#[allow(dead_code)] // Public API for users
83#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
84pub struct CollectionData {
85    /// The name of the collection.
86    pub name: String,
87    /// The description of the collection.
88    pub description: String,
89    /// The URI for collection metadata.
90    pub uri: String,
91    /// The current supply.
92    pub current_supply: u64,
93    /// The maximum supply (0 for unlimited).
94    pub maximum_supply: u64,
95}
96
97/// Token data for NFTs (v2).
98#[allow(dead_code)] // Public API for users
99#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
100pub struct TokenData {
101    /// The name of the token.
102    pub name: String,
103    /// The description of the token.
104    pub description: String,
105    /// The URI for token metadata.
106    pub uri: String,
107    /// The collection this token belongs to.
108    pub collection: AccountAddress,
109}
110
111/// Stake pool resource.
112#[allow(dead_code)] // Public API for users
113#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
114pub struct StakePool {
115    /// Active stake amount.
116    pub active: u64,
117    /// Inactive stake amount.
118    pub inactive: u64,
119    /// Pending active stake.
120    pub pending_active: u64,
121    /// Pending inactive stake.
122    pub pending_inactive: u64,
123    /// The operator address.
124    pub operator_address: AccountAddress,
125    /// The delegated voter address.
126    pub delegated_voter: AccountAddress,
127}
128
129/// Staking config resource.
130#[allow(dead_code)] // Public API for users
131#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
132pub struct StakingConfig {
133    /// Minimum stake required.
134    pub minimum_stake: u64,
135    /// Maximum stake allowed.
136    pub maximum_stake: u64,
137    /// Recurring lockup duration in seconds.
138    pub recurring_lockup_duration_secs: u64,
139    /// Whether rewards are enabled.
140    pub rewards_rate: u64,
141    /// The rewards rate denominator.
142    pub rewards_rate_denominator: u64,
143}
144
145#[cfg(test)]
146mod tests {
147    use super::*;
148
149    #[test]
150    fn test_account_resource_type() {
151        assert_eq!(AccountResource::TYPE, "0x1::account::Account");
152    }
153
154    #[test]
155    fn test_coin_store_balance() {
156        let coin_store = CoinStoreResource {
157            coin: CoinInfo { value: 1000 },
158            frozen: false,
159            deposit_events: EventHandle {
160                counter: 0,
161                guid: crate::types::events::EventGuid {
162                    creation_number: 0,
163                    account_address: AccountAddress::ONE,
164                },
165            },
166            withdraw_events: EventHandle {
167                counter: 0,
168                guid: crate::types::events::EventGuid {
169                    creation_number: 1,
170                    account_address: AccountAddress::ONE,
171                },
172            },
173        };
174        assert_eq!(coin_store.balance(), 1000);
175    }
176}