Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement max_burns for burn_nft #174

Merged
merged 1 commit into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pallets/rmrk-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ pub mod pallet {
transferable: bool,
) -> DispatchResult {
let sender = ensure_signed(origin.clone())?;
if let Some(collection_issuer) = pallet_uniques::Pallet::<T>::collection_owner(collection_id)
if let Some(collection_issuer) =
pallet_uniques::Pallet::<T>::collection_owner(collection_id)
{
ensure!(collection_issuer == sender, Error::<T>::NoPermission);
} else {
Expand Down Expand Up @@ -411,13 +412,13 @@ pub mod pallet {
origin: OriginFor<T>,
collection_id: CollectionId,
nft_id: NftId,
max_burns: u32,
) -> DispatchResult {
let sender = ensure_signed(origin.clone())?;
let (root_owner, _) = Pallet::<T>::lookup_root_owner(collection_id, nft_id)?;
// Check ownership
ensure!(sender == root_owner, Error::<T>::NoPermission);
let max_recursions = T::MaxRecursions::get();
let (_collection_id, nft_id) = Self::nft_burn(collection_id, nft_id, max_recursions)?;
let (_collection_id, nft_id) = Self::nft_burn(collection_id, nft_id, max_burns)?;

pallet_uniques::Pallet::<T>::do_burn(collection_id, nft_id, |_, _| Ok(()))?;

Expand Down
1 change: 1 addition & 0 deletions pallets/rmrk-core/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ pub const NOT_EXISTING_CLASS_ID: <Test as pallet_uniques::Config>::CollectionId
pub const RESOURCE_ZERO: ResourceId = 0;
pub const RESOURCE_ONE: ResourceId = 1;
pub const RESOURCE_TWO: ResourceId = 2;
pub const MAX_BURNS: u32 = 4;

pub struct ExtBuilder;
impl Default for ExtBuilder {
Expand Down
18 changes: 9 additions & 9 deletions pallets/rmrk-core/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fn lock_collection_works() {
// Attempt to mint in a locked collection should fail
assert_noop!(basic_mint(), Error::<Test>::CollectionFullOrLocked);
// Burn an NFT
assert_ok!(RMRKCore::burn_nft(Origin::signed(ALICE), COLLECTION_ID_0, NFT_ID_0));
assert_ok!(RMRKCore::burn_nft(Origin::signed(ALICE), COLLECTION_ID_0, NFT_ID_0, MAX_BURNS));
// Should now have only three NFTS in collection
assert_eq!(RMRKCore::collections(COLLECTION_ID_0).unwrap().nfts_count, 3);
// Still we should be unable to mint another NFT
Expand All @@ -172,7 +172,7 @@ fn destroy_collection_works() {
Error::<Test>::CollectionNotEmpty
);
// Burn the single NFT in collection
assert_ok!(RMRKCore::burn_nft(Origin::signed(ALICE), COLLECTION_ID_0, NFT_ID_0));
assert_ok!(RMRKCore::burn_nft(Origin::signed(ALICE), COLLECTION_ID_0, NFT_ID_0, MAX_BURNS));
// Empty collection can be destroyed
assert_ok!(RMRKCore::destroy_collection(Origin::signed(ALICE), COLLECTION_ID_0));
// Destroy event is triggered by successful destroy_collection
Expand Down Expand Up @@ -283,7 +283,7 @@ fn mint_collection_max_logic_works() {
// Minting beyond collection max (5) should fail
assert_noop!(basic_mint(), Error::<Test>::CollectionFullOrLocked);
// Burn an NFT
assert_ok!(RMRKCore::burn_nft(Origin::signed(ALICE), COLLECTION_ID_0, 0));
assert_ok!(RMRKCore::burn_nft(Origin::signed(ALICE), COLLECTION_ID_0, 0, MAX_BURNS));
// Minting should still fail, as burning should not affect "fullness" of collection
assert_noop!(basic_mint(), Error::<Test>::CollectionFullOrLocked);
});
Expand Down Expand Up @@ -732,11 +732,11 @@ fn burn_nft_works() {

// BOB should not be able to burn ALICE's NFT
assert_noop!(
RMRKCore::burn_nft(Origin::signed(BOB), COLLECTION_ID_0, NFT_ID_0),
RMRKCore::burn_nft(Origin::signed(BOB), COLLECTION_ID_0, NFT_ID_0, MAX_BURNS),
Error::<Test>::NoPermission
);
// ALICE burns her NFT
assert_ok!(RMRKCore::burn_nft(Origin::signed(ALICE), COLLECTION_ID_0, NFT_ID_0));
assert_ok!(RMRKCore::burn_nft(Origin::signed(ALICE), COLLECTION_ID_0, NFT_ID_0, MAX_BURNS));
// Successful burn creates NFTBurned event
System::assert_last_event(MockEvent::RmrkCore(crate::Event::NFTBurned {
owner: ALICE,
Expand All @@ -746,7 +746,7 @@ fn burn_nft_works() {
assert_eq!(RMRKCore::collections(COLLECTION_ID_0).unwrap().nfts_count, 0);
// ALICE can't burn an NFT twice
assert_noop!(
RMRKCore::burn_nft(Origin::signed(ALICE), COLLECTION_ID_0, NFT_ID_0),
RMRKCore::burn_nft(Origin::signed(ALICE), COLLECTION_ID_0, NFT_ID_0, MAX_BURNS),
Error::<Test>::NoAvailableNftId
);
// Burned NFT no longer exists
Expand Down Expand Up @@ -812,7 +812,7 @@ fn burn_nft_with_great_grandchildren_works() {
// Great-grandchild NFT (0, 3) exists
assert_eq!(RMRKCore::nfts(COLLECTION_ID_0, 3).is_some(), true);
// Burn great-grandparent NFT (0, 0)
assert_ok!(RMRKCore::burn_nft(Origin::signed(ALICE), COLLECTION_ID_0, NFT_ID_0));
assert_ok!(RMRKCore::burn_nft(Origin::signed(ALICE), COLLECTION_ID_0, NFT_ID_0, MAX_BURNS));
// Great-grandchild NFT (0, 3) is dead :'-(
assert!(RMRKCore::nfts(COLLECTION_ID_0, 3).is_none());
// Great-grandchild resources are gone
Expand Down Expand Up @@ -866,7 +866,7 @@ fn burn_nft_beyond_max_recursions_fails_gracefully() {
assert_eq!(RMRKCore::nfts(COLLECTION_ID_0, 4).is_some(), true);
// Burn great-grandparent NFT (0, 0)
assert_noop!(
RMRKCore::burn_nft(Origin::signed(ALICE), COLLECTION_ID_0, NFT_ID_0),
RMRKCore::burn_nft(Origin::signed(ALICE), COLLECTION_ID_0, NFT_ID_0, MAX_BURNS),
Error::<Test>::TooManyRecursions
);
// All NFTs still exist
Expand Down Expand Up @@ -901,7 +901,7 @@ fn burn_child_nft_removes_parents_children() {
// NFT (0, 0) should have 1 Children storage member
assert_eq!(Children::<Test>::iter_prefix((0, 0)).count(), 1);
// Burn NFT (0, 1)
assert_ok!(RMRKCore::burn_nft(Origin::signed(ALICE), 0, 1),);
assert_ok!(RMRKCore::burn_nft(Origin::signed(ALICE), 0, 1, MAX_BURNS),);
// NFT (0, 0) should have 0 Children storage members
assert_eq!(Children::<Test>::iter_prefix((0, 0)).count(), 0);
});
Expand Down
2 changes: 1 addition & 1 deletion traits/src/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub trait Nft<AccountId, BoundedString> {
fn nft_burn(
collection_id: CollectionId,
nft_id: NftId,
max_recursions: u32,
max_burns: u32,
) -> Result<(CollectionId, NftId), DispatchError>;
fn nft_send(
sender: AccountId,
Expand Down