Skip to content

Commit c8cf3e8

Browse files
committed
chore: fork pallet nfts (#382)
1 parent 87049f1 commit c8cf3e8

11 files changed

+620
-921
lines changed

pallets/nfts/src/benchmarking.rs

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,6 @@ fn add_collection_metadata<T: Config<I>, I: 'static>() -> (T::AccountId, Account
6262
(caller, caller_lookup)
6363
}
6464

65-
fn approve_collection<T: Config<I>, I: 'static>(
66-
index: u32,
67-
) -> (T::AccountId, AccountIdLookupOf<T>) {
68-
let caller = Collection::<T, I>::get(T::Helper::collection(0)).unwrap().owner;
69-
if caller != whitelisted_caller() {
70-
whitelist_account!(caller);
71-
}
72-
let caller_lookup = T::Lookup::unlookup(caller.clone());
73-
let delegate: T::AccountId = account("delegate", 0, SEED + index);
74-
let delegate_lookup = T::Lookup::unlookup(delegate.clone());
75-
let deadline = BlockNumberFor::<T>::max_value();
76-
assert_ok!(Nfts::<T, I>::approve_transfer(
77-
SystemOrigin::Signed(caller.clone()).into(),
78-
T::Helper::collection(0),
79-
None,
80-
delegate_lookup.clone(),
81-
Some(deadline),
82-
));
83-
(caller, caller_lookup)
84-
}
85-
8665
fn mint_item<T: Config<I>, I: 'static>(
8766
index: u16,
8867
) -> (T::ItemId, T::AccountId, AccountIdLookupOf<T>) {
@@ -96,7 +75,7 @@ fn mint_item<T: Config<I>, I: 'static>(
9675
let item_exists = Item::<T, I>::contains_key(collection, item);
9776
let item_config = ItemConfigOf::<T, I>::get(collection, item);
9877
if item_exists {
99-
return (item, caller, caller_lookup);
78+
return (item, caller, caller_lookup)
10079
} else if let Some(item_config) = item_config {
10180
assert_ok!(Nfts::<T, I>::force_mint(
10281
SystemOrigin::Signed(caller.clone()).into(),
@@ -269,8 +248,6 @@ benchmarks_instance_pallet! {
269248
let m in 0 .. 1_000;
270249
let c in 0 .. 1_000;
271250
let a in 0 .. 1_000;
272-
let h in 0 .. 1_000;
273-
let l in 0 .. 1_000;
274251

275252
let (collection, caller, _) = create_collection::<T, I>();
276253
add_collection_metadata::<T, I>();
@@ -288,13 +265,6 @@ benchmarks_instance_pallet! {
288265
for i in 0..a {
289266
add_collection_attribute::<T, I>(i as u16);
290267
}
291-
for i in 0..h {
292-
mint_item::<T, I>(i as u16);
293-
burn_item::<T, I>(i as u16);
294-
}
295-
for i in 0..l {
296-
approve_collection::<T, I>(i);
297-
}
298268
let witness = Collection::<T, I>::get(collection).unwrap().destroy_witness();
299269
}: _(SystemOrigin::Signed(caller), collection, witness)
300270
verify {
@@ -601,45 +571,27 @@ benchmarks_instance_pallet! {
601571
}
602572

603573
approve_transfer {
604-
let i in 0..1;
605-
606574
let (collection, caller, _) = create_collection::<T, I>();
607575
let (item, ..) = mint_item::<T, I>(0);
608576
let delegate: T::AccountId = account("delegate", 0, SEED);
609577
let delegate_lookup = T::Lookup::unlookup(delegate.clone());
610-
let maybe_deadline = if i == 0 {
611-
None
612-
} else {
613-
Some(BlockNumberFor::<T>::max_value())
614-
};
615-
let maybe_item = if i == 0 {
616-
None
617-
} else {
618-
Some(item)
619-
};
620-
}: _(SystemOrigin::Signed(caller.clone()), collection, maybe_item, delegate_lookup, maybe_deadline)
578+
let deadline = BlockNumberFor::<T>::max_value();
579+
}: _(SystemOrigin::Signed(caller.clone()), collection, item, delegate_lookup, Some(deadline))
621580
verify {
622-
assert_last_event::<T, I>(Event::TransferApproved { collection, item: maybe_item, owner: caller, delegate, deadline: maybe_deadline }.into());
581+
assert_last_event::<T, I>(Event::TransferApproved { collection, item, owner: caller, delegate, deadline: Some(deadline) }.into());
623582
}
624583

625584
cancel_approval {
626-
let i in 0..1;
627-
628585
let (collection, caller, _) = create_collection::<T, I>();
629586
let (item, ..) = mint_item::<T, I>(0);
630587
let delegate: T::AccountId = account("delegate", 0, SEED);
631588
let delegate_lookup = T::Lookup::unlookup(delegate.clone());
632589
let origin = SystemOrigin::Signed(caller.clone()).into();
633590
let deadline = BlockNumberFor::<T>::max_value();
634-
let maybe_item = if i == 0 {
635-
None
636-
} else {
637-
Some(item)
638-
};
639-
Nfts::<T, I>::approve_transfer(origin, collection, maybe_item, delegate_lookup.clone(), Some(deadline))?;
640-
}: _(SystemOrigin::Signed(caller.clone()), collection, maybe_item, delegate_lookup)
591+
Nfts::<T, I>::approve_transfer(origin, collection, item, delegate_lookup.clone(), Some(deadline))?;
592+
}: _(SystemOrigin::Signed(caller.clone()), collection, item, delegate_lookup)
641593
verify {
642-
assert_last_event::<T, I>(Event::ApprovalCancelled { collection, item: maybe_item, owner: caller, delegate }.into());
594+
assert_last_event::<T, I>(Event::ApprovalCancelled { collection, item, owner: caller, delegate }.into());
643595
}
644596

645597
clear_all_transfer_approvals {
@@ -649,7 +601,7 @@ benchmarks_instance_pallet! {
649601
let delegate_lookup = T::Lookup::unlookup(delegate.clone());
650602
let origin = SystemOrigin::Signed(caller.clone()).into();
651603
let deadline = BlockNumberFor::<T>::max_value();
652-
Nfts::<T, I>::approve_transfer(origin, collection, Some(item), delegate_lookup.clone(), Some(deadline))?;
604+
Nfts::<T, I>::approve_transfer(origin, collection, item, delegate_lookup.clone(), Some(deadline))?;
653605
}: _(SystemOrigin::Signed(caller.clone()), collection, item)
654606
verify {
655607
assert_last_event::<T, I>(Event::AllApprovalsCancelled {collection, item, owner: caller}.into());

pallets/nfts/src/common_functions.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,6 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
3434
Collection::<T, I>::get(collection).map(|i| i.owner)
3535
}
3636

37-
/// Get the total number of items in the collection, if the collection exists.
38-
pub fn collection_items(collection: T::CollectionId) -> Option<u32> {
39-
Collection::<T, I>::get(collection).map(|i| i.items)
40-
}
41-
42-
/// Get the allowances to spend items within the collection.
43-
pub fn collection_allowances(collection: T::CollectionId) -> Option<u32> {
44-
Collection::<T, I>::get(collection).map(|i| i.allowances)
45-
}
46-
47-
/// Get the metadata of the collection item.
48-
pub fn item_metadata(
49-
collection: T::CollectionId,
50-
item: T::ItemId,
51-
) -> Option<BoundedVec<u8, T::StringLimit>> {
52-
ItemMetadataOf::<T, I>::get(collection, item).map(|metadata| metadata.data)
53-
}
54-
5537
/// Validates the signature of the given data with the provided signer's account ID.
5638
///
5739
/// # Errors

pallets/nfts/src/features/approvals.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
6464
if let Some(check_origin) = maybe_check_origin {
6565
ensure!(check_origin == details.owner, Error::<T, I>::NoPermission);
6666
}
67+
6768
let now = frame_system::Pallet::<T>::block_number();
6869
let deadline = maybe_deadline.map(|d| d.saturating_add(now));
6970

@@ -74,11 +75,12 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
7475
Item::<T, I>::insert(collection, item, &details);
7576
Self::deposit_event(Event::TransferApproved {
7677
collection,
77-
item: Some(item),
78+
item,
7879
owner: details.owner,
7980
delegate,
8081
deadline,
8182
});
83+
8284
Ok(())
8385
}
8486

@@ -124,7 +126,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
124126

125127
Self::deposit_event(Event::ApprovalCancelled {
126128
collection,
127-
item: Some(item),
129+
item,
128130
owner: details.owner,
129131
delegate,
130132
});

pallets/nfts/src/features/create_delete_collection.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
5454
items: 0,
5555
item_metadatas: 0,
5656
item_configs: 0,
57-
item_holders: 0,
5857
attributes: 0,
59-
allowances: 0,
6058
},
6159
);
6260
CollectionRoleOf::<T, I>::insert(
@@ -121,11 +119,6 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
121119
collection_details.item_configs == witness.item_configs,
122120
Error::<T, I>::BadWitness
123121
);
124-
ensure!(
125-
collection_details.item_holders == witness.item_holders,
126-
Error::<T, I>::BadWitness
127-
);
128-
ensure!(collection_details.allowances == witness.allowances, Error::<T, I>::BadWitness);
129122

130123
for (_, metadata) in ItemMetadataOf::<T, I>::drain_prefix(collection) {
131124
if let Some(depositor) = metadata.deposit.account {
@@ -144,9 +137,6 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
144137
}
145138
}
146139

147-
let _ =
148-
AccountBalance::<T, I>::clear_prefix(collection, collection_details.items, None);
149-
let _ = Allowances::<T, I>::clear_prefix((collection,), collection_details.items, None);
150140
CollectionAccount::<T, I>::remove(&collection_details.owner, &collection);
151141
T::Currency::unreserve(&collection_details.owner, collection_details.owner_deposit);
152142
CollectionConfigOf::<T, I>::remove(collection);
@@ -157,9 +147,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
157147
Ok(DestroyWitness {
158148
item_metadatas: collection_details.item_metadatas,
159149
item_configs: collection_details.item_configs,
160-
item_holders: collection_details.item_holders,
161150
attributes: collection_details.attributes,
162-
allowances: collection_details.allowances,
163151
})
164152
})
165153
}

