|
1 |
| -// This file is part of Substrate. |
2 |
| - |
3 |
| -// Copyright (C) 2018-2022 Parity Technologies (UK) Ltd. |
4 |
| -// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 |
5 |
| - |
6 |
| -// This program is free software: you can redistribute it and/or modify |
7 |
| -// it under the terms of the GNU General Public License as published by |
8 |
| -// the Free Software Foundation, either version 3 of the License, or |
9 |
| -// (at your option) any later version. |
10 |
| - |
11 |
| -// This program is distributed in the hope that it will be useful, |
12 |
| -// but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
| -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
| -// GNU General Public License for more details. |
15 |
| - |
16 |
| -// You should have received a copy of the GNU General Public License |
17 |
| -// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 1 | +// Copyright (C) 2023 Parity Technologies (UK) Ltd. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
18 | 15 |
|
19 | 16 | //! Runtime API definition for assets.
|
20 | 17 |
|
21 | 18 | use crate::runtime_api::FungiblesAccessError;
|
| 19 | +use frame_support::traits::Contains; |
22 | 20 | use sp_std::{borrow::Borrow, vec::Vec};
|
23 | 21 | use xcm::latest::{MultiAsset, MultiLocation};
|
24 |
| -use xcm_builder::ConvertedConcreteId; |
| 22 | +use xcm_builder::{ConvertedConcreteId, MatchedConvertedConcreteId}; |
25 | 23 | use xcm_executor::traits::{Convert, MatchesFungibles};
|
26 | 24 |
|
27 | 25 | /// Converting any [`(AssetId, Balance)`] to [`MultiAsset`]
|
|
60 | 58 | }
|
61 | 59 | }
|
62 | 60 |
|
| 61 | +impl< |
| 62 | + AssetId: Clone, |
| 63 | + Balance: Clone, |
| 64 | + MatchAssetId: Contains<MultiLocation>, |
| 65 | + ConvertAssetId: Convert<MultiLocation, AssetId>, |
| 66 | + ConvertBalance: Convert<u128, Balance>, |
| 67 | + > MultiAssetConverter<AssetId, Balance, ConvertAssetId, ConvertBalance> |
| 68 | + for MatchedConvertedConcreteId<AssetId, Balance, MatchAssetId, ConvertAssetId, ConvertBalance> |
| 69 | +{ |
| 70 | + fn convert_ref( |
| 71 | + value: impl Borrow<(AssetId, Balance)>, |
| 72 | + ) -> Result<MultiAsset, FungiblesAccessError> { |
| 73 | + let (asset_id, balance) = value.borrow(); |
| 74 | + match ConvertAssetId::reverse_ref(asset_id) { |
| 75 | + Ok(asset_id_as_multilocation) => match ConvertBalance::reverse_ref(balance) { |
| 76 | + Ok(amount) => Ok((asset_id_as_multilocation, amount).into()), |
| 77 | + Err(_) => Err(FungiblesAccessError::AmountToBalanceConversionFailed), |
| 78 | + }, |
| 79 | + Err(_) => Err(FungiblesAccessError::AssetIdConversionFailed), |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
63 | 84 | /// Helper function to convert collections with [`(AssetId, Balance)`] to [`MultiAsset`]
|
64 | 85 | pub fn convert<'a, AssetId, Balance, ConvertAssetId, ConvertBalance, Converter>(
|
65 | 86 | items: impl Iterator<Item = &'a (AssetId, Balance)>,
|
@@ -90,11 +111,12 @@ pub fn convert_balance<
|
90 | 111 | #[cfg(test)]
|
91 | 112 | mod tests {
|
92 | 113 | use super::*;
|
| 114 | + use frame_support::traits::Everything; |
93 | 115 |
|
94 | 116 | use xcm::latest::prelude::*;
|
95 | 117 | use xcm_executor::traits::{Identity, JustTry};
|
96 | 118 |
|
97 |
| - type Converter = ConvertedConcreteId<MultiLocation, u64, Identity, JustTry>; |
| 119 | + type Converter = MatchedConvertedConcreteId<MultiLocation, u64, Everything, Identity, JustTry>; |
98 | 120 |
|
99 | 121 | #[test]
|
100 | 122 | fn converted_concrete_id_fungible_multi_asset_conversion_roundtrip_works() {
|
|
0 commit comments