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::pallet::Origin, crate::Config, frame_support::pallet_prelude::*,
19
    sp_runtime::traits::Morph,
20
};
21

            
22
/// Implementation of the [EnsureOrigin] trait for the [Origin::EthereumTokenTransfers] origin.
23
pub struct EnsureEthereumTokenTransfersOrigin<T>(core::marker::PhantomData<T>);
24
impl<T: Config, O: OriginTrait + From<Origin<T>>> EnsureOrigin<O>
25
    for EnsureEthereumTokenTransfersOrigin<T>
26
where
27
    for<'a> &'a O::PalletsOrigin: TryInto<&'a Origin<T>>,
28
{
29
    type Success = T::AccountId;
30

            
31
14
    fn try_origin(o: O) -> Result<Self::Success, O> {
32
14
        match o.caller().try_into() {
33
12
            Ok(Origin::EthereumTokenTransfers(account_id)) => return Ok(account_id.clone()),
34
2
            _ => (),
35
        }
36

            
37
2
        Err(o)
38
14
    }
39

            
40
    #[cfg(feature = "runtime-benchmarks")]
41
    fn try_successful_origin() -> Result<O, ()> {
42
        Ok(O::root())
43
    }
44
}
45

            
46
pub struct ConvertUnitTo<T>(core::marker::PhantomData<T>);
47

            
48
impl<OutcomeType> Morph<()> for ConvertUnitTo<OutcomeType>
49
where
50
    OutcomeType: From<xcm::latest::Location>,
51
{
52
    type Outcome = OutcomeType;
53

            
54
2
    fn morph(_: ()) -> OutcomeType {
55
2
        xcm::latest::Location::here().into()
56
2
    }
57
}
58

            
59
pub struct ConvertAccountIdTo<AccountId, T, Network>(
60
    core::marker::PhantomData<(AccountId, T, Network)>,
61
);
62

            
63
impl<T, AccountId, OutcomeType, Network> Morph<T>
64
    for ConvertAccountIdTo<AccountId, OutcomeType, Network>
65
where
66
    AccountId: From<T> + Into<[u8; 32]>,
67
    OutcomeType: From<xcm::latest::Junction>,
68
    Network: Get<Option<xcm::latest::NetworkId>>,
69
{
70
    type Outcome = OutcomeType;
71

            
72
5
    fn morph(account_id: T) -> OutcomeType {
73
5
        let account_id_32: AccountId = AccountId::from(account_id);
74
5
        xcm::latest::Junction::AccountId32 {
75
5
            network: Network::get(),
76
5
            id: account_id_32.into(),
77
5
        }
78
5
        .into()
79
5
    }
80
}