Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Asset conversion nits #2860

Merged
merged 29 commits into from
Jul 21, 2023
Merged

Asset conversion nits #2860

merged 29 commits into from
Jul 21, 2023

Conversation

bkontur
Copy link
Contributor

@bkontur bkontur commented Jul 13, 2023

paritytech/substrate#14572

PR:

  • fixes benchmarks for pallet_asset_conversion - create_pool error
  • small refactor to reuse conversion stuff for assetId vs MultiLocation
  • Allow set_storage for AllowMultiAssetPools / LiquidityWithdrawalFee
  • small nits

TODOs

Copy link
Contributor

@joepetrowski joepetrowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have some pools with parents: 0. I don't mind nuking them on Westend, but could you either provide a migration or a set_storage call to sudo?

@bkontur bkontur added B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. labels Jul 14, 2023
@bkontur
Copy link
Contributor Author

bkontur commented Jul 14, 2023

bot fmt

@command-bot
Copy link

command-bot bot commented Jul 14, 2023

@bkontur https://gitlab.parity.io/parity/mirrors/cumulus/-/jobs/3194070 was started for your command "$PIPELINE_SCRIPTS_DIR/commands/fmt/fmt.sh". Check out https://gitlab.parity.io/parity/mirrors/cumulus/-/pipelines?page=1&scope=all&username=group_605_bot to know what else is being executed currently.

Comment bot cancel 1-4748247b-48e4-4946-a07c-2569cc0cff3a to cancel this command or bot cancel to cancel all commands in this pull request.

@command-bot
Copy link

command-bot bot commented Jul 14, 2023

@bkontur Command "$PIPELINE_SCRIPTS_DIR/commands/fmt/fmt.sh" has finished. Result: https://gitlab.parity.io/parity/mirrors/cumulus/-/jobs/3194070 has finished. If any artifacts were generated, you can download them from https://gitlab.parity.io/parity/mirrors/cumulus/-/jobs/3194070/artifacts/download.

@command-bot
Copy link

command-bot bot commented Jul 17, 2023

@bkontur https://gitlab.parity.io/parity/mirrors/cumulus/-/jobs/3203923 was started for your command "$PIPELINE_SCRIPTS_DIR/commands/bench/bench.sh" pallet dev pallet_asset_conversion. Check out https://gitlab.parity.io/parity/mirrors/cumulus/-/pipelines?page=1&scope=all&username=group_605_bot to know what else is being executed currently.

Comment bot cancel 10-91ba7436-8498-4481-a1a3-3f9de0540048 to cancel this command or bot cancel to cancel all commands in this pull request.

@command-bot
Copy link

command-bot bot commented Jul 17, 2023

@bkontur Command "$PIPELINE_SCRIPTS_DIR/commands/bench/bench.sh" pallet dev pallet_asset_conversion has finished. Result: https://gitlab.parity.io/parity/mirrors/cumulus/-/jobs/3203923 has finished. If any artifacts were generated, you can download them from https://gitlab.parity.io/parity/mirrors/cumulus/-/jobs/3203923/artifacts/download.

@bkontur bkontur marked this pull request as ready for review July 18, 2023 20:34
@bkontur bkontur added the A0-please_review Pull request needs code review. label Jul 18, 2023
Copy link
Contributor

@joepetrowski joepetrowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks OK, @jsidorenko can check for the migration especially?

@bkontur
Copy link
Contributor Author

bkontur commented Jul 19, 2023

Looks OK, @jsidorenko can check for the migration especially?

for that migration, I have a working test:

pub use asset_hub_westend_runtime::{AssetConversion, PoolAssets};

