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
24
        Location {
35
24
            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
        let d = dest.as_ref().ok_or(MissingArgument)?;
82
2
        let xcm = msg.take().ok_or(MissingArgument)?;
83
        // Check if the destination is an Ethereum location
84
2
        if !is_ethereum_location(&d.clone(), EthereumNetwork::get()) {
85
            return Err(NotApplicable);
86
2
        }
87

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

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

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

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

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

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

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

            
137
2
        Ok((v, cost))
138
2
    }
139

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

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

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