Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

feat: pallet asset-rate #13608

Merged
merged 21 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 17 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand Down
52 changes: 52 additions & 0 deletions frame/asset-rate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
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",
]
86 changes: 86 additions & 0 deletions frame/asset-rate/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -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 }
}
Loading