-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add an adapter for configuring AssetExchanger #5130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
bb0e969
feat(xcm-builder): add an adapter for configuring AssetExchanger
franciscoaguirre 4849d8f
doc: add prdoc
franciscoaguirre 648c6a2
feat(xcm-builder): return leftover assets on a minimal exchange
franciscoaguirre 0a3d491
Merge branch 'master' into single-asset-exchange-adapter
franciscoaguirre 200f9f5
fix(xcm-builder): fix quote_exchange_price behaviour in SingleAssetEx…
franciscoaguirre dc85ede
Merge branch 'master' into single-asset-exchange-adapter
franciscoaguirre f462689
fix(xcm-builder): issue with runtime-benchmarks feature on
franciscoaguirre e812770
fix(xcm-builder): wrong value being asserted
franciscoaguirre b139ba4
chore(xcm-builder): don't use sp-std
franciscoaguirre fdd829b
feat(xcm-builder): no non-fungible assets allowed in SingleAssetExcha…
franciscoaguirre 76e96ab
chore(xcm-executor): add default implementation for quote_exchange_price
franciscoaguirre 1518217
Merge branch 'master' into single-asset-exchange-adapter
franciscoaguirre 1af2dec
fix(xcm-executor): mark unused variables with underscores
franciscoaguirre 89df1cf
doc(xcm-executor): improved doc comment for quote_exchange_price
franciscoaguirre 444f98d
chore: the new function is a breaking change
franciscoaguirre 7761091
Merge branch 'master' into single-asset-exchange-adapter
franciscoaguirre 2c6d729
Update prdoc/pr_5130.prdoc
franciscoaguirre c3f2afb
Update polkadot/xcm/xcm-executor/src/traits/asset_exchange.rs
franciscoaguirre c5edffd
Update polkadot/xcm/xcm-executor/src/traits/asset_exchange.rs
franciscoaguirre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Copyright (C) Parity Technologies (UK) Ltd. | ||
| // This file is part of Polkadot. | ||
|
|
||
| // Polkadot is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Polkadot is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Polkadot. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! Adapters for the AssetExchanger config item. | ||
| //! | ||
| //! E.g. types that implement the [`xcm_executor::traits::AssetExchange`] trait. | ||
|
|
||
| mod single_asset_adapter; | ||
| pub use single_asset_adapter::SingleAssetExchangeAdapter; |
205 changes: 205 additions & 0 deletions
205
polkadot/xcm/xcm-builder/src/asset_exchange/single_asset_adapter/adapter.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| // Copyright (C) Parity Technologies (UK) Ltd. | ||
| // This file is part of Polkadot. | ||
|
|
||
| // Polkadot is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Polkadot is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Polkadot. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! Single asset exchange adapter. | ||
|
|
||
| extern crate alloc; | ||
| use alloc::vec; | ||
| use core::marker::PhantomData; | ||
| use frame_support::{ensure, traits::tokens::fungibles}; | ||
| use pallet_asset_conversion::{QuotePrice, SwapCredit}; | ||
| use xcm::prelude::*; | ||
| use xcm_executor::{ | ||
| traits::{AssetExchange, MatchesFungibles}, | ||
| AssetsInHolding, | ||
| }; | ||
|
|
||
| /// An adapter from [`pallet_asset_conversion::SwapCredit`] and | ||
| /// [`pallet_asset_conversion::QuotePrice`] to [`xcm_executor::traits::AssetExchange`]. | ||
| /// | ||
| /// This adapter takes just one fungible asset in `give` and allows only one fungible asset in | ||
| /// `want`. If you need to handle more assets in either `give` or `want`, then you should use | ||
| /// another type that implements [`xcm_executor::traits::AssetExchange`] or build your own. | ||
| /// | ||
| /// This adapter also only works for fungible assets. | ||
| /// | ||
| /// `exchange_asset` will return an error if there's more than one asset in `give` or `want`. | ||
| pub struct SingleAssetExchangeAdapter<AssetConversion, Fungibles, Matcher, AccountId>( | ||
| PhantomData<(AssetConversion, Fungibles, Matcher, AccountId)>, | ||
| ); | ||
| impl<AssetConversion, Fungibles, Matcher, AccountId> AssetExchange | ||
| for SingleAssetExchangeAdapter<AssetConversion, Fungibles, Matcher, AccountId> | ||
| where | ||
| AssetConversion: SwapCredit< | ||
| AccountId, | ||
| Balance = u128, | ||
| AssetKind = Fungibles::AssetId, | ||
| Credit = fungibles::Credit<AccountId, Fungibles>, | ||
| > + QuotePrice<Balance = u128, AssetKind = Fungibles::AssetId>, | ||
| Fungibles: fungibles::Balanced<AccountId, Balance = u128>, | ||
| Matcher: MatchesFungibles<Fungibles::AssetId, Fungibles::Balance>, | ||
| { | ||
| fn exchange_asset( | ||
| _: Option<&Location>, | ||
| give: AssetsInHolding, | ||
| want: &Assets, | ||
| maximal: bool, | ||
| ) -> Result<AssetsInHolding, AssetsInHolding> { | ||
| let mut give_iter = give.fungible_assets_iter(); | ||
| let give_asset = give_iter.next().ok_or_else(|| { | ||
| log::trace!( | ||
| target: "xcm::SingleAssetExchangeAdapter::exchange_asset", | ||
| "No fungible asset was in `give`.", | ||
| ); | ||
| give.clone() | ||
| })?; | ||
| ensure!(give_iter.next().is_none(), give.clone()); // We only support 1 asset in `give`. | ||
|
franciscoaguirre marked this conversation as resolved.
|
||
| ensure!(give.non_fungible_assets_iter().next().is_none(), give.clone()); // We don't allow non-fungible assets. | ||
| ensure!(want.len() == 1, give.clone()); // We only support 1 asset in `want`. | ||
| let want_asset = want.get(0).ok_or_else(|| give.clone())?; | ||
|
franciscoaguirre marked this conversation as resolved.
|
||
| let (give_asset_id, give_amount) = | ||
| Matcher::matches_fungibles(&give_asset).map_err(|error| { | ||
| log::trace!( | ||
| target: "xcm::SingleAssetExchangeAdapter::exchange_asset", | ||
| "Could not map XCM asset give {:?} to FRAME asset. Error: {:?}", | ||
| give_asset, | ||
| error, | ||
| ); | ||
| give.clone() | ||
| })?; | ||
| let (want_asset_id, want_amount) = | ||
| Matcher::matches_fungibles(&want_asset).map_err(|error| { | ||
| log::trace!( | ||
| target: "xcm::SingleAssetExchangeAdapter::exchange_asset", | ||
| "Could not map XCM asset want {:?} to FRAME asset. Error: {:?}", | ||
| want_asset, | ||
| error, | ||
| ); | ||
| give.clone() | ||
| })?; | ||
|
|
||
| // We have to do this to convert the XCM assets into credit the pool can use. | ||
| let swap_asset = give_asset_id.clone().into(); | ||
| let credit_in = Fungibles::issue(give_asset_id, give_amount); | ||
|
|
||
| // Do the swap. | ||
| let (credit_out, maybe_credit_change) = if maximal { | ||
| // If `maximal`, then we swap exactly `credit_in` to get as much of `want_asset_id` as | ||
| // we can, with a minimum of `want_amount`. | ||
| let credit_out = <AssetConversion as SwapCredit<_>>::swap_exact_tokens_for_tokens( | ||
| vec![swap_asset, want_asset_id], | ||
| credit_in, | ||
| Some(want_amount), | ||
| ) | ||
| .map_err(|(credit_in, error)| { | ||
| log::error!( | ||
| target: "xcm::SingleAssetExchangeAdapter::exchange_asset", | ||
| "Could not perform the swap, error: {:?}.", | ||
| error | ||
| ); | ||
| drop(credit_in); | ||
| give.clone() | ||
| })?; | ||
|
|
||
| // We don't have leftover assets if exchange was maximal. | ||
| (credit_out, None) | ||
| } else { | ||
| // If `minimal`, then we swap as little of `credit_in` as we can to get exactly | ||
| // `want_amount` of `want_asset_id`. | ||
| let (credit_out, credit_change) = | ||
| <AssetConversion as SwapCredit<_>>::swap_tokens_for_exact_tokens( | ||
| vec![swap_asset, want_asset_id], | ||
| credit_in, | ||
| want_amount, | ||
| ) | ||
| .map_err(|(credit_in, error)| { | ||
| log::error!( | ||
| target: "xcm::SingleAssetExchangeAdapter::exchange_asset", | ||
| "Could not perform the swap, error: {:?}.", | ||
| error | ||
| ); | ||
| drop(credit_in); | ||
| give.clone() | ||
| })?; | ||
|
|
||
| (credit_out, Some(credit_change)) | ||
| }; | ||
|
|
||
| // We create an `AssetsInHolding` instance by putting in the resulting asset | ||
| // of the exchange. | ||
| let resulting_asset: Asset = (want_asset.id.clone(), credit_out.peek()).into(); | ||
| let mut result: AssetsInHolding = resulting_asset.into(); | ||
|
|
||
| // If we have some leftover assets from the exchange, also put them in the result. | ||
| if let Some(credit_change) = maybe_credit_change { | ||
| let leftover_asset: Asset = (give_asset.id.clone(), credit_change.peek()).into(); | ||
| result.subsume(leftover_asset); | ||
| } | ||
|
|
||
| Ok(result.into()) | ||
| } | ||
|
|
||
| fn quote_exchange_price(asset1: &Asset, asset2: &Asset, maximal: bool) -> Option<u128> { | ||
| // We first match both XCM assets to the asset ID types `AssetConversion` can handle. | ||
| let (asset1_id, asset1_amount) = Matcher::matches_fungibles(asset1) | ||
| .map_err(|error| { | ||
| log::trace!( | ||
| target: "xcm::SingleAssetExchangeAdapter::quote_exchange_price", | ||
| "Could not map XCM asset {:?} to FRAME asset. Error: {:?}.", | ||
| asset1, | ||
| error, | ||
| ); | ||
| () | ||
| }) | ||
| .ok()?; | ||
| // For `asset2`, we also want the desired amount. | ||
| let (asset2_id, asset2_amount) = Matcher::matches_fungibles(asset2) | ||
| .map_err(|error| { | ||
| log::trace!( | ||
| target: "xcm::SingleAssetExchangeAdapter::quote_exchange_price", | ||
| "Could not map XCM asset {:?} to FRAME asset. Error: {:?}.", | ||
| asset2, | ||
| error, | ||
| ); | ||
| () | ||
| }) | ||
| .ok()?; | ||
| // We quote the price. | ||
| if maximal { | ||
| // The amount of `asset2` resulting from swapping the `asset1_amount` of `asset1`. | ||
| let resulting_asset2_amount = | ||
| <AssetConversion as QuotePrice>::quote_price_exact_tokens_for_tokens( | ||
| asset1_id, | ||
| asset2_id, | ||
| asset1_amount, | ||
| true, | ||
| )?; | ||
|
|
||
| Some(resulting_asset2_amount) | ||
| } else { | ||
| // The `asset1` amount required to obtain `asset2_amount` of `asset2`. | ||
| let necessary_asset1_amount = | ||
| <AssetConversion as QuotePrice>::quote_price_tokens_for_exact_tokens( | ||
| asset1_id, | ||
| asset2_id, | ||
| asset2_amount, | ||
| true, | ||
| )?; | ||
|
|
||
| Some(necessary_asset1_amount) | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.