difftreelog
refactor don't use default impl in common ops
in: master
3 files changed
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -1239,15 +1239,15 @@
fn nest(
&self,
- _under: TokenId,
- _to_nest: (CollectionId, TokenId)
- ) {}
+ under: TokenId,
+ to_nest: (CollectionId, TokenId)
+ );
fn unnest(
&self,
- _under: TokenId,
- _to_nest: (CollectionId, TokenId)
- ) {}
+ under: TokenId,
+ to_nest: (CollectionId, TokenId)
+ );
fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId>;
fn collection_tokens(&self) -> Vec<TokenId>;
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};20use up_data_structs::{TokenId, CollectionId, CreateItemExData, budget::Budget, CreateItemData};21use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};22use sp_runtime::ArithmeticError;23use sp_std::{vec::Vec, vec};24use up_data_structs::{Property, PropertyKey, PropertyValue, PropertyKeyPermission};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(_data: &[CreateItemData]) -> Weight {37 // All items minted for the same user, so it works same as create_item38 Self::create_item()39 }4041 fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {42 match data {43 CreateItemExData::Fungible(f) => {44 <SelfWeightOf<T>>::create_multiple_items_ex(f.len() as u32)45 }46 _ => 0,47 }48 }4950 fn burn_item() -> Weight {51 <SelfWeightOf<T>>::burn_item()52 }5354 fn set_collection_properties(_amount: u32) -> Weight {55 // Error56 057 }5859 fn delete_collection_properties(_amount: u32) -> Weight {60 // Error61 062 }6364 fn set_token_properties(_amount: u32) -> Weight {65 // Error66 067 }6869 fn delete_token_properties(_amount: u32) -> Weight {70 // Error71 072 }7374 fn set_property_permissions(_amount: u32) -> Weight {75 // Error76 077 }7879 fn transfer() -> Weight {80 <SelfWeightOf<T>>::transfer()81 }8283 fn approve() -> Weight {84 <SelfWeightOf<T>>::approve()85 }8687 fn transfer_from() -> Weight {88 <SelfWeightOf<T>>::transfer_from()89 }9091 fn burn_from() -> Weight {92 <SelfWeightOf<T>>::burn_from()93 }94}9596impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {97 fn create_item(98 &self,99 sender: T::CrossAccountId,100 to: T::CrossAccountId,101 data: up_data_structs::CreateItemData,102 nesting_budget: &dyn Budget,103 ) -> DispatchResultWithPostInfo {104 match data {105 up_data_structs::CreateItemData::Fungible(data) => with_weight(106 <Pallet<T>>::create_item(self, &sender, (to, data.value), nesting_budget),107 <CommonWeights<T>>::create_item(),108 ),109 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),110 }111 }112113 fn create_multiple_items(114 &self,115 sender: T::CrossAccountId,116 to: T::CrossAccountId,117 data: Vec<up_data_structs::CreateItemData>,118 nesting_budget: &dyn Budget,119 ) -> DispatchResultWithPostInfo {120 let mut sum: u128 = 0;121 for data in data {122 match data {123 up_data_structs::CreateItemData::Fungible(data) => {124 sum = sum125 .checked_add(data.value)126 .ok_or(ArithmeticError::Overflow)?;127 }128 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),129 }130 }131132 with_weight(133 <Pallet<T>>::create_item(self, &sender, (to, sum), nesting_budget),134 <CommonWeights<T>>::create_item(),135 )136 }137138 fn create_multiple_items_ex(139 &self,140 sender: <T>::CrossAccountId,141 data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,142 nesting_budget: &dyn Budget,143 ) -> DispatchResultWithPostInfo {144 let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);145 let data = match data {146 up_data_structs::CreateItemExData::Fungible(f) => f,147 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),148 };149150 with_weight(151 <Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),152 weight,153 )154 }155156 fn burn_item(157 &self,158 sender: T::CrossAccountId,159 token: TokenId,160 amount: u128,161 ) -> DispatchResultWithPostInfo {162 ensure!(163 token == TokenId::default(),164 <Error<T>>::FungibleItemsHaveNoId165 );166167 with_weight(168 <Pallet<T>>::burn(self, &sender, amount),169 <CommonWeights<T>>::burn_item(),170 )171 }172173 fn transfer(174 &self,175 from: T::CrossAccountId,176 to: T::CrossAccountId,177 token: TokenId,178 amount: u128,179 nesting_budget: &dyn Budget,180 ) -> DispatchResultWithPostInfo {181 ensure!(182 token == TokenId::default(),183 <Error<T>>::FungibleItemsHaveNoId184 );185186 with_weight(187 <Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),188 <CommonWeights<T>>::transfer(),189 )190 }191192 fn approve(193 &self,194 sender: T::CrossAccountId,195 spender: T::CrossAccountId,196 token: TokenId,197 amount: u128,198 ) -> DispatchResultWithPostInfo {199 ensure!(200 token == TokenId::default(),201 <Error<T>>::FungibleItemsHaveNoId202 );203204 with_weight(205 <Pallet<T>>::set_allowance(self, &sender, &spender, amount),206 <CommonWeights<T>>::approve(),207 )208 }209210 fn transfer_from(211 &self,212 sender: T::CrossAccountId,213 from: T::CrossAccountId,214 to: T::CrossAccountId,215 token: TokenId,216 amount: u128,217 nesting_budget: &dyn Budget,218 ) -> DispatchResultWithPostInfo {219 ensure!(220 token == TokenId::default(),221 <Error<T>>::FungibleItemsHaveNoId222 );223224 with_weight(225 <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),226 <CommonWeights<T>>::transfer_from(),227 )228 }229230 fn burn_from(231 &self,232 sender: T::CrossAccountId,233 from: T::CrossAccountId,234 token: TokenId,235 amount: u128,236 nesting_budget: &dyn Budget,237 ) -> DispatchResultWithPostInfo {238 ensure!(239 token == TokenId::default(),240 <Error<T>>::FungibleItemsHaveNoId241 );242243 with_weight(244 <Pallet<T>>::burn_from(self, &sender, &from, amount, nesting_budget),245 <CommonWeights<T>>::burn_from(),246 )247 }248249 fn set_collection_properties(250 &self,251 _sender: T::CrossAccountId,252 _property: Vec<Property>,253 ) -> DispatchResultWithPostInfo {254 fail!(<Error<T>>::SettingPropertiesNotAllowed)255 }256257 fn delete_collection_properties(258 &self,259 _sender: &T::CrossAccountId,260 _property_keys: Vec<PropertyKey>,261 ) -> DispatchResultWithPostInfo {262 fail!(<Error<T>>::SettingPropertiesNotAllowed)263 }264265 fn set_token_properties(266 &self,267 _sender: T::CrossAccountId,268 _token_id: TokenId,269 _property: Vec<Property>,270 ) -> DispatchResultWithPostInfo {271 fail!(<Error<T>>::SettingPropertiesNotAllowed)272 }273274 fn set_property_permissions(275 &self,276 _sender: &T::CrossAccountId,277 _property_permissions: Vec<PropertyKeyPermission>,278 ) -> DispatchResultWithPostInfo {279 fail!(<Error<T>>::SettingPropertiesNotAllowed)280 }281282 fn delete_token_properties(283 &self,284 _sender: T::CrossAccountId,285 _token_id: TokenId,286 _property_keys: Vec<PropertyKey>,287 ) -> DispatchResultWithPostInfo {288 fail!(<Error<T>>::SettingPropertiesNotAllowed)289 }290291 fn check_nesting(292 &self,293 _sender: <T>::CrossAccountId,294 _from: (CollectionId, TokenId),295 _under: TokenId,296 _budget: &dyn Budget,297 ) -> sp_runtime::DispatchResult {298 fail!(<Error<T>>::FungibleDisallowsNesting)299 }300301 fn collection_tokens(&self) -> Vec<TokenId> {302 vec![TokenId::default()]303 }304305 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {306 if <Balance<T>>::get((self.id, account)) != 0 {307 vec![TokenId::default()]308 } else {309 vec![]310 }311 }312313 fn token_exists(&self, token: TokenId) -> bool {314 token == TokenId::default()315 }316317 fn last_token_id(&self) -> TokenId {318 TokenId::default()319 }320321 fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {322 None323 }324325 fn token_property(&self, _token_id: TokenId, _key: &PropertyKey) -> Option<PropertyValue> {326 None327 }328329 fn token_properties(330 &self,331 _token_id: TokenId,332 _keys: Option<Vec<PropertyKey>>,333 ) -> Vec<Property> {334 Vec::new()335 }336337 fn total_supply(&self) -> u32 {338 1339 }340341 fn account_balance(&self, account: T::CrossAccountId) -> u32 {342 if <Balance<T>>::get((self.id, account)) != 0 {343 1344 } else {345 0346 }347 }348349 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {350 if token != TokenId::default() {351 return 0;352 }353 <Balance<T>>::get((self.id, account))354 }355356 fn allowance(357 &self,358 sender: T::CrossAccountId,359 spender: T::CrossAccountId,360 token: TokenId,361 ) -> u128 {362 if token != TokenId::default() {363 return 0;364 }365 <Allowance<T>>::get((self.id, sender, spender))366 }367}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};20use up_data_structs::{TokenId, CollectionId, CreateItemExData, budget::Budget, CreateItemData};21use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};22use sp_runtime::ArithmeticError;23use sp_std::{vec::Vec, vec};24use up_data_structs::{Property, PropertyKey, PropertyValue, PropertyKeyPermission};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(_data: &[CreateItemData]) -> Weight {37 // All items minted for the same user, so it works same as create_item38 Self::create_item()39 }4041 fn create_multiple_items_ex(data: &CreateItemExData<T::CrossAccountId>) -> Weight {42 match data {43 CreateItemExData::Fungible(f) => {44 <SelfWeightOf<T>>::create_multiple_items_ex(f.len() as u32)45 }46 _ => 0,47 }48 }4950 fn burn_item() -> Weight {51 <SelfWeightOf<T>>::burn_item()52 }5354 fn set_collection_properties(_amount: u32) -> Weight {55 // Error56 057 }5859 fn delete_collection_properties(_amount: u32) -> Weight {60 // Error61 062 }6364 fn set_token_properties(_amount: u32) -> Weight {65 // Error66 067 }6869 fn delete_token_properties(_amount: u32) -> Weight {70 // Error71 072 }7374 fn set_property_permissions(_amount: u32) -> Weight {75 // Error76 077 }7879 fn transfer() -> Weight {80 <SelfWeightOf<T>>::transfer()81 }8283 fn approve() -> Weight {84 <SelfWeightOf<T>>::approve()85 }8687 fn transfer_from() -> Weight {88 <SelfWeightOf<T>>::transfer_from()89 }9091 fn burn_from() -> Weight {92 <SelfWeightOf<T>>::burn_from()93 }94}9596impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {97 fn create_item(98 &self,99 sender: T::CrossAccountId,100 to: T::CrossAccountId,101 data: up_data_structs::CreateItemData,102 nesting_budget: &dyn Budget,103 ) -> DispatchResultWithPostInfo {104 match data {105 up_data_structs::CreateItemData::Fungible(data) => with_weight(106 <Pallet<T>>::create_item(self, &sender, (to, data.value), nesting_budget),107 <CommonWeights<T>>::create_item(),108 ),109 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),110 }111 }112113 fn create_multiple_items(114 &self,115 sender: T::CrossAccountId,116 to: T::CrossAccountId,117 data: Vec<up_data_structs::CreateItemData>,118 nesting_budget: &dyn Budget,119 ) -> DispatchResultWithPostInfo {120 let mut sum: u128 = 0;121 for data in data {122 match data {123 up_data_structs::CreateItemData::Fungible(data) => {124 sum = sum125 .checked_add(data.value)126 .ok_or(ArithmeticError::Overflow)?;127 }128 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),129 }130 }131132 with_weight(133 <Pallet<T>>::create_item(self, &sender, (to, sum), nesting_budget),134 <CommonWeights<T>>::create_item(),135 )136 }137138 fn create_multiple_items_ex(139 &self,140 sender: <T>::CrossAccountId,141 data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,142 nesting_budget: &dyn Budget,143 ) -> DispatchResultWithPostInfo {144 let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);145 let data = match data {146 up_data_structs::CreateItemExData::Fungible(f) => f,147 _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),148 };149150 with_weight(151 <Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),152 weight,153 )154 }155156 fn burn_item(157 &self,158 sender: T::CrossAccountId,159 token: TokenId,160 amount: u128,161 ) -> DispatchResultWithPostInfo {162 ensure!(163 token == TokenId::default(),164 <Error<T>>::FungibleItemsHaveNoId165 );166167 with_weight(168 <Pallet<T>>::burn(self, &sender, amount),169 <CommonWeights<T>>::burn_item(),170 )171 }172173 fn transfer(174 &self,175 from: T::CrossAccountId,176 to: T::CrossAccountId,177 token: TokenId,178 amount: u128,179 nesting_budget: &dyn Budget,180 ) -> DispatchResultWithPostInfo {181 ensure!(182 token == TokenId::default(),183 <Error<T>>::FungibleItemsHaveNoId184 );185186 with_weight(187 <Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),188 <CommonWeights<T>>::transfer(),189 )190 }191192 fn approve(193 &self,194 sender: T::CrossAccountId,195 spender: T::CrossAccountId,196 token: TokenId,197 amount: u128,198 ) -> DispatchResultWithPostInfo {199 ensure!(200 token == TokenId::default(),201 <Error<T>>::FungibleItemsHaveNoId202 );203204 with_weight(205 <Pallet<T>>::set_allowance(self, &sender, &spender, amount),206 <CommonWeights<T>>::approve(),207 )208 }209210 fn transfer_from(211 &self,212 sender: T::CrossAccountId,213 from: T::CrossAccountId,214 to: T::CrossAccountId,215 token: TokenId,216 amount: u128,217 nesting_budget: &dyn Budget,218 ) -> DispatchResultWithPostInfo {219 ensure!(220 token == TokenId::default(),221 <Error<T>>::FungibleItemsHaveNoId222 );223224 with_weight(225 <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget),226 <CommonWeights<T>>::transfer_from(),227 )228 }229230 fn burn_from(231 &self,232 sender: T::CrossAccountId,233 from: T::CrossAccountId,234 token: TokenId,235 amount: u128,236 nesting_budget: &dyn Budget,237 ) -> DispatchResultWithPostInfo {238 ensure!(239 token == TokenId::default(),240 <Error<T>>::FungibleItemsHaveNoId241 );242243 with_weight(244 <Pallet<T>>::burn_from(self, &sender, &from, amount, nesting_budget),245 <CommonWeights<T>>::burn_from(),246 )247 }248249 fn set_collection_properties(250 &self,251 _sender: T::CrossAccountId,252 _property: Vec<Property>,253 ) -> DispatchResultWithPostInfo {254 fail!(<Error<T>>::SettingPropertiesNotAllowed)255 }256257 fn delete_collection_properties(258 &self,259 _sender: &T::CrossAccountId,260 _property_keys: Vec<PropertyKey>,261 ) -> DispatchResultWithPostInfo {262 fail!(<Error<T>>::SettingPropertiesNotAllowed)263 }264265 fn set_token_properties(266 &self,267 _sender: T::CrossAccountId,268 _token_id: TokenId,269 _property: Vec<Property>,270 ) -> DispatchResultWithPostInfo {271 fail!(<Error<T>>::SettingPropertiesNotAllowed)272 }273274 fn set_property_permissions(275 &self,276 _sender: &T::CrossAccountId,277 _property_permissions: Vec<PropertyKeyPermission>,278 ) -> DispatchResultWithPostInfo {279 fail!(<Error<T>>::SettingPropertiesNotAllowed)280 }281282 fn delete_token_properties(283 &self,284 _sender: T::CrossAccountId,285 _token_id: TokenId,286 _property_keys: Vec<PropertyKey>,287 ) -> DispatchResultWithPostInfo {288 fail!(<Error<T>>::SettingPropertiesNotAllowed)289 }290291 fn check_nesting(292 &self,293 _sender: <T>::CrossAccountId,294 _from: (CollectionId, TokenId),295 _under: TokenId,296 _budget: &dyn Budget,297 ) -> sp_runtime::DispatchResult {298 fail!(<Error<T>>::FungibleDisallowsNesting)299 }300301 fn nest(302 &self,303 _under: TokenId,304 _to_nest: (CollectionId, TokenId)305 ) {}306307 fn unnest(308 &self,309 _under: TokenId,310 _to_nest: (CollectionId, TokenId)311 ) {}312313 fn collection_tokens(&self) -> Vec<TokenId> {314 vec![TokenId::default()]315 }316317 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {318 if <Balance<T>>::get((self.id, account)) != 0 {319 vec![TokenId::default()]320 } else {321 vec![]322 }323 }324325 fn token_exists(&self, token: TokenId) -> bool {326 token == TokenId::default()327 }328329 fn last_token_id(&self) -> TokenId {330 TokenId::default()331 }332333 fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {334 None335 }336337 fn token_property(&self, _token_id: TokenId, _key: &PropertyKey) -> Option<PropertyValue> {338 None339 }340341 fn token_properties(342 &self,343 _token_id: TokenId,344 _keys: Option<Vec<PropertyKey>>,345 ) -> Vec<Property> {346 Vec::new()347 }348349 fn total_supply(&self) -> u32 {350 1351 }352353 fn account_balance(&self, account: T::CrossAccountId) -> u32 {354 if <Balance<T>>::get((self.id, account)) != 0 {355 1356 } else {357 0358 }359 }360361 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {362 if token != TokenId::default() {363 return 0;364 }365 <Balance<T>>::get((self.id, account))366 }367368 fn allowance(369 &self,370 sender: T::CrossAccountId,371 spender: T::CrossAccountId,372 token: TokenId,373 ) -> u128 {374 if token != TokenId::default() {375 return 0;376 }377 <Allowance<T>>::get((self.id, sender, spender))378 }379}pallets/refungible/src/common.rsdiffbeforeafterboth--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -313,6 +313,18 @@
fail!(<Error<T>>::RefungibleDisallowsNesting)
}
+ fn nest(
+ &self,
+ _under: TokenId,
+ _to_nest: (CollectionId, TokenId)
+ ) {}
+
+ fn unnest(
+ &self,
+ _under: TokenId,
+ _to_nest: (CollectionId, TokenId)
+ ) {}
+
fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {
<Owned<T>>::iter_prefix((self.id, account))
.map(|(id, _)| id)