difftreelog
feat(rpc) totalSupply
in: master
8 files changed
client/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());
pallets/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)
pallets/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 {
pallets/nonfungible/src/common.rsdiffbeforeafterboth1// 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, CustomDataLimit, CreateItemExData, CollectionId, budget::Budget};21use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};22use sp_runtime::DispatchError;23use sp_std::vec::Vec;2425use crate::{26 AccountBalance, Allowance, Config, CreateItemData, Error, NonfungibleHandle, Owned, Pallet,27 SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,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_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {37 match data {38 CreateItemExData::NFT(t) => <SelfWeightOf<T>>::create_multiple_items_ex(t.len() as u32),39 _ => 0,40 }41 }4243 fn create_multiple_items(amount: u32) -> Weight {44 <SelfWeightOf<T>>::create_multiple_items(amount)45 }4647 fn burn_item() -> Weight {48 <SelfWeightOf<T>>::burn_item()49 }5051 fn transfer() -> Weight {52 <SelfWeightOf<T>>::transfer()53 }5455 fn approve() -> Weight {56 <SelfWeightOf<T>>::approve()57 }5859 fn transfer_from() -> Weight {60 <SelfWeightOf<T>>::transfer_from()61 }6263 fn burn_from() -> Weight {64 <SelfWeightOf<T>>::burn_from()65 }6667 fn set_variable_metadata(bytes: u32) -> Weight {68 <SelfWeightOf<T>>::set_variable_metadata(bytes)69 }70}7172fn map_create_data<T: Config>(73 data: up_data_structs::CreateItemData,74 to: &T::CrossAccountId,75) -> Result<CreateItemData<T>, DispatchError> {76 match data {77 up_data_structs::CreateItemData::NFT(data) => Ok(CreateItemData::<T> {78 const_data: data.const_data,79 variable_data: data.variable_data,80 owner: to.clone(),81 }),82 _ => fail!(<Error<T>>::NotNonfungibleDataUsedToMintFungibleCollectionToken),83 }84}8586impl<T: Config> CommonCollectionOperations<T> for NonfungibleHandle<T> {87 fn create_item(88 &self,89 sender: T::CrossAccountId,90 to: T::CrossAccountId,91 data: up_data_structs::CreateItemData,92 nesting_budget: &dyn Budget,93 ) -> DispatchResultWithPostInfo {94 with_weight(95 <Pallet<T>>::create_item(96 self,97 &sender,98 map_create_data::<T>(data, &to)?,99 nesting_budget,100 ),101 <CommonWeights<T>>::create_item(),102 )103 }104105 fn create_multiple_items(106 &self,107 sender: T::CrossAccountId,108 to: T::CrossAccountId,109 data: Vec<up_data_structs::CreateItemData>,110 nesting_budget: &dyn Budget,111 ) -> DispatchResultWithPostInfo {112 let data = data113 .into_iter()114 .map(|d| map_create_data::<T>(d, &to))115 .collect::<Result<Vec<_>, DispatchError>>()?;116117 let amount = data.len();118 with_weight(119 <Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),120 <CommonWeights<T>>::create_multiple_items(amount as u32),121 )122 }123124 fn create_multiple_items_ex(125 &self,126 sender: <T>::CrossAccountId,127 data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,128 nesting_budget: &dyn Budget,129 ) -> DispatchResultWithPostInfo {130 let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);131 let data = match data {132 up_data_structs::CreateItemExData::NFT(nft) => nft,133 _ => fail!(Error::<T>::NotNonfungibleDataUsedToMintFungibleCollectionToken),134 };135136 with_weight(137 <Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),138 weight,139 )140 }141142 fn burn_item(143 &self,144 sender: T::CrossAccountId,145 token: TokenId,146 amount: u128,147 ) -> DispatchResultWithPostInfo {148 ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);149 if amount == 1 {150 with_weight(151 <Pallet<T>>::burn(self, &sender, token),152 <CommonWeights<T>>::burn_item(),153 )154 } else {155 Ok(().into())156 }157 }158159 fn transfer(160 &self,161 from: T::CrossAccountId,162 to: T::CrossAccountId,163 token: TokenId,164 amount: u128,165 nesting_budget: &dyn Budget,166 ) -> DispatchResultWithPostInfo {167 ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);168 if amount == 1 {169 with_weight(170 <Pallet<T>>::transfer(self, &from, &to, token, nesting_budget),171 <CommonWeights<T>>::transfer(),172 )173 } else {174 Ok(().into())175 }176 }177178 fn approve(179 &self,180 sender: T::CrossAccountId,181 spender: T::CrossAccountId,182 token: TokenId,183 amount: u128,184 ) -> DispatchResultWithPostInfo {185 ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);186187 with_weight(188 if amount == 1 {189 <Pallet<T>>::set_allowance(self, &sender, token, Some(&spender))190 } else {191 <Pallet<T>>::set_allowance(self, &sender, token, None)192 },193 <CommonWeights<T>>::approve(),194 )195 }196197 fn transfer_from(198 &self,199 sender: T::CrossAccountId,200 from: T::CrossAccountId,201 to: T::CrossAccountId,202 token: TokenId,203 amount: u128,204 nesting_budget: &dyn Budget,205 ) -> DispatchResultWithPostInfo {206 ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);207208 if amount == 1 {209 with_weight(210 <Pallet<T>>::transfer_from(self, &sender, &from, &to, token, nesting_budget),211 <CommonWeights<T>>::transfer_from(),212 )213 } else {214 Ok(().into())215 }216 }217218 fn burn_from(219 &self,220 sender: T::CrossAccountId,221 from: T::CrossAccountId,222 token: TokenId,223 amount: u128,224 nesting_budget: &dyn Budget,225 ) -> DispatchResultWithPostInfo {226 ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);227228 if amount == 1 {229 with_weight(230 <Pallet<T>>::burn_from(self, &sender, &from, token, nesting_budget),231 <CommonWeights<T>>::burn_from(),232 )233 } else {234 Ok(().into())235 }236 }237238 fn set_variable_metadata(239 &self,240 sender: T::CrossAccountId,241 token: TokenId,242 data: BoundedVec<u8, CustomDataLimit>,243 ) -> DispatchResultWithPostInfo {244 let len = data.len();245 with_weight(246 <Pallet<T>>::set_variable_metadata(self, &sender, token, data),247 <CommonWeights<T>>::set_variable_metadata(len as u32),248 )249 }250251 fn check_nesting(252 &self,253 sender: T::CrossAccountId,254 from: CollectionId,255 under: TokenId,256 budget: &dyn Budget,257 ) -> sp_runtime::DispatchResult {258 <Pallet<T>>::check_nesting(self, sender, from, under, budget)259 }260261 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {262 <Owned<T>>::iter_prefix((self.id, account))263 .map(|(id, _)| id)264 .collect()265 }266267 fn collection_tokens(&self) -> Vec<TokenId> {268 <TokenData<T>>::iter_prefix((self.id,))269 .map(|(id, _)| id)270 .collect()271 }272273 fn token_exists(&self, token: TokenId) -> bool {274 <Pallet<T>>::token_exists(self, token)275 }276277 fn last_token_id(&self) -> TokenId {278 TokenId(<TokensMinted<T>>::get(self.id))279 }280281 fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId> {282 <TokenData<T>>::get((self.id, token)).map(|t| t.owner)283 }284 fn const_metadata(&self, token: TokenId) -> Vec<u8> {285 <TokenData<T>>::get((self.id, token))286 .map(|t| t.const_data)287 .unwrap_or_default()288 .into_inner()289 }290 fn variable_metadata(&self, token: TokenId) -> Vec<u8> {291 <TokenData<T>>::get((self.id, token))292 .map(|t| t.variable_data)293 .unwrap_or_default()294 .into_inner()295 }296297 fn account_balance(&self, account: T::CrossAccountId) -> u32 {298 <AccountBalance<T>>::get((self.id, account))299 }300301 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {302 if <TokenData<T>>::get((self.id, token))303 .map(|a| a.owner == account)304 .unwrap_or(false)305 {306 1307 } else {308 0309 }310 }311312 fn allowance(313 &self,314 sender: T::CrossAccountId,315 spender: T::CrossAccountId,316 token: TokenId,317 ) -> u128 {318 if <TokenData<T>>::get((self.id, token))319 .map(|a| a.owner != sender)320 .unwrap_or(true)321 {322 0323 } else if <Allowance<T>>::get((self.id, token)) == Some(spender) {324 1325 } else {326 0327 }328 }329}pallets/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))
}
primitives/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(
runtime/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))
tests/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'),