Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: benchmarks
  • Loading branch information
wischli committed Mar 16, 2023
commit 0adf9519fc1a85b8323caaa2e5dec7662d3a0151
3 changes: 1 addition & 2 deletions frame/treasury-oracle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ authors = ["William Freudenberger <[email protected]>"]
homepage = "https://substrate.io"
edition = "2021"
license = "Unlicense"
publish = false
repository = "https://github.com/paritytech/substrate/"

[package.metadata.docs.rs]
Expand All @@ -20,7 +19,7 @@ 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", path = "../../primitives/runtime" }
sp-runtime = { version = "7.0.0", default-features = false, path = "../../primitives/runtime" }

[dev-dependencies]
pallet-balances = { version = "4.0.0-dev", path = "../balances" }
Expand Down
9 changes: 4 additions & 5 deletions frame/treasury-oracle/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@

//! The crate's benchmarks.

#![cfg(feature = "runtime-benchmarks")]

use super::*;
use crate::Pallet as TreasuryOracle;

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_float(0.1)
FixedU128::from_u32(1u32)
}

#[benchmarks(where <T as Config>::AssetId: From<u32>)]
Expand Down Expand Up @@ -58,13 +57,13 @@ mod benchmarks {
));

#[extrinsic_call]
_(RawOrigin::Root, ASSET_ID.into(), FixedU128::from_float(1.5));
_(RawOrigin::Root, ASSET_ID.into(), FixedU128::from_u32(2));

assert_eq!(
TreasuryOracle::<T>::conversion_rate_to_native::<<T as Config>::AssetId>(
ASSET_ID.into()
),
Some(FixedU128::from_float(1.5))
Some(FixedU128::from_u32(2))
);
Ok(())
}
Expand Down
29 changes: 23 additions & 6 deletions frame/treasury-oracle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! # Treasury Oracle Pallet
//!
//! A simple oracle pallet for the treasury.
//!
//! - [`Config`]
//! - [`Call`]
//!
//! ## Overview
//!
//! The TreasuryOracle pallet provides means of setting conversion rates
Expand Down Expand Up @@ -72,15 +77,14 @@ use sp_runtime::{traits::Zero, FixedPointNumber, FixedPointOperand, FixedU128};

pub use pallet::*;

#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;
#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

// 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.
Expand Down Expand Up @@ -124,12 +128,12 @@ pub mod pallet {
type AssetId: Member + Parameter + Copy + MaybeSerializeDeserialize + MaxEncodedLen;
}

#[pallet::storage]
#[pallet::getter(fn conversion_rate_to_native)]
/// Maps an asset to its fixed point representation in the native balance.
///
/// E.g. `native_amount = asset_amount * ConversionRateToNative::<T>::get(asset_id)`
pub(super) type ConversionRateToNative<T: Config> =
#[pallet::storage]
#[pallet::getter(fn conversion_rate_to_native)]
pub type ConversionRateToNative<T: Config> =
StorageMap<_, Blake2_128Concat, T::AssetId, FixedU128, OptionQuery>;

#[pallet::event]
Expand All @@ -153,6 +157,10 @@ pub mod pallet {

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Initialize a conversion rate for the given asset.
///
/// ## Complexity
/// - O(1)
#[pallet::call_index(0)]
#[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())]
pub fn create(
Expand All @@ -172,6 +180,10 @@ pub mod pallet {
Ok(())
}

/// Remove the conversion rate for the given asset.
///
/// ## Complexity
/// - O(1)
#[pallet::call_index(1)]
#[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())]
pub fn update(
Expand All @@ -197,6 +209,10 @@ pub mod pallet {
Ok(())
}

/// Update an existing conversion rate for the given asset.
///
/// ## Complexity
/// - O(1)
#[pallet::call_index(2)]
#[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())]
pub fn remove(origin: OriginFor<T>, asset_id: T::AssetId) -> DispatchResult {
Expand All @@ -214,6 +230,7 @@ pub mod pallet {
}
}

// Exposes conversion of an arbitrary balance of an asset to native balance.
impl<T> BalanceConversion<BalanceOf<T>, AssetIdOf<T>, BalanceOf<T>> for Pallet<T>
where
T: Config,
Expand Down