iota_json_rpc_types/
iota_coin.rs1use 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 pub decimals: u8,
69 pub name: String,
71 pub symbol: String,
73 pub description: String,
75 pub icon_url: Option<String>,
77 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 Ok(metadata.into())
86 }
87}
88
89impl From<CoinMetadata> for IotaCoinMetadata {
90 fn from(metadata: CoinMetadata) -> Self {
91 let CoinMetadata {
92 decimals,
93 name,
94 symbol,
95 description,
96 icon_url,
97 id,
98 } = metadata;
99 Self {
100 id: Some(*id.object_id()),
101 decimals,
102 name,
103 symbol,
104 description,
105 icon_url,
106 }
107 }
108}
109
110#[serde_as]
112#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
113#[serde(rename_all = "camelCase")]
114pub struct IotaCirculatingSupply {
115 pub value: u64,
117 pub circulating_supply_percentage: f64,
120 pub at_checkpoint: CheckpointSequenceNumber,
122}