Skip to content
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

Replaced CurrencyId enum by Spacewalk Primitives CurrencyId. #272

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions runtime/integration-tests/pendulum/src/setup.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
}

Expand All @@ -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
}
Expand All @@ -45,7 +45,7 @@ impl ExtBuilderPendulum {
.unwrap();

orml_tokens::GenesisConfig::<Runtime> {
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();
Expand Down
43 changes: 13 additions & 30 deletions runtime/integration-tests/pendulum/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(|| {
Expand Down Expand Up @@ -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
);
});
Expand All @@ -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 }) }
Expand Down Expand Up @@ -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.
Expand All @@ -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
);
});
Expand Down Expand Up @@ -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
);
});
Expand All @@ -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
);
});
Expand Down Expand Up @@ -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
);
});
Expand All @@ -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(
Expand All @@ -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!(
Expand Down
3 changes: 3 additions & 0 deletions runtime/pendulum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
31 changes: 0 additions & 31 deletions runtime/pendulum/src/currency.rs

This file was deleted.

5 changes: 2 additions & 3 deletions runtime/pendulum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

mod currency;
mod weights;
pub mod xcm_config;
pub mod zenlink;
use crate::zenlink::*;
use xcm::v3::MultiLocation;
use zenlink_protocol::{AssetBalance, MultiAssetsHandler, PairInfo};

pub use currency::CurrencyId as PendulumCurrencyId;
pub use parachain_staking::InflationInfo;

use codec::Encode;
Expand All @@ -29,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;
Expand Down Expand Up @@ -65,7 +65,6 @@ 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};

Expand Down
55 changes: 10 additions & 45 deletions runtime/pendulum/src/zenlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use sp_runtime::{DispatchError, DispatchResult};
use sp_std::marker::PhantomData;

use zenlink_protocol::{
AssetId, Config as ZenlinkConfig, LocalAssetHandler, PairLpGenerate, ZenlinkMultiAssets, LOCAL,
NATIVE,
AssetId, Config as ZenlinkConfig, LocalAssetHandler, PairLpGenerate, ZenlinkMultiAssets,
};
pub type ZenlinkAssetId = AssetId;

Expand Down Expand Up @@ -38,23 +37,23 @@ where
Local: MultiCurrency<AccountId, CurrencyId = CurrencyId>,
{
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::<AssetBalance>::try_into(Local::free_balance(currency_id, &who))
.unwrap_or_default()
}
AssetBalance::default()
}

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::<AssetBalance>::try_into(Local::total_issuance(currency_id))
.unwrap_or_default()
}
AssetBalance::default()
}

fn local_is_exists(asset_id: ZenlinkAssetId) -> bool {
let currency_id: Result<CurrencyId, ()> = asset_id.try_into();
let currency_id: Result<CurrencyId, ()> = zenlink_id_to_currency_id(asset_id);
match currency_id {
Ok(_) => true,
Err(_) => false,
Expand All @@ -67,7 +66,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,
Expand All @@ -86,7 +85,7 @@ where
origin: &AccountId,
amount: AssetBalance,
) -> Result<AssetBalance, DispatchError> {
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,
Expand All @@ -106,7 +105,7 @@ where
origin: &AccountId,
amount: AssetBalance,
) -> Result<AssetBalance, DispatchError> {
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,
Expand All @@ -122,41 +121,7 @@ where
}
}

impl TryFrom<CurrencyId> for ZenlinkAssetId {
type Error = ();

fn try_from(currency_id: CurrencyId) -> Result<Self, Self::Error> {
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<ZenlinkAssetId> for CurrencyId {
type Error = ();
fn try_from(asset_id: ZenlinkAssetId) -> Result<Self, Self::Error> {
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<CurrencyId, ()> {
adelarja marked this conversation as resolved.
Show resolved Hide resolved
Ok(CurrencyId::Native)
}
Loading