Skip to main content

hiero_sdk/token/
token_type.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use hiero_sdk_proto::services;
4
5use crate::{
6    FromProtobuf,
7    ToProtobuf,
8};
9
10/// Possible token types.
11///
12/// Apart from fungible and non-fungible, tokens can have either a common or
13/// unique representation.
14///
15/// Only `FungibleCommon` and `NonFungibleUnique` are supported right now. More
16/// may be added in the future.
17///
18#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
19#[repr(C)]
20pub enum TokenType {
21    /// Interchangeable value with one another, where any quantity of them has the same value as
22    /// another equal quantity if they are in the same class.  Share a single set of properties, not
23    /// distinct from one another. Simply represented as a balance or quantity to a given Hiero
24    /// account.
25    FungibleCommon = 0,
26
27    /// Unique, not interchangeable with other tokens of the same type as they typically have
28    /// different values.  Individually traced and can carry unique properties (e.g. serial number).
29    NonFungibleUnique = 1,
30}
31
32impl FromProtobuf<services::TokenType> for TokenType {
33    fn from_protobuf(pb: services::TokenType) -> crate::Result<Self> {
34        Ok(match pb {
35            services::TokenType::FungibleCommon => Self::FungibleCommon,
36            services::TokenType::NonFungibleUnique => Self::NonFungibleUnique,
37        })
38    }
39}
40
41impl ToProtobuf for TokenType {
42    type Protobuf = services::TokenType;
43
44    fn to_protobuf(&self) -> Self::Protobuf {
45        match self {
46            Self::FungibleCommon => Self::Protobuf::FungibleCommon,
47            Self::NonFungibleUnique => Self::Protobuf::NonFungibleUnique,
48        }
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use hiero_sdk_proto::services;
55
56    use crate::token::token_type::TokenType;
57    use crate::{
58        FromProtobuf,
59        ToProtobuf,
60    };
61
62    #[test]
63    fn it_can_convert_to_protobuf() -> anyhow::Result<()> {
64        let nft_token_type = TokenType::NonFungibleUnique;
65        let fungible_token_type = TokenType::FungibleCommon;
66
67        let nft_protobuf = nft_token_type.to_protobuf();
68        let fungible_protobuf = fungible_token_type.to_protobuf();
69
70        assert_eq!(nft_protobuf, services::TokenType::NonFungibleUnique);
71        assert_eq!(fungible_protobuf, services::TokenType::FungibleCommon);
72
73        Ok(())
74    }
75
76    #[test]
77    fn it_can_be_created_from_protobuf() -> anyhow::Result<()> {
78        let nft_protobuf = services::TokenType::NonFungibleUnique;
79        let fungible_protobuf = services::TokenType::FungibleCommon;
80
81        let nft_token_type = TokenType::from_protobuf(nft_protobuf)?;
82        let fungible_token_type = TokenType::from_protobuf(fungible_protobuf)?;
83
84        assert_eq!(nft_token_type, TokenType::NonFungibleUnique);
85        assert_eq!(fungible_token_type, TokenType::FungibleCommon);
86
87        Ok(())
88    }
89}