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
use fuel_chain_config::MessageConfig;
#[cfg(feature = "fuel-core-lib")]
use fuel_core::model::Message as ClientMessage;

#[cfg(not(feature = "fuel-core-lib"))]
use fuel_gql_client::client::schema::message::Message as ClientMessage;

use fuel_tx::{Input, MessageId};

use crate::bech32::Bech32Address;

#[derive(Debug, Clone)]
pub struct Message {
    pub amount: u64,
    pub sender: Bech32Address,
    pub recipient: Bech32Address,
    pub nonce: u64,
    pub data: Vec<u8>,
    pub da_height: u64,
    pub fuel_block_spend: Option<u64>,
}

impl Message {
    pub fn message_id(&self) -> MessageId {
        Input::compute_message_id(
            &(&self.sender).into(),
            &(&self.recipient).into(),
            self.nonce,
            self.amount,
            &self.data,
        )
    }
}

impl From<ClientMessage> for Message {
    fn from(message: ClientMessage) -> Self {
        Self {
            amount: message.amount.0,
            sender: message.sender.0 .0.into(),
            recipient: message.recipient.0 .0.into(),
            nonce: message.nonce.0,
            data: message.data.0 .0,
            da_height: message.da_height.0,
            fuel_block_spend: message.fuel_block_spend.map(|bs| bs.0),
        }
    }
}

impl From<Message> for MessageConfig {
    fn from(message: Message) -> MessageConfig {
        MessageConfig {
            sender: message.sender.into(),
            recipient: message.recipient.into(),
            nonce: message.nonce,
            amount: message.amount,
            data: message.data,
            da_height: message.da_height.into(),
        }
    }
}