Skip to content

Commit

Permalink
Replaced CurrencyId enum by Spacewalk Primitives CurrencyId.
Browse files Browse the repository at this point in the history
  • Loading branch information
adelarja committed Jul 11, 2023
1 parent 72ac423 commit 7c4f004
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 80 deletions.
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,16 +6,15 @@
#[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;
pub use spacewalk_primitives::CurrencyId as PendulumCurrencyId;

use codec::Encode;

Expand Down Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion runtime/pendulum/src/xcm_config.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand Down
56 changes: 11 additions & 45 deletions runtime/pendulum/src/zenlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -38,23 +38,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 +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,
Expand All @@ -86,7 +86,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 +106,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 +122,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, ()> {
Ok(CurrencyId::Native)
}

0 comments on commit 7c4f004

Please sign in to comment.