Skip to content

Commit

Permalink
try to reuse InspectRegistry trait
Browse files Browse the repository at this point in the history
  • Loading branch information
vgantchev committed Nov 7, 2023
1 parent 74de4ac commit 9215cb7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
10 changes: 9 additions & 1 deletion pallets/asset-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,20 @@ impl<T: Config> CreateRegistry<T::AssetId, T::Balance> for Pallet<T> {
}
}

impl<T: Config> InspectRegistry<T::AssetId> for Pallet<T> {
impl<T: Config> InspectRegistry<T::AssetId, BoundedVec<u8, <T as Config>::StringLimit>> for Pallet<T> {
fn exists(asset_id: T::AssetId) -> bool {
Assets::<T>::contains_key(asset_id)
}

fn decimals(asset_id: T::AssetId) -> Option<u8> {
Some(AssetMetadataMap::<T>::get(asset_id)?.decimals)
}

fn asset_name(asset_id: T::AssetId) -> Option<BoundedVec<u8, <T as Config>::StringLimit>> {
Some(Assets::<T>::get(asset_id)?.name)
}

fn asset_symbol(asset_id: T::AssetId) -> Option<BoundedVec<u8, <T as Config>::StringLimit>> {
Some(AssetMetadataMap::<T>::get(asset_id)?.symbol)
}
}
3 changes: 2 additions & 1 deletion pallets/stableswap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ use hydradx_traits::pools::DustRemovalAccountWhitelist;
use orml_traits::MultiCurrency;
use sp_std::collections::btree_map::BTreeMap;
use weights::WeightInfo;
use pallet_asset_registry::Config as PalletAssetRegistryConfig;

#[cfg(test)]
pub(crate) mod tests;
Expand Down Expand Up @@ -126,7 +127,7 @@ pub mod pallet {
type ShareAccountId: AccountIdFor<Self::AssetId, AccountId = Self::AccountId>;

/// Asset registry mechanism
type AssetInspection: InspectRegistry<Self::AssetId>;
type AssetInspection: InspectRegistry<Self::AssetId, BoundedVec<u8, <T as PalletAssetRegistryConfig>::StringLimit>>;

/// The origin which can create a new pool
type AuthorityOrigin: EnsureOrigin<Self::RuntimeOrigin>;
Expand Down
19 changes: 10 additions & 9 deletions runtime/hydradx/src/evm/precompiles/multicurrency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use primitive_types::H160;
use primitives::{AssetId, Balance};
use sp_runtime::{traits::Dispatchable, RuntimeDebug};
use sp_std::{marker::PhantomData, prelude::*};
use hydradx_traits::InspectRegistry;

#[module_evm_utility_macro::generate_function_selector]
#[derive(RuntimeDebug, Eq, PartialEq, TryFromPrimitive, IntoPrimitive)]
Expand Down Expand Up @@ -121,15 +122,15 @@ where
let input = handle.read_input()?;
input.expect_arguments(0)?;

match <pallet_asset_registry::Pallet<Runtime>>::retrieve_asset_name(asset_id.into()) {
Ok(name) => {
match <pallet_asset_registry::Pallet<Runtime>>::asset_name(asset_id.into()) {
Some(name) => {
log::debug!(target: "evm", "multicurrency: symbol: {:?}", name);

let encoded = Output::encode_bytes(name.as_slice());

Ok(succeed(encoded))
}
Err(_) => Err(PrecompileFailure::Error {
None => Err(PrecompileFailure::Error {
exit_status: pallet_evm::ExitError::Other("Non-existing asset.".into()),
}),
}
Expand All @@ -142,15 +143,15 @@ where
let input = handle.read_input()?;
input.expect_arguments(0)?;

match <pallet_asset_registry::Pallet<Runtime>>::retrieve_asset_symbol(asset_id.into()) {
Ok(symbol) => {
match <pallet_asset_registry::Pallet<Runtime>>::asset_symbol(asset_id.into()) {
Some(symbol) => {
log::debug!(target: "evm", "multicurrency: name: {:?}", symbol);

let encoded = Output::encode_bytes(symbol.as_slice());

Ok(succeed(encoded))
}
Err(_) => Err(PrecompileFailure::Error {
None => Err(PrecompileFailure::Error {
exit_status: pallet_evm::ExitError::Other("Non-existing asset.".into()),
}),
}
Expand All @@ -163,15 +164,15 @@ where
let input = handle.read_input()?;
input.expect_arguments(0)?;

match <pallet_asset_registry::Pallet<Runtime>>::retrieve_asset_decimals(asset_id.into()) {
Ok(decimals) => {
match <pallet_asset_registry::Pallet<Runtime>>::decimals(asset_id.into()) {
Some(decimals) => {
log::debug!(target: "evm", "multicurrency: decimals: {:?}", decimals);

let encoded = Output::encode_uint::<u8>(decimals);

Ok(succeed(encoded))
}
Err(_) => Err(PrecompileFailure::Error {
None => Err(PrecompileFailure::Error {
exit_status: pallet_evm::ExitError::Other("Non-existing asset.".into()),
}),
}
Expand Down
6 changes: 3 additions & 3 deletions traits/src/registry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use frame_support::BoundedVec;
use sp_std::vec::Vec;

pub trait Registry<AssetId, AssetName, Balance, Error> {
fn exists(name: AssetId) -> bool;

Expand Down Expand Up @@ -41,9 +39,11 @@ pub trait ShareTokenRegistry<AssetId, AssetName, Balance, Error>: Registry<Asset
}
}

pub trait InspectRegistry<AssetId> {
pub trait InspectRegistry<AssetId, BoundedString> {
fn exists(asset_id: AssetId) -> bool;
fn decimals(asset_id: AssetId) -> Option<u8>;
fn asset_name(asset_id: AssetId) -> Option<BoundedString>;
fn asset_symbol(asset_id: AssetId) -> Option<BoundedString>;
}

#[derive(Eq, PartialEq, Copy, Clone)]
Expand Down

0 comments on commit 9215cb7

Please sign in to comment.