git.delta.rocks / unique-network / refs/commits / f387a596f37a

difftreelog

feat(rpc) totalSupply

Yaroslav Bolyukin2022-04-14parent: #bbd154a.patch.diff
in: master

8 files changed

modifiedclient/rpc/src/lib.rsdiffbeforeafterboth
--- a/client/rpc/src/lib.rs
+++ b/client/rpc/src/lib.rs
@@ -76,6 +76,12 @@
 		at: Option<BlockHash>,
 	) -> Result<Vec<u8>>;
 
+	#[rpc(name = "unique_totalSupply")]
+	fn total_supply(
+		&self,
+		collection: CollectionId,
+		at: Option<BlockHash>,
+	) -> Result<u32>;
 	#[rpc(name = "unique_accountBalance")]
 	fn account_balance(
 		&self,
@@ -239,6 +245,8 @@
 	pass_method!(topmost_token_owner(collection: CollectionId, token: TokenId) -> Option<CrossAccountId>);
 	pass_method!(const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);
 	pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);
+
+	pass_method!(total_supply(collection: CollectionId) -> u32);
 	pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);
 	pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> String => |v| v.to_string());
 	pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> String => |v| v.to_string());
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -932,6 +932,8 @@
 	fn const_metadata(&self, token: TokenId) -> Vec<u8>;
 	fn variable_metadata(&self, token: TokenId) -> Vec<u8>;
 
+	/// Amount of unique collection tokens
+	fn total_supply(&self) -> u32;
 	/// Amount of different tokens account has (Applicable to nonfungible/refungible)
 	fn account_balance(&self, account: T::CrossAccountId) -> u32;
 	/// Amount of specific token account have (Applicable to fungible/refungible)
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -244,6 +244,10 @@
 		fail!(<Error<T>>::FungibleDisallowsNesting)
 	}
 
