1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Tanssi is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Tanssi.  If not, see <http://www.gnu.org/licenses/>
16

            
17
use parity_scale_codec::DecodeAll;
18
use snowbridge_inbound_queue_primitives::v1::{
19
    Command, Destination, MessageV1, VersionedXcmMessage,
20
};
21
use sp_core::H256;
22

            
23
/// Information needed to process a native token transfer message from ethereum.
24
pub struct NativeTokenTransferData {
25
    pub token_id: H256,
26
    pub destination: Destination,
27
    pub amount: u128,
28
    pub fee: u128,
29
}
30

            
31
impl NativeTokenTransferData {
32
2184
    pub fn decode_native_token_message(mut payload: &[u8]) -> Option<Self> {
33
2184
        match VersionedXcmMessage::decode_all(&mut payload) {
34
            Ok(VersionedXcmMessage::V1(MessageV1 {
35
                command:
36
                    Command::SendNativeToken {
37
2132
                        token_id,
38
2132
                        destination,
39
2132
                        amount,
40
2132
                        fee,
41
                    },
42
                ..
43
2132
            })) => Some(NativeTokenTransferData {
44
2132
                token_id,
45
2132
                destination,
46
2132
                amount,
47
2132
                fee,
48
2132
            }),
49
52
            Ok(msg) => {
50
52
                log::trace!("NativeTokenTransferData: unexpected message: {:?}", msg);
51
52
                None
52
            }
53
            Err(e) => {
54
                log::trace!("NativeTokenTransferData: failed to decode message. This is expected if the message is not related to a SendNativeToken command. Error: {:?}", e);
55
                None
56
            }
57
        }
58
2184
    }
59
}