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

Commit

Permalink
Add CreateOrigin to Assets Pallet (#12586)
Browse files Browse the repository at this point in the history
* add CreateOrigin to Assets pallet

* fix asset-tx-payment test

* use AccountId > u64 in test

* Update frame/assets/src/benchmarking.rs

Co-authored-by: Jegor Sidorenko <5252494+jsidorenko@users.noreply.github.com>

Co-authored-by: Jegor Sidorenko <5252494+jsidorenko@users.noreply.github.com>
  • Loading branch information
2 people authored and bkontur committed Nov 22, 2022
1 parent 87f3fde commit 207229c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 15 deletions.
1 change: 1 addition & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,7 @@ impl pallet_assets::Config for Runtime {
type Balance = u128;
type AssetId = u32;
type Currency = Balances;
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<AccountId>>;
type ForceOrigin = EnsureRoot<AccountId>;
type AssetDeposit = AssetDeposit;
type AssetAccountDeposit = ConstU128<DOLLARS>;
Expand Down
8 changes: 5 additions & 3 deletions frame/assets/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,14 @@ fn assert_event<T: Config<I>, I: 'static>(generic_event: <T as Config<I>>::Runti

benchmarks_instance_pallet! {
create {
let caller: T::AccountId = whitelisted_caller();
let asset_id = Default::default();
let origin = T::CreateOrigin::successful_origin(&asset_id);
let caller = T::CreateOrigin::ensure_origin(origin, &asset_id).unwrap();
let caller_lookup = T::Lookup::unlookup(caller.clone());
T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value());
}: _(SystemOrigin::Signed(caller.clone()), Default::default(), caller_lookup, 1u32.into())
}: _(SystemOrigin::Signed(caller.clone()), asset_id, caller_lookup, 1u32.into())
verify {
assert_last_event::<T, I>(Event::Created { asset_id: Default::default(), creator: caller.clone(), owner: caller }.into());
assert_last_event::<T, I>(Event::Created { asset_id, creator: caller.clone(), owner: caller }.into());
}

force_create {
Expand Down
27 changes: 18 additions & 9 deletions frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
//!
//! ### Terminology
//!
//! * **Admin**: An account ID uniquely privileged to be able to unfreeze (thaw) an account and it's
//! * **Admin**: An account ID uniquely privileged to be able to unfreeze (thaw) an account and its
//! assets, as well as forcibly transfer a particular class of assets between arbitrary accounts
//! and reduce the balance of a particular class of assets of arbitrary accounts.
//! * **Asset issuance/minting**: The creation of a new asset, whose total supply will belong to the
//! account that issues the asset. This is a privileged operation.
//! account designated as the beneficiary of the asset. This is a privileged operation.
//! * **Asset transfer**: The reduction of the balance of an asset of one account with the
//! corresponding increase in the balance of another.
//! * **Asset destruction**: The process of reduce the balance of an asset of one account. This is a
//! privileged operation.
//! * **Asset destruction**: The process of reducing the balance of an asset of one account. This is
//! a privileged operation.
//! * **Fungible asset**: An asset whose units are interchangeable.
//! * **Issuer**: An account ID uniquely privileged to be able to mint a particular class of assets.
//! * **Freezer**: An account ID uniquely privileged to be able to freeze an account from
Expand All @@ -63,12 +63,12 @@
//!
//! The assets system in Substrate is designed to make the following possible:
//!
//! * Issue a new assets in a permissioned or permissionless way, if permissionless, then with a
//! * Issue new assets in a permissioned or permissionless way, if permissionless, then with a
//! deposit required.
//! * Allow accounts to be delegated the ability to transfer assets without otherwise existing
//! on-chain (*approvals*).
//! * Move assets between accounts.
//! * Update the asset's total supply.
//! * Update an asset class's total supply.
//! * Allow administrative activities by specially privileged accounts including freezing account
//! balances and minting/burning assets.
//!
Expand All @@ -92,6 +92,7 @@
//! * `force_cancel_approval`: Rescind a previous approval.
//!
//! ### Privileged Functions
//!
//! * `destroy`: Destroys an entire asset class; called by the asset class's Owner.
//! * `mint`: Increases the asset balance of an account; called by the asset class's Issuer.
//! * `burn`: Decreases the asset balance of an account; called by the asset class's Admin.
Expand Down Expand Up @@ -156,7 +157,7 @@ use frame_support::{
traits::{
tokens::{fungibles, DepositConsequence, WithdrawConsequence},
BalanceStatus::Reserved,
Currency, ReservableCurrency, StoredMap,
Currency, EnsureOriginWithArg, ReservableCurrency, StoredMap,
},
};
use frame_system::Config as SystemConfig;
Expand Down Expand Up @@ -206,6 +207,14 @@ pub mod pallet {
/// The currency mechanism.
type Currency: ReservableCurrency<Self::AccountId>;

/// Standard asset class creation is only allowed if the origin attempting it and the
/// asset class are in this set.
type CreateOrigin: EnsureOriginWithArg<
Self::RuntimeOrigin,
Self::AssetId,
Success = Self::AccountId,
>;

/// The origin which may forcibly create or destroy an asset or otherwise alter privileged
/// attributes.
type ForceOrigin: EnsureOrigin<Self::RuntimeOrigin>;
Expand Down Expand Up @@ -485,7 +494,7 @@ pub mod pallet {
///
/// This new asset class has no assets initially and its owner is the origin.
///
/// The origin must be Signed and the sender must have sufficient funds free.
/// The origin must conform to the configured `CreateOrigin` and have sufficient funds free.
///
/// Funds of sender are reserved by `AssetDeposit`.
///
Expand All @@ -507,7 +516,7 @@ pub mod pallet {
admin: AccountIdLookupOf<T>,
min_balance: T::Balance,
) -> DispatchResult {
let owner = ensure_signed(origin)?;
let owner = T::CreateOrigin::ensure_origin(origin, &id)?;
let admin = T::Lookup::lookup(admin)?;

ensure!(!Asset::<T, I>::contains_key(id), Error::<T, I>::InUse);
Expand Down
3 changes: 2 additions & 1 deletion frame/assets/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate as pallet_assets;

use frame_support::{
construct_runtime, parameter_types,
traits::{ConstU32, ConstU64, GenesisBuild},
traits::{AsEnsureOriginWithArg, ConstU32, ConstU64, GenesisBuild},
};
use sp_core::H256;
use sp_runtime::{
Expand Down Expand Up @@ -89,6 +89,7 @@ impl Config for Test {
type Balance = u64;
type AssetId = u32;
type Currency = Balances;
type CreateOrigin = AsEnsureOriginWithArg<frame_system::EnsureSigned<u64>>;
type ForceOrigin = frame_system::EnsureRoot<u64>;
type AssetDeposit = ConstU64<1>;
type AssetAccountDeposit = ConstU64<10>;
Expand Down
3 changes: 2 additions & 1 deletion frame/transaction-payment/asset-tx-payment/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use frame_support::{
dispatch::{DispatchClass, DispatchInfo, PostDispatchInfo},
pallet_prelude::*,
parameter_types,
traits::{fungibles::Mutate, ConstU32, ConstU64, ConstU8, FindAuthor},
traits::{fungibles::Mutate, AsEnsureOriginWithArg, ConstU32, ConstU64, ConstU8, FindAuthor},
weights::{Weight, WeightToFee as WeightToFeeT},
ConsensusEngineId,
};
Expand Down Expand Up @@ -157,6 +157,7 @@ impl pallet_assets::Config for Runtime {
type Balance = Balance;
type AssetId = u32;
type Currency = Balances;
type CreateOrigin = AsEnsureOriginWithArg<frame_system::EnsureSigned<AccountId>>;
type ForceOrigin = EnsureRoot<AccountId>;
type AssetDeposit = ConstU64<2>;
type AssetAccountDeposit = ConstU64<2>;
Expand Down
2 changes: 1 addition & 1 deletion frame/uniques/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ pub mod pallet {
///
/// This new collection has no items initially and its owner is the origin.
///
/// The origin must be Signed and the sender must have sufficient funds free.
/// The origin must conform to the configured `CreateOrigin` and have sufficient funds free.
///
/// `ItemDeposit` funds of sender are reserved.
///
Expand Down

0 comments on commit 207229c

Please sign in to comment.