iota_graphql_rpc/types/
balance_change.rs1use async_graphql::*;
6use iota_json_rpc_types::BalanceChange as StoredBalanceChange;
7use iota_types::object::Owner as NativeOwner;
8
9use crate::{
10 error::Error,
11 types::{big_int::BigInt, iota_address::IotaAddress, move_type::MoveType, owner::Owner},
12};
13
14pub(crate) struct BalanceChange {
15 stored: StoredBalanceChange,
16 checkpoint_viewed_at: u64,
18}
19
20#[Object]
23impl BalanceChange {
24 async fn owner(&self) -> Option<Owner> {
26 use NativeOwner as O;
27
28 match self.stored.owner {
29 O::AddressOwner(addr) | O::ObjectOwner(addr) => Some(Owner {
30 address: IotaAddress::from(addr),
31 checkpoint_viewed_at: self.checkpoint_viewed_at,
32 root_version: None,
33 }),
34
35 O::Shared { .. } | O::Immutable => None,
36 }
37 }
38
39 async fn coin_type(&self) -> Option<MoveType> {
42 Some(MoveType::new(self.stored.coin_type.clone()))
43 }
44
45 async fn amount(&self) -> Option<BigInt> {
47 Some(BigInt::from(self.stored.amount))
48 }
49}
50
51impl BalanceChange {
52 pub(crate) fn read(bytes: &[u8], checkpoint_viewed_at: u64) -> Result<Self, Error> {
57 let stored = bcs::from_bytes(bytes)
58 .map_err(|e| Error::Internal(format!("Error deserializing BalanceChange: {e}")))?;
59
60 Ok(Self {
61 stored,
62 checkpoint_viewed_at,
63 })
64 }
65}