sui_graphql_rpc/types/
balance_change.rsuse async_graphql::*;
use sui_json_rpc_types::BalanceChange as StoredBalanceChange;
use sui_types::object::Owner as NativeOwner;
use super::{big_int::BigInt, move_type::MoveType, owner::Owner, sui_address::SuiAddress};
use crate::error::Error;
pub(crate) struct BalanceChange {
stored: StoredBalanceChange,
checkpoint_viewed_at: u64,
}
#[Object]
impl BalanceChange {
async fn owner(&self) -> Option<Owner> {
use NativeOwner as O;
match self.stored.owner {
O::AddressOwner(addr)
| O::ObjectOwner(addr)
| O::ConsensusAddressOwner { owner: addr, .. } => Some(Owner {
address: SuiAddress::from(addr),
checkpoint_viewed_at: self.checkpoint_viewed_at,
root_version: None,
}),
O::Shared { .. } | O::Immutable => None,
}
}
async fn coin_type(&self) -> Option<MoveType> {
Some(self.stored.coin_type.clone().into())
}
async fn amount(&self) -> Option<BigInt> {
Some(BigInt::from(self.stored.amount))
}
}
impl BalanceChange {
pub(crate) fn read(bytes: &[u8], checkpoint_viewed_at: u64) -> Result<Self, Error> {
let stored = bcs::from_bytes(bytes)
.map_err(|e| Error::Internal(format!("Error deserializing BalanceChange: {e}")))?;
Ok(Self {
stored,
checkpoint_viewed_at,
})
}
}