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
#[cfg(feature = "fuel-core-lib")]
use fuel_core::model::{Coin as ClientCoin, CoinStatus as ClientCoinStatus};

#[cfg(not(feature = "fuel-core-lib"))]
use fuel_gql_client::client::schema::coin::{Coin as ClientCoin, CoinStatus as ClientCoinStatus};

use fuel_chain_config::CoinConfig;
use fuel_tx::{AssetId, UtxoId};

use crate::bech32::Bech32Address;

#[derive(Debug, Clone)]

pub enum CoinStatus {
    Unspent,
    Spent,
}

impl From<ClientCoinStatus> for CoinStatus {
    fn from(coin_status: ClientCoinStatus) -> Self {
        match coin_status {
            ClientCoinStatus::Unspent => CoinStatus::Unspent,
            ClientCoinStatus::Spent => CoinStatus::Spent,
        }
    }
}

#[derive(Debug, Clone)]
pub struct Coin {
    pub amount: u64,
    pub block_created: u64,
    pub asset_id: AssetId,
    pub utxo_id: UtxoId,
    pub maturity: u64,
    pub owner: Bech32Address,
    pub status: CoinStatus,
}

impl From<ClientCoin> for Coin {
    fn from(coin: ClientCoin) -> Self {
        Self {
            amount: coin.amount.0,
            block_created: coin.block_created.0,
            asset_id: coin.asset_id.0 .0,
            utxo_id: coin.utxo_id.0 .0,
            maturity: coin.maturity.0,
            owner: coin.owner.0 .0.into(),
            status: coin.status.into(),
        }
    }
}

impl From<Coin> for CoinConfig {
    fn from(coin: Coin) -> CoinConfig {
        Self {
            tx_id: Some(*coin.utxo_id.tx_id()),
            output_index: Some(coin.utxo_id.output_index() as u64),
            block_created: Some(coin.block_created.into()),
            maturity: Some(coin.maturity.into()),
            owner: coin.owner.into(),
            amount: coin.amount,
            asset_id: coin.asset_id,
        }
    }
}