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
#![cfg(feature = "std")]

use fuel_core_client::client::schema::coins::CoinType as ClientCoinType;
use fuel_types::AssetId;

use crate::{coin::Coin, constants::BASE_ASSET_ID, message::Message};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum CoinType {
    Coin(Coin),
    Message(Message),
}

impl TryFrom<ClientCoinType> for CoinType {
    type Error = std::io::Error;

    fn try_from(client_resource: ClientCoinType) -> Result<Self, Self::Error> {
        match client_resource {
            ClientCoinType::Coin(coin) => Ok(CoinType::Coin(coin.into())),
            ClientCoinType::MessageCoin(message) => Ok(CoinType::Message(message.into())),
            ClientCoinType::Unknown => Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Got unknown `ClientCoinType`",
            )),
        }
    }
}

impl CoinType {
    pub fn amount(&self) -> u64 {
        match self {
            CoinType::Coin(coin) => coin.amount,
            CoinType::Message(message) => message.amount,
        }
    }

    pub fn asset_id(&self) -> AssetId {
        match self {
            CoinType::Coin(coin) => coin.asset_id,
            CoinType::Message(_) => BASE_ASSET_ID,
        }
    }
}