Skip to content

Commit

Permalink
UnionOf types for merged fungible and fungibles implementations (
Browse files Browse the repository at this point in the history
…paritytech#2033)

Introduces `UnionOf` types, crafted to merge `fungible` and `fungibles`
implementations or two `fungibles` implementations into a single type
implementing `fungibles`.

This also addresses an issue where `ItemOf` initiates a double drop for
an imbalance type, leading to inaccurate total issuance accounting.

Find the application of these types in this PR -
[link](paritytech#2031), places in
code -
[1](https://github.com/paritytech/polkadot-sdk/blob/4ec7496fa2632385b08fae860fcf28a523a7b5de/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs#L327),
[2](https://github.com/paritytech/polkadot-sdk/blob/4ec7496fa2632385b08fae860fcf28a523a7b5de/cumulus/parachains/runtimes/assets/asset-hub-kusama/src/lib.rs#L343).

---------

Co-authored-by: Liam Aharon <liam.aharon@hotmail.com>
Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: joepetrowski <joe@parity.io>
  • Loading branch information
5 people authored Dec 19, 2023
1 parent 81156d0 commit 0b74812
Show file tree
Hide file tree
Showing 15 changed files with 2,353 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,18 @@ where
}
}

fn should_touch(asset_id: MultiLocation, who: &AccountId) -> bool {
if let Some(asset_id) = LocalAssetIdConverter::convert(&asset_id) {
Assets::should_touch(asset_id, who)
} else {
ForeignAssets::should_touch(asset_id, who)
}
}

fn touch(
asset_id: MultiLocation,
who: AccountId,
depositor: AccountId,
who: &AccountId,
depositor: &AccountId,
) -> Result<(), DispatchError> {
if let Some(asset_id) = LocalAssetIdConverter::convert(&asset_id) {
Assets::touch(asset_id, who, depositor)
Expand Down
14 changes: 14 additions & 0 deletions prdoc/pr_2033.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: "`UnionOf` types for merged `fungible` and `fungibles` implementations"

doc:
- audience: Runtime Dev
description: |
Introduces `UnionOf` types, crafted to merge `fungible` and `fungibles` implementations or two
`fungibles` implementations into a single type implementing `fungibles`. This also addresses
an issue where `ItemOf` initiates a double drop for an imbalance type, leading to inaccurate
total issuance accounting.

crates: [ ]
6 changes: 3 additions & 3 deletions substrate/frame/asset-conversion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,15 +417,15 @@ pub mod pallet {
match T::MultiAssetIdConverter::try_convert(asset1) {
MultiAssetIdConversionResult::Converted(asset) =>
if !T::Assets::contains(&asset, &pool_account) {
T::Assets::touch(asset, pool_account.clone(), sender.clone())?
T::Assets::touch(asset, &pool_account, &sender)?
},
MultiAssetIdConversionResult::Unsupported(_) => Err(Error::<T>::UnsupportedAsset)?,
MultiAssetIdConversionResult::Native => (),
}
match T::MultiAssetIdConverter::try_convert(asset2) {
MultiAssetIdConversionResult::Converted(asset) =>
if !T::Assets::contains(&asset, &pool_account) {
T::Assets::touch(asset, pool_account.clone(), sender.clone())?
T::Assets::touch(asset, &pool_account, &sender)?
},
MultiAssetIdConversionResult::Unsupported(_) => Err(Error::<T>::UnsupportedAsset)?,
MultiAssetIdConversionResult::Native => (),
Expand All @@ -438,7 +438,7 @@ pub mod pallet {
NextPoolAssetId::<T>::set(Some(next_lp_token_id));

T::PoolAssets::create(lp_token.clone(), pool_account.clone(), false, 1u32.into())?;
T::PoolAssets::touch(lp_token.clone(), pool_account.clone(), sender.clone())?;
T::PoolAssets::touch(lp_token.clone(), &pool_account, &sender)?;

let pool_info = PoolInfo { lp_token: lp_token.clone() };
Pools::<T>::insert(pool_id.clone(), pool_info);
Expand Down
16 changes: 14 additions & 2 deletions substrate/frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1648,8 +1648,20 @@ pub mod pallet {
T::AssetAccountDeposit::get()
}

fn touch(asset: T::AssetId, who: T::AccountId, depositor: T::AccountId) -> DispatchResult {
Self::do_touch(asset, who, depositor, false)
fn should_touch(asset: T::AssetId, who: &T::AccountId) -> bool {
match Asset::<T, I>::get(&asset) {
Some(info) if info.is_sufficient => false,
Some(_) => !Account::<T, I>::contains_key(asset, who),
_ => true,
}
}

fn touch(
asset: T::AssetId,
who: &T::AccountId,
depositor: &T::AccountId,
) -> DispatchResult {
Self::do_touch(asset, who.clone(), depositor.clone(), false)
}
}

Expand Down
2 changes: 2 additions & 0 deletions substrate/frame/assets/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use pallet_balances::Error as BalancesError;
use sp_io::storage;
use sp_runtime::{traits::ConvertInto, TokenError};

mod sets;

fn asset_ids() -> Vec<u32> {
let mut s: Vec<_> = Assets::asset_ids().collect();
s.sort();
Expand Down
Loading

0 comments on commit 0b74812

Please sign in to comment.