From 3a1aecf0f0ee521878f7529258fbc3baa93c33f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 6 Feb 2024 23:30:24 +0100 Subject: [PATCH] add mock asset_registry (#1721) --- Cargo.lock | 1 + libs/mocks/Cargo.toml | 2 + libs/mocks/src/asset_registry.rs | 121 +++++++++++++++++++++++++++++++ libs/mocks/src/lib.rs | 1 + 4 files changed, 125 insertions(+) create mode 100644 libs/mocks/src/asset_registry.rs diff --git a/Cargo.lock b/Cargo.lock index 371efe3447..3ec1840f0a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1158,6 +1158,7 @@ dependencies = [ "sp-io", "sp-runtime", "sp-std", + "xcm", ] [[package]] diff --git a/libs/mocks/Cargo.toml b/libs/mocks/Cargo.toml index 81c8ad5989..edc46dbc77 100644 --- a/libs/mocks/Cargo.toml +++ b/libs/mocks/Cargo.toml @@ -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 } @@ -42,6 +43,7 @@ std = [ "sp-io/std", "sp-runtime/std", "orml-traits/std", + "xcm/std", "mock-builder/std", ] runtime-benchmarks = [ diff --git a/libs/mocks/src/asset_registry.rs b/libs/mocks/src/asset_registry.rs new file mode 100644 index 0000000000..6cf34b6644 --- /dev/null +++ b/libs/mocks/src/asset_registry.rs @@ -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(_); + + #[pallet::storage] + pub(super) type CallIds = StorageMap< + _, + Blake2_128Concat, + ::Output, + mock_builder::CallId, + >; + + impl Pallet { + pub fn mock_asset_id(f: impl Fn(&MultiLocation) -> Option + 'static) { + register_call!(f); + } + + pub fn mock_metadata( + f: impl Fn(&T::AssetId) -> Option> + 'static, + ) { + register_call!(f); + } + + pub fn mock_metadata_by_location( + f: impl Fn(&MultiLocation) -> Option> + 'static, + ) { + register_call!(f); + } + + pub fn mock_location( + f: impl Fn(&T::AssetId) -> Result, DispatchError> + 'static, + ) { + register_call!(f); + } + + pub fn mock_register_asset( + f: impl Fn( + Option, + AssetMetadata, + ) -> DispatchResult + + 'static, + ) { + register_call!(move |(a, b)| f(a, b)); + } + + pub fn mock_update_asset( + f: impl Fn( + T::AssetId, + Option, + Option>, + Option>, + Option, + Option>, + Option, + ) -> DispatchResult + + 'static, + ) { + register_call!(move |(a, b, c, d, e, g, h)| f(a, b, c, d, e, g, h)); + } + } + + impl Inspect for Pallet { + type AssetId = T::AssetId; + type Balance = T::Balance; + type CustomMetadata = T::CustomMetadata; + + fn asset_id(a: &MultiLocation) -> Option { + execute_call!(a) + } + + fn metadata( + a: &Self::AssetId, + ) -> Option> { + execute_call!(a) + } + + fn metadata_by_location( + a: &MultiLocation, + ) -> Option> { + execute_call!(a) + } + + fn location(a: &Self::AssetId) -> Result, DispatchError> { + execute_call!(a) + } + } + + impl Mutate for Pallet { + fn register_asset( + a: Option, + b: AssetMetadata, + ) -> DispatchResult { + execute_call!((a, b)) + } + + fn update_asset( + a: Self::AssetId, + b: Option, + c: Option>, + d: Option>, + e: Option, + g: Option>, + h: Option, + ) -> DispatchResult { + execute_call!((a, b, c, d, e, g, h)) + } + } +} diff --git a/libs/mocks/src/lib.rs b/libs/mocks/src/lib.rs index c9b83cb792..7a90e59638 100644 --- a/libs/mocks/src/lib.rs +++ b/libs/mocks/src/lib.rs @@ -1,3 +1,4 @@ +pub mod asset_registry; pub mod change_guard; pub mod currency_conversion; pub mod data;