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
extern crate alloc;
18

            
19
use {
20
    alloc::collections::btree_set::BTreeSet,
21
    core::marker::PhantomData,
22
    frame_support::{
23
        parameter_types,
24
        traits::{Contains, ContainsPair, Get},
25
    },
26
    xcm::latest::prelude::*,
27
};
28

            
29
/// The pallet index of the Ethereum inbound queue pallet in the Tanssi runtime.
30
pub const PARENT_INBOUND_QUEUE_PALLET_INDEX: u8 = 24;
31

            
32
parameter_types! {
33
    pub EthereumNetworkSepolia: NetworkId = NetworkId::Ethereum { chain_id: 11155111 };
34
    pub EthereumNetworkMainnet: NetworkId = NetworkId::Ethereum { chain_id: 1 };
35

            
36
    pub ParentWithEthereumInboundQueueInstance: Location = Location::new(
37
        1,
38
        [
39
            PalletInstance(PARENT_INBOUND_QUEUE_PALLET_INDEX)
40
        ]
41
    );
42

            
43
    /// Universal aliases common to frontier and simple templates.
44
    pub CommonUniversalAliases: BTreeSet<(Location, Junction)> = BTreeSet::from_iter(
45
        alloc::vec![
46
            (ParentWithEthereumInboundQueueInstance::get(), GlobalConsensus(EthereumNetworkSepolia::get())),
47
            (ParentWithEthereumInboundQueueInstance::get(), GlobalConsensus(EthereumNetworkMainnet::get()))
48
        ]
49
    );
50
}
51

            
52
impl Contains<(Location, Junction)> for CommonUniversalAliases {
53
104
    fn contains(alias: &(Location, Junction)) -> bool {
54
104
        CommonUniversalAliases::get().contains(alias)
55
104
    }
56
}
57

            
58
#[cfg(feature = "runtime-benchmarks")]
59
pub struct AliasingBenchmarksHelper;
60

            
61
#[cfg(feature = "runtime-benchmarks")]
62
impl AliasingBenchmarksHelper {
63
    pub fn prepare_universal_alias() -> Option<(Location, Junction)> {
64
        let alias = CommonUniversalAliases::get()
65
            .into_iter()
66
            .find_map(|(location, junction)| {
67
                match ParentWithEthereumInboundQueueInstance::get().eq(&location) {
68
                    true => Some((location, junction)),
69
                    false => None,
70
                }
71
            });
72
        Some(alias.expect("Tanssi's InboundQueue to container-chain mapping expected here"))
73
    }
74
}
75

            
76
/// Allows the parent (relay chain) to alias as the Ethereum origin.
77
/// This is needed for container native token transfers from Ethereum where the relay
78
/// sends XCM with preserve_origin: true and the origin needs to represent Ethereum.
79
pub struct ParentAsEthereumAliaser<EthereumLocation>(PhantomData<EthereumLocation>);
80
impl<EthereumLocation: Get<Location>> ContainsPair<Location, Location>
81
    for ParentAsEthereumAliaser<EthereumLocation>
82
{
83
6
    fn contains(origin: &Location, target: &Location) -> bool {
84
        // Allow parent (relay chain) to alias as Ethereum global consensus
85
6
        *origin == Location::parent() && *target == EthereumLocation::get()
86
6
    }
87
}