#[test]
fn test_migration() {
	use frame_support::traits::{
		fungible::Inspect, fungibles::roles::Inspect as InspectRoles, OnRuntimeUpgrade,
	};

	let account = AccountId::from(ALICE);
	let existential_deposit = ExistentialDeposit::get();

	ExtBuilder::<Runtime>::default()
		.with_collators(vec![AccountId::from(ALICE)])
		.with_session_keys(vec![(
			AccountId::from(ALICE),
			AccountId::from(ALICE),
			SessionKeys { aura: AuraId::from(sp_core::sr25519::Public::from_raw(ALICE)) },
		)])
		.with_balances(vec![
			(account.clone(), existential_deposit * AssetDeposit::get() * 100),
		])
		.build()
		.execute_with(|| {
			const ASSET_ID: u32 = 1;
			let asset_native = Box::new(MultiLocation::new(0, Here));
			let asset_one = Box::new(MultiLocation {
				parents: 0,
				interior: X2(PalletInstance(50), GeneralIndex(ASSET_ID.into())),
			});
			assert_ok!(Assets::create(
				RuntimeHelper::<Runtime>::origin_of(account.clone()),
				ASSET_ID.into(),
				account.clone().into(),
				1000,
			));
			assert_ok!(Assets::mint(
				RuntimeHelper::<Runtime>::origin_of(account.clone()),
				ASSET_ID.into(),
				account.clone().into(),
				3_000_000_000_000,
			));
			assert_ok!(AssetConversion::create_pool(
				RuntimeHelper::<Runtime>::origin_of(account.clone()),
				asset_native.clone(),
				asset_one.clone(),
			));
			assert_ok!(AssetConversion::add_liquidity(
				RuntimeHelper::<Runtime>::origin_of(account.clone()),
				asset_native.clone(),
				asset_one.clone(),
				1_000_000_000_000,
				2_000_000_000_000,
				0,
				0,
				account.into()
			));

			let lp_token = PoolAssets::asset_ids().next().unwrap();

			let old_pool_id = AssetConversion::get_pool_id(asset_native.clone(), asset_one.clone());
			let old_pool_account = AssetConversion::get_pool_account(&old_pool_id);
			let new_pool_id = AssetConversion::get_pool_id(Box::new(WestendLocation::get()), asset_one.clone());
			let new_pool_account = AssetConversion::get_pool_account(&new_pool_id);

			assert_ne!(0, Balances::balance(&old_pool_account));
			assert_eq!(0, Balances::balance(&new_pool_account));
			assert_ne!(0, Assets::balance(ASSET_ID, old_pool_account.clone()));
			assert_eq!(0, Assets::balance(ASSET_ID, new_pool_account.clone()));
			assert_ne!(0, PoolAssets::balance(lp_token, old_pool_account.clone()));
			assert_eq!(0, PoolAssets::balance(lp_token, new_pool_account.clone()));
			assert_eq!(Some(old_pool_account.clone()), PoolAssets::owner(lp_token));
                        assert!(pallet_asset_conversion::Pools::<Runtime>::contains_key(&old_pool_id));
			assert!(!pallet_asset_conversion::Pools::<Runtime>::contains_key(&new_pool_id));
			println!("before: {:?}", PoolAssets::owner(lp_token));

			asset_hub_westend_runtime::migrations::NativeAssetParents0ToParents1Migration::<Runtime>::on_runtime_upgrade();

			assert_eq!(0, Balances::balance(&old_pool_account));
			assert_ne!(0, Balances::balance(&new_pool_account));
			assert_eq!(0, Assets::balance(ASSET_ID, old_pool_account.clone()));
			assert_ne!(0, Assets::balance(ASSET_ID, new_pool_account.clone()));
			assert_eq!(0, PoolAssets::balance(lp_token, old_pool_account.clone()));
			assert_ne!(0, PoolAssets::balance(lp_token, new_pool_account.clone()));
			assert_eq!(Some(new_pool_account), PoolAssets::owner(lp_token));
                        assert!(!pallet_asset_conversion::Pools::<Runtime>::contains_key(&old_pool_id));
			assert!(pallet_asset_conversion::Pools::<Runtime>::contains_key(&new_pool_id));
			println!("after: {:?}", PoolAssets::owner(lp_token));
		})
}

but it only works with this hardcoded hack, so I better not pushed it

	fn is_native(asset_id: &Box<MultiLocation>) -> bool {
		*asset_id == Self::get_native() ||
                        // THIS IS HACK FOR MIGRATION
			*asset_id == Box::new(MultiLocation::new(0, xcm::latest::prelude::Here))
	}

@bkontur
Copy link
Contributor Author

bkontur commented Jul 19, 2023

Looks OK, @jsidorenko can check for the migration especially?

for that migration, I have a working test:

pub use asset_hub_westend_runtime::{AssetConversion, PoolAssets};

