Skip to content

Commit 450cc38

Browse files
girazokishawntabrizi
authored andcommitted
Add inspect trait for asset roles (paritytech#11738)
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
1 parent ac37092 commit 450cc38

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

frame/assets/src/impl_fungibles.rs

+20
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,23 @@ impl<T: Config<I>, I: 'static> fungibles::approvals::Mutate<<T as SystemConfig>:
263263
Self::do_transfer_approved(asset, owner, delegate, dest, amount)
264264
}
265265
}
266+
267+
impl<T: Config<I>, I: 'static> fungibles::roles::Inspect<<T as SystemConfig>::AccountId>
268+
for Pallet<T, I>
269+
{
270+
fn owner(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
271+
Asset::<T, I>::get(asset).map(|x| x.owner)
272+
}
273+
274+
fn issuer(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
275+
Asset::<T, I>::get(asset).map(|x| x.issuer)
276+
}
277+
278+
fn admin(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
279+
Asset::<T, I>::get(asset).map(|x| x.admin)
280+
}
281+
282+
fn freezer(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
283+
Asset::<T, I>::get(asset).map(|x| x.freezer)
284+
}
285+
}

frame/assets/src/tests.rs

+22
Original file line numberDiff line numberDiff line change
@@ -977,3 +977,25 @@ fn transfer_large_asset() {
977977
assert_ok!(Assets::transfer(Origin::signed(1), 0, 2, amount - 1));
978978
})
979979
}
980+
981+
#[test]
982+
fn querying_roles_should_work() {
983+
new_test_ext().execute_with(|| {
984+
use frame_support::traits::tokens::fungibles::roles::Inspect;
985+
assert_ok!(Assets::force_create(Origin::root(), 0, 1, true, 1));
986+
assert_ok!(Assets::set_team(
987+
Origin::signed(1),
988+
0,
989+
// Issuer
990+
2,
991+
// Admin
992+
3,
993+
// Freezer
994+
4,
995+
));
996+
assert_eq!(Assets::owner(0), Some(1));
997+
assert_eq!(Assets::issuer(0), Some(2));
998+
assert_eq!(Assets::admin(0), Some(3));
999+
assert_eq!(Assets::freezer(0), Some(4));
1000+
});
1001+
}

frame/support/src/traits/tokens/fungibles.rs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub mod metadata;
3131
pub use balanced::{Balanced, Unbalanced};
3232
mod imbalance;
3333
pub use imbalance::{CreditOf, DebtOf, HandleImbalanceDrop, Imbalance};
34+
pub mod roles;
3435

3536
/// Trait for providing balance-inspection access to a set of named fungible assets.
3637
pub trait Inspect<AccountId> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
//! Inspect traits for Asset roles
19+
20+
pub trait Inspect<AccountId>: super::Inspect<AccountId> {
21+
// Get owner for an AssetId.
22+
fn owner(asset: Self::AssetId) -> Option<AccountId>;
23+
// Get issuer for an AssetId.
24+
fn issuer(asset: Self::AssetId) -> Option<AccountId>;
25+
// Get admin for an AssetId.
26+
fn admin(asset: Self::AssetId) -> Option<AccountId>;
27+
// Get freezer for an AssetId.
28+
fn freezer(asset: Self::AssetId) -> Option<AccountId>;
29+
}

0 commit comments

Comments
 (0)