difftreelog
Add properties extrinsics
in: master
10 files changed
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -736,7 +736,24 @@
) -> DispatchResult {
collection.check_is_owner_or_admin(sender)?;
- CollectionProperties::<T>::get(collection.id).try_change_property(property)?;
+ CollectionProperties::<T>::try_mutate(
+ collection.id,
+ |properties| properties.try_change_property(property.clone())
+ )?;
+
+ <Pallet<T>>::deposit_event(Event::CollectionPropertySet(collection.id, property));
+
+ Ok(())
+ }
+
+ pub fn change_collection_properties(
+ collection: &CollectionHandle<T>,
+ sender: &T::CrossAccountId,
+ properties: Vec<Property>,
+ ) -> DispatchResult {
+ for property in properties {
+ Self::change_collection_property(collection, sender, property)?;
+ }
Ok(())
}
@@ -909,7 +926,8 @@
fn create_multiple_items(amount: u32) -> Weight;
fn create_multiple_items_ex(cost: &CreateItemExData<CrossAccountId>) -> Weight;
fn burn_item() -> Weight;
- fn set_property() -> Weight;
+ fn change_collection_properties(amount: u32) -> Weight;
+ fn change_token_properties(amount: u32) -> Weight;
fn transfer() -> Weight;
fn approve() -> Weight;
fn transfer_from() -> Weight;
@@ -945,17 +963,17 @@
amount: u128,
) -> DispatchResultWithPostInfo;
- fn change_collection_property(
+ fn change_collection_properties(
&self,
sender: T::CrossAccountId,
- property: Property,
+ properties: Vec<Property>,
) -> DispatchResultWithPostInfo;
- fn change_token_property(
+ fn change_token_properties(
&self,
sender: T::CrossAccountId,
token_id: TokenId,
- property: Property,
+ property: Vec<Property>,
) -> DispatchResultWithPostInfo;
fn transfer(
pallets/fungible/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, 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, Property};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 change_collection_properties(amount: u32) -> Weight {54 <SelfWeightOf<T>>::change_collection_properties(amount)55 }5657 fn change_token_properties(amount: u32) -> Weight {58 <SelfWeightOf<T>>::change_token_properties(amount)59 }6061 fn transfer() -> Weight {62 <SelfWeightOf<T>>::transfer()63 }6465 fn approve() -> Weight {66 <SelfWeightOf<T>>::approve()67 }6869 fn transfer_from() -> Weight {70 <SelfWeightOf<T>>::transfer_from()71 }7273 fn burn_from() -> Weight {74 <SelfWeightOf<T>>::burn_from()75 }7677 fn set_variable_metadata(_bytes: u32) -> Weight {78 // Error79 080 }81}8283impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {84 fn create_item(85 &self,86 sender: T::CrossAccountId,87 to: T::CrossAccountId,88 data: up_data_structs::CreateItemData,89 nesting_budget: &dyn Budget,90 ) -> DispatchResultWithPostInfo {91 match data {92 up_data_structs::CreateItemData::Fungible(data) => with_weight(93 <Pallet<T>>::create_item(self, &sender, (to, data.value), nesting_budget),94 <CommonWeights<T>>::create_item(),95 ),96 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),97 }98 }99100 fn create_multiple_items(101 &self,102 sender: T::CrossAccountId,103 to: T::CrossAccountId,104 data: Vec<up_data_structs::CreateItemData>,105 nesting_budget: &dyn Budget,106 ) -> DispatchResultWithPostInfo {107 let mut sum: u128 = 0;108 for data in data {109 match data {110 up_data_structs::CreateItemData::Fungible(data) => {111 sum = sum112 .checked_add(data.value)113 .ok_or(ArithmeticError::Overflow)?;114 }115 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),116 }117 }118119 with_weight(120 <Pallet<T>>::create_item(self, &sender, (to, sum), nesting_budget),121 <CommonWeights<T>>::create_item(),122 )123 }124125 fn create_multiple_items_ex(126 &self,127 sender: <T>::CrossAccountId,128 data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,129 nesting_budget: &dyn Budget,130 ) -> DispatchResultWithPostInfo {131 let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);132 let data = match data {133 up_data_structs::CreateItemExData::Fungible(f) => f,134 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),135 };136137 with_weight(138 <Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),139 weight,140 )141 }142143 fn burn_item(144 &self,145 sender: T::CrossAccountId,146 token: TokenId,147 amount: u128,148 ) -> DispatchResultWithPostInfo {149 ensure!(150 token == TokenId::default(),151 <Error<T>>::FungibleItemsHaveNoId152 );153154 with_weight(155 <Pallet<T>>::burn(self, &sender, amount),156 <CommonWeights<T>>::burn_item(),157 )158 }159160 fn transfer(161 &self,162 from: T::CrossAccountId,163 to: T::CrossAccountId,164 token: TokenId,165 amount: u128,166 nesting_budget: &dyn Budget,167 ) -> DispatchResultWithPostInfo {168 ensure!(169 token == TokenId::default(),170 <Error<T>>::FungibleItemsHaveNoId171 );172173 with_weight(174 <Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),175 <CommonWeights<T>>::transfer(),176 )177 }178179 fn approve(180 &self,181 sender: T::CrossAccountId,182 spender: T::CrossAccountId,183 token: TokenId,184 amount: u128,185 ) -> DispatchResultWithPostInfo {186 ensure!(187 token == TokenId::default(),188 <Error<T>>::FungibleItemsHaveNoId189 );190191 with_weight(192 <Pallet<T>>::set_allowance(self, &sender, &spender, amount),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!(207 token == TokenId::default(),208 <Error<T>>::FungibleItemsHaveNoId209 );210211 with_weight(212 <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),213 <CommonWeights<T>>::transfer_from(),214 )215 }216217 fn burn_from(218 &self,219 sender: T::CrossAccountId,220 from: T::CrossAccountId,221 token: TokenId,222 amount: u128,223 nesting_budget: &dyn Budget,224 ) -> DispatchResultWithPostInfo {225 ensure!(226 token == TokenId::default(),227 <Error<T>>::FungibleItemsHaveNoId228 );229230 with_weight(231 <Pallet<T>>::burn_from(self, &sender, &from, amount, nesting_budget),232 <CommonWeights<T>>::burn_from(),233 )234 }235236 fn change_collection_properties(237 &self,238 _sender: T::CrossAccountId,239 _property: Vec<Property>,240 ) -> DispatchResultWithPostInfo {241 fail!(<Error<T>>::PropertiesNotAllowed)242 }243244 fn change_token_properties(245 &self,246 _sender: T::CrossAccountId,247 _token_id: TokenId,248 _property: Vec<Property>,249 ) -> DispatchResultWithPostInfo {250 fail!(<Error<T>>::PropertiesNotAllowed)251 }252253 fn set_variable_metadata(254 &self,255 _sender: T::CrossAccountId,256 _token: TokenId,257 _data: BoundedVec<u8, CustomDataLimit>,258 ) -> DispatchResultWithPostInfo {259 fail!(<Error<T>>::FungibleItemsDontHaveData)260 }261262 fn check_nesting(263 &self,264 _sender: <T>::CrossAccountId,265 _from: (CollectionId, TokenId),266 _under: TokenId,267 _budget: &dyn Budget,268 ) -> sp_runtime::DispatchResult {269 fail!(<Error<T>>::FungibleDisallowsNesting)270 }271272 fn collection_tokens(&self) -> Vec<TokenId> {273 vec![TokenId::default()]274 }275276 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {277 if <Balance<T>>::get((self.id, account)) != 0 {278 vec![TokenId::default()]279 } else {280 vec![]281 }282 }283284 fn token_exists(&self, token: TokenId) -> bool {285 token == TokenId::default()286 }287288 fn last_token_id(&self) -> TokenId {289 TokenId::default()290 }291292 fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {293 None294 }295 fn const_metadata(&self, _token: TokenId) -> Vec<u8> {296 Vec::new()297 }298 fn variable_metadata(&self, _token: TokenId) -> Vec<u8> {299 Vec::new()300 }301302 fn total_supply(&self) -> u32 {303 1304 }305306 fn account_balance(&self, account: T::CrossAccountId) -> u32 {307 if <Balance<T>>::get((self.id, account)) != 0 {308 1309 } else {310 0311 }312 }313314 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {315 if token != TokenId::default() {316 return 0;317 }318 <Balance<T>>::get((self.id, account))319 }320321 fn allowance(322 &self,323 sender: T::CrossAccountId,324 spender: T::CrossAccountId,325 token: TokenId,326 ) -> u128 {327 if token != TokenId::default() {328 return 0;329 }330 <Allowance<T>>::get((self.id, sender, spender))331 }332}pallets/fungible/src/weights.rsdiffbeforeafterboth--- a/pallets/fungible/src/weights.rs
+++ b/pallets/fungible/src/weights.rs
@@ -35,7 +35,8 @@
fn create_item() -> Weight;
fn create_multiple_items_ex(b: u32, ) -> Weight;
fn burn_item() -> Weight;
- fn set_property() -> Weight;
+ fn change_collection_properties(amount: u32) -> Weight;
+ fn change_token_properties(amount: u32) -> Weight;
fn transfer() -> Weight;
fn approve() -> Weight;
fn transfer_from() -> Weight;
@@ -71,11 +72,16 @@
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
- fn set_property() -> Weight {
+ fn change_collection_properties(amount: u32) -> Weight {
// Error
0
}
+ fn change_token_properties(amount: u32) -> Weight {
+ // Error
+ 0
+ }
+
// Storage: Fungible Balance (r:2 w:2)
fn transfer() -> Weight {
(17_713_000 as Weight)
@@ -134,7 +140,12 @@
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
- fn set_property() -> Weight {
+ fn change_collection_properties(amount: u32) -> Weight {
+ // Error
+ 0
+ }
+
+ fn change_token_properties(amount: u32) -> Weight {
// Error
0
}
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -50,8 +50,12 @@
<SelfWeightOf<T>>::burn_item()
}
- fn set_property() -> Weight {
- <SelfWeightOf<T>>::set_property()
+ fn change_collection_properties(amount: u32) -> Weight {
+ <SelfWeightOf<T>>::change_collection_properties(amount)
+ }
+
+ fn change_token_properties(amount: u32) -> Weight {
+ <SelfWeightOf<T>>::change_token_properties(amount)
}
fn transfer() -> Weight {
@@ -145,6 +149,33 @@
)
}
+ fn change_collection_properties(
+ &self,
+ sender: T::CrossAccountId,
+ properties: Vec<Property>,
+ ) -> DispatchResultWithPostInfo {
+ let weight = <CommonWeights<T>>::change_collection_properties(properties.len() as u32);
+
+ with_weight(
+ <Pallet<T>>::change_collection_properties(self, &sender, properties),
+ weight
+ )
+ }
+
+ fn change_token_properties(
+ &self,
+ sender: T::CrossAccountId,
+ token_id: TokenId,
+ properties: Vec<Property>,
+ ) -> DispatchResultWithPostInfo {
+ let weight = <CommonWeights<T>>::change_token_properties(properties.len() as u32);
+
+ with_weight(
+ <Pallet<T>>::change_token_properties(self, &sender, token_id, properties),
+ weight
+ )
+ }
+
fn burn_item(
&self,
sender: T::CrossAccountId,
@@ -239,32 +270,6 @@
} else {
Ok(().into())
}
- }
-
- fn change_collection_property(
- &self,
- sender: T::CrossAccountId,
- property: Property,
- ) -> DispatchResultWithPostInfo {
- // let token_id = None;
- with_weight(
- // <Pallet<T>>::change_property(self, &sender, token_id, property),
- Ok(()),
- <CommonWeights<T>>::set_property(),
- )
- }
-
- fn change_token_property(
- &self,
- sender: T::CrossAccountId,
- token_id: TokenId,
- property: Property,
- ) -> DispatchResultWithPostInfo {
- with_weight(
- // <Pallet<T>>::change_property(self, &sender, Some(token_id), property),
- Ok(()),
- <CommonWeights<T>>::set_property(),
- )
}
fn set_variable_metadata(
pallets/nonfungible/src/lib.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -265,12 +265,11 @@
.map(|p| p.clone())
.unwrap_or(PropertyPermission::None);
- let check_token_owner = || -> DispatchResult {
- let token_data = <TokenData<T>>::get((collection.id, token_id))
- .ok_or(<CommonError<T>>::TokenNotFound)?;
+ let token_data = <TokenData<T>>::get((collection.id, token_id))
+ .ok_or(<CommonError<T>>::TokenNotFound)?;
+ let check_token_owner = || -> DispatchResult {
ensure!(&token_data.owner == sender, <CommonError<T>>::NoPermission);
-
Ok(())
};
@@ -304,6 +303,27 @@
Ok(())
}
+ pub fn change_token_properties(
+ collection: &NonfungibleHandle<T>,
+ sender: &T::CrossAccountId,
+ token_id: TokenId,
+ properties: Vec<Property>,
+ ) -> DispatchResult {
+ for property in properties {
+ Self::change_token_property(collection, sender, token_id, property)?;
+ }
+
+ Ok(())
+ }
+
+ pub fn change_collection_properties(
+ collection: &NonfungibleHandle<T>,
+ sender: &T::CrossAccountId,
+ properties: Vec<Property>,
+ ) -> DispatchResult {
+ <PalletCommon<T>>::change_collection_properties(collection, sender, properties)
+ }
+
pub fn transfer(
collection: &NonfungibleHandle<T>,
from: &T::CrossAccountId,
pallets/nonfungible/src/weights.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/weights.rs
+++ b/pallets/nonfungible/src/weights.rs
@@ -36,7 +36,8 @@
fn create_multiple_items(b: u32, ) -> Weight;
fn create_multiple_items_ex(b: u32, ) -> Weight;
fn burn_item() -> Weight;
- fn set_property() -> Weight;
+ fn change_collection_properties(amount: u32) -> Weight;
+ fn change_token_properties(amount: u32) -> Weight;
fn transfer() -> Weight;
fn approve() -> Weight;
fn transfer_from() -> Weight;
@@ -92,11 +93,16 @@
.saturating_add(T::DbWeight::get().writes(4 as Weight))
}
- fn set_property() -> Weight {
+ fn change_collection_properties(amount: u32) -> Weight {
// TODO calculate appropriate weight
- 50_000_000 as Weight
+ (50_000_000 as Weight).saturating_mul(amount as Weight)
}
+ fn change_token_properties(amount: u32) -> Weight {
+ // TODO calculate appropriate weight
+ (50_000_000 as Weight).saturating_mul(amount as Weight)
+ }
+
// Storage: Nonfungible TokenData (r:1 w:1)
// Storage: Nonfungible AccountBalance (r:2 w:2)
// Storage: Nonfungible Allowance (r:1 w:0)
@@ -187,9 +193,14 @@
.saturating_add(RocksDbWeight::get().writes(4 as Weight))
}
- fn set_property() -> Weight {
+ fn change_collection_properties(amount: u32) -> Weight {
+ // TODO calculate appropriate weight
+ (50_000_000 as Weight).saturating_mul(amount as Weight)
+ }
+
+ fn change_token_properties(amount: u32) -> Weight {
// TODO calculate appropriate weight
- 50_000_000 as Weight
+ (50_000_000 as Weight).saturating_mul(amount as Weight)
}
// Storage: Nonfungible TokenData (r:1 w:1)
pallets/refungible/src/common.rsdiffbeforeafterboth--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -66,8 +66,12 @@
max_weight_of!(burn_item_partial(), burn_item_fully())
}
- fn set_property() -> Weight {
- <SelfWeightOf<T>>::set_property()
+ fn change_collection_properties(amount: u32) -> Weight {
+ <SelfWeightOf<T>>::change_collection_properties(amount)
+ }
+
+ fn change_token_properties(amount: u32) -> Weight {
+ <SelfWeightOf<T>>::change_token_properties(amount)
}
fn transfer() -> Weight {
@@ -248,19 +252,19 @@
)
}
- fn change_collection_property(
+ fn change_collection_properties(
&self,
_sender: T::CrossAccountId,
- _property: Property,
+ _property: Vec<Property>,
) -> DispatchResultWithPostInfo {
fail!(<Error<T>>::PropertiesNotAllowed)
}
- fn change_token_property(
+ fn change_token_properties(
&self,
_sender: T::CrossAccountId,
_token_id: TokenId,
- _property: Property,
+ _property: Vec<Property>,
) -> DispatchResultWithPostInfo {
fail!(<Error<T>>::PropertiesNotAllowed)
}
pallets/refungible/src/weights.rsdiffbeforeafterboth--- a/pallets/refungible/src/weights.rs
+++ b/pallets/refungible/src/weights.rs
@@ -38,7 +38,8 @@
fn create_multiple_items_ex_multiple_owners(b: u32, ) -> Weight;
fn burn_item_partial() -> Weight;
fn burn_item_fully() -> Weight;
- fn set_property() -> Weight;
+ fn change_collection_properties(amount: u32) -> Weight;
+ fn change_token_properties(amount: u32) -> Weight;
fn transfer_normal() -> Weight;
fn transfer_creating() -> Weight;
fn transfer_removing() -> Weight;
@@ -131,11 +132,16 @@
.saturating_add(T::DbWeight::get().writes(6 as Weight))
}
- fn set_property() -> Weight {
+ fn change_collection_properties(amount: u32) -> Weight {
// Error
0
}
+ fn change_token_properties(amount: u32) -> Weight {
+ // Error
+ 0
+ }
+
// Storage: Refungible Balance (r:2 w:2)
fn transfer_normal() -> Weight {
(19_766_000 as Weight)
@@ -305,7 +311,12 @@
.saturating_add(RocksDbWeight::get().writes(6 as Weight))
}
- fn set_property() -> Weight {
+ fn change_collection_properties(amount: u32) -> Weight {
+ // Error
+ 0
+ }
+
+ fn change_token_properties(amount: u32) -> Weight {
// Error
0
}
pallets/unique/src/lib.rsdiffbeforeafterboth--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -694,6 +694,35 @@
dispatch_call::<T, _>(collection_id, |d| d.create_multiple_items(sender, owner, items_data, &budget))
}
+ #[weight = T::CommonWeightInfo::change_collection_properties(properties.len() as u32)]
+ #[transactional]
+ pub fn change_collection_properties(
+ origin,
+ collection_id: CollectionId,
+ properties: Vec<Property>
+ ) -> DispatchResultWithPostInfo {
+ ensure!(!properties.is_empty(), Error::<T>::EmptyArgument);
+
+ let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+
+ dispatch_call::<T, _>(collection_id, |d| d.change_collection_properties(sender, properties))
+ }
+
+ #[weight = T::CommonWeightInfo::change_token_properties(properties.len() as u32)]
+ #[transactional]
+ pub fn change_token_properties(
+ origin,
+ collection_id: CollectionId,
+ token_id: TokenId,
+ properties: Vec<Property>
+ ) -> DispatchResultWithPostInfo {
+ ensure!(!properties.is_empty(), Error::<T>::EmptyArgument);
+
+ let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+
+ dispatch_call::<T, _>(collection_id, |d| d.change_token_properties(sender, token_id, properties))
+ }
+
#[weight = T::CommonWeightInfo::create_multiple_items_ex(&data)]
#[transactional]
pub fn create_multiple_items_ex(origin, collection_id: CollectionId, data: CreateItemExData<T::CrossAccountId>) -> DispatchResultWithPostInfo {
runtime/common/src/weights.rsdiffbeforeafterboth--- a/runtime/common/src/weights.rs
+++ b/runtime/common/src/weights.rs
@@ -54,8 +54,12 @@
dispatch_weight::<T>() + max_weight_of!(burn_item())
}
- fn set_property() -> Weight {
- dispatch_weight::<T>() + max_weight_of!(set_property())
+ fn change_collection_properties(amount: u32) -> Weight {
+ dispatch_weight::<T>() + max_weight_of!(change_collection_properties(amount))
+ }
+
+ fn change_token_properties(amount: u32) -> Weight {
+ dispatch_weight::<T>() + max_weight_of!(change_token_properties(amount))
}
fn transfer() -> Weight {