From 7c4f004125f48745d562931861c51cf0c57e67cf Mon Sep 17 00:00:00 2001 From: adelarja Date: Tue, 11 Jul 2023 15:49:23 -0300 Subject: [PATCH 1/2] Replaced CurrencyId enum by Spacewalk Primitives CurrencyId. --- runtime/pendulum/Cargo.toml | 3 ++ runtime/pendulum/src/currency.rs | 31 ----------------- runtime/pendulum/src/lib.rs | 5 ++- runtime/pendulum/src/xcm_config.rs | 3 +- runtime/pendulum/src/zenlink.rs | 56 ++++++------------------------ 5 files changed, 18 insertions(+), 80 deletions(-) delete mode 100644 runtime/pendulum/src/currency.rs diff --git a/runtime/pendulum/Cargo.toml b/runtime/pendulum/Cargo.toml index 2af166323..51a5f06e2 100644 --- a/runtime/pendulum/Cargo.toml +++ b/runtime/pendulum/Cargo.toml @@ -25,6 +25,9 @@ smallvec = "1.9.0" # Local runtime-common = {path = "../common", default-features = false} +# Spacewalk libraries +spacewalk-primitives = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "06873f16c4a7ab884e4759d98b7b86c712e0c0ae"} + # Substrate frame-benchmarking = {git = "https://github.com/paritytech/substrate", default-features = false, optional = true, branch = "polkadot-v0.9.40"} frame-executive = {git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.40"} diff --git a/runtime/pendulum/src/currency.rs b/runtime/pendulum/src/currency.rs deleted file mode 100644 index f5e0558e5..000000000 --- a/runtime/pendulum/src/currency.rs +++ /dev/null @@ -1,31 +0,0 @@ -use codec::{Decode, Encode, MaxEncodedLen}; -use scale_info::TypeInfo; -use sp_runtime::RuntimeDebug; - -#[cfg(feature = "std")] -use serde::{Deserialize, Serialize}; - -#[derive( - Encode, - Decode, - MaxEncodedLen, - Eq, - PartialEq, - Copy, - Clone, - PartialOrd, - RuntimeDebug, - Ord, - TypeInfo, -)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -pub enum CurrencyId { - Native, - XCM(u8), -} - -impl Default for CurrencyId { - fn default() -> Self { - CurrencyId::Native - } -} diff --git a/runtime/pendulum/src/lib.rs b/runtime/pendulum/src/lib.rs index 5eb37a129..be1b2eede 100644 --- a/runtime/pendulum/src/lib.rs +++ b/runtime/pendulum/src/lib.rs @@ -6,7 +6,6 @@ #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); -mod currency; mod weights; pub mod xcm_config; pub mod zenlink; @@ -14,8 +13,8 @@ use crate::zenlink::*; use xcm::v3::MultiLocation; use zenlink_protocol::{AssetBalance, MultiAssetsHandler, PairInfo}; -pub use currency::CurrencyId as PendulumCurrencyId; pub use parachain_staking::InflationInfo; +pub use spacewalk_primitives::CurrencyId as PendulumCurrencyId; use codec::Encode; @@ -65,9 +64,9 @@ use dia_oracle::DiaOracle; use xcm_config::{XcmConfig, XcmOriginToTransactDispatchOrigin}; -use currency::CurrencyId; use orml_currencies::BasicCurrencyAdapter; use orml_traits::{currency::MutationHooks, parameter_type_with_key}; +use spacewalk_primitives::CurrencyId; const CONTRACTS_DEBUG_OUTPUT: bool = true; diff --git a/runtime/pendulum/src/xcm_config.rs b/runtime/pendulum/src/xcm_config.rs index 161a1ee23..318ebe393 100644 --- a/runtime/pendulum/src/xcm_config.rs +++ b/runtime/pendulum/src/xcm_config.rs @@ -1,5 +1,5 @@ use super::{ - AccountId, Balance, Balances, CurrencyId, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, + AccountId, Balance, Balances, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, Tokens, WeightToFee, XcmpQueue, }; use crate::ConstU32; @@ -17,6 +17,7 @@ use polkadot_parachain::primitives::Sibling; use polkadot_runtime_common::impls::ToAuthor; use runtime_common::parachains::polkadot::statemint; use sp_runtime::traits::Convert; +use spacewalk_primitives::CurrencyId; use xcm::latest::{prelude::*, Weight as XCMWeight}; use xcm_builder::{ AccountId32Aliases, AllowUnpaidExecutionFrom, ConvertedConcreteId, EnsureXcmOrigin, diff --git a/runtime/pendulum/src/zenlink.rs b/runtime/pendulum/src/zenlink.rs index 34a91e47d..1d25fecfc 100644 --- a/runtime/pendulum/src/zenlink.rs +++ b/runtime/pendulum/src/zenlink.rs @@ -6,9 +6,9 @@ use orml_traits::MultiCurrency; use sp_runtime::{DispatchError, DispatchResult}; use sp_std::marker::PhantomData; +use spacewalk_primitives::CurrencyId; use zenlink_protocol::{ - AssetId, Config as ZenlinkConfig, LocalAssetHandler, PairLpGenerate, ZenlinkMultiAssets, LOCAL, - NATIVE, + AssetId, Config as ZenlinkConfig, LocalAssetHandler, PairLpGenerate, ZenlinkMultiAssets, }; pub type ZenlinkAssetId = AssetId; @@ -38,7 +38,7 @@ where Local: MultiCurrency, { fn local_balance_of(asset_id: ZenlinkAssetId, who: &AccountId) -> AssetBalance { - if let Ok(currency_id) = asset_id.try_into() { + if let Ok(currency_id) = zenlink_id_to_currency_id(asset_id) { return TryInto::::try_into(Local::free_balance(currency_id, &who)) .unwrap_or_default() } @@ -46,7 +46,7 @@ where } fn local_total_supply(asset_id: ZenlinkAssetId) -> AssetBalance { - if let Ok(currency_id) = asset_id.try_into() { + if let Ok(currency_id) = zenlink_id_to_currency_id(asset_id) { return TryInto::::try_into(Local::total_issuance(currency_id)) .unwrap_or_default() } @@ -54,7 +54,7 @@ where } fn local_is_exists(asset_id: ZenlinkAssetId) -> bool { - let currency_id: Result = asset_id.try_into(); + let currency_id: Result = zenlink_id_to_currency_id(asset_id); match currency_id { Ok(_) => true, Err(_) => false, @@ -67,7 +67,7 @@ where target: &AccountId, amount: AssetBalance, ) -> DispatchResult { - if let Ok(currency_id) = asset_id.try_into() { + if let Ok(currency_id) = zenlink_id_to_currency_id(asset_id) { Local::transfer( currency_id, &origin, @@ -86,7 +86,7 @@ where origin: &AccountId, amount: AssetBalance, ) -> Result { - if let Ok(currency_id) = asset_id.try_into() { + if let Ok(currency_id) = zenlink_id_to_currency_id(asset_id) { Local::deposit( currency_id, &origin, @@ -106,7 +106,7 @@ where origin: &AccountId, amount: AssetBalance, ) -> Result { - if let Ok(currency_id) = asset_id.try_into() { + if let Ok(currency_id) = zenlink_id_to_currency_id(asset_id) { Local::withdraw( currency_id, &origin, @@ -122,41 +122,7 @@ where } } -impl TryFrom for ZenlinkAssetId { - type Error = (); - - fn try_from(currency_id: CurrencyId) -> Result { - let para_chain_id: u32 = ParachainInfo::parachain_id().into(); - match currency_id { - CurrencyId::Native => - Ok(ZenlinkAssetId { chain_id: para_chain_id, asset_type: NATIVE, asset_index: 0 }), - CurrencyId::XCM(xcm) => Ok(ZenlinkAssetId { - chain_id: para_chain_id, - asset_type: LOCAL, - asset_index: xcm as u64, - }), - } - } -} - -impl TryFrom for CurrencyId { - type Error = (); - fn try_from(asset_id: ZenlinkAssetId) -> Result { - let para_chain_id: u32 = ParachainInfo::parachain_id().into(); - if asset_id.chain_id != para_chain_id { - return Err(()) - } - - match asset_id.asset_type { - NATIVE => Ok(CurrencyId::Native), - LOCAL => { - let foreign_currency_id_option = asset_id.asset_index.try_into(); - match foreign_currency_id_option { - Ok(foreign_currency_id) => Ok(CurrencyId::XCM(foreign_currency_id)), - Err(_) => Err(()), - } - }, - _ => Err(()), - } - } +// This will be replaced with PR 241 +pub fn zenlink_id_to_currency_id(asset_id: ZenlinkAssetId) -> Result { + Ok(CurrencyId::Native) } From c30785e70fbf7239a79a640f363add64c203c2d1 Mon Sep 17 00:00:00 2001 From: adelarja Date: Wed, 12 Jul 2023 14:53:25 -0300 Subject: [PATCH 2/2] Replaced PendulumCurrencyId alias for CurrencyId --- .../integration-tests/pendulum/src/setup.rs | 8 ++-- .../integration-tests/pendulum/src/tests.rs | 43 ++++++------------- runtime/pendulum/src/lib.rs | 4 +- runtime/pendulum/src/xcm_config.rs | 3 +- runtime/pendulum/src/zenlink.rs | 1 - 5 files changed, 20 insertions(+), 39 deletions(-) diff --git a/runtime/integration-tests/pendulum/src/setup.rs b/runtime/integration-tests/pendulum/src/setup.rs index c316b4649..115cb38ff 100644 --- a/runtime/integration-tests/pendulum/src/setup.rs +++ b/runtime/integration-tests/pendulum/src/setup.rs @@ -1,6 +1,6 @@ use crate::{PENDULUM_ID, STATEMINT_ID}; use frame_support::traits::GenesisBuild; -use pendulum_runtime::{PendulumCurrencyId, Runtime, System}; +use pendulum_runtime::{CurrencyId, Runtime, System}; use polkadot_core_primitives::{AccountId, Balance}; pub fn units(amount: Balance) -> Balance { @@ -12,7 +12,7 @@ pub const BOB: [u8; 32] = [5u8; 32]; pub const INITIAL_BALANCE: u128 = 1_000_000_000; pub struct ExtBuilderPendulum { - balances: Vec<(AccountId, PendulumCurrencyId, Balance)>, + balances: Vec<(AccountId, CurrencyId, Balance)>, parachain_id: u32, } @@ -23,7 +23,7 @@ impl Default for ExtBuilderPendulum { } impl ExtBuilderPendulum { - pub fn balances(mut self, balances: Vec<(AccountId, PendulumCurrencyId, Balance)>) -> Self { + pub fn balances(mut self, balances: Vec<(AccountId, CurrencyId, Balance)>) -> Self { self.balances = balances; self } @@ -45,7 +45,7 @@ impl ExtBuilderPendulum { .unwrap(); orml_tokens::GenesisConfig:: { - balances: vec![(AccountId::from(BOB), PendulumCurrencyId::XCM(0), units(100))], + balances: vec![(AccountId::from(BOB), CurrencyId::XCM(0), units(100))], } .assimilate_storage(&mut t) .unwrap(); diff --git a/runtime/integration-tests/pendulum/src/tests.rs b/runtime/integration-tests/pendulum/src/tests.rs index d81fee64a..da518da2a 100644 --- a/runtime/integration-tests/pendulum/src/tests.rs +++ b/runtime/integration-tests/pendulum/src/tests.rs @@ -3,7 +3,7 @@ use frame_support::{ assert_ok, traits::{fungible::Mutate, fungibles::Inspect, Currency}, }; -use pendulum_runtime::{Balances, PendulumCurrencyId, RuntimeOrigin, Tokens, XTokens}; +use pendulum_runtime::{Balances, CurrencyId, RuntimeOrigin, Tokens, XTokens}; use sp_runtime::{traits::AccountIdConversion, MultiAddress}; use xcm::latest::{Junction, Junction::*, Junctions::*, MultiLocation, NetworkId, WeightLimit}; use xcm_emulator::TestExt; @@ -25,10 +25,8 @@ fn transfer_dot_from_relay_chain_to_pendulum() { let transfer_amount: Balance = units(20); let mut orml_tokens_before = 0; PendulumParachain::execute_with(|| { - orml_tokens_before = pendulum_runtime::Tokens::balance( - pendulum_runtime::PendulumCurrencyId::XCM(0), - &ALICE.into(), - ); + orml_tokens_before = + pendulum_runtime::Tokens::balance(pendulum_runtime::CurrencyId::XCM(0), &ALICE.into()); }); Relay::execute_with(|| { @@ -59,10 +57,7 @@ fn transfer_dot_from_relay_chain_to_pendulum() { PendulumParachain::execute_with(|| { assert_eq!( - pendulum_runtime::Tokens::balance( - pendulum_runtime::PendulumCurrencyId::XCM(0), - &ALICE.into() - ), + pendulum_runtime::Tokens::balance(pendulum_runtime::CurrencyId::XCM(0), &ALICE.into()), orml_tokens_before + transfer_amount - DOT_FEE_WHEN_TRANSFER_TO_PARACHAIN ); }); @@ -83,7 +78,7 @@ fn transfer_dot_from_pendulum_to_relay_chain() { PendulumParachain::execute_with(|| { assert_ok!(pendulum_runtime::XTokens::transfer( pendulum_runtime::RuntimeOrigin::signed(BOB.into()), - pendulum_runtime::PendulumCurrencyId::XCM(0), + pendulum_runtime::CurrencyId::XCM(0), transfer_dot_amount, Box::new( MultiLocation { parents: 1, interior: X1(AccountId32 { network: None, id: BOB }) } @@ -138,7 +133,7 @@ fn transfer_dot_from_pendulum_to_relay_chain() { }); } -//pendulum_runtime::PendulumCurrencyId::XCM(1) is the representation of USDT from Statemint on Pendulum chain. +//pendulum_runtime::CurrencyId::XCM(1) is the representation of USDT from Statemint on Pendulum chain. //The asset id for USDT on Statemint is 1984. and pendulum support only this asset id to recive it on chain. //we are going to execute XCM call to sent incorrect Asset Id and expect to see cumulus_pallet_xcmp_queue::Event::Fail event with an error FailedToTransactAsset. //we what to be sure that the initial USDT balance for BOB is the same after XCM call from statemint when we tried to send wrong ASSET_ID from system parachain. @@ -149,10 +144,7 @@ fn statemint_transfer_incorrect_asset_to_pendulum_should_fails() { let extected_base_usdt_balance = 0; PendulumParachain::execute_with(|| { assert_eq!( - pendulum_runtime::Tokens::balance( - pendulum_runtime::PendulumCurrencyId::XCM(1), - &BOB.into() - ), + pendulum_runtime::Tokens::balance(pendulum_runtime::CurrencyId::XCM(1), &BOB.into()), extected_base_usdt_balance ); }); @@ -220,10 +212,7 @@ fn statemint_transfer_incorrect_asset_to_pendulum_should_fails() { PendulumParachain::execute_with(|| { assert_eq!( - pendulum_runtime::Tokens::balance( - pendulum_runtime::PendulumCurrencyId::XCM(1), - &BOB.into() - ), + pendulum_runtime::Tokens::balance(pendulum_runtime::CurrencyId::XCM(1), &BOB.into()), extected_base_usdt_balance ); }); @@ -235,10 +224,7 @@ fn statemint_transfer_asset_to_pendulum() { PendulumParachain::execute_with(|| { assert_eq!( - pendulum_runtime::Tokens::balance( - pendulum_runtime::PendulumCurrencyId::XCM(1), - &BOB.into() - ), + pendulum_runtime::Tokens::balance(pendulum_runtime::CurrencyId::XCM(1), &BOB.into()), 0 ); }); @@ -310,10 +296,7 @@ fn statemint_transfer_asset_to_pendulum() { ))); assert_eq!( - pendulum_runtime::Tokens::balance( - pendulum_runtime::PendulumCurrencyId::XCM(1), - &BOB.into() - ), + pendulum_runtime::Tokens::balance(pendulum_runtime::CurrencyId::XCM(1), &BOB.into()), TEN_UNITS ); }); @@ -327,13 +310,13 @@ fn statemint_transfer_asset_to_statemint() { Statemint::execute_with(|| {}); PendulumParachain::execute_with(|| { - assert_eq!(TEN_UNITS, Tokens::balance(PendulumCurrencyId::XCM(1), &AccountId::from(BOB))); + assert_eq!(TEN_UNITS, Tokens::balance(CurrencyId::XCM(1), &AccountId::from(BOB))); // ensure sender has enough PEN balance to be charged as fee assert_ok!(Balances::mint_into(&AccountId::from(BOB), TEN_UNITS)); assert_ok!(XTokens::transfer( RuntimeOrigin::signed(BOB.into()), - PendulumCurrencyId::XCM(1), + CurrencyId::XCM(1), UNIT * 1, Box::new( MultiLocation::new( @@ -353,7 +336,7 @@ fn statemint_transfer_asset_to_statemint() { assert_eq!( TEN_UNITS - 1 * UNIT, //inital balance - one unit - Tokens::balance(PendulumCurrencyId::XCM(1), &AccountId::from(BOB)) + Tokens::balance(CurrencyId::XCM(1), &AccountId::from(BOB)) ); assert!(System::events().iter().any(|r| matches!( diff --git a/runtime/pendulum/src/lib.rs b/runtime/pendulum/src/lib.rs index be1b2eede..4f9b7e7dc 100644 --- a/runtime/pendulum/src/lib.rs +++ b/runtime/pendulum/src/lib.rs @@ -14,7 +14,6 @@ use xcm::v3::MultiLocation; use zenlink_protocol::{AssetBalance, MultiAssetsHandler, PairInfo}; pub use parachain_staking::InflationInfo; -pub use spacewalk_primitives::CurrencyId as PendulumCurrencyId; use codec::Encode; @@ -28,6 +27,8 @@ use sp_runtime::{ ApplyExtrinsicResult, SaturatedConversion, }; +pub use spacewalk_primitives::CurrencyId; + use sp_std::{marker::PhantomData, prelude::*}; #[cfg(feature = "std")] use sp_version::NativeVersion; @@ -66,7 +67,6 @@ use xcm_config::{XcmConfig, XcmOriginToTransactDispatchOrigin}; use orml_currencies::BasicCurrencyAdapter; use orml_traits::{currency::MutationHooks, parameter_type_with_key}; -use spacewalk_primitives::CurrencyId; const CONTRACTS_DEBUG_OUTPUT: bool = true; diff --git a/runtime/pendulum/src/xcm_config.rs b/runtime/pendulum/src/xcm_config.rs index 318ebe393..161a1ee23 100644 --- a/runtime/pendulum/src/xcm_config.rs +++ b/runtime/pendulum/src/xcm_config.rs @@ -1,5 +1,5 @@ use super::{ - AccountId, Balance, Balances, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, + AccountId, Balance, Balances, CurrencyId, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, Tokens, WeightToFee, XcmpQueue, }; use crate::ConstU32; @@ -17,7 +17,6 @@ use polkadot_parachain::primitives::Sibling; use polkadot_runtime_common::impls::ToAuthor; use runtime_common::parachains::polkadot::statemint; use sp_runtime::traits::Convert; -use spacewalk_primitives::CurrencyId; use xcm::latest::{prelude::*, Weight as XCMWeight}; use xcm_builder::{ AccountId32Aliases, AllowUnpaidExecutionFrom, ConvertedConcreteId, EnsureXcmOrigin, diff --git a/runtime/pendulum/src/zenlink.rs b/runtime/pendulum/src/zenlink.rs index 1d25fecfc..69798954d 100644 --- a/runtime/pendulum/src/zenlink.rs +++ b/runtime/pendulum/src/zenlink.rs @@ -6,7 +6,6 @@ use orml_traits::MultiCurrency; use sp_runtime::{DispatchError, DispatchResult}; use sp_std::marker::PhantomData; -use spacewalk_primitives::CurrencyId; use zenlink_protocol::{ AssetId, Config as ZenlinkConfig, LocalAssetHandler, PairLpGenerate, ZenlinkMultiAssets, };