|
| 1 | +// Copyright 2020 Parity Technologies (UK) Ltd. |
| 2 | +// This file is part of Polkadot. |
| 3 | + |
| 4 | +// Polkadot 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 | +// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +use sp_std::{result, convert::TryInto, marker::PhantomData}; |
| 18 | +use xcm::v0::{Error, Result, MultiAsset, MultiLocation}; |
| 19 | +use sp_arithmetic::traits::SaturatedConversion; |
| 20 | +use frame_support::traits::{ExistenceRequirement::AllowDeath, WithdrawReason}; |
| 21 | +use xcm_executor::traits::{MatchesFungible, LocationConversion, TransactAsset}; |
| 22 | + |
| 23 | +pub struct CurrencyAdapter<Currency, Matcher, AccountIdConverter, AccountId>( |
| 24 | + PhantomData<Currency>, |
| 25 | + PhantomData<Matcher>, |
| 26 | + PhantomData<AccountIdConverter>, |
| 27 | + PhantomData<AccountId>, |
| 28 | +); |
| 29 | + |
| 30 | +impl< |
| 31 | + Matcher: MatchesFungible<Currency::Balance>, |
| 32 | + AccountIdConverter: LocationConversion<AccountId>, |
| 33 | + Currency: frame_support::traits::Currency<AccountId>, |
| 34 | + AccountId, // can't get away without it since Currency is generic over it. |
| 35 | +> TransactAsset for CurrencyAdapter<Currency, Matcher, AccountIdConverter, AccountId> { |
| 36 | + |
| 37 | + fn deposit_asset(what: &MultiAsset, who: &MultiLocation) -> Result { |
| 38 | + // Check we handle this asset. |
| 39 | + let amount = Matcher::matches_fungible(&what).ok_or(())?.saturated_into(); |
| 40 | + let who = AccountIdConverter::from_location(who).ok_or(())?; |
| 41 | + let balance_amount = amount.try_into().map_err(|_| ())?; |
| 42 | + let _imbalance = Currency::deposit_creating(&who, balance_amount); |
| 43 | + Ok(()) |
| 44 | + } |
| 45 | + |
| 46 | + fn withdraw_asset(what: &MultiAsset, who: &MultiLocation) -> result::Result<MultiAsset, Error> { |
| 47 | + // Check we handle this asset. |
| 48 | + let amount = Matcher::matches_fungible(&what).ok_or(())?.saturated_into(); |
| 49 | + let who = AccountIdConverter::from_location(who).ok_or(())?; |
| 50 | + let balance_amount = amount.try_into().map_err(|_| ())?; |
| 51 | + Currency::withdraw(&who, balance_amount, WithdrawReason::Transfer.into(), AllowDeath).map_err(|_| ())?; |
| 52 | + Ok(what.clone()) |
| 53 | + } |
| 54 | +} |
0 commit comments