iota_json_rpc_types/
iota_coin.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use iota_types::{
6    base_types::{ObjectDigest, ObjectID, ObjectRef, SequenceNumber, TransactionDigest},
7    coin::CoinMetadata,
8    error::IotaError,
9    iota_serde::{BigInt, SequenceNumber as AsSequenceNumber},
10    messages_checkpoint::CheckpointSequenceNumber,
11    object::Object,
12};
13use schemars::JsonSchema;
14use serde::{Deserialize, Serialize};
15use serde_with::serde_as;
16
17use crate::Page;
18
19pub type CoinPage = Page<Coin, ObjectID>;
20
21#[serde_as]
22#[derive(Serialize, Deserialize, Debug, JsonSchema, PartialEq, Eq, Clone)]
23#[serde(rename_all = "camelCase")]
24pub struct Balance {
25    pub coin_type: String,
26    pub coin_object_count: usize,
27    #[schemars(with = "BigInt<u128>")]
28    #[serde_as(as = "BigInt<u128>")]
29    pub total_balance: u128,
30}
31
32impl Balance {
33    pub fn zero(coin_type: String) -> Self {
34        Self {
35            coin_type,
36            coin_object_count: 0,
37            total_balance: 0,
38        }
39    }
40}
41
42#[serde_as]
43#[derive(Serialize, Deserialize, Debug, JsonSchema, PartialEq, Eq, Clone)]
44#[serde(rename_all = "camelCase")]
45pub struct Coin {
46    pub coin_type: String,
47    pub coin_object_id: ObjectID,
48    #[schemars(with = "AsSequenceNumber")]
49    #[serde_as(as = "AsSequenceNumber")]
50    pub version: SequenceNumber,
51    pub digest: ObjectDigest,
52    #[schemars(with = "BigInt<u64>")]
53    #[serde_as(as = "BigInt<u64>")]
54    pub balance: u64,
55    pub previous_transaction: TransactionDigest,
56}
57
58impl Coin {
59    pub fn object_ref(&self) -> ObjectRef {
60        (self.coin_object_id, self.version, self.digest)
61    }
62}
63
64#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone, PartialEq, Eq)]
65#[serde(rename_all = "camelCase")]
66pub struct IotaCoinMetadata {
67    /// Number of decimal places the coin uses.
68    pub decimals: u8,
69    /// Name for the token
70    pub name: String,
71    /// Symbol for the token
72    pub symbol: String,
73    /// Description of the token
74    pub description: String,
75    /// URL for the token logo
76    pub icon_url: Option<String>,
77    /// Object id for the CoinMetadata object
78    pub id: Option<ObjectID>,
79}
80
81impl TryFrom<Object> for IotaCoinMetadata {
82    type Error = IotaError;
83    fn try_from(object: Object) -> Result<Self, Self::Error> {
84        let metadata: CoinMetadata = object.try_into()?;
85        let CoinMetadata {
86            decimals,
87            name,
88            symbol,
89            description,
90            icon_url,
91            id,
92        } = metadata;
93        Ok(Self {
94            id: Some(*id.object_id()),
95            decimals,
96            name,
97            symbol,
98            description,
99            icon_url,
100        })
101    }
102}
103
104/// Provides a summary of the circulating IOTA supply.
105#[serde_as]
106#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
107#[serde(rename_all = "camelCase")]
108pub struct IotaCirculatingSupply {
109    /// Circulating supply in NANOS at the given timestamp.
110    pub value: u64,
111    /// Percentage of total supply that is currently circulating (range: 0.0 to
112    /// 1.0).
113    pub circulating_supply_percentage: f64,
114    /// Timestamp (UTC) when the circulating supply was calculated.
115    pub at_checkpoint: CheckpointSequenceNumber,
116}