Skip to content

Commit

Permalink
add mock asset_registry (#1721)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm authored Feb 6, 2024
1 parent 468a361 commit 3a1aecf
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions libs/mocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ cfg-primitives = { path = "../primitives", default-features = false }
cfg-traits = { path = "../traits", default-features = false }
cfg-types = { path = "../types", default-features = false }
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", default-features = false, branch = "polkadot-v0.9.43" }
xcm = { workspace = true }

mock-builder = { workspace = true }

Expand All @@ -42,6 +43,7 @@ std = [
"sp-io/std",
"sp-runtime/std",
"orml-traits/std",
"xcm/std",
"mock-builder/std",
]
runtime-benchmarks = [
Expand Down
121 changes: 121 additions & 0 deletions libs/mocks/src/asset_registry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#[frame_support::pallet]
pub mod pallet {
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};
use orml_traits::asset_registry::{AssetMetadata, Inspect, Mutate};
use xcm::{v3::prelude::MultiLocation, VersionedMultiLocation};

#[pallet::config]
pub trait Config: frame_system::Config {
type AssetId;
type Balance;
type CustomMetadata: Parameter + Member + TypeInfo;
}

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::storage]
pub(super) type CallIds<T: Config> = StorageMap<
_,
Blake2_128Concat,
<Blake2_128 as frame_support::StorageHasher>::Output,
mock_builder::CallId,
>;

impl<T: Config> Pallet<T> {
pub fn mock_asset_id(f: impl Fn(&MultiLocation) -> Option<T::AssetId> + 'static) {
register_call!(f);
}

pub fn mock_metadata(
f: impl Fn(&T::AssetId) -> Option<AssetMetadata<T::Balance, T::CustomMetadata>> + 'static,
) {
register_call!(f);
}

pub fn mock_metadata_by_location(
f: impl Fn(&MultiLocation) -> Option<AssetMetadata<T::Balance, T::CustomMetadata>> + 'static,
) {
register_call!(f);
}

pub fn mock_location(
f: impl Fn(&T::AssetId) -> Result<Option<MultiLocation>, DispatchError> + 'static,
) {
register_call!(f);
}

pub fn mock_register_asset(
f: impl Fn(
Option<T::AssetId>,
AssetMetadata<T::Balance, T::CustomMetadata>,
) -> DispatchResult
+ 'static,
) {
register_call!(move |(a, b)| f(a, b));
}

pub fn mock_update_asset(
f: impl Fn(
T::AssetId,
Option<u32>,
Option<Vec<u8>>,
Option<Vec<u8>>,
Option<T::Balance>,
Option<Option<VersionedMultiLocation>>,
Option<T::CustomMetadata>,
) -> DispatchResult
+ 'static,
) {
register_call!(move |(a, b, c, d, e, g, h)| f(a, b, c, d, e, g, h));
}
}

impl<T: Config> Inspect for Pallet<T> {
type AssetId = T::AssetId;
type Balance = T::Balance;
type CustomMetadata = T::CustomMetadata;

fn asset_id(a: &MultiLocation) -> Option<Self::AssetId> {
execute_call!(a)
}

fn metadata(
a: &Self::AssetId,
) -> Option<AssetMetadata<Self::Balance, Self::CustomMetadata>> {
execute_call!(a)
}

fn metadata_by_location(
a: &MultiLocation,
) -> Option<AssetMetadata<Self::Balance, Self::CustomMetadata>> {
execute_call!(a)
}

fn location(a: &Self::AssetId) -> Result<Option<MultiLocation>, DispatchError> {
execute_call!(a)
}
}

impl<T: Config> Mutate for Pallet<T> {
fn register_asset(
a: Option<Self::AssetId>,
b: AssetMetadata<Self::Balance, Self::CustomMetadata>,
) -> DispatchResult {
execute_call!((a, b))
}

fn update_asset(
a: Self::AssetId,
b: Option<u32>,
c: Option<Vec<u8>>,
d: Option<Vec<u8>>,
e: Option<Self::Balance>,
g: Option<Option<VersionedMultiLocation>>,
h: Option<Self::CustomMetadata>,
) -> DispatchResult {
execute_call!((a, b, c, d, e, g, h))
}
}
}
1 change: 1 addition & 0 deletions libs/mocks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod asset_registry;
pub mod change_guard;
pub mod currency_conversion;
pub mod data;
Expand Down

0 comments on commit 3a1aecf

Please sign in to comment.