Skip to content

Commit

Permalink
Adjust XCM to use Currencies (zeitgeist runtime)
Browse files Browse the repository at this point in the history
  • Loading branch information
sea212 committed Jan 16, 2024
1 parent 0b019e0 commit a3339eb
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 48 deletions.
5 changes: 4 additions & 1 deletion runtime/zeitgeist/src/integration_tests/xcm/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use xcm::{
latest::{Junction::Parachain, Junctions::X2, MultiLocation},
VersionedMultiLocation,
};
use zeitgeist_primitives::types::{Asset, Currencies, CustomMetadata};
use zeitgeist_primitives::types::{Currencies, CustomMetadata};

pub(super) struct ExtBuilder {
balances: Vec<(AccountId, Assets, Balance)>,
Expand Down Expand Up @@ -71,6 +71,9 @@ impl ExtBuilder {
.balances
.into_iter()
.filter(|(_, currency_id, _)| *currency_id != native_currency_id)
.map(|(account_id, currency_id, initial_balance)| {
(account_id, currency_id.try_into().unwrap(), initial_balance)
})
.collect::<Vec<_>>(),
}
.assimilate_storage(&mut t)
Expand Down
4 changes: 2 additions & 2 deletions runtime/zeitgeist/src/integration_tests/xcm/test_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ pub(super) fn para_ext(parachain_id: u32) -> sp_io::TestExternalities {
ExtBuilder::default()
.set_balances(vec![
(ALICE, Assets::Ztg, ztg(10)),
(ALICE, FOREIGN_PARENT_ID, dot(10)),
(ZeitgeistTreasuryAccount::get(), FOREIGN_PARENT_ID, dot(10)),
(ALICE, FOREIGN_PARENT_ID.into(), dot(10)),
(ZeitgeistTreasuryAccount::get(), FOREIGN_PARENT_ID.into(), dot(10)),
])
.set_parachain_id(parachain_id)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@ fn convert_any_registered_parent_multilocation() {
foreign_parent_multilocation()
);

assert_eq!(<AssetConvert as C2<_, _>>::convert(FOREIGN_PARENT_ID), None);
assert_eq!(<AssetConvert as C2<_, _>>::convert(Assets::from(FOREIGN_PARENT_ID)), None);

// Register parent as foreign asset in the Zeitgeist parachain
register_foreign_parent(None);

assert_eq!(
<AssetConvert as C1<_, _>>::convert(foreign_parent_multilocation()),
Ok(FOREIGN_PARENT_ID),
Ok(FOREIGN_PARENT_ID.into()),
);

assert_eq!(
<AssetConvert as C2<_, _>>::convert(FOREIGN_PARENT_ID),
<AssetConvert as C2<_, _>>::convert(Assets::from(FOREIGN_PARENT_ID)),
Some(foreign_parent_multilocation())
);
});
Expand All @@ -86,18 +86,18 @@ fn convert_any_registered_sibling_multilocation() {
foreign_sibling_multilocation()
);

assert_eq!(<AssetConvert as C2<_, _>>::convert(FOREIGN_SIBLING_ID), None);
assert_eq!(<AssetConvert as C2<_, _>>::convert(Assets::from(FOREIGN_SIBLING_ID)), None);

// Register sibling as foreign asset in the Zeitgeist parachain
register_foreign_sibling(None);

assert_eq!(
<AssetConvert as C1<_, _>>::convert(foreign_sibling_multilocation()),
Ok(FOREIGN_SIBLING_ID),
Ok(FOREIGN_SIBLING_ID.into()),
);