+	fn collection_tokens(&self) -> Vec<TokenId> {
+		vec![TokenId::default()]
+	}
+
 	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {
 		if <Balance<T>>::get((self.id, account)) != 0 {
 			vec![TokenId::default()]
@@ -270,8 +274,8 @@
 		Vec::new()
 	}
 
-	fn collection_tokens(&self) -> Vec<TokenId> {
-		vec![TokenId::default()]
+	fn total_supply(&self) -> u32 {
+		1
 	}
 
 	fn account_balance(&self, account: T::CrossAccountId) -> u32 {
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -294,6 +294,10 @@
 			.into_inner()
 	}
 
+	fn total_supply(&self) -> u32 {
+		<Pallet<T>>::total_supply(self)
+	}
+
 	fn account_balance(&self, account: T::CrossAccountId) -> u32 {
 		<AccountBalance<T>>::get((self.id, account))
 	}
modifiedpallets/refungible/src/common.rsdiffbeforeafterboth
before · pallets/refungible/src/common.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use core::marker::PhantomData;1819use sp_std::collections::btree_map::BTreeMap;20use frame_support::{dispatch::DispatchResultWithPostInfo, fail, weights::Weight, BoundedVec};21use up_data_structs::{22	CollectionId, TokenId, CustomDataLimit, CreateItemExData, CreateRefungibleExData,23	budget::Budget,24};25use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};26use sp_runtime::DispatchError;27use sp_std::{vec::Vec, vec};2829use crate::{30	AccountBalance, Allowance, Balance, Config, Error, Owned, Pallet, RefungibleHandle,31	SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,32};3334macro_rules! max_weight_of {35	($($method:ident ($($args:tt)*)),*) => {36		037		$(38			.max(<SelfWeightOf<T>>::$method($($args)*))39		)*40	};41}4243pub struct CommonWeights<T: Config>(PhantomData<T>);44impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {45	fn create_item() -> Weight {46		<SelfWeightOf<T>>::create_item()47	}4849	fn create_multiple_items(amount: u32) -> Weight {50		<SelfWeightOf<T>>::create_multiple_items(amount)51	}5253	fn create_multiple_items_ex(call: &CreateItemExData<T::CrossAccountId>) -> Weight {54		match call {55			CreateItemExData::RefungibleMultipleOwners(i) => {56				<SelfWeightOf<T>>::create_multiple_items_ex_multiple_owners(i.users.len() as u32)57			}58			CreateItemExData::RefungibleMultipleItems(i) => {59				<SelfWeightOf<T>>::create_multiple_items_ex_multiple_items(i.len() as u32)60			}61			_ => 0,62		}63	}6465	fn burn_item() -> Weight {66		max_weight_of!(burn_item_partial(), burn_item_fully())67	}6869	fn transfer() -> Weight {70		max_weight_of!(71			transfer_normal(),72			transfer_creating(),73			transfer_removing(),74			transfer_creating_removing()75		)76	}7778	fn approve() -> Weight {79		<SelfWeightOf<T>>::approve()80	}8182	fn transfer_from() -> Weight {83		max_weight_of!(84			transfer_from_normal(),85			transfer_from_creating(),86			transfer_from_removing(),87			transfer_from_creating_removing()88		)89	}9091	fn burn_from() -> Weight {92		<SelfWeightOf<T>>::burn_from()93	}9495	fn set_variable_metadata(bytes: u32) -> Weight {96		<SelfWeightOf<T>>::set_variable_metadata(bytes)97	}98}99100fn map_create_data<T: Config>(101	data: up_data_structs::CreateItemData,102	to: &T::CrossAccountId,103) -> Result<CreateRefungibleExData<T::CrossAccountId>, DispatchError> {104	match data {105		up_data_structs::CreateItemData::ReFungible(data) => Ok(CreateRefungibleExData {106			const_data: data.const_data,107			variable_data: data.variable_data,108			users: {109				let mut out = BTreeMap::new();110				out.insert(to.clone(), data.pieces);111				out.try_into().expect("limit > 0")112			},113		}),114		_ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),115	}116}117118impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {119	fn create_item(120		&self,121		sender: T::CrossAccountId,122		to: T::CrossAccountId,123		data: up_data_structs::CreateItemData,124		nesting_budget: &dyn Budget,125	) -> DispatchResultWithPostInfo {126		with_weight(127			<Pallet<T>>::create_item(128				self,129				&sender,130				map_create_data::<T>(data, &to)?,131				nesting_budget,132			),133			<CommonWeights<T>>::create_item(),134		)135	}136137	fn create_multiple_items(138		&self,139		sender: T::CrossAccountId,140		to: T::CrossAccountId,141		data: Vec<up_data_structs::CreateItemData>,142		nesting_budget: &dyn Budget,143	) -> DispatchResultWithPostInfo {144		let data = data145			.into_iter()146			.map(|d| map_create_data::<T>(d, &to))147			.collect::<Result<Vec<_>, DispatchError>>()?;148149		let amount = data.len();150		with_weight(151			<Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),152			<CommonWeights<T>>::create_multiple_items(amount as u32),153		)154	}155156	fn create_multiple_items_ex(157		&self,158		sender: <T>::CrossAccountId,159		data: CreateItemExData<T::CrossAccountId>,160		nesting_budget: &dyn Budget,161	) -> DispatchResultWithPostInfo {162		let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);163		let data = match data {164			CreateItemExData::RefungibleMultipleOwners(r) => vec![r],165			CreateItemExData::RefungibleMultipleItems(r)166				if r.iter().all(|i| i.users.len() == 1) =>167			{168				r.into_inner()169			}170			_ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),171		};172173		with_weight(174			<Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),175			weight,176		)177	}178179	fn burn_item(180		&self,181		sender: T::CrossAccountId,182		token: TokenId,183		amount: u128,184	) -> DispatchResultWithPostInfo {185		with_weight(186			<Pallet<T>>::burn(self, &sender, token, amount),187			<CommonWeights<T>>::burn_item(),188		)189	}190191	fn transfer(192		&self,193		from: T::CrossAccountId,194		to: T::CrossAccountId,195		token: TokenId,196		amount: u128,197		nesting_budget: &dyn Budget,198	) -> DispatchResultWithPostInfo {199		with_weight(200			<Pallet<T>>::transfer(self, &from, &to, token, amount, nesting_budget),201			<CommonWeights<T>>::transfer(),202		)203	}204205	fn approve(206		&self,207		sender: T::CrossAccountId,208		spender: T::CrossAccountId,209		token: TokenId,210		amount: u128,211	) -> DispatchResultWithPostInfo {212		with_weight(213			<Pallet<T>>::set_allowance(self, &sender, &spender, token, amount),214			<CommonWeights<T>>::approve(),215		)216	}217218	fn transfer_from(219		&self,220		sender: T::CrossAccountId,221		from: T::CrossAccountId,222		to: T::CrossAccountId,223		token: TokenId,224		amount: u128,225		nesting_budget: &dyn Budget,226	) -> DispatchResultWithPostInfo {227		with_weight(228			<Pallet<T>>::transfer_from(self, &sender, &from, &to, token, amount, nesting_budget),229			<CommonWeights<T>>::transfer_from(),230		)231	}232233	fn burn_from(234		&self,235		sender: T::CrossAccountId,236		from: T::CrossAccountId,237		token: TokenId,238		amount: u128,239		nesting_budget: &dyn Budget,240	) -> DispatchResultWithPostInfo {241		with_weight(242			<Pallet<T>>::burn_from(self, &sender, &from, token, amount, nesting_budget),243			<CommonWeights<T>>::burn_from(),244		)245	}246247	fn set_variable_metadata(248		&self,249		sender: T::CrossAccountId,250		token: TokenId,251		data: BoundedVec<u8, CustomDataLimit>,252	) -> DispatchResultWithPostInfo {253		let len = data.len();254		with_weight(255			<Pallet<T>>::set_variable_metadata(self, &sender, token, data),256			<CommonWeights<T>>::set_variable_metadata(len as u32),257		)258	}259260	fn check_nesting(261		&self,262		_sender: <T>::CrossAccountId,263		_from: CollectionId,264		_under: TokenId,265		_budget: &dyn Budget,266	) -> sp_runtime::DispatchResult {267		fail!(<Error<T>>::RefungibleDisallowsNesting)268	}269270	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {271		<Owned<T>>::iter_prefix((self.id, account))272			.map(|(id, _)| id)273			.collect()274	}275276	fn collection_tokens(&self) -> Vec<TokenId> {277		<TokenData<T>>::iter_prefix((self.id,))278			.map(|(id, _)| id)279			.collect()280	}281282	fn token_exists(&self, token: TokenId) -> bool {283		<Pallet<T>>::token_exists(self, token)284	}285286	fn last_token_id(&self) -> TokenId {287		TokenId(<TokensMinted<T>>::get(self.id))288	}289290	fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {291		None292	}293	fn const_metadata(&self, token: TokenId) -> Vec<u8> {294		<TokenData<T>>::get((self.id, token))295			.const_data296			.into_inner()297	}298	fn variable_metadata(&self, token: TokenId) -> Vec<u8> {299		<TokenData<T>>::get((self.id, token))300			.variable_data301			.into_inner()302	}303304	fn account_balance(&self, account: T::CrossAccountId) -> u32 {305		<AccountBalance<T>>::get((self.id, account))306	}307308	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {309		<Balance<T>>::get((self.id, token, account))310	}311312	fn allowance(313		&self,314		sender: T::CrossAccountId,315		spender: T::CrossAccountId,316		token: TokenId,317	) -> u128 {318		<Allowance<T>>::get((self.id, token, sender, spender))319	}320}
modifiedprimitives/rpc/src/lib.rsdiffbeforeafterboth
--- a/primitives/rpc/src/lib.rs
+++ b/primitives/rpc/src/lib.rs
@@ -41,6 +41,7 @@
 		fn const_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>>;
 		fn variable_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>>;
 
+		fn total_supply(collection: CollectionId) -> Result<u32>;
 		fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32>;
 		fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<u128>;
 		fn allowance(
modifiedruntime/common/src/runtime_apis.rsdiffbeforeafterboth
--- a/runtime/common/src/runtime_apis.rs
+++ b/runtime/common/src/runtime_apis.rs
@@ -14,6 +14,9 @@
                 fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Result<Vec<TokenId>, DispatchError> {
                     dispatch_unique_runtime!(collection.account_tokens(account))
                 }
+                fn collection_tokens(collection: CollectionId) -> Result<Vec<TokenId>, DispatchError> {
+                    dispatch_unique_runtime!(collection.collection_tokens())
+                }
                 fn token_exists(collection: CollectionId, token: TokenId) -> Result<bool, DispatchError> {
                     dispatch_unique_runtime!(collection.token_exists(token))
                 }
@@ -33,8 +36,8 @@
                     dispatch_unique_runtime!(collection.variable_metadata(token))
                 }
 
-                fn collection_tokens(collection: CollectionId) -> Result<Vec<TokenId>, DispatchError> {
-                    dispatch_unique_runtime!(collection.collection_tokens())
+                fn total_supply(collection: CollectionId) -> Result<u32, DispatchError> {
+                    dispatch_unique_runtime!(collection.total_supply())
                 }
                 fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32, DispatchError> {
                     dispatch_unique_runtime!(collection.account_balance(account))
modifiedtests/src/interfaces/unique/definitions.tsdiffbeforeafterboth
--- a/tests/src/interfaces/unique/definitions.ts
+++ b/tests/src/interfaces/unique/definitions.ts
@@ -45,6 +45,7 @@
     collectionTokens: fun('Get tokens contained in collection', [collectionParam], 'Vec<u32>'),
 
     lastTokenId: fun('Get last token id', [collectionParam], 'u32'),
+    totalSupply: fun('Get amount of unique collection tokens', [collectionParam], 'u32'),
     accountBalance: fun('Get amount of different user tokens', [collectionParam, crossAccountParam()], 'u32'),
     balance: fun('Get amount of specific account token', [collectionParam, crossAccountParam(), tokenParam], 'u128'),
     allowance: fun('Get allowed amount', [collectionParam, crossAccountParam('sender'), crossAccountParam('spender'), tokenParam], 'u128'),