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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::client::schema::{
    coin::Coin,
    message::Message,
    schema,
    Address,
    AssetId,
    ConversionError,
    MessageId,
    UtxoId,
    U64,
};
use itertools::Itertools;
use std::str::FromStr;

#[derive(cynic::InputObject, Clone, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct ExcludeInput {
    /// Utxos to exclude from the result.
    utxos: Vec<UtxoId>,
    /// Messages to exclude from teh result.
    messages: Vec<MessageId>,
}

impl ExcludeInput {
    pub fn from_tuple(tuple: (Vec<&str>, Vec<&str>)) -> Result<Self, ConversionError> {
        let utxos = tuple.0.into_iter().map(UtxoId::from_str).try_collect()?;
        let messages = tuple.1.into_iter().map(MessageId::from_str).try_collect()?;

        Ok(Self { utxos, messages })
    }
}

#[derive(cynic::InputObject, Clone, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub struct SpendQueryElementInput {
    /// asset ID of the coins
    pub asset_id: AssetId,
    /// address of the owner
    pub amount: U64,
    /// the maximum number of resources per asset from the owner to return.
    pub max: Option<U64>,
}

#[derive(cynic::InlineFragments, Debug)]
#[cynic(schema_path = "./assets/schema.sdl")]
pub enum Resource {
    Coin(Coin),
    Message(Message),
    #[cynic(fallback)]
    Unknown,
}

impl Resource {
    pub fn amount(&self) -> u64 {
        match self {
            Resource::Coin(c) => c.amount.0,
            Resource::Message(m) => m.amount.0,
            Resource::Unknown => 0,
        }
    }
}

#[derive(cynic::QueryVariables, Debug)]
pub struct ResourcesToSpendArgs {
    /// The `Address` of the assets' resources owner.
    owner: Address,
    /// The total amount of each asset type to spend
    query_per_asset: Vec<SpendQueryElementInput>,
    /// A list of ids to exclude from the selection
    excluded_ids: Option<ExcludeInput>,
}

pub(crate) type ResourcesToSpendArgsTuple =
    (Address, Vec<SpendQueryElementInput>, Option<ExcludeInput>);

impl From<ResourcesToSpendArgsTuple> for ResourcesToSpendArgs {
    fn from(r: ResourcesToSpendArgsTuple) -> Self {
        ResourcesToSpendArgs {
            owner: r.0,
            query_per_asset: r.1,
            excluded_ids: r.2,
        }
    }
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(
    schema_path = "./assets/schema.sdl",
    graphql_type = "Query",
    variables = "ResourcesToSpendArgs"
)]
pub struct ResourcesToSpendQuery {
    #[arguments(owner: $owner, queryPerAsset: $query_per_asset, excludedIds: $excluded_ids)]
    pub resources_to_spend: Vec<Vec<Resource>>,
}