pallets/nfts/src/features/create_delete_item.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,6 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
259259
}
260260
}
261261

262-
if AccountBalance::<T, I>::get(collection, &details.owner) == 1 {
263-
collection_details.item_holders.saturating_dec();
264-
}
265-
266262
Ok(details.owner)
267263
},
268264
)?;
@@ -272,9 +268,6 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
272268
ItemPriceOf::<T, I>::remove(&collection, &item);
273269
PendingSwapOf::<T, I>::remove(&collection, &item);
274270
ItemAttributesApprovalsOf::<T, I>::remove(&collection, &item);
275-
AccountBalance::<T, I>::mutate(collection, &owner, |balance| {
276-
balance.saturating_dec();
277-
});
278271

279272
if remove_config {
280273
ItemConfigOf::<T, I>::remove(collection, item);

pallets/nfts/src/features/transfer.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
5454
) -> DispatchResult,
5555
) -> DispatchResult {
5656
// Retrieve collection details.
57-
let mut collection_details =
57+
let collection_details =
5858
Collection::<T, I>::get(&collection).ok_or(Error::<T, I>::UnknownCollection)?;
5959

6060
// Ensure the item is not locked.
@@ -86,24 +86,6 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
8686
// Perform the transfer with custom details using the provided closure.
8787
with_details(&collection_details, &mut details)?;
8888

89-
// Update account balance of the owner.
90-
let owner_balance =
91-
AccountBalance::<T, I>::mutate(collection, &details.owner, |balance| -> u32 {
92-
balance.saturating_dec();
93-
*balance
94-
});
95-
if owner_balance == 0 {
96-
collection_details.item_holders.saturating_dec();
97-
}
98-
// Update account balance of the destination account.
99-
let dest_balance = AccountBalance::<T, I>::mutate(collection, &dest, |balance| -> u32 {
100-
balance.saturating_inc();
101-
*balance
102-
});
103-
if dest_balance == 1 {
104-
collection_details.item_holders.saturating_inc();
105-
}
106-
10789
// Update account ownership information.
10890
Account::<T, I>::remove((&details.owner, &collection, &item));
10991
Account::<T, I>::insert((&dest, &collection, &item), ());

0 commit comments

Comments
 (0)