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
    alloc::vec,
19
    core::marker::PhantomData,
20
    cumulus_primitives_core::ParaId,
21
    frame_support::traits::Get,
22
    xcm::{
23
        latest::{Location, NetworkId},
24
        prelude::*,
25
    },
26
    xcm_builder::InspectMessageQueues,
27
    SendError::*,
28
};
29

            
30
// We check if the destination is ETH
31
24
fn is_ethereum_location(loc: &Location, ethereum_network: NetworkId) -> bool {
32
24
    matches!(
33
24
        loc,
34
        Location {
35
            parents: 2,
36
24
            interior: Junctions::X1(juncs)
37
24
        } if juncs[0] == GlobalConsensus(ethereum_network)
38
    )
39
24
}
40

            
41
pub struct SovereignPaidRemoteExporter<
42
    Router,
43
    UniversalLocation,
44
    EthereumNetwork,
45
    ExecutionFee,
46
    SelfParaId,
47
>(
48
    PhantomData<(
49
        Router,
50
        UniversalLocation,
51
        EthereumNetwork,
52
        ExecutionFee,
53
        SelfParaId,
54
    )>,
55
);
56
impl<
57
        Router: SendXcm,
58
        UniversalLocation: Get<InteriorLocation>,
59
        EthereumNetwork,
60
        ExecutionFee,
61
        SelfParaId,
62
    > SendXcm
63
    for SovereignPaidRemoteExporter<
64
        Router,
65
        UniversalLocation,
66
        EthereumNetwork,
67
        ExecutionFee,
68
        SelfParaId,
69
    >
70
where
71
    EthereumNetwork: Get<NetworkId>,
72
    ExecutionFee: Get<u128>,
73
    SelfParaId: Get<ParaId>,
74
{
75
    type Ticket = Router::Ticket;
76

            
77
2
    fn validate(
78
2
        dest: &mut Option<Location>,
79
2
        msg: &mut Option<Xcm<()>>,
80
2
    ) -> SendResult<Router::Ticket> {
81
2
        log::trace!(target: "xcm::sovereign_paid_remote_exporter", "validate params: dest={dest:?}, msg={msg:?}");
82

            
83
2
        let d = dest.as_ref().ok_or(MissingArgument)?;
84
2
        let xcm = msg.take().ok_or(MissingArgument)?;
85
        // Check if the destination is an Ethereum location
86
2
        if !is_ethereum_location(&d.clone(), EthereumNetwork::get()) {
87
            return Err(NotApplicable);
88
2
        }
89

            
90
        // `xcm` should already end with `SetTopic` - if it does, then extract and derive into
91
        // an onward topic ID.
92
2
        let maybe_forward_id = match xcm.last() {
93
2
            Some(SetTopic(t)) => Some(*t),
94
            _ => None,
95
        };
96

            
97
2
        let export_instruction = ExportMessage {
98
2
            network: EthereumNetwork::get(),
99
2
            destination: Here,
100
2
            xcm: xcm.clone(),
101
2
        };
102

            
103
        // For now hardcoding fees, but later it should be converted from fixed Tanssi relay amount
104
2
        let fees = Asset {
105
2
            id: AssetId(Location::here()),
106
2
            fun: Fungible(ExecutionFee::get()),
107
2
        };
108
2
        let container_location = Location::new(0, Parachain(SelfParaId::get().into()));
109

            
110
        // Prepare the message to send
111
2
        let mut message = Xcm(vec![
112
2
            WithdrawAsset(fees.clone().into()),
113
2
            BuyExecution {
114
2
                fees,
115
2
                weight_limit: Unlimited,
116
2
            },
117
2
            SetAppendix(Xcm(vec![DepositAsset {
118
2
                assets: AllCounted(1).into(),
119
2
                beneficiary: container_location,
120
2
            }])),
121
2
            export_instruction,
122
2
        ]);
123

            
124
2
        let tanssi_location = Location {
125
2
            parents: 1,
126
2
            interior: Here,
127
2
        };
128

            
129
2
        if let Some(forward_id) = maybe_forward_id {
130
2
            message.0.push(SetTopic(forward_id));
131
2
        }
132

            
133
2
        let (v, cost) = validate_send::<Router>(tanssi_location, message).inspect_err(|err| {
134
            if let NotApplicable = err {
135
                *msg = Some(xcm);
136
            }
137
        })?;
138

            
139
2
        Ok((v, cost))
140
2
    }
141

            
142
2
    fn deliver(ticket: Router::Ticket) -> Result<XcmHash, SendError> {
143
2
        Router::deliver(ticket)
144
2
    }
145
}
146

            
147
impl<
148
        Router: SendXcm,
149
        UniversalLocation: Get<InteriorLocation>,
150
        EthereumNetwork,
151
        ExecutionFee,
152
        SelfParaId,
153
    > InspectMessageQueues
154
    for SovereignPaidRemoteExporter<
155
        Router,
156
        UniversalLocation,
157
        EthereumNetwork,
158
        ExecutionFee,
159
        SelfParaId,
160
    >
161
{
162
    fn clear_messages() {}
163

            
164
    fn get_messages() -> vec::Vec<(VersionedLocation, vec::Vec<VersionedXcm<()>>)> {
165
        vec::Vec::new()
166
    }
167
}