difftreelog
feat burn children when destroying a collection
in: master
12 files changed
pallets/common/src/dispatch.rsdiffbeforeafterboth--- a/pallets/common/src/dispatch.rs
+++ b/pallets/common/src/dispatch.rs
@@ -6,7 +6,7 @@
weights::Pays,
traits::Get,
};
-use up_data_structs::{CollectionId, CreateCollectionData};
+use up_data_structs::{CollectionId, CreateCollectionData, budget::Budget};
use crate::{pallet::Config, CommonCollectionOperations, CollectionHandle};
@@ -57,7 +57,11 @@
pub trait CollectionDispatch<T: Config> {
fn create(sender: T::AccountId, data: CreateCollectionData<T::AccountId>) -> DispatchResult;
- fn destroy(sender: T::CrossAccountId, handle: CollectionHandle<T>) -> DispatchResult;
+ fn destroy(
+ sender: T::CrossAccountId,
+ handle: CollectionHandle<T>,
+ nesting_budget: &dyn Budget,
+ ) -> DispatchResult;
fn dispatch(handle: CollectionHandle<T>) -> Self;
fn into_inner(self) -> CollectionHandle<T>;
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -1169,6 +1169,12 @@
token: TokenId,
amount: u128,
) -> DispatchResultWithPostInfo;
+ fn burn_item_unchecked(
+ &self,
+ owner: &T::CrossAccountId,
+ token: TokenId,
+ amount: u128,
+ ) -> DispatchResult;
fn set_collection_properties(
&self,
sender: T::CrossAccountId,
pallets/fungible/src/common.rsdiffbeforeafterboth--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -170,6 +170,17 @@
)
}
+ fn burn_item_unchecked(
+ &self,
+ owner: &T::CrossAccountId,
+ _token: TokenId,
+ amount: u128,
+ ) -> sp_runtime::DispatchResult {
+ <Pallet<T>>::burn_item_unchecked(self, owner, amount)?;
+
+ Ok(())
+ }
+
fn transfer(
&self,
from: T::CrossAccountId,
pallets/fungible/src/lib.rsdiffbeforeafterboth--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -160,6 +160,36 @@
owner: &T::CrossAccountId,
amount: u128,
) -> DispatchResult {
+ if collection.access == AccessMode::AllowList {
+ collection.check_allowlist(owner)?;
+ }
+
+ // =========
+
+ Self::burn_item_unchecked(collection, owner, amount)?;
+
+ <PalletEvm<T>>::deposit_log(
+ ERC20Events::Transfer {
+ from: *owner.as_eth(),
+ to: H160::default(),
+ value: amount.into(),
+ }
+ .to_log(collection_id_to_address(collection.id)),
+ );
+ <PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(
+ collection.id,
+ TokenId::default(),
+ owner.clone(),
+ amount,
+ ));
+ Ok(())
+ }
+
+ pub fn burn_item_unchecked(
+ collection: &FungibleHandle<T>,
+ owner: &T::CrossAccountId,
+ amount: u128,
+ ) -> DispatchResult {
let total_supply = <TotalSupply<T>>::get(collection.id)
.checked_sub(amount)
.ok_or(<CommonError<T>>::TokenValueTooLow)?;
@@ -186,20 +216,6 @@
}
<TotalSupply<T>>::insert(collection.id, total_supply);
- <PalletEvm<T>>::deposit_log(
- ERC20Events::Transfer {
- from: *owner.as_eth(),
- to: H160::default(),
- value: amount.into(),
- }
- .to_log(collection_id_to_address(collection.id)),
- );
- <PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(
- collection.id,
- TokenId::default(),
- owner.clone(),
- amount,
- ));
Ok(())
}
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -264,6 +264,19 @@
}
}
+ fn burn_item_unchecked(
+ &self,
+ owner:& T::CrossAccountId,
+ token: TokenId,
+ amount: u128,
+ ) -> sp_runtime::DispatchResult {
+ if amount == 1 {
+ <Pallet<T>>::burn_item_unchecked(self, owner, token)
+ } else {
+ Ok(())
+ }
+ }
+
fn transfer(
&self,
from: T::CrossAccountId,
pallets/nonfungible/src/lib.rsdiffbeforeafterboth27use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};27use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};28use pallet_common::{28use pallet_common::{29 Error as CommonError, Pallet as PalletCommon, Event as CommonEvent, CollectionHandle,29 Error as CommonError, Pallet as PalletCommon, Event as CommonEvent, CollectionHandle,30 dispatch::CollectionDispatch,30 eth::collection_id_to_address,31 eth::collection_id_to_address,31};32};32use pallet_structure::Pallet as PalletStructure;33use pallet_structure::Pallet as PalletStructure;79 NonfungibleItemsHaveNoAmount,80 NonfungibleItemsHaveNoAmount,80 /// Unable to burn NFT with children81 /// Unable to burn NFT with children81 CantBurnNftWithChildren,82 CantBurnNftWithChildren,83 /// Too many children to burn when destroying a collection84 TooManyChildrenToBurn,82 }85 }838684 #[pallet::config]87 #[pallet::config]290 pub fn destroy_collection(293 pub fn destroy_collection(291 collection: NonfungibleHandle<T>,294 collection: NonfungibleHandle<T>,292 sender: &T::CrossAccountId,295 sender: &T::CrossAccountId,296 nesting_budget: &dyn Budget,293 ) -> DispatchResult {297 ) -> DispatchResult {294 let id = collection.id;298 let id = collection.id;295299296 // =========300 // =========297301302 Self::burn_children_in_collection(id, nesting_budget)?;298 PalletCommon::destroy_collection(collection.0, sender)?;303 PalletCommon::destroy_collection(collection.0, sender)?;299300 <TokenData<T>>::remove_prefix((id,), None);304 <TokenData<T>>::remove_prefix((id,), None);307 Ok(())311 Ok(())308 }312 }313314 #[transactional]315 fn burn_children_in_collection(collection_id: CollectionId, nesting_budget: &dyn Budget) -> DispatchResult {316 for (parent_id, child) in <TokenChildren<T>>::drain_prefix((collection_id,))317 .map(|((parent_id, child), _)| (parent_id, child)) {318319 let parent_address = T::CrossTokenAddressMapping::token_to_address(collection_id, parent_id);320 Self::burn_tree(parent_address, child.0, child.1, nesting_budget)?;321 }322323 Ok(())324 }325326 fn burn_tree(327 parent: T::CrossAccountId,328 collection_id: CollectionId,329 token_id: TokenId,330 nesting_budget: &dyn Budget331 ) -> DispatchResult {332 if !nesting_budget.consume() {333 return Err(<Error<T>>::TooManyChildrenToBurn.into());334 }335336 let handle = <CollectionHandle<T>>::try_get(collection_id)?;337 let handle = T::CollectionDispatch::dispatch(handle);338 let handle = handle.as_dyn();339340 let amount = handle.balance(parent.clone(), token_id);341342 handle.burn_item_unchecked(&parent, token_id, amount)?;343344 for child in <TokenChildren<T>>::drain_prefix((collection_id, token_id)).map(|(child, _)| child) {345 let parent = T::CrossTokenAddressMapping::token_to_address(collection_id, token_id);346 Self::burn_tree(parent, child.0, child.1, nesting_budget)?;347 }348349 Ok(())350 }309351310 pub fn burn(352 pub fn burn(311 collection: &NonfungibleHandle<T>,353 collection: &NonfungibleHandle<T>,328 return Err(<Error<T>>::CantBurnNftWithChildren.into());370 return Err(<Error<T>>::CantBurnNftWithChildren.into());329 }371 }330372331 let burnt = <TokensBurnt<T>>::get(collection.id)373 let old_spender = <Allowance<T>>::get((collection.id, token));332 .checked_add(1)333 .ok_or(ArithmeticError::Overflow)?;334335 let balance = <AccountBalance<T>>::get((collection.id, token_data.owner.clone()))336 .checked_sub(1)337 .ok_or(ArithmeticError::Overflow)?;338339 if balance == 0 {340 <AccountBalance<T>>::remove((collection.id, token_data.owner.clone()));341 } else {342 <AccountBalance<T>>::insert((collection.id, token_data.owner.clone()), balance);343 }344345 if let Some(owner) = T::CrossTokenAddressMapping::address_to_token(&token_data.owner) {346 Self::unnest(owner, (collection.id, token));347 }348374349 // =========375 // =========350376351 <Owned<T>>::remove((collection.id, &token_data.owner, token));377 Self::burn_item_unchecked(collection, &token_data.owner, token)?;352 <TokensBurnt<T>>::insert(collection.id, burnt);353 <TokenData<T>>::remove((collection.id, token));354 <TokenProperties<T>>::remove((collection.id, token));355 let old_spender = <Allowance<T>>::take((collection.id, token));356378357 if let Some(old_spender) = old_spender {379 if let Some(old_spender) = old_spender {358 <PalletCommon<T>>::deposit_event(CommonEvent::Approved(380 <PalletCommon<T>>::deposit_event(CommonEvent::Approved(381 Ok(())403 Ok(())382 }404 }405406 pub fn burn_item_unchecked(407 collection: &NonfungibleHandle<T>,408 owner: &T::CrossAccountId,409 token: TokenId,410 ) -> DispatchResult {411 let burnt = <TokensBurnt<T>>::get(collection.id)412 .checked_add(1)413 .ok_or(ArithmeticError::Overflow)?;414415 let balance = <AccountBalance<T>>::get((collection.id, owner.clone()))416 .checked_sub(1)417 .ok_or(ArithmeticError::Overflow)?;418419 // =========420421 if let Some(owner) = T::CrossTokenAddressMapping::address_to_token(owner) {422 Self::unnest(owner, (collection.id, token));423 }424425 if balance == 0 {426 <AccountBalance<T>>::remove((collection.id, owner.clone()));427 } else {428 <AccountBalance<T>>::insert((collection.id, owner.clone()), balance);429 }430431 <Owned<T>>::remove((collection.id, owner, token));432 <TokensBurnt<T>>::insert(collection.id, burnt);433 <TokenData<T>>::remove((collection.id, token));434 <TokenProperties<T>>::remove((collection.id, token));435 <Allowance<T>>::remove((collection.id, token));436437 Ok(())438 }383439384 pub fn set_token_property(440 pub fn set_token_property(385 collection: &NonfungibleHandle<T>,441 collection: &NonfungibleHandle<T>,pallets/proxy-rmrk-core/src/lib.rsdiffbeforeafterboth--- a/pallets/proxy-rmrk-core/src/lib.rs
+++ b/pallets/proxy-rmrk-core/src/lib.rs
@@ -179,7 +179,8 @@
ensure!(collection.total_supply() == 0, <Error<T>>::CollectionNotEmpty);
- <PalletNft<T>>::destroy_collection(collection, &cross_sender)
+ let empty_budget = budget::Value::new(0);
+ <PalletNft<T>>::destroy_collection(collection, &cross_sender, &empty_budget)
.map_err(Self::map_common_err_to_proxy)?;
Self::deposit_event(Event::CollectionDestroyed { issuer: sender, collection_id });
pallets/refungible/src/common.rsdiffbeforeafterboth--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -205,6 +205,15 @@
)
}
+ fn burn_item_unchecked(
+ &self,
+ owner: &T::CrossAccountId,
+ token: TokenId,
+ amount: u128,
+ ) -> sp_runtime::DispatchResult {
+ <Pallet<T>>::burn_item_unchecked(self, owner, token, amount)
+ }
+
fn transfer(
&self,
from: T::CrossAccountId,
pallets/refungible/src/lib.rsdiffbeforeafterboth--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -245,6 +245,25 @@
token: TokenId,
amount: u128,
) -> DispatchResult {
+ Self::burn_item_unchecked(collection, owner, token, amount)?;
+
+ // TODO: ERC20 transfer event
+ <PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(
+ collection.id,
+ token,
+ owner.clone(),
+ amount,
+ ));
+
+ Ok(())
+ }
+
+ pub fn burn_item_unchecked(
+ collection: &RefungibleHandle<T>,
+ owner: &T::CrossAccountId,
+ token: TokenId,
+ amount: u128,
+ ) -> DispatchResult {
let total_supply = <TotalSupply<T>>::get((collection.id, token))
.checked_sub(amount)
.ok_or(<CommonError<T>>::TokenValueTooLow)?;
@@ -299,13 +318,6 @@
<Balance<T>>::insert((collection.id, token, owner), balance);
}
<TotalSupply<T>>::insert((collection.id, token), total_supply);
- // TODO: ERC20 transfer event
- <PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(
- collection.id,
- token,
- owner.clone(),
- amount,
- ));
Ok(())
}
pallets/unique/src/lib.rsdiffbeforeafterboth--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -332,15 +332,24 @@
/// # Arguments
///
/// * collection_id: collection to destroy.
- #[weight = <SelfWeightOf<T>>::destroy_collection()]
+ #[weight =
+ <SelfWeightOf<T>>::destroy_collection()
+ + <SelfWeightOf<T>>::burn_children_in_collection(*max_children_to_burn)
+ ]
#[transactional]
- pub fn destroy_collection(origin, collection_id: CollectionId) -> DispatchResult {
+ pub fn destroy_collection(
+ origin,
+ collection_id: CollectionId,
+ max_children_to_burn: u32,
+ ) -> DispatchResult {
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
let collection = <CollectionHandle<T>>::try_get(collection_id)?;
+ let budget = budget::Value::new(max_children_to_burn);
+
// =========
- T::CollectionDispatch::destroy(sender, collection)?;
+ T::CollectionDispatch::destroy(sender, collection, &budget)?;
<NftTransferBasket<T>>::remove_prefix(collection_id, None);
<FungibleTransferBasket<T>>::remove_prefix(collection_id, None);
pallets/unique/src/weights.rsdiffbeforeafterboth--- a/pallets/unique/src/weights.rs
+++ b/pallets/unique/src/weights.rs
@@ -34,6 +34,7 @@
pub trait WeightInfo {
fn create_collection() -> Weight;
fn destroy_collection() -> Weight;
+ fn burn_children_in_collection(max: u32) -> Weight;
fn add_to_allow_list() -> Weight;
fn remove_from_allow_list() -> Weight;
fn set_public_access_mode() -> Weight;
@@ -73,6 +74,12 @@
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(5 as Weight))
}
+
+ fn burn_children_in_collection(max: u32) -> Weight {
+ // TODO
+ (50_000_000 as Weight).saturating_mul(max as Weight)
+ }
+
// Storage: Common CollectionById (r:1 w:0)
// Storage: Common Allowlist (r:0 w:1)
fn add_to_allow_list() -> Weight {
@@ -192,6 +199,12 @@
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(5 as Weight))
}
+
+ fn burn_children_in_collection(max: u32) -> Weight {
+ // TODO
+ (50_000_000 as Weight).saturating_mul(max as Weight)
+ }
+
// Storage: Common CollectionById (r:1 w:0)
// Storage: Common Allowlist (r:0 w:1)
fn add_to_allow_list() -> Weight {
runtime/common/src/dispatch.rsdiffbeforeafterboth--- a/runtime/common/src/dispatch.rs
+++ b/runtime/common/src/dispatch.rs
@@ -1,4 +1,4 @@
-use frame_support::{dispatch::DispatchResult, ensure};
+use frame_support::{dispatch::{DispatchResult}, ensure};
use pallet_evm::PrecompileResult;
use sp_core::{H160, U256};
use sp_std::{borrow::ToOwned, vec::Vec};
@@ -12,6 +12,7 @@
use pallet_refungible::{Pallet as PalletRefungible, RefungibleHandle, erc::RefungibleTokenHandle};
use up_data_structs::{
CollectionMode, CreateCollectionData, MAX_DECIMAL_POINTS, mapping::TokenAddressMapping,
+ budget::Budget,
};
pub enum CollectionDispatchT<T>
@@ -46,7 +47,11 @@
Ok(())
}
- fn destroy(sender: T::CrossAccountId, collection: CollectionHandle<T>) -> DispatchResult {
+ fn destroy(
+ sender: T::CrossAccountId,
+ collection: CollectionHandle<T>,
+ nesting_budget: &dyn Budget,
+ ) -> DispatchResult {
match collection.mode {
CollectionMode::ReFungible => {
PalletRefungible::destroy_collection(RefungibleHandle::cast(collection), &sender)?
@@ -55,7 +60,11 @@
PalletFungible::destroy_collection(FungibleHandle::cast(collection), &sender)?
}
CollectionMode::NFT => {
- PalletNonfungible::destroy_collection(NonfungibleHandle::cast(collection), &sender)?
+ PalletNonfungible::destroy_collection(
+ NonfungibleHandle::cast(collection),
+ &sender,
+ nesting_budget,
+ )?
}
}
Ok(())