assert_eq!(
<AssetConvert as C2<_, _>>::convert(FOREIGN_SIBLING_ID),
<AssetConvert as C2<_, _>>::convert(Assets::from(FOREIGN_SIBLING_ID)),
Some(foreign_sibling_multilocation())
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn transfer_ztg_sibling_to_zeitgeist() {
assert_eq!(Tokens::free_balance(FOREIGN_ZTG_ID, &BOB), bob_initial_balance);
assert_ok!(XTokens::transfer(
RuntimeOrigin::signed(BOB),
FOREIGN_ZTG_ID,
FOREIGN_ZTG_ID.into(),
transfer_amount,
Box::new(
MultiLocation::new(
Expand Down Expand Up @@ -250,7 +250,7 @@ fn transfer_btc_zeitgeist_to_sibling() {
Zeitgeist::execute_with(|| {
assert_ok!(XTokens::transfer(
RuntimeOrigin::signed(ALICE),
BTC_ID,
BTC_ID.into(),
transfer_amount,
Box::new(
MultiLocation::new(
Expand Down Expand Up @@ -371,7 +371,7 @@ fn transfer_eth_zeitgeist_to_sibling() {
Zeitgeist::execute_with(|| {
assert_ok!(XTokens::transfer(
RuntimeOrigin::signed(ALICE),
ETH_ID,
ETH_ID.into(),
transfer_amount,
Box::new(
MultiLocation::new(
Expand Down Expand Up @@ -446,7 +446,7 @@ fn transfer_dot_to_relay_chain() {

assert_ok!(XTokens::transfer(
RuntimeOrigin::signed(ALICE),
FOREIGN_PARENT_ID,
FOREIGN_PARENT_ID.into(),
transfer_amount,
Box::new(
MultiLocation::new(1, X1(Junction::AccountId32 { id: BOB.into(), network: None }))
Expand Down
2 changes: 1 addition & 1 deletion runtime/zeitgeist/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ parameter_type_with_key! {
Currencies::ForeignAsset(id) => {
let maybe_metadata = <
orml_asset_registry::Pallet<Runtime> as orml_traits::asset_registry::Inspect
>::metadata(&Asset::ForeignAsset(*id));
>::metadata(&Currencies::ForeignAsset(*id));

if let Some(metadata) = maybe_metadata {
return metadata.existential_deposit;
Expand Down
12 changes: 6 additions & 6 deletions runtime/zeitgeist/src/xcm_config/asset_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with Zeitgeist. If not, see <https://www.gnu.org/licenses/>.

use crate::{Assets, Balance};
use crate::{Balance, Currencies};
use orml_traits::asset_registry::{AssetMetadata, AssetProcessor};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
Expand All @@ -29,21 +29,21 @@ use zeitgeist_primitives::types::CustomMetadata;
/// Only pre check is to ensure an asset id was passed.
pub struct CustomAssetProcessor;

impl AssetProcessor<Assets, AssetMetadata<Balance, CustomMetadata>> for CustomAssetProcessor {
impl AssetProcessor<Currencies, AssetMetadata<Balance, CustomMetadata>> for CustomAssetProcessor {
fn pre_register(
id: Option<Assets>,
id: Option<Currencies>,
metadata: AssetMetadata<Balance, CustomMetadata>,
) -> Result<(Assets, AssetMetadata<Balance, CustomMetadata>), DispatchError> {
) -> Result<(Currencies, AssetMetadata<Balance, CustomMetadata>), DispatchError> {
match id {
Some(id) => Ok((id, metadata)),
None => Err(DispatchError::Other("asset-registry: AssetId is required")),
}
}

fn post_register(
_id: Assets,
_id: Currencies,
_asset_metadata: AssetMetadata<Balance, CustomMetadata>,
) -> Result<(), DispatchError> {
Ok(())
}
}
}
74 changes: 46 additions & 28 deletions runtime/zeitgeist/src/xcm_config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ use xcm_builder::{
TakeWeightCredit,
};
use xcm_executor::{traits::TransactAsset, Assets as ExecutorAssets, Config};
use zeitgeist_primitives::{constants::BalanceFractionalDecimals, types::Asset};
use zeitgeist_primitives::{
constants::BalanceFractionalDecimals,
types::{Asset, Currencies},
};

pub mod zeitgeist {
#[cfg(test)]
Expand Down Expand Up @@ -206,7 +209,7 @@ pub struct AlignedFractionalTransactAsset<
}

impl<
AssetRegistry: Inspect<AssetId = Assets>,
AssetRegistry: Inspect<AssetId = Currencies>,
FracDecPlaces: Get<u8>,
CurrencyIdConvert: Convert<MultiAsset, Option<Assets>>,
TransactAssetDelegate: TransactAsset,
Expand All @@ -219,37 +222,49 @@ impl<
>
{
fn adjust_fractional_places(asset: &MultiAsset) -> MultiAsset {
if let Some(ref asset_id) = CurrencyIdConvert::convert(asset.clone()) {
if let Fungible(amount) = asset.fun {
let mut asset_updated = asset.clone();
let native_decimals = u32::from(FracDecPlaces::get());
let metadata = AssetRegistry::metadata(asset_id);

if let Some(metadata) = metadata {
let decimals = metadata.decimals;

asset_updated.fun = if decimals > native_decimals {
let power = decimals.saturating_sub(native_decimals);
let adjust_factor = 10u128.saturating_pow(power);
// Floors the adjusted token amount, thus no tokens are generated
Fungible(amount.saturating_div(adjust_factor))
} else {
let power = native_decimals.saturating_sub(decimals);
let adjust_factor = 10u128.saturating_pow(power);
Fungible(amount.saturating_mul(adjust_factor))
};

return asset_updated;
let (asset_id, amount) =
if let Some(ref asset_id) = CurrencyIdConvert::convert(asset.clone()) {
if let Fungible(amount) = asset.fun {
(asset_id.clone(), amount)
} else {
return asset.clone();
}
}
} else {
return asset.clone();
};

let currency = if let Ok(currency) = Currencies::try_from(asset_id) {
currency
} else {
return asset.clone();
};

let metadata = AssetRegistry::metadata(&currency);
if let Some(metadata) = metadata {
let mut asset_adjusted = asset.clone();
let decimals = metadata.decimals;
let native_decimals = u32::from(FracDecPlaces::get());

asset_adjusted.fun = if decimals > native_decimals {
let power = decimals.saturating_sub(native_decimals);
let adjust_factor = 10u128.saturating_pow(power);
// Floors the adjusted token amount, thus no tokens are generated
Fungible(amount.saturating_div(adjust_factor))
} else {
let power = native_decimals.saturating_sub(decimals);
let adjust_factor = 10u128.saturating_pow(power);
Fungible(amount.saturating_mul(adjust_factor))
};

return asset_adjusted;
}

asset.clone()
}
}

impl<
AssetRegistry: Inspect<AssetId = Assets>,
AssetRegistry: Inspect<AssetId = Currencies>,
CurrencyIdConvert: Convert<MultiAsset, Option<Assets>>,
FracDecPlaces: Get<u8>,
TransactAssetDelegate: TransactAsset,
Expand Down Expand Up @@ -337,7 +352,10 @@ impl Convert<Assets, Option<MultiLocation>> for AssetConvert {
general_key(zeitgeist::KEY),
),
)),
Asset::ForeignAsset(_) => AssetRegistry::multilocation(&id).ok()?,
Asset::ForeignAsset(_) => {
let currency = Currencies::try_from(id).ok()?;
AssetRegistry::multilocation(&currency).ok()?
}
_ => None,
}
}
Expand Down Expand Up @@ -372,9 +390,9 @@ impl xcm_executor::traits::Convert<MultiLocation, Assets> for AssetConvert {
return Err(location);
}

AssetRegistry::location_to_asset_id(location).ok_or(location)
AssetRegistry::location_to_asset_id(location).ok_or(location).map(|a| a.into())
}
_ => AssetRegistry::location_to_asset_id(location).ok_or(location),
_ => AssetRegistry::location_to_asset_id(location).ok_or(location).map(|a| a.into()),
}
}
}
Expand Down

0 comments on commit a3339eb

Please sign in to comment.