fuel_core/schema/
assets.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
62
63
64
65
66
67
68
69
use async_graphql::{
    Context,
    Object,
};

use crate::{
    fuel_core_graphql_api::query_costs,
    graphql_api::storage::assets::AssetDetails,
    schema::{
        scalars::{
            AssetId,
            ContractId,
            SubId,
            U128,
        },
        ReadViewProvider,
    },
};

#[derive(Default)]
pub struct AssetInfoQuery;

#[Object]
impl AssetInfoQuery {
    #[graphql(complexity = "query_costs().storage_read")]
    async fn asset_details(
        &self,
        ctx: &Context<'_>,
        #[graphql(desc = "ID of the Asset")] id: AssetId,
    ) -> async_graphql::Result<AssetInfoDetails> {
        let query = ctx.read_view()?;
        query
            .get_asset_details(&id.into())
            .map(|details| details.into())
            .map_err(async_graphql::Error::from)
    }
}

#[derive(Clone, Debug)]
pub struct AssetInfoDetails {
    pub contract_id: ContractId,
    pub sub_id: SubId,
    pub total_supply: U128,
}

impl From<AssetDetails> for AssetInfoDetails {
    fn from(details: AssetDetails) -> Self {
        AssetInfoDetails {
            contract_id: details.contract_id.into(),
            sub_id: details.sub_id.into(),
            total_supply: details.total_supply.into(),
        }
    }
}

#[Object]
impl AssetInfoDetails {
    async fn contract_id(&self) -> &ContractId {
        &self.contract_id
    }

    async fn sub_id(&self) -> &SubId {
        &self.sub_id
    }

    async fn total_supply(&self) -> &U128 {
        &self.total_supply
    }
}