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 {
18
    super::*,
19
    ethabi::H256,
20
    frame_support::traits::EnqueueMessage,
21
    snowbridge_core::{outbound::SendError, PRIMARY_GOVERNANCE_CHANNEL},
22
    sp_std::marker::PhantomData,
23
};
24

            
25
/// Alternative to [snowbridge_pallet_outbound_queue::Pallet::deliver] using a different
26
/// origin.
27
pub struct CustomSendMessage<T, GetAggregateMessageOrigin>(
28
    PhantomData<(T, GetAggregateMessageOrigin)>,
29
);
30

            
31
impl<T, GetAggregateMessageOrigin> DeliverMessage
32
    for CustomSendMessage<T, GetAggregateMessageOrigin>
33
where
34
    T: snowbridge_pallet_outbound_queue::Config,
35
    GetAggregateMessageOrigin:
36
        Convert<ChannelId, <T as snowbridge_pallet_outbound_queue::Config>::AggregateMessageOrigin>,
37
{
38
    type Ticket = Ticket<T>;
39

            
40
42
    fn deliver(ticket: Self::Ticket) -> Result<H256, SendError> {
41
42
        let origin = GetAggregateMessageOrigin::convert(ticket.channel_id);
42
42

            
43
42
        if ticket.channel_id != PRIMARY_GOVERNANCE_CHANNEL {
44
            ensure!(
45
                !<snowbridge_pallet_outbound_queue::Pallet<T>>::operating_mode().is_halted(),
46
                SendError::Halted
47
            );
48
42
        }
49

            
50
42
        let message = ticket.message.as_bounded_slice();
51
42

            
52
42
        <T as snowbridge_pallet_outbound_queue::Config>::MessageQueue::enqueue_message(
53
42
            message, origin,
54
42
        );
55
42
        snowbridge_pallet_outbound_queue::Pallet::<T>::deposit_event(
56
42
            snowbridge_pallet_outbound_queue::Event::MessageQueued {
57
42
                id: ticket.message_id,
58
42
            },
59
42
        );
60
42

            
61
42
        Ok(ticket.message_id)
62
42
    }
63
}