difftreelog
feat nest on create/transfer
in: master
12 files changed
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -723,17 +723,20 @@
sender: T::CrossAccountId,
to: T::CrossAccountId,
data: CreateItemData,
+ nesting_budget: &dyn Budget,
) -> DispatchResultWithPostInfo;
fn create_multiple_items(
&self,
sender: T::CrossAccountId,
to: T::CrossAccountId,
data: Vec<CreateItemData>,
+ nesting_budget: &dyn Budget,
) -> DispatchResultWithPostInfo;
fn create_multiple_items_ex(
&self,
sender: T::CrossAccountId,
data: CreateItemExData<T::CrossAccountId>,
+ nesting_budget: &dyn Budget,
) -> DispatchResultWithPostInfo;
fn burn_item(
&self,
@@ -748,6 +751,7 @@
to: T::CrossAccountId,
token: TokenId,
amount: u128,
+ nesting_budget: &dyn Budget,
) -> DispatchResultWithPostInfo;
fn approve(
&self,
@@ -781,11 +785,12 @@
data: BoundedVec<u8, CustomDataLimit>,
) -> DispatchResultWithPostInfo;
- fn nest_token(
+ fn check_nesting(
&self,
sender: T::CrossAccountId,
- from: (CollectionId, TokenId),
+ from: CollectionId,
under: TokenId,
+ budget: &dyn Budget,
) -> DispatchResult;
fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId>;
pallets/evm-coder-substrate/src/lib.rsdiffbeforeafterboth--- a/pallets/evm-coder-substrate/src/lib.rs
+++ b/pallets/evm-coder-substrate/src/lib.rs
@@ -55,7 +55,6 @@
use frame_system::ensure_signed;
pub use frame_support::dispatch::DispatchResult;
- use frame_support::{pallet_prelude::*, traits::PalletInfo};
use frame_system::pallet_prelude::*;
/// DispatchError is opaque, but we need to somehow extract correct error in case of OutOfGas failure
pallets/fungible/src/common.rsdiffbeforeafterboth--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -17,7 +17,7 @@
use core::marker::PhantomData;
use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, BoundedVec};
-use up_data_structs::{TokenId, CreateItemExData, budget::Budget};
+use up_data_structs::{TokenId, CollectionId, CreateItemExData, budget::Budget};
use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};
use sp_runtime::ArithmeticError;
use sp_std::{vec::Vec, vec};
@@ -78,10 +78,11 @@
sender: T::CrossAccountId,
to: T::CrossAccountId,
data: up_data_structs::CreateItemData,
+ nesting_budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
match data {
up_data_structs::CreateItemData::Fungible(data) => with_weight(
- <Pallet<T>>::create_item(self, &sender, (to, data.value)),
+ <Pallet<T>>::create_item(self, &sender, (to, data.value), nesting_budget),
<CommonWeights<T>>::create_item(),
),
_ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),
@@ -93,6 +94,7 @@
sender: T::CrossAccountId,
to: T::CrossAccountId,
data: Vec<up_data_structs::CreateItemData>,
+ nesting_budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
let mut sum: u128 = 0;
for data in data {
@@ -107,7 +109,7 @@
}
with_weight(
- <Pallet<T>>::create_item(self, &sender, (to, sum)),
+ <Pallet<T>>::create_item(self, &sender, (to, sum), nesting_budget),
<CommonWeights<T>>::create_item(),
)
}
@@ -116,6 +118,7 @@
&self,
sender: <T>::CrossAccountId,
data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,
+ nesting_budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);
let data = match data {
@@ -124,7 +127,7 @@
};
with_weight(
- <Pallet<T>>::create_multiple_items(self, &sender, data.into_inner()),
+ <Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),
weight,
)
}
@@ -152,6 +155,7 @@
to: T::CrossAccountId,
token: TokenId,
amount: u128,
+ nesting_budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
ensure!(
token == TokenId::default(),
@@ -159,7 +163,7 @@
);
with_weight(
- <Pallet<T>>::transfer(self, &from, &to, amount),
+ <Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget),
<CommonWeights<T>>::transfer(),
)
}
@@ -230,11 +234,12 @@
fail!(<Error<T>>::FungibleItemsDontHaveData)
}
- fn nest_token(
+ fn check_nesting(
&self,
_sender: <T>::CrossAccountId,
- _from: (up_data_structs::CollectionId, TokenId),
+ _from: CollectionId,
_under: TokenId,
+ _budget: &dyn Budget,
) -> sp_runtime::DispatchResult {
fail!(<Error<T>>::FungibleDisallowsNesting)
}
pallets/fungible/src/erc.rsdiffbeforeafterboth--- a/pallets/fungible/src/erc.rs
+++ b/pallets/fungible/src/erc.rs
@@ -81,8 +81,11 @@
let caller = T::CrossAccountId::from_eth(caller);
let to = T::CrossAccountId::from_eth(to);
let amount = amount.try_into().map_err(|_| "amount overflow")?;
+ let budget = self
+ .recorder
+ .weight_calls_budget(<StructureWeight<T>>::find_parent());
- <Pallet<T>>::transfer(self, &caller, &to, amount).map_err(|_| "transfer error")?;
+ <Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;
Ok(true)
}
#[weight(<SelfWeightOf<T>>::transfer_from())]
pallets/fungible/src/lib.rsdiffbeforeafterboth--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -193,6 +193,7 @@
from: &T::CrossAccountId,
to: &T::CrossAccountId,
amount: u128,
+ nesting_budget: &dyn Budget,
) -> DispatchResult {
ensure!(
collection.limits.transfers_enabled(),
@@ -222,12 +223,12 @@
let handle = <CollectionHandle<T>>::try_get(target.0)?;
let dispatch = T::CollectionDispatch::dispatch(handle);
let dispatch = dispatch.as_dyn();
-
- // =========
- dispatch.nest_token(from.clone(), (collection.id, TokenId::default()), target.1)?;
+ dispatch.check_nesting(from.clone(), collection.id, target.1, nesting_budget)?;
}
+ // =========
+
if let Some(balance_to) = balance_to {
// from != to
if balance_from == 0 {
@@ -257,6 +258,7 @@
collection: &FungibleHandle<T>,
sender: &T::CrossAccountId,
data: BTreeMap<T::CrossAccountId, u128>,
+ nesting_budget: &dyn Budget,
) -> DispatchResult {
if !collection.is_owner_or_admin(sender) {
ensure!(
@@ -285,6 +287,16 @@
.ok_or(ArithmeticError::Overflow)?;
}
+ for (to, _) in balances.iter() {
+ if let Some(target) = T::CrossTokenAddressMapping::address_to_token(to) {
+ let handle = <CollectionHandle<T>>::try_get(target.0)?;
+ let dispatch = T::CollectionDispatch::dispatch(handle);
+ let dispatch = dispatch.as_dyn();
+
+ dispatch.check_nesting(sender.clone(), collection.id, target.1, nesting_budget)?;
+ }
+ }
+
// =========
<TotalSupply<T>>::insert(collection.id, total_supply);
@@ -407,7 +419,7 @@
// =========
- Self::transfer(collection, from, to, amount)?;
+ Self::transfer(collection, from, to, amount, nesting_budget)?;
if let Some(allowance) = allowance {
Self::set_allowance_unchecked(collection, from, spender, allowance);
}
@@ -437,7 +449,13 @@
collection: &FungibleHandle<T>,
sender: &T::CrossAccountId,
data: CreateItemData<T>,
+ nesting_budget: &dyn Budget,
) -> DispatchResult {
- Self::create_multiple_items(collection, sender, [(data.0, data.1)].into_iter().collect())
+ Self::create_multiple_items(
+ collection,
+ sender,
+ [(data.0, data.1)].into_iter().collect(),
+ nesting_budget,
+ )
}
}
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -89,9 +89,15 @@
sender: T::CrossAccountId,
to: T::CrossAccountId,
data: up_data_structs::CreateItemData,
+ nesting_budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
with_weight(
- <Pallet<T>>::create_item(self, &sender, map_create_data::<T>(data, &to)?),
+ <Pallet<T>>::create_item(
+ self,
+ &sender,
+ map_create_data::<T>(data, &to)?,
+ nesting_budget,
+ ),
<CommonWeights<T>>::create_item(),
)
}
@@ -101,6 +107,7 @@
sender: T::CrossAccountId,
to: T::CrossAccountId,
data: Vec<up_data_structs::CreateItemData>,
+ nesting_budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
let data = data
.into_iter()
@@ -109,7 +116,7 @@
let amount = data.len();
with_weight(
- <Pallet<T>>::create_multiple_items(self, &sender, data),
+ <Pallet<T>>::create_multiple_items(self, &sender, data, nesting_budget),
<CommonWeights<T>>::create_multiple_items(amount as u32),
)
}
@@ -118,6 +125,7 @@
&self,
sender: <T>::CrossAccountId,
data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,
+ nesting_budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);
let data = match data {
@@ -126,7 +134,7 @@
};
with_weight(
- <Pallet<T>>::create_multiple_items(self, &sender, data.into_inner()),
+ <Pallet<T>>::create_multiple_items(self, &sender, data.into_inner(), nesting_budget),
weight,
)
}
@@ -154,11 +162,12 @@
to: T::CrossAccountId,
token: TokenId,
amount: u128,
+ nesting_budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
ensure!(amount <= 1, <Error<T>>::NonfungibleItemsHaveNoAmount);
if amount == 1 {
with_weight(
- <Pallet<T>>::transfer(self, &from, &to, token),
+ <Pallet<T>>::transfer(self, &from, &to, token, nesting_budget),
<CommonWeights<T>>::transfer(),
)
} else {
@@ -239,13 +248,14 @@
)
}
- fn nest_token(
+ fn check_nesting(
&self,
sender: T::CrossAccountId,
- (from, _): (CollectionId, TokenId),
+ from: CollectionId,
under: TokenId,
+ budget: &dyn Budget,
) -> sp_runtime::DispatchResult {
- <Pallet<T>>::nest_token(self, sender, from, under)
+ <Pallet<T>>::check_nesting(self, sender, from, under, budget)
}
fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {
pallets/nonfungible/src/erc.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -256,6 +256,10 @@
let caller = T::CrossAccountId::from_eth(caller);
let to = T::CrossAccountId::from_eth(to);
let token_id: u32 = token_id.try_into()?;
+ let budget = self
+ .recorder
+ .weight_calls_budget(<StructureWeight<T>>::find_parent());
+
if <TokensMinted<T>>::get(self.id)
.checked_add(1)
.ok_or("item id overflow")?
@@ -272,6 +276,7 @@
variable_data: BoundedVec::default(),
owner: to,
},
+ &budget,
)
.map_err(dispatch_to_evm::<T>)?;
@@ -296,6 +301,10 @@
let caller = T::CrossAccountId::from_eth(caller);
let to = T::CrossAccountId::from_eth(to);
let token_id: u32 = token_id.try_into().map_err(|_| "amount overflow")?;
+ let budget = self
+ .recorder
+ .weight_calls_budget(<StructureWeight<T>>::find_parent());
+
if <TokensMinted<T>>::get(self.id)
.checked_add(1)
.ok_or("item id overflow")?
@@ -314,6 +323,7 @@
variable_data: BoundedVec::default(),
owner: to,
},
+ &budget,
)
.map_err(dispatch_to_evm::<T>)?;
Ok(true)
@@ -338,8 +348,11 @@
let caller = T::CrossAccountId::from_eth(caller);
let to = T::CrossAccountId::from_eth(to);
let token = token_id.try_into()?;
+ let budget = self
+ .recorder
+ .weight_calls_budget(<StructureWeight<T>>::find_parent());
- <Pallet<T>>::transfer(self, &caller, &to, token).map_err(dispatch_to_evm::<T>)?;
+ <Pallet<T>>::transfer(self, &caller, &to, token, &budget).map_err(dispatch_to_evm::<T>)?;
Ok(())
}
@@ -409,6 +422,9 @@
let mut expected_index = <TokensMinted<T>>::get(self.id)
.checked_add(1)
.ok_or("item id overflow")?;
+ let budget = self
+ .recorder
+ .weight_calls_budget(<StructureWeight<T>>::find_parent());
let total_tokens = token_ids.len();
for id in token_ids.into_iter() {
@@ -426,7 +442,8 @@
})
.collect();
- <Pallet<T>>::create_multiple_items(self, &caller, data).map_err(dispatch_to_evm::<T>)?;
+ <Pallet<T>>::create_multiple_items(self, &caller, data, &budget)
+ .map_err(dispatch_to_evm::<T>)?;
Ok(true)
}
@@ -447,6 +464,9 @@
let mut expected_index = <TokensMinted<T>>::get(self.id)
.checked_add(1)
.ok_or("item id overflow")?;
+ let budget = self
+ .recorder
+ .weight_calls_budget(<StructureWeight<T>>::find_parent());
let mut data = Vec::with_capacity(tokens.len());
for (id, token_uri) in tokens {
@@ -465,7 +485,8 @@
});
}
- <Pallet<T>>::create_multiple_items(self, &caller, data).map_err(dispatch_to_evm::<T>)?;
+ <Pallet<T>>::create_multiple_items(self, &caller, data, &budget)
+ .map_err(dispatch_to_evm::<T>)?;
Ok(true)
}
}
pallets/nonfungible/src/lib.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -251,6 +251,7 @@
from: &T::CrossAccountId,
to: &T::CrossAccountId,
token: TokenId,
+ nesting_budget: &dyn Budget,
) -> DispatchResult {
ensure!(
collection.limits.transfers_enabled(),
@@ -294,12 +295,12 @@
let handle = <CollectionHandle<T>>::try_get(target.0)?;
let dispatch = T::CollectionDispatch::dispatch(handle);
let dispatch = dispatch.as_dyn();
-
- // =========
- dispatch.nest_token(from.clone(), (collection.id, token), target.1)?;
+ dispatch.check_nesting(from.clone(), collection.id, target.1, nesting_budget)?;
}
+ // =========
+
<TokenData<T>>::insert(
(collection.id, token),
ItemData {
@@ -340,6 +341,7 @@
collection: &NonfungibleHandle<T>,
sender: &T::CrossAccountId,
data: Vec<CreateItemData<T>>,
+ nesting_budget: &dyn Budget,
) -> DispatchResult {
if !collection.is_owner_or_admin(sender) {
ensure!(
@@ -379,6 +381,16 @@
);
}
+ for (to, _) in balances.iter() {
+ if let Some(target) = T::CrossTokenAddressMapping::address_to_token(to) {
+ let handle = <CollectionHandle<T>>::try_get(target.0)?;
+ let dispatch = T::CollectionDispatch::dispatch(handle);
+ let dispatch = dispatch.as_dyn();
+
+ dispatch.check_nesting(sender.clone(), collection.id, target.1, nesting_budget)?;
+ }
+ }
+
// =========
<TokensMinted<T>>::insert(collection.id, tokens_minted);
@@ -556,7 +568,7 @@
// =========
// Allowance is reset in [`transfer`]
- Self::transfer(collection, from, to, token)
+ Self::transfer(collection, from, to, token, nesting_budget)
}
pub fn burn_from(
@@ -595,35 +607,34 @@
Ok(())
}
- pub fn nest_token(
+ pub fn check_nesting(
handle: &NonfungibleHandle<T>,
sender: T::CrossAccountId,
from: CollectionId,
under: TokenId,
+ nesting_budget: &dyn Budget,
) -> DispatchResult {
fn ensure_sender_allowed<T: Config>(
collection: CollectionId,
token: TokenId,
sender: T::CrossAccountId,
+ budget: &dyn Budget,
) -> DispatchResult {
ensure!(
- <TokenData<T>>::get((collection, token))
- .ok_or(<CommonError<T>>::TokenNotFound)?
- .owner
- .conv_eq(&sender),
+ <PalletStructure<T>>::indirectly_owned(sender, collection, token, budget)?,
<CommonError<T>>::OnlyOwnerAllowedToNest,
);
Ok(())
}
match handle.limits.nesting_rule() {
NestingRule::Disabled => fail!(<CommonError<T>>::NestingIsDisabled),
- NestingRule::Owner => ensure_sender_allowed::<T>(from, under, sender)?,
+ NestingRule::Owner => ensure_sender_allowed::<T>(from, under, sender, nesting_budget)?,
NestingRule::OwnerRestricted(whitelist) => {
ensure!(
whitelist.contains(&from),
<CommonError<T>>::SourceCollectionIsNotAllowedToNest
);
- ensure_sender_allowed::<T>(from, under, sender)?
+ ensure_sender_allowed::<T>(from, under, sender, nesting_budget)?
}
}
Ok(())
@@ -634,7 +645,8 @@
collection: &NonfungibleHandle<T>,
sender: &T::CrossAccountId,
data: CreateItemData<T>,
+ nesting_budget: &dyn Budget,
) -> DispatchResult {
- Self::create_multiple_items(collection, sender, vec![data])
+ Self::create_multiple_items(collection, sender, vec![data], nesting_budget)
}
}
pallets/refungible/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 sp_std::collections::btree_map::BTreeMap;20use frame_support::{dispatch::DispatchResultWithPostInfo, fail, weights::Weight, BoundedVec};21use up_data_structs::{22 TokenId, CustomDataLimit, CreateItemExData, CreateRefungibleExData, budget::Budget,23};24use pallet_common::{CommonCollectionOperations, CommonWeightInfo, with_weight};25use sp_runtime::DispatchError;26use sp_std::{vec::Vec, vec};2728use crate::{29 AccountBalance, Allowance, Balance, Config, Error, Owned, Pallet, RefungibleHandle,30 SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,31};3233macro_rules! max_weight_of {34 ($($method:ident ($($args:tt)*)),*) => {35 036 $(37 .max(<SelfWeightOf<T>>::$method($($args)*))38 )*39 };40}4142pub struct CommonWeights<T: Config>(PhantomData<T>);43impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {44 fn create_item() -> Weight {45 <SelfWeightOf<T>>::create_item()46 }4748 fn create_multiple_items(amount: u32) -> Weight {49 <SelfWeightOf<T>>::create_multiple_items(amount)50 }5152 fn create_multiple_items_ex(call: &CreateItemExData<T::CrossAccountId>) -> Weight {53 match call {54 CreateItemExData::RefungibleMultipleOwners(i) => {55 <SelfWeightOf<T>>::create_multiple_items_ex_multiple_owners(i.users.len() as u32)56 }57 CreateItemExData::RefungibleMultipleItems(i) => {58 <SelfWeightOf<T>>::create_multiple_items_ex_multiple_items(i.len() as u32)59 }60 _ => 0,61 }62 }6364 fn burn_item() -> Weight {65 max_weight_of!(burn_item_partial(), burn_item_fully())66 }6768 fn transfer() -> Weight {69 max_weight_of!(70 transfer_normal(),71 transfer_creating(),72 transfer_removing(),73 transfer_creating_removing()74 )75 }7677 fn approve() -> Weight {78 <SelfWeightOf<T>>::approve()79 }8081 fn transfer_from() -> Weight {82 max_weight_of!(83 transfer_from_normal(),84 transfer_from_creating(),85 transfer_from_removing(),86 transfer_from_creating_removing()87 )88 }8990 fn burn_from() -> Weight {91 <SelfWeightOf<T>>::burn_from()92 }9394 fn set_variable_metadata(bytes: u32) -> Weight {95 <SelfWeightOf<T>>::set_variable_metadata(bytes)96 }97}9899fn map_create_data<T: Config>(100 data: up_data_structs::CreateItemData,101 to: &T::CrossAccountId,102) -> Result<CreateRefungibleExData<T::CrossAccountId>, DispatchError> {103 match data {104 up_data_structs::CreateItemData::ReFungible(data) => Ok(CreateRefungibleExData {105 const_data: data.const_data,106 variable_data: data.variable_data,107 users: {108 let mut out = BTreeMap::new();109 out.insert(to.clone(), data.pieces);110 out.try_into().expect("limit > 0")111 },112 }),113 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),114 }115}116117impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {118 fn create_item(119 &self,120 sender: T::CrossAccountId,121 to: T::CrossAccountId,122 data: up_data_structs::CreateItemData,123 ) -> DispatchResultWithPostInfo {124 with_weight(125 <Pallet<T>>::create_item(self, &sender, map_create_data::<T>(data, &to)?),126 <CommonWeights<T>>::create_item(),127 )128 }129130 fn create_multiple_items(131 &self,132 sender: T::CrossAccountId,133 to: T::CrossAccountId,134 data: Vec<up_data_structs::CreateItemData>,135 ) -> DispatchResultWithPostInfo {136 let data = data137 .into_iter()138 .map(|d| map_create_data::<T>(d, &to))139 .collect::<Result<Vec<_>, DispatchError>>()?;140141 let amount = data.len();142 with_weight(143 <Pallet<T>>::create_multiple_items(self, &sender, data),144 <CommonWeights<T>>::create_multiple_items(amount as u32),145 )146 }147148 fn create_multiple_items_ex(149 &self,150 sender: <T>::CrossAccountId,151 data: CreateItemExData<T::CrossAccountId>,152 ) -> DispatchResultWithPostInfo {153 let weight = <CommonWeights<T>>::create_multiple_items_ex(&data);154 let data = match data {155 CreateItemExData::RefungibleMultipleOwners(r) => vec![r],156 CreateItemExData::RefungibleMultipleItems(r)157 if r.iter().all(|i| i.users.len() == 1) =>158 {159 r.into_inner()160 }161 _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),162 };163164 with_weight(165 <Pallet<T>>::create_multiple_items(self, &sender, data),166 weight,167 )168 }169170 fn burn_item(171 &self,172 sender: T::CrossAccountId,173 token: TokenId,174 amount: u128,175 ) -> DispatchResultWithPostInfo {176 with_weight(177 <Pallet<T>>::burn(self, &sender, token, amount),178 <CommonWeights<T>>::burn_item(),179 )180 }181182 fn transfer(183 &self,184 from: T::CrossAccountId,185 to: T::CrossAccountId,186 token: TokenId,187 amount: u128,188 ) -> DispatchResultWithPostInfo {189 with_weight(190 <Pallet<T>>::transfer(self, &from, &to, token, amount),191 <CommonWeights<T>>::transfer(),192 )193 }194195 fn approve(196 &self,197 sender: T::CrossAccountId,198 spender: T::CrossAccountId,199 token: TokenId,200 amount: u128,201 ) -> DispatchResultWithPostInfo {202 with_weight(203 <Pallet<T>>::set_allowance(self, &sender, &spender, token, amount),204 <CommonWeights<T>>::approve(),205 )206 }207208 fn transfer_from(209 &self,210 sender: T::CrossAccountId,211 from: T::CrossAccountId,212 to: T::CrossAccountId,213 token: TokenId,214 amount: u128,215 nesting_budget: &dyn Budget,216 ) -> DispatchResultWithPostInfo {217 with_weight(218 <Pallet<T>>::transfer_from(self, &sender, &from, &to, token, amount, nesting_budget),219 <CommonWeights<T>>::transfer_from(),220 )221 }222223 fn burn_from(224 &self,225 sender: T::CrossAccountId,226 from: T::CrossAccountId,227 token: TokenId,228 amount: u128,229 nesting_budget: &dyn Budget,230 ) -> DispatchResultWithPostInfo {231 with_weight(232 <Pallet<T>>::burn_from(self, &sender, &from, token, amount, nesting_budget),233 <CommonWeights<T>>::burn_from(),234 )235 }236237 fn set_variable_metadata(238 &self,239 sender: T::CrossAccountId,240 token: TokenId,241 data: BoundedVec<u8, CustomDataLimit>,242 ) -> DispatchResultWithPostInfo {243 let len = data.len();244 with_weight(245 <Pallet<T>>::set_variable_metadata(self, &sender, token, data),246 <CommonWeights<T>>::set_variable_metadata(len as u32),247 )248 }249250 fn nest_token(251 &self,252 _sender: <T>::CrossAccountId,253 _from: (up_data_structs::CollectionId, TokenId),254 _under: TokenId,255 ) -> sp_runtime::DispatchResult {256 fail!(<Error<T>>::RefungibleDisallowsNesting)257 }258259 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {260 <Owned<T>>::iter_prefix((self.id, account))261 .map(|(id, _)| id)262 .collect()263 }264265 fn token_exists(&self, token: TokenId) -> bool {266 <Pallet<T>>::token_exists(self, token)267 }268269 fn last_token_id(&self) -> TokenId {270 TokenId(<TokensMinted<T>>::get(self.id))271 }272273 fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {274 None275 }276 fn const_metadata(&self, token: TokenId) -> Vec<u8> {277 <TokenData<T>>::get((self.id, token))278 .const_data279 .into_inner()280 }281 fn variable_metadata(&self, token: TokenId) -> Vec<u8> {282 <TokenData<T>>::get((self.id, token))283 .variable_data284 .into_inner()285 }286287 fn collection_tokens(&self) -> u32 {288 <Pallet<T>>::total_supply(self)289 }290291 fn account_balance(&self, account: T::CrossAccountId) -> u32 {292 <AccountBalance<T>>::get((self.id, account))293 }294295 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {296 <Balance<T>>::get((self.id, token, account))297 }298299 fn allowance(300 &self,301 sender: T::CrossAccountId,302 spender: T::CrossAccountId,303 token: TokenId,304 ) -> u128 {305 <Allowance<T>>::get((self.id, token, sender, spender))306 }307}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 token_exists(&self, token: TokenId) -> bool {277 <Pallet<T>>::token_exists(self, token)278 }279280 fn last_token_id(&self) -> TokenId {281 TokenId(<TokensMinted<T>>::get(self.id))282 }283284 fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {285 None286 }287 fn const_metadata(&self, token: TokenId) -> Vec<u8> {288 <TokenData<T>>::get((self.id, token))289 .const_data290 .into_inner()291 }292 fn variable_metadata(&self, token: TokenId) -> Vec<u8> {293 <TokenData<T>>::get((self.id, token))294 .variable_data295 .into_inner()296 }297298 fn collection_tokens(&self) -> u32 {299 <Pallet<T>>::total_supply(self)300 }301302 fn account_balance(&self, account: T::CrossAccountId) -> u32 {303 <AccountBalance<T>>::get((self.id, account))304 }305306 fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {307 <Balance<T>>::get((self.id, token, account))308 }309310 fn allowance(311 &self,312 sender: T::CrossAccountId,313 spender: T::CrossAccountId,314 token: TokenId,315 ) -> u128 {316 <Allowance<T>>::get((self.id, token, sender, spender))317 }318}pallets/refungible/src/lib.rsdiffbeforeafterboth--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -289,6 +289,7 @@
to: &T::CrossAccountId,
token: TokenId,
amount: u128,
+ nesting_budget: &dyn Budget,
) -> DispatchResult {
ensure!(
collection.limits.transfers_enabled(),
@@ -351,10 +352,10 @@
let dispatch = T::CollectionDispatch::dispatch(handle);
let dispatch = dispatch.as_dyn();
- // =========
+ dispatch.check_nesting(from.clone(), collection.id, target.1, nesting_budget)?;
+ }
- dispatch.nest_token(from.clone(), (collection.id, token), target.1)?;
- }
+ // =========
if let Some(balance_to) = balance_to {
// from != to
@@ -389,6 +390,7 @@
collection: &RefungibleHandle<T>,
sender: &T::CrossAccountId,
data: Vec<CreateRefungibleExData<T::CrossAccountId>>,
+ nesting_budget: &dyn Budget,
) -> DispatchResult {
if !collection.is_owner_or_admin(sender) {
ensure!(
@@ -453,6 +455,23 @@
}
}
+ for token in data.iter() {
+ for (to, _) in token.users.iter() {
+ if let Some(target) = T::CrossTokenAddressMapping::address_to_token(to) {
+ let handle = <CollectionHandle<T>>::try_get(target.0)?;
+ let dispatch = T::CollectionDispatch::dispatch(handle);
+ let dispatch = dispatch.as_dyn();
+
+ dispatch.check_nesting(
+ sender.clone(),
+ collection.id,
+ target.1,
+ nesting_budget,
+ )?;
+ }
+ }
+ }
+
// =========
<TokensMinted<T>>::insert(collection.id, tokens_minted);
@@ -591,7 +610,7 @@
// =========
- Self::transfer(collection, from, to, token, amount)?;
+ Self::transfer(collection, from, to, token, amount, nesting_budget)?;
if let Some(allowance) = allowance {
Self::set_allowance_unchecked(collection, from, spender, token, allowance);
}
@@ -648,7 +667,8 @@
collection: &RefungibleHandle<T>,
sender: &T::CrossAccountId,
data: CreateRefungibleExData<T::CrossAccountId>,
+ nesting_budget: &dyn Budget,
) -> DispatchResult {
- Self::create_multiple_items(collection, sender, vec![data])
+ Self::create_multiple_items(collection, sender, vec![data], nesting_budget)
}
}
pallets/structure/src/lib.rsdiffbeforeafterboth--- a/pallets/structure/src/lib.rs
+++ b/pallets/structure/src/lib.rs
@@ -19,7 +19,6 @@
use frame_support::Parameter;
use frame_support::dispatch::{GetDispatchInfo, UnfilteredDispatchable};
use frame_support::pallet_prelude::*;
- use frame_system::pallet_prelude::*;
use super::*;
pallets/unique/src/lib.rsdiffbeforeafterboth--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -693,8 +693,9 @@
#[transactional]
pub fn create_item(origin, collection_id: CollectionId, owner: T::CrossAccountId, data: CreateItemData) -> DispatchResultWithPostInfo {
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+ let budget = budget::Value::new(2);
- dispatch_call::<T, _>(collection_id, |d| d.create_item(sender, owner, data))
+ dispatch_call::<T, _>(collection_id, |d| d.create_item(sender, owner, data, &budget))
}
/// This method creates multiple items in a collection created with CreateCollection method.
@@ -720,16 +721,18 @@
pub fn create_multiple_items(origin, collection_id: CollectionId, owner: T::CrossAccountId, items_data: Vec<CreateItemData>) -> DispatchResultWithPostInfo {
ensure!(!items_data.is_empty(), Error::<T>::EmptyArgument);
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+ let budget = budget::Value::new(2);
- dispatch_call::<T, _>(collection_id, |d| d.create_multiple_items(sender, owner, items_data))
+ dispatch_call::<T, _>(collection_id, |d| d.create_multiple_items(sender, owner, items_data, &budget))
}
#[weight = <CommonWeights<T>>::create_multiple_items_ex(&data)]
#[transactional]
pub fn create_multiple_items_ex(origin, collection_id: CollectionId, data: CreateItemExData<T::CrossAccountId>) -> DispatchResultWithPostInfo {
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+ let budget = budget::Value::new(2);
- dispatch_call::<T, _>(collection_id, |d| d.create_multiple_items_ex(sender, data))
+ dispatch_call::<T, _>(collection_id, |d| d.create_multiple_items_ex(sender, data, &budget))
}
// TODO! transaction weight
@@ -839,8 +842,9 @@
#[transactional]
pub fn transfer(origin, recipient: T::CrossAccountId, collection_id: CollectionId, item_id: TokenId, value: u128) -> DispatchResultWithPostInfo {
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+ let budget = budget::Value::new(2);
- dispatch_call::<T, _>(collection_id, |d| d.transfer(sender, recipient, item_id, value))
+ dispatch_call::<T, _>(collection_id, |d| d.transfer(sender, recipient, item_id, value, &budget))
}
/// Set, change, or remove approved address to transfer the ownership of the NFT.