Repository: MystenLabs/sui
File: crates/sui-indexer-alt-graphql/src/api/types/coin_metadata.rs (Rust)

Code before:
    /// Total balance across coins owned by this address, grouped by coin type.
    pub(crate) async fn balances(
        &self,
        ctx: &Context<'_>,
        first: Option<u64>,
        after: Option<balance::Cursor>,
        last: Option<u64>,
        before: Option<balance::Cursor>,
    ) -> Option<Result<Connection<String, Balance>, RpcError<balance::Error>>> {
        self.super_
            .balances(ctx, first, after, last, before)
            .await
            .ok()?
    }

    /// The structured representation of the object's contents.
    pub(crate) async fn contents(&self, ctx: &Context<'_>) -> Option<Result<MoveValue, RpcError>> {
        self.super_.contents(ctx).await.ok()?
    }

    /// Number of decimal places the coin uses.
    pub(crate) async fn decimals(&self, ctx: &Context<'_>) -> Option<Result<u8, RpcError>> {
        async {
            let Some(contents) = self.metadata_contents(ctx).await? else {
                return Ok(None);
            };

            Ok(Some(match &contents.native {
                NativeContents::Metadata(metadata) => metadata.decimals,
                NativeContents::Registry(currency) => currency.decimals,
            }))
        }
        .await
        .transpose()
    }

    /// The domain explicitly configured as the default Name Service name for this address.
    pub(crate) async fn default_name_record(
        &self,
        ctx: &Context<'_>,
    ) -> Option<Result<NameRecord, RpcError<object::Error>>> {
        self.super_.default_name_record(ctx).await.ok()?
    }

    /// Description of the coin.
    pub(crate) async fn description(&self, ctx: &Context<'_>) -> Option<Result<&str, RpcError>> {
        async {
            let Some(contents) = self.metadata_contents(ctx).await? else {
                return Ok(None);
            };


Review comment (suggestion):
Seems like you could pull this out of the `async` block, perhaps (here and below), which also unlocks further simplifications.

Code after:
    /// Total balance across coins owned by this address, grouped by coin type.
    pub(crate) async fn balances(
        &self,
        ctx: &Context<'_>,
        first: Option<u64>,
        after: Option<balance::Cursor>,
        last: Option<u64>,
        before: Option<balance::Cursor>,
    ) -> Option<Result<Connection<String, Balance>, RpcError<balance::Error>>> {
        self.super_
            .balances(ctx, first, after, last, before)
            .await
            .ok()?
    }

    /// The structured representation of the object's contents.
    pub(crate) async fn contents(&self, ctx: &Context<'_>) -> Option<Result<MoveValue, RpcError>> {
        self.super_.contents(ctx).await.ok()?
    }

    /// Number of decimal places the coin uses.
    pub(crate) async fn decimals(&self, ctx: &Context<'_>) -> Option<Result<u8, RpcError>> {
        let contents = self
            .metadata_contents(ctx)
            .await
            .map(Option::as_ref)
            .transpose()?;
        Some(contents.map(|c| match &c.native {
            NativeContents::Metadata(metadata) => metadata.decimals,
            NativeContents::Registry(currency) => currency.decimals,
        }))
    }

    /// The domain explicitly configured as the default Name Service name for this address.
    pub(crate) async fn default_name_record(
        &self,
        ctx: &Context<'_>,
    ) -> Option<Result<NameRecord, RpcError<object::Error>>> {
        self.super_.default_name_record(ctx).await.ok()?
    }

    /// Description of the coin.
    pub(crate) async fn description(&self, ctx: &Context<'_>) -> Option<Result<&str, RpcError>> {
        let contents = self
            .metadata_contents(ctx)
            .await
            .map(Option::as_ref)
            .transpose()?;
        Some(contents.map(|c| match &c.native {
            NativeContents::Metadata(metadata) => metadata.description.as_str(),
            NativeContents::Registry(currency) => currency.description.as_str(),