#[test]
fn test_migration() {
	use frame_support::traits::{
		fungible::Inspect, fungibles::roles::Inspect as InspectRoles, OnRuntimeUpgrade,
	};

	let account = AccountId::from(ALICE);
	let existential_deposit = ExistentialDeposit::get();

	ExtBuilder::<Runtime>::default()
		.with_collators(vec![AccountId::from(ALICE)])
		.with_session_keys(vec![(
			AccountId::from(ALICE),
			AccountId::from(ALICE),
			SessionKeys { aura: AuraId::from(sp_core::sr25519::Public::from_raw(ALICE)) },
		)])
		.with_balances(vec![
			(account.clone(), existential_deposit * AssetDeposit::get() * 100),
		])
		.build()
		.execute_with(|| {
			const ASSET_ID: u32 = 1;
			let asset_native = Box::new(MultiLocation::new(0, Here));
			let asset_one = Box::new(MultiLocation {
				parents: 0,
				interior: X2(PalletInstance(50), GeneralIndex(ASSET_ID.into())),
			});
			assert_ok!(Assets::create(
				RuntimeHelper::<Runtime>::origin_of(account.clone()),
				ASSET_ID.into(),
				account.clone().into(),
				1000,
			));
			assert_ok!(Assets::mint(
				RuntimeHelper::<Runtime>::origin_of(account.clone()),
				ASSET_ID.into(),
				account.clone().into(),
				3_000_000_000_000,
			));
			assert_ok!(AssetConversion::create_pool(
				RuntimeHelper::<Runtime>::origin_of(account.clone()),
				asset_native.clone(),
				asset_one.clone(),
			));
			assert_ok!(AssetConversion::add_liquidity(
				RuntimeHelper::<Runtime>::origin_of(account.clone()),
				asset_native.clone(),
				asset_one.clone(),
				1_000_000_000_000,
				2_000_000_000_000,
				0,
				0,
				account.into()
			));

			let lp_token = PoolAssets::asset_ids().next().unwrap();

			let old_pool_id = AssetConversion::get_pool_id(asset_native.clone(), asset_one.clone());
			let old_pool_account = AssetConversion::get_pool_account(&old_pool_id);
			let new_pool_id = AssetConversion::get_pool_id(Box::new(WestendLocation::get()), asset_one.clone());
			let new_pool_account = AssetConversion::get_pool_account(&new_pool_id);

			assert_ne!(0, Balances::balance(&old_pool_account));
			assert_eq!(0, Balances::balance(&new_pool_account));
			assert_ne!(0, Assets::balance(ASSET_ID, old_pool_account.clone()));
			assert_eq!(0, Assets::balance(ASSET_ID, new_pool_account.clone()));
			assert_ne!(0, PoolAssets::balance(lp_token, old_pool_account.clone()));
			assert_eq!(0, PoolAssets::balance(lp_token, new_pool_account.clone()));
			assert_eq!(Some(old_pool_account.clone()), PoolAssets::owner(lp_token));
			println!("before: {:?}", PoolAssets::owner(lp_token));

			asset_hub_westend_runtime::migrations::NativeAssetParents0ToParents1Migration::<Runtime>::on_runtime_upgrade();

			assert_eq!(0, Balances::balance(&old_pool_account));
			assert_ne!(0, Balances::balance(&new_pool_account));
			assert_eq!(0, Assets::balance(ASSET_ID, old_pool_account.clone()));
			assert_ne!(0, Assets::balance(ASSET_ID, new_pool_account.clone()));
			assert_eq!(0, PoolAssets::balance(lp_token, old_pool_account.clone()));
			assert_ne!(0, PoolAssets::balance(lp_token, new_pool_account.clone()));
			assert_eq!(Some(new_pool_account), PoolAssets::owner(lp_token));
			println!("after: {:?}", PoolAssets::owner(lp_token));
		})
}

but it only works with this hardcoded hack, so I better not pushed it

	fn is_native(asset_id: &Box<MultiLocation>) -> bool {
		*asset_id == Self::get_native() ||
                        // THIS IS HACK FOR MIGRATION
			*asset_id == Box::new(MultiLocation::new(0, xcm::latest::prelude::Here))
	}

actually, the migration misses the real pool_id change, omg... I am on it

@bkontur
Copy link
Contributor Author

bkontur commented Jul 21, 2023

bot rebase

@paritytech-processbot
Copy link

Rebased

@paritytech-processbot
Copy link

Merge cancelled due to error. Error: Command 'Command { std: cd "/storage/repositories/cumulus" && "git" "push" "paritytech" "bko-asset-conv", kill_on_drop: false }' failed with status Some(128); output: remote: Internal Server Error
fatal: unable to access 'https://github.com/paritytech/cumulus.git/': The requested URL returned error: 500

@paritytech-ci paritytech-ci requested a review from a team July 21, 2023 14:46
@bkchr bkchr merged commit 1ac85ed into master Jul 21, 2023
@bkchr bkchr deleted the bko-asset-conv branch July 21, 2023 21:53
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants