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
before · pallets/fungible/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 frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, BoundedVec};20use up_data_structs::{TokenId, CollectionId, CreateItemExData, budget::Budget};21use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};22use sp_runtime::ArithmeticError;23use sp_std::{vec::Vec, vec};24use up_data_structs::CustomDataLimit;2526use crate::{27	Allowance, Balance, Config, Error, FungibleHandle, Pallet, SelfWeightOf, weights::WeightInfo,28};2930pub struct CommonWeights<T: Config>(PhantomData<T>);31impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {32	fn create_item() -> Weight {33		<SelfWeightOf<T>>::create_item()34	}3536	fn create_multiple_items(_amount: u32) -> Weight {37		Self::create_item()38	}3940	fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {41		match data {42			CreateItemExData::Fungible(f) => {43				<SelfWeightOf<T>>::create_multiple_items_ex(f.len() as u32)44			}45			_ => 0,46		}47	}4849	fn burn_item() -> Weight {50		<SelfWeightOf<T>>::burn_item()51	}5253	fn transfer() -> Weight {54		<SelfWeightOf<T>>::transfer()55	}5657	fn approve() -> Weight {58		<SelfWeightOf<T>>::approve()59	}6061	fn transfer_from() -> Weight {62		<SelfWeightOf<T>>::transfer_from()63	}6465	fn burn_from() -> Weight {66		<SelfWeightOf<T>>::burn_from()67	}6869	fn set_variable_metadata(_bytes: u32) -> Weight {70		// Error71		072	}73}7475impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {76	fn create_item(77		&self,78		sender: T::CrossAccountId,79		to: T::CrossAccountId,80		data: up_data_structs::CreateItemData,81		nesting_budget: &dyn Budget,82	) -> DispatchResultWithPostInfo {83		match data {84			up_data_structs::CreateItemData::Fungible(data) => with_weight(85				<Pallet<T>>::create_item(self, &sender, (to, data.value), nesting_budget),86				<CommonWeights<T>>::create_item(),87			),88			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),89		}90	}9192	fn create_multiple_items(93		&self,94		sender: T::CrossAccountId,95		to: T::CrossAccountId,96		data: Vec<up_data_structs::CreateItemData>,97		nesting_budget: &dyn Budget,98	) -> DispatchResultWithPostInfo {99		let mut sum: u128 = 0;100		for data in data {101			match data {102				up_data_structs::CreateItemData::Fungible(data) => {103					sum = sum104						.checked_add(data.value)105						.ok_or(ArithmeticError::Overflow)?;106				}107				_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),108			}109		}110111		with_weight(112			<Pallet<T>>::create_item(self, &sender, (to, sum), nesting_budget),113			<CommonWeights<T>>::create_item(),114		)115	}116117	fn create_multiple_items_ex(118		&self,119		sender: <T>::CrossAccountId,120		data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,121		nesting_budget: &dyn Budget,122	) -> DispatchResultWithPostInfo {123		let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);124		let data = match data {125			up_data_structs::CreateItemExData::Fungible(f) => f,126			_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),127		};128129		with_weight(130			<Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),131			weight,132		)133	}134135	fn burn_item(136		&self,137		sender: T::CrossAccountId,138		token: TokenId,139		amount: u128,140	) -> DispatchResultWithPostInfo {141		ensure!(142			token == TokenId::default(),143			<Error<T>>::FungibleItemsHaveNoId144		);145146		with_weight(147			<Pallet<T>>::burn(self, &sender, amount),148			<CommonWeights<T>>::burn_item(),149		)150	}151152	fn transfer(153		&self,154		from: T::CrossAccountId,155		to: T::CrossAccountId,156		token: TokenId,157		amount: u128,158		nesting_budget: &dyn Budget,159	) -> DispatchResultWithPostInfo {160		ensure!(161			token == TokenId::default(),162			<Error<T>>::FungibleItemsHaveNoId163		);164165		with_weight(166			<Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),167			<CommonWeights<T>>::transfer(),168		)169	}170171	fn approve(172		&self,173		sender: T::CrossAccountId,174		spender: T::CrossAccountId,175		token: TokenId,176		amount: u128,177	) -> DispatchResultWithPostInfo {178		ensure!(179			token == TokenId::default(),180			<Error<T>>::FungibleItemsHaveNoId181		);182183		with_weight(184			<Pallet<T>>::set_allowance(self, &sender, &spender, amount),185			<CommonWeights<T>>::approve(),186		)187	}188189	fn transfer_from(190		&self,191		sender: T::CrossAccountId,192		from: T::CrossAccountId,193		to: T::CrossAccountId,194		token: TokenId,195		amount: u128,196		nesting_budget: &dyn Budget,197	) -> DispatchResultWithPostInfo {198		ensure!(199			token == TokenId::default(),200			<Error<T>>::FungibleItemsHaveNoId201		);202203		with_weight(204			<Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),205			<CommonWeights<T>>::transfer_from(),206		)207	}208209	fn burn_from(210		&self,211		sender: T::CrossAccountId,212		from: T::CrossAccountId,213		token: TokenId,214		amount: u128,215		nesting_budget: &dyn Budget,216	) -> DispatchResultWithPostInfo {217		ensure!(218			token == TokenId::default(),219			<Error<T>>::FungibleItemsHaveNoId220		);221222		with_weight(223			<Pallet<T>>::burn_from(self, &sender, &from, amount, nesting_budget),224			<CommonWeights<T>>::burn_from(),225		)226	}227228	fn set_variable_metadata(229		&self,230		_sender: T::CrossAccountId,231		_token: TokenId,232		_data: BoundedVec<u8, CustomDataLimit>,233	) -> DispatchResultWithPostInfo {234		fail!(<Error<T>>::FungibleItemsDontHaveData)235	}236237	fn check_nesting(238		&self,239		_sender: <T>::CrossAccountId,240		_from: CollectionId,241		_under: TokenId,242		_budget: &dyn Budget,243	) -> sp_runtime::DispatchResult {244		fail!(<Error<T>>::FungibleDisallowsNesting)245	}246247	fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {248		if <Balance<T>>::get((self.id, account)) != 0 {249			vec![TokenId::default()]250		} else {251			vec![]252		}253	}254255	fn token_exists(&self, token: TokenId) -> bool {256		token == TokenId::default()257	}258259	fn last_token_id(&self) -> TokenId {260		TokenId::default()261	}262263	fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {264		None265	}266	fn const_metadata(&self, _token: TokenId) -> Vec<u8> {267		Vec::new()268	}269	fn variable_metadata(&self, _token: TokenId) -> Vec<u8> {270		Vec::new()271	}272273	fn collection_tokens(&self) -> Vec<TokenId> {274		vec![TokenId::default()]275	}276277	fn account_balance(&self, account: T::CrossAccountId) -> u32 {278		if <Balance<T>>::get((self.id, account)) != 0 {279			1280		} else {281			0282		}283	}284285	fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {286		if token != TokenId::default() {287			return 0;288		}289		<Balance<T>>::get((self.id, account))290	}291292	fn allowance(293		&self,294		sender: T::CrossAccountId,295		spender: T::CrossAccountId,296		token: TokenId,297	) -> u128 {298		if token != TokenId::default() {299			return 0;300		}301		<Allowance<T>>::get((self.id, sender, spender))302	}303}
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
--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -301,6 +301,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))
 	}
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'),