sui_graphql_rpc/types/balance_change.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use 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,
/// The checkpoint sequence number this was viewed at.
checkpoint_viewed_at: u64,
}
/// Effects to the balance (sum of coin values per coin type) owned by an address or object.
#[Object]
impl BalanceChange {
/// The address or object whose balance has changed.
async fn owner(&self) -> Option<Owner> {
use NativeOwner as O;
match self.stored.owner {
O::AddressOwner(addr) | O::ObjectOwner(addr) => Some(Owner {
address: SuiAddress::from(addr),
checkpoint_viewed_at: self.checkpoint_viewed_at,
root_version: None,
}),
O::Shared { .. } | O::Immutable => None,
// TODO: Implement support for ConsensusV2 objects.
O::ConsensusV2 { .. } => todo!(),
}
}
/// The inner type of the coin whose balance has changed (e.g. `0x2::sui::SUI`).
async fn coin_type(&self) -> Option<MoveType> {
Some(self.stored.coin_type.clone().into())
}
/// The signed balance change.
async fn amount(&self) -> Option<BigInt> {
Some(BigInt::from(self.stored.amount))
}
}
impl BalanceChange {
/// `checkpoint_viewed_at` represents the checkpoint sequence number at which this
/// `BalanceChange` was queried for. This is stored on `BalanceChange` so that when viewing that
/// entity's state, it will be as if it was read at the same checkpoint.
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,
})
}
}