mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-11 13:01:07 +00:00
feat: pallet asset-rate (#13608)
* poc * fix: remove AssetIdParameter * tests: add * docs: add pallet description * feat: add benches * refactor: UnknownAssetId * fix: normalize mock cfg * fix: benchmarks * chore: add weights * refactor: remove storage getter * chore: apply suggestions from code review * docs: add native balance to calls * chore: apply suggestions from code review * chore: apply ConversionFromAssetBalance * tests: update balance mock * chore: apply suggestions from code review * ci: set publish to false * docs: fix missing rustdoc --------- Co-authored-by: parity-processbot <>
This commit is contained in:
committed by
GitHub
parent
cbc8e5f7df
commit
9eb63f216c
Generated
+17
@@ -3634,6 +3634,7 @@ dependencies = [
|
||||
"log",
|
||||
"node-primitives",
|
||||
"pallet-alliance",
|
||||
"pallet-asset-rate",
|
||||
"pallet-asset-tx-payment",
|
||||
"pallet-assets",
|
||||
"pallet-authority-discovery",
|
||||
@@ -5556,6 +5557,22 @@ dependencies = [
|
||||
"sp-std",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pallet-asset-rate"
|
||||
version = "4.0.0-dev"
|
||||
dependencies = [
|
||||
"frame-benchmarking",
|
||||
"frame-support",
|
||||
"frame-system",
|
||||
"pallet-balances",
|
||||
"parity-scale-codec",
|
||||
"scale-info",
|
||||
"sp-core",
|
||||
"sp-io",
|
||||
"sp-runtime",
|
||||
"sp-std",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pallet-asset-tx-payment"
|
||||
version = "4.0.0-dev"
|
||||
|
||||
@@ -166,6 +166,7 @@ members = [
|
||||
"frame/transaction-payment/rpc/runtime-api",
|
||||
"frame/transaction-storage",
|
||||
"frame/treasury",
|
||||
"frame/asset-rate",
|
||||
"frame/tips",
|
||||
"frame/uniques",
|
||||
"frame/utility",
|
||||
|
||||
@@ -53,6 +53,7 @@ frame-election-provider-support = { version = "4.0.0-dev", default-features = fa
|
||||
frame-system-rpc-runtime-api = { version = "4.0.0-dev", default-features = false, path = "../../../frame/system/rpc/runtime-api/" }
|
||||
frame-try-runtime = { version = "0.10.0-dev", default-features = false, path = "../../../frame/try-runtime", optional = true }
|
||||
pallet-alliance = { version = "4.0.0-dev", default-features = false, path = "../../../frame/alliance" }
|
||||
pallet-asset-rate = { version = "4.0.0-dev", default-features = false, path = "../../../frame/asset-rate" }
|
||||
pallet-assets = { version = "4.0.0-dev", default-features = false, path = "../../../frame/assets" }
|
||||
pallet-authority-discovery = { version = "4.0.0-dev", default-features = false, path = "../../../frame/authority-discovery" }
|
||||
pallet-authorship = { version = "4.0.0-dev", default-features = false, path = "../../../frame/authorship" }
|
||||
@@ -201,6 +202,7 @@ std = [
|
||||
"pallet-transaction-payment/std",
|
||||
"pallet-transaction-storage/std",
|
||||
"pallet-treasury/std",
|
||||
"pallet-asset-rate/std",
|
||||
"sp-transaction-pool/std",
|
||||
"pallet-utility/std",
|
||||
"sp-version/std",
|
||||
@@ -272,6 +274,7 @@ runtime-benchmarks = [
|
||||
"pallet-tips/runtime-benchmarks",
|
||||
"pallet-transaction-storage/runtime-benchmarks",
|
||||
"pallet-treasury/runtime-benchmarks",
|
||||
"pallet-asset-rate/runtime-benchmarks",
|
||||
"pallet-utility/runtime-benchmarks",
|
||||
"pallet-uniques/runtime-benchmarks",
|
||||
"pallet-nfts/runtime-benchmarks",
|
||||
@@ -333,6 +336,7 @@ try-runtime = [
|
||||
"pallet-timestamp/try-runtime",
|
||||
"pallet-tips/try-runtime",
|
||||
"pallet-treasury/try-runtime",
|
||||
"pallet-asset-rate/try-runtime",
|
||||
"pallet-utility/try-runtime",
|
||||
"pallet-transaction-payment/try-runtime",
|
||||
"pallet-asset-tx-payment/try-runtime",
|
||||
|
||||
@@ -1131,6 +1131,17 @@ impl pallet_treasury::Config for Runtime {
|
||||
type SpendOrigin = EnsureWithSuccess<EnsureRoot<AccountId>, AccountId, MaxBalance>;
|
||||
}
|
||||
|
||||
impl pallet_asset_rate::Config for Runtime {
|
||||
type CreateOrigin = EnsureRoot<AccountId>;
|
||||
type RemoveOrigin = EnsureRoot<AccountId>;
|
||||
type UpdateOrigin = EnsureRoot<AccountId>;
|
||||
type Balance = Balance;
|
||||
type Currency = Balances;
|
||||
type AssetId = u32;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type WeightInfo = pallet_asset_rate::weights::SubstrateWeight<Runtime>;
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
pub const BountyCuratorDeposit: Permill = Permill::from_percent(50);
|
||||
pub const BountyValueMinimum: Balance = 5 * DOLLARS;
|
||||
@@ -1765,6 +1776,7 @@ construct_runtime!(
|
||||
TechnicalMembership: pallet_membership::<Instance1>,
|
||||
Grandpa: pallet_grandpa,
|
||||
Treasury: pallet_treasury,
|
||||
AssetRate: pallet_asset_rate,
|
||||
Contracts: pallet_contracts,
|
||||
Sudo: pallet_sudo,
|
||||
ImOnline: pallet_im_online,
|
||||
@@ -1922,6 +1934,7 @@ mod benches {
|
||||
[pallet_tips, Tips]
|
||||
[pallet_transaction_storage, TransactionStorage]
|
||||
[pallet_treasury, Treasury]
|
||||
[pallet_asset_rate, AssetRate]
|
||||
[pallet_uniques, Uniques]
|
||||
[pallet_nfts, Nfts]
|
||||
[pallet_utility, Utility]
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
[package]
|
||||
name = "pallet-asset-rate"
|
||||
version = "4.0.0-dev"
|
||||
description = "Whitelist non-native assets for treasury spending and provide conversion to native balance"
|
||||
authors = ["William Freudenberger <w.freude@icloud.com>"]
|
||||
homepage = "https://substrate.io"
|
||||
edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
repository = "https://github.com/paritytech/substrate/"
|
||||
publish = false
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = [
|
||||
"derive",
|
||||
] }
|
||||
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
|
||||
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../benchmarking" }
|
||||
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
|
||||
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
|
||||
sp-runtime = { version = "7.0.0", default-features = false, path = "../../primitives/runtime" }
|
||||
sp-std = { version = "5.0.0", default-features = false, path = "../../primitives/std" }
|
||||
|
||||
[dev-dependencies]
|
||||
pallet-balances = { version = "4.0.0-dev", path = "../balances" }
|
||||
sp-core = { version = "7.0.0", path = "../../primitives/core" }
|
||||
sp-io = { version = "7.0.0", path = "../../primitives/io" }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"codec/std",
|
||||
"frame-benchmarking?/std",
|
||||
"frame-support/std",
|
||||
"frame-system/std",
|
||||
"scale-info/std",
|
||||
"sp-runtime/std",
|
||||
"sp-std/std",
|
||||
]
|
||||
runtime-benchmarks = [
|
||||
"frame-benchmarking/runtime-benchmarks",
|
||||
"frame-support/runtime-benchmarks",
|
||||
"frame-system/runtime-benchmarks",
|
||||
"sp-runtime/runtime-benchmarks",
|
||||
]
|
||||
try-runtime = [
|
||||
"frame-support/try-runtime",
|
||||
"frame-system/try-runtime",
|
||||
"sp-runtime/try-runtime",
|
||||
]
|
||||
@@ -0,0 +1,86 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! The crate's benchmarks.
|
||||
|
||||
use super::*;
|
||||
use crate::{pallet as pallet_asset_rate, Pallet as AssetRate};
|
||||
|
||||
use frame_benchmarking::v2::*;
|
||||
use frame_support::assert_ok;
|
||||
use frame_system::RawOrigin;
|
||||
|
||||
const ASSET_ID: u32 = 1;
|
||||
|
||||
fn default_conversion_rate() -> FixedU128 {
|
||||
FixedU128::from_u32(1u32)
|
||||
}
|
||||
|
||||
#[benchmarks(where <T as Config>::AssetId: From<u32>)]
|
||||
mod benchmarks {
|
||||
use super::*;
|
||||
|
||||
#[benchmark]
|
||||
fn create() -> Result<(), BenchmarkError> {
|
||||
let asset_id: T::AssetId = ASSET_ID.into();
|
||||
#[extrinsic_call]
|
||||
_(RawOrigin::Root, asset_id, default_conversion_rate());
|
||||
|
||||
assert_eq!(
|
||||
pallet_asset_rate::ConversionRateToNative::<T>::get(asset_id),
|
||||
Some(default_conversion_rate())
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn update() -> Result<(), BenchmarkError> {
|
||||
let asset_id: T::AssetId = ASSET_ID.into();
|
||||
assert_ok!(AssetRate::<T>::create(
|
||||
RawOrigin::Root.into(),
|
||||
asset_id,
|
||||
default_conversion_rate()
|
||||
));
|
||||
|
||||
#[extrinsic_call]
|
||||
_(RawOrigin::Root, asset_id, FixedU128::from_u32(2));
|
||||
|
||||
assert_eq!(
|
||||
pallet_asset_rate::ConversionRateToNative::<T>::get(asset_id),
|
||||
Some(FixedU128::from_u32(2))
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn remove() -> Result<(), BenchmarkError> {
|
||||
let asset_id: T::AssetId = ASSET_ID.into();
|
||||
assert_ok!(AssetRate::<T>::create(
|
||||
RawOrigin::Root.into(),
|
||||
ASSET_ID.into(),
|
||||
default_conversion_rate()
|
||||
));
|
||||
|
||||
#[extrinsic_call]
|
||||
_(RawOrigin::Root, asset_id);
|
||||
|
||||
assert!(pallet_asset_rate::ConversionRateToNative::<T>::get(asset_id).is_none());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl_benchmark_test_suite! { AssetRate, crate::mock::new_test_ext(), crate::mock::Test }
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! # Asset Rate Pallet
|
||||
//!
|
||||
//! - [`Config`]
|
||||
//! - [`Call`]
|
||||
//!
|
||||
//! ## Overview
|
||||
//!
|
||||
//! The AssetRate pallet provides means of setting conversion rates for some asset to native
|
||||
//! balance.
|
||||
//!
|
||||
//! The supported dispatchable functions are documented in the [`Call`] enum.
|
||||
//!
|
||||
//! ### Terminology
|
||||
//!
|
||||
//! * **Asset balance**: The balance type of an arbitrary asset. The network might only know about
|
||||
//! the identifier of the asset and nothing more.
|
||||
//! * **Native balance**: The balance type of the network's native currency.
|
||||
//!
|
||||
//! ### Goals
|
||||
//!
|
||||
//! The asset-rate system in Substrate is designed to make the following possible:
|
||||
//!
|
||||
//! * Providing a soft conversion for the balance of supported assets to a default asset class.
|
||||
//! * Updating existing conversion rates.
|
||||
//!
|
||||
//! ## Interface
|
||||
//!
|
||||
//! ### Permissioned Functions
|
||||
//!
|
||||
//! * `create`: Creates a new asset conversion rate.
|
||||
//! * `remove`: Removes an existing asset conversion rate.
|
||||
//! * `update`: Overwrites an existing assert conversion rate.
|
||||
//!
|
||||
//! Please refer to the [`Call`] enum and its associated variants for documentation on each
|
||||
//! function.
|
||||
//!
|
||||
//! ### Assumptions
|
||||
//!
|
||||
//! * Conversion rates are only used as estimates, and are not designed to be precise or closely
|
||||
//! tracking real world values.
|
||||
//! * All conversion rates reflect the ration of some asset to native, e.g. native = asset * rate.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
use frame_support::traits::{
|
||||
fungible::Inspect,
|
||||
tokens::{Balance, ConversionFromAssetBalance},
|
||||
};
|
||||
use sp_runtime::{traits::Zero, FixedPointNumber, FixedPointOperand, FixedU128};
|
||||
|
||||
pub use pallet::*;
|
||||
pub use weights::WeightInfo;
|
||||
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
pub mod benchmarking;
|
||||
#[cfg(test)]
|
||||
mod mock;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
pub mod weights;
|
||||
|
||||
// Type alias for `frame_system`'s account id.
|
||||
type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
|
||||
// This pallet's asset id and balance type.
|
||||
type AssetIdOf<T> = <T as Config>::AssetId;
|
||||
// Generic fungible balance type.
|
||||
type BalanceOf<T> = <<T as Config>::Currency as Inspect<AccountIdOf<T>>>::Balance;
|
||||
|
||||
#[frame_support::pallet]
|
||||
pub mod pallet {
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_system::pallet_prelude::*;
|
||||
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(_);
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {
|
||||
/// The Weight information for extrinsics in this pallet.
|
||||
type WeightInfo: WeightInfo;
|
||||
|
||||
/// The runtime event type.
|
||||
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
|
||||
/// The origin permissioned to create a conversion rate for an asset.
|
||||
type CreateOrigin: EnsureOrigin<Self::RuntimeOrigin>;
|
||||
|
||||
/// The origin permissioned to remove an existing conversion rate for an asset.
|
||||
type RemoveOrigin: EnsureOrigin<Self::RuntimeOrigin>;
|
||||
|
||||
/// The origin permissioned to update an existiing conversion rate for an asset.
|
||||
type UpdateOrigin: EnsureOrigin<Self::RuntimeOrigin>;
|
||||
|
||||
/// The units in which we record balances.
|
||||
type Balance: Balance + FixedPointOperand;
|
||||
|
||||
/// The currency mechanism for this pallet.
|
||||
type Currency: Inspect<Self::AccountId, Balance = Self::Balance>;
|
||||
|
||||
/// The identifier for the class of asset.
|
||||
type AssetId: frame_support::traits::tokens::AssetId;
|
||||
}
|
||||
|
||||
/// Maps an asset to its fixed point representation in the native balance.
|
||||
///
|
||||
/// E.g. `native_amount = asset_amount * ConversionRateToNative::<T>::get(asset_id)`
|
||||
#[pallet::storage]
|
||||
pub type ConversionRateToNative<T: Config> =
|
||||
StorageMap<_, Blake2_128Concat, T::AssetId, FixedU128, OptionQuery>;
|
||||
|
||||
#[pallet::event]
|
||||
#[pallet::generate_deposit(pub(super) fn deposit_event)]
|
||||
pub enum Event<T: Config> {
|
||||
// Some `asset_id` conversion rate was created.
|
||||
AssetRateCreated { asset_id: T::AssetId, rate: FixedU128 },
|
||||
// Some `asset_id` conversion rate was removed.
|
||||
AssetRateRemoved { asset_id: T::AssetId },
|
||||
// Some existing `asset_id` conversion rate was updated from `old` to `new`.
|
||||
AssetRateUpdated { asset_id: T::AssetId, old: FixedU128, new: FixedU128 },
|
||||
}
|
||||
|
||||
#[pallet::error]
|
||||
pub enum Error<T> {
|
||||
/// The given asset ID is unknown.
|
||||
UnknownAssetId,
|
||||
/// The given asset ID already has an assigned conversion rate and cannot be re-created.
|
||||
AlreadyExists,
|
||||
}
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {
|
||||
/// Initialize a conversion rate to native balance for the given asset.
|
||||
///
|
||||
/// ## Complexity
|
||||
/// - O(1)
|
||||
#[pallet::call_index(0)]
|
||||
#[pallet::weight(T::WeightInfo::create())]
|
||||
pub fn create(
|
||||
origin: OriginFor<T>,
|
||||
asset_id: T::AssetId,
|
||||
rate: FixedU128,
|
||||
) -> DispatchResult {
|
||||
T::CreateOrigin::ensure_origin(origin)?;
|
||||
|
||||
ensure!(
|
||||
!ConversionRateToNative::<T>::contains_key(asset_id),
|
||||
Error::<T>::AlreadyExists
|
||||
);
|
||||
ConversionRateToNative::<T>::set(asset_id, Some(rate));
|
||||
|
||||
Self::deposit_event(Event::AssetRateCreated { asset_id, rate });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Update the conversion rate to native balance for the given asset.
|
||||
///
|
||||
/// ## Complexity
|
||||
/// - O(1)
|
||||
#[pallet::call_index(1)]
|
||||
#[pallet::weight(T::WeightInfo::update())]
|
||||
pub fn update(
|
||||
origin: OriginFor<T>,
|
||||
asset_id: T::AssetId,
|
||||
rate: FixedU128,
|
||||
) -> DispatchResult {
|
||||
T::UpdateOrigin::ensure_origin(origin)?;
|
||||
|
||||
let mut old = FixedU128::zero();
|
||||
ConversionRateToNative::<T>::mutate(asset_id, |maybe_rate| {
|
||||
if let Some(r) = maybe_rate {
|
||||
old = *r;
|
||||
*r = rate;
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::<T>::UnknownAssetId)
|
||||
}
|
||||
})?;
|
||||
|
||||
Self::deposit_event(Event::AssetRateUpdated { asset_id, old, new: rate });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Remove an existing conversion rate to native balance for the given asset.
|
||||
///
|
||||
/// ## Complexity
|
||||
/// - O(1)
|
||||
#[pallet::call_index(2)]
|
||||
#[pallet::weight(T::WeightInfo::remove())]
|
||||
pub fn remove(origin: OriginFor<T>, asset_id: T::AssetId) -> DispatchResult {
|
||||
T::RemoveOrigin::ensure_origin(origin)?;
|
||||
|
||||
ensure!(
|
||||
ConversionRateToNative::<T>::contains_key(asset_id),
|
||||
Error::<T>::UnknownAssetId
|
||||
);
|
||||
ConversionRateToNative::<T>::remove(asset_id);
|
||||
|
||||
Self::deposit_event(Event::AssetRateRemoved { asset_id });
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Exposes conversion of an arbitrary balance of an asset to native balance.
|
||||
impl<T> ConversionFromAssetBalance<BalanceOf<T>, AssetIdOf<T>, BalanceOf<T>> for Pallet<T>
|
||||
where
|
||||
T: Config,
|
||||
BalanceOf<T>: FixedPointOperand + Zero,
|
||||
{
|
||||
type Error = pallet::Error<T>;
|
||||
|
||||
fn from_asset_balance(
|
||||
balance: BalanceOf<T>,
|
||||
asset_id: AssetIdOf<T>,
|
||||
) -> Result<BalanceOf<T>, pallet::Error<T>> {
|
||||
let rate = pallet::ConversionRateToNative::<T>::get(asset_id)
|
||||
.ok_or(pallet::Error::<T>::UnknownAssetId.into())?;
|
||||
Ok(rate.saturating_mul_int(balance))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! The crate's mock.
|
||||
|
||||
use crate as pallet_asset_rate;
|
||||
use frame_support::traits::{ConstU16, ConstU64};
|
||||
use sp_core::H256;
|
||||
use sp_runtime::{
|
||||
testing::Header,
|
||||
traits::{BlakeTwo256, IdentityLookup},
|
||||
};
|
||||
|
||||
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
|
||||
type Block = frame_system::mocking::MockBlock<Test>;
|
||||
|
||||
frame_support::construct_runtime!(
|
||||
pub enum Test where
|
||||
Block = Block,
|
||||
NodeBlock = Block,
|
||||
UncheckedExtrinsic = UncheckedExtrinsic,
|
||||
{
|
||||
System: frame_system,
|
||||
AssetRate: pallet_asset_rate,
|
||||
Balances: pallet_balances,
|
||||
}
|
||||
);
|
||||
|
||||
impl frame_system::Config for Test {
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type BlockWeights = ();
|
||||
type BlockLength = ();
|
||||
type DbWeight = ();
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type Index = u64;
|
||||
type BlockNumber = u64;
|
||||
type Hash = H256;
|
||||
type Hashing = BlakeTwo256;
|
||||
type AccountId = u64;
|
||||
type Lookup = IdentityLookup<Self::AccountId>;
|
||||
type Header = Header;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type BlockHashCount = ConstU64<250>;
|
||||
type Version = ();
|
||||
type PalletInfo = PalletInfo;
|
||||
type AccountData = pallet_balances::AccountData<u64>;
|
||||
type OnNewAccount = ();
|
||||
type OnKilledAccount = ();
|
||||
type SystemWeightInfo = ();
|
||||
type SS58Prefix = ConstU16<42>;
|
||||
type OnSetCode = ();
|
||||
type MaxConsumers = frame_support::traits::ConstU32<16>;
|
||||
}
|
||||
|
||||
impl pallet_balances::Config for Test {
|
||||
type Balance = u64;
|
||||
type DustRemoval = ();
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type ExistentialDeposit = ConstU64<1>;
|
||||
type AccountStore = System;
|
||||
type WeightInfo = ();
|
||||
type MaxLocks = ();
|
||||
type MaxReserves = ();
|
||||
type ReserveIdentifier = [u8; 8];
|
||||
type HoldIdentifier = ();
|
||||
type FreezeIdentifier = ();
|
||||
type MaxHolds = ();
|
||||
type MaxFreezes = ();
|
||||
}
|
||||
|
||||
impl pallet_asset_rate::Config for Test {
|
||||
type WeightInfo = ();
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type CreateOrigin = frame_system::EnsureRoot<u64>;
|
||||
type RemoveOrigin = frame_system::EnsureRoot<u64>;
|
||||
type UpdateOrigin = frame_system::EnsureRoot<u64>;
|
||||
type Balance = u64;
|
||||
type Currency = Balances;
|
||||
type AssetId = u32;
|
||||
}
|
||||
|
||||
// Build genesis storage according to the mock runtime.
|
||||
pub fn new_test_ext() -> sp_io::TestExternalities {
|
||||
frame_system::GenesisConfig::default().build_storage::<Test>().unwrap().into()
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! The crate's tests.
|
||||
|
||||
use super::*;
|
||||
use crate::pallet as pallet_asset_rate;
|
||||
use frame_support::{assert_noop, assert_ok};
|
||||
use mock::{new_test_ext, AssetRate, RuntimeOrigin, Test};
|
||||
use sp_runtime::FixedU128;
|
||||
|
||||
const ASSET_ID: u32 = 42;
|
||||
|
||||
#[test]
|
||||
fn create_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert!(pallet_asset_rate::ConversionRateToNative::<Test>::get(ASSET_ID).is_none());
|
||||
assert_ok!(AssetRate::create(RuntimeOrigin::root(), ASSET_ID, FixedU128::from_float(0.1)));
|
||||
|
||||
assert_eq!(
|
||||
pallet_asset_rate::ConversionRateToNative::<Test>::get(ASSET_ID),
|
||||
Some(FixedU128::from_float(0.1))
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_existing_throws() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert!(pallet_asset_rate::ConversionRateToNative::<Test>::get(ASSET_ID).is_none());
|
||||
assert_ok!(AssetRate::create(RuntimeOrigin::root(), ASSET_ID, FixedU128::from_float(0.1)));
|
||||
|
||||
assert_noop!(
|
||||
AssetRate::create(RuntimeOrigin::root(), ASSET_ID, FixedU128::from_float(0.1)),
|
||||
Error::<Test>::AlreadyExists
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(AssetRate::create(RuntimeOrigin::root(), ASSET_ID, FixedU128::from_float(0.1)));
|
||||
|
||||
assert_ok!(AssetRate::remove(RuntimeOrigin::root(), ASSET_ID,));
|
||||
assert!(pallet_asset_rate::ConversionRateToNative::<Test>::get(ASSET_ID).is_none());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_unknown_throws() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_noop!(
|
||||
AssetRate::remove(RuntimeOrigin::root(), ASSET_ID,),
|
||||
Error::<Test>::UnknownAssetId
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(AssetRate::create(RuntimeOrigin::root(), ASSET_ID, FixedU128::from_float(0.1)));
|
||||
assert_ok!(AssetRate::update(RuntimeOrigin::root(), ASSET_ID, FixedU128::from_float(0.5)));
|
||||
|
||||
assert_eq!(
|
||||
pallet_asset_rate::ConversionRateToNative::<Test>::get(ASSET_ID),
|
||||
Some(FixedU128::from_float(0.5))
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_unknown_throws() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_noop!(
|
||||
AssetRate::update(RuntimeOrigin::root(), ASSET_ID, FixedU128::from_float(0.5)),
|
||||
Error::<Test>::UnknownAssetId
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn convert_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(AssetRate::create(RuntimeOrigin::root(), ASSET_ID, FixedU128::from_float(2.51)));
|
||||
|
||||
let conversion = <AssetRate as ConversionFromAssetBalance<
|
||||
BalanceOf<Test>,
|
||||
<Test as pallet_asset_rate::Config>::AssetId,
|
||||
BalanceOf<Test>,
|
||||
>>::from_asset_balance(10, ASSET_ID);
|
||||
assert_eq!(conversion.expect("Conversion rate exists for asset"), 25);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn convert_unknown_throws() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let conversion = <AssetRate as ConversionFromAssetBalance<
|
||||
BalanceOf<Test>,
|
||||
<Test as pallet_asset_rate::Config>::AssetId,
|
||||
BalanceOf<Test>,
|
||||
>>::from_asset_balance(10, ASSET_ID);
|
||||
assert!(conversion.is_err());
|
||||
});
|
||||
}
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Autogenerated weights for pallet_asset_rate
|
||||
//!
|
||||
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
|
||||
//! DATE: 2023-03-16, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
|
||||
//! WORST CASE MAP SIZE: `1000000`
|
||||
//! HOSTNAME: `local`, CPU: `<UNKNOWN>`
|
||||
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
|
||||
|
||||
// Executed Command:
|
||||
// ./target/production/substrate
|
||||
// benchmark
|
||||
// pallet
|
||||
// --chain=dev
|
||||
// --steps=50
|
||||
// --repeat=20
|
||||
// --pallet=pallet_asset_rate
|
||||
// --extrinsic=*
|
||||
// --execution=wasm
|
||||
// --wasm-execution=compiled
|
||||
// --heap-pages=4096
|
||||
// --output=./frame/asset-rate/src/weights.rs
|
||||
// --header=./HEADER-APACHE2
|
||||
// --template=./.maintain/frame-weight-template.hbs
|
||||
|
||||
#![cfg_attr(rustfmt, rustfmt_skip)]
|
||||
#![allow(unused_parens)]
|
||||
#![allow(unused_imports)]
|
||||
|
||||
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
|
||||
use sp_std::marker::PhantomData;
|
||||
|
||||
/// Weight functions needed for pallet_asset_rate.
|
||||
pub trait WeightInfo {
|
||||
fn create() -> Weight;
|
||||
fn update() -> Weight;
|
||||
fn remove() -> Weight;
|
||||
}
|
||||
|
||||
/// Weights for pallet_asset_rate using the Substrate node and recommended hardware.
|
||||
pub struct SubstrateWeight<T>(PhantomData<T>);
|
||||
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
|
||||
/// Storage: AssetRate ConversionRateToNative (r:1 w:1)
|
||||
/// Proof: AssetRate ConversionRateToNative (max_values: None, max_size: Some(36), added: 2511, mode: MaxEncodedLen)
|
||||
fn create() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `76`
|
||||
// Estimated: `3501`
|
||||
// Minimum execution time: 6_000_000 picoseconds.
|
||||
Weight::from_parts(7_000_000, 3501)
|
||||
.saturating_add(T::DbWeight::get().reads(1_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(1_u64))
|
||||
}
|
||||
/// Storage: AssetRate ConversionRateToNative (r:1 w:1)
|
||||
/// Proof: AssetRate ConversionRateToNative (max_values: None, max_size: Some(36), added: 2511, mode: MaxEncodedLen)
|
||||
fn update() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `137`
|
||||
// Estimated: `3501`
|
||||
// Minimum execution time: 7_000_000 picoseconds.
|
||||
Weight::from_parts(8_000_000, 3501)
|
||||
.saturating_add(T::DbWeight::get().reads(1_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(1_u64))
|
||||
}
|
||||
/// Storage: AssetRate ConversionRateToNative (r:1 w:1)
|
||||
/// Proof: AssetRate ConversionRateToNative (max_values: None, max_size: Some(36), added: 2511, mode: MaxEncodedLen)
|
||||
fn remove() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `137`
|
||||
// Estimated: `3501`
|
||||
// Minimum execution time: 7_000_000 picoseconds.
|
||||
Weight::from_parts(8_000_000, 3501)
|
||||
.saturating_add(T::DbWeight::get().reads(1_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(1_u64))
|
||||
}
|
||||
}
|
||||
|
||||
// For backwards compatibility and tests
|
||||
impl WeightInfo for () {
|
||||
/// Storage: AssetRate ConversionRateToNative (r:1 w:1)
|
||||
/// Proof: AssetRate ConversionRateToNative (max_values: None, max_size: Some(36), added: 2511, mode: MaxEncodedLen)
|
||||
fn create() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `76`
|
||||
// Estimated: `3501`
|
||||
// Minimum execution time: 6_000_000 picoseconds.
|
||||
Weight::from_parts(7_000_000, 3501)
|
||||
.saturating_add(RocksDbWeight::get().reads(1_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(1_u64))
|
||||
}
|
||||
/// Storage: AssetRate ConversionRateToNative (r:1 w:1)
|
||||
/// Proof: AssetRate ConversionRateToNative (max_values: None, max_size: Some(36), added: 2511, mode: MaxEncodedLen)
|
||||
fn update() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `137`
|
||||
// Estimated: `3501`
|
||||
// Minimum execution time: 7_000_000 picoseconds.
|
||||
Weight::from_parts(8_000_000, 3501)
|
||||
.saturating_add(RocksDbWeight::get().reads(1_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(1_u64))
|
||||
}
|
||||
/// Storage: AssetRate ConversionRateToNative (r:1 w:1)
|
||||
/// Proof: AssetRate ConversionRateToNative (max_values: None, max_size: Some(36), added: 2511, mode: MaxEncodedLen)
|
||||
fn remove() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `137`
|
||||
// Estimated: `3501`
|
||||
// Minimum execution time: 7_000_000 picoseconds.
|
||||
Weight::from_parts(8_000_000, 3501)
|
||||
.saturating_add(RocksDbWeight::get().reads(1_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(1_u64))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user