Skip to main content

aptos_sdk/types/
mod.rs

1//! Core Aptos types.
2//!
3//! This module contains the fundamental types used throughout the SDK,
4//! including addresses, chain IDs, type tags, and hash values.
5
6mod address;
7mod chain_id;
8mod events;
9mod hash;
10mod move_types;
11mod resources;
12
13pub use address::{ADDRESS_LENGTH, AccountAddress};
14pub use chain_id::ChainId;
15pub use events::{
16    Event, EventGuid, EventHandle, EventHandleGuid, EventHandleGuidId, EventKey, VersionedEvent,
17    framework as event_types,
18};
19pub use hash::{HASH_LENGTH, HashValue};
20pub use move_types::{
21    EntryFunctionId, Identifier, MoveModuleId, MoveResource, MoveStruct, MoveStructTag, MoveType,
22    MoveValue, StructTag, TypeTag,
23};
24pub use resources::{
25    APT_COIN_STORE_TYPE, AccountResource, CoinInfo, CoinStoreResource, CollectionData,
26    FungibleAssetBalance, FungibleAssetMetadata, StakePool, StakingConfig, TokenData,
27};
28
29/// Serde helpers for integers that the Aptos REST (fullnode) API encodes as
30/// JSON strings (e.g. `"42"`).
31///
32/// The fullnode encodes all `u64` / `u128` values as JSON strings to avoid
33/// precision loss in JSON parsers that use IEEE-754 doubles. The resource and
34/// event types in this module deserialize that shape. For convenience (and to
35/// keep round-tripping simple) these helpers also accept a plain JSON number,
36/// and always serialize back to a string so the output matches the API shape.
37pub(crate) mod string_num {
38    use serde::de::{self, Visitor};
39    use serde::{Deserializer, Serializer};
40    use std::fmt;
41    use std::marker::PhantomData;
42    use std::str::FromStr;
43
44    /// Serializes a value as its decimal string representation.
45    pub(crate) fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
46    where
47        S: Serializer,
48        T: fmt::Display,
49    {
50        serializer.collect_str(value)
51    }
52
53    /// Deserializes a value from either a JSON string or a JSON number.
54    pub(crate) fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
55    where
56        D: Deserializer<'de>,
57        T: FromStr,
58        <T as FromStr>::Err: fmt::Display,
59    {
60        deserializer.deserialize_any(NumVisitor(PhantomData))
61    }
62
63    struct NumVisitor<T>(PhantomData<T>);
64
65    impl<T> Visitor<'_> for NumVisitor<T>
66    where
67        T: FromStr,
68        <T as FromStr>::Err: fmt::Display,
69    {
70        type Value = T;
71
72        fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73            f.write_str("a string-encoded integer or an integer")
74        }
75
76        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
77        where
78            E: de::Error,
79        {
80            v.parse().map_err(de::Error::custom)
81        }
82
83        fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
84        where
85            E: de::Error,
86        {
87            v.to_string().parse().map_err(de::Error::custom)
88        }
89
90        fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
91        where
92            E: de::Error,
93        {
94            v.to_string().parse().map_err(de::Error::custom)
95        }
96
97        fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E>
98        where
99            E: de::Error,
100        {
101            v.to_string().parse().map_err(de::Error::custom)
102        }
103
104        fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E>
105        where
106            E: de::Error,
107        {
108            v.to_string().parse().map_err(de::Error::custom)
109        }
110    }
111}