Skip to content

Commit 5b2fc91

Browse files
jsidorenkonathanwhit
authored andcommitted
[NFTs] Add minting price to the pre-signed mint object (paritytech#14242)
* Add minting price to the pre-signed mint object * Box the param
1 parent 78cf0d8 commit 5b2fc91

File tree

5 files changed

+45
-19
lines changed

5 files changed

+45
-19
lines changed

frame/nfts/src/benchmarking.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -828,14 +828,15 @@ benchmarks_instance_pallet! {
828828
metadata: metadata.clone(),
829829
only_account: None,
830830
deadline: One::one(),
831+
mint_price: Some(DepositBalanceOf::<T, I>::min_value()),
831832
};
832833
let message = Encode::encode(&mint_data);
833834
let signature = MultiSignature::Sr25519(sr25519_sign(0.into(), &caller_public, &message).unwrap());
834835

835836
let target: T::AccountId = account("target", 0, SEED);
836837
T::Currency::make_free_balance_be(&target, DepositBalanceOf::<T, I>::max_value());
837838
frame_system::Pallet::<T>::set_block_number(One::one());
838-
}: _(SystemOrigin::Signed(target.clone()), mint_data, signature.into(), caller)
839+
}: _(SystemOrigin::Signed(target.clone()), Box::new(mint_data), signature.into(), caller)
839840
verify {
840841
let metadata: BoundedVec<_, _> = metadata.try_into().unwrap();
841842
assert_last_event::<T, I>(Event::ItemMetadataSet { collection, item, data: metadata }.into());

frame/nfts/src/features/create_delete_item.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// limitations under the License.
1717

1818
use crate::*;
19-
use frame_support::pallet_prelude::*;
19+
use frame_support::{pallet_prelude::*, traits::ExistenceRequirement};
2020

2121
impl<T: Config<I>, I: 'static> Pallet<T, I> {
2222
pub fn do_mint(
@@ -91,8 +91,15 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
9191
mint_data: PreSignedMintOf<T, I>,
9292
signer: T::AccountId,
9393
) -> DispatchResult {
94-
let PreSignedMint { collection, item, attributes, metadata, deadline, only_account } =
95-
mint_data;
94+
let PreSignedMint {
95+
collection,
96+
item,
97+
attributes,
98+
metadata,
99+
deadline,
100+
only_account,
101+
mint_price,
102+
} = mint_data;
96103
let metadata = Self::construct_metadata(metadata)?;
97104

98105
ensure!(
@@ -118,7 +125,17 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
118125
Some(mint_to.clone()),
119126
mint_to.clone(),
120127
item_config,
121-
|_, _| Ok(()),
128+
|collection_details, _| {
129+
if let Some(price) = mint_price {
130+
T::Currency::transfer(
131+
&mint_to,
132+
&collection_details.owner,
133+
price,
134+
ExistenceRequirement::KeepAlive,
135+
)?;
136+
}
137+
Ok(())
138+
},
122139
)?;
123140
let admin_account = Self::find_account_by_role(&collection, CollectionRole::Admin);
124141
if let Some(admin_account) = admin_account {

frame/nfts/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1835,13 +1835,13 @@ pub mod pallet {
18351835
#[pallet::weight(T::WeightInfo::mint_pre_signed(mint_data.attributes.len() as u32))]
18361836
pub fn mint_pre_signed(
18371837
origin: OriginFor<T>,
1838-
mint_data: PreSignedMintOf<T, I>,
1838+
mint_data: Box<PreSignedMintOf<T, I>>,
18391839
signature: T::OffchainSignature,
18401840
signer: T::AccountId,
18411841
) -> DispatchResult {
18421842
let origin = ensure_signed(origin)?;
18431843
Self::validate_signature(&Encode::encode(&mint_data), &signature, &signer)?;
1844-
Self::do_mint_pre_signed(origin, mint_data, signer)
1844+
Self::do_mint_pre_signed(origin, *mint_data, signer)
18451845
}
18461846

18471847
/// Set attributes for an item by providing the pre-signed approval.

frame/nfts/src/tests.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -3146,13 +3146,14 @@ fn validate_signature() {
31463146
let user_1_pair = sp_core::sr25519::Pair::from_string("//Alice", None).unwrap();
31473147
let user_1_signer = MultiSigner::Sr25519(user_1_pair.public());
31483148
let user_1 = user_1_signer.clone().into_account();
3149-
let mint_data: PreSignedMint<u32, u32, AccountId, u32> = PreSignedMint {
3149+
let mint_data: PreSignedMint<u32, u32, AccountId, u32, u64> = PreSignedMint {
31503150
collection: 0,
31513151
item: 0,
31523152
attributes: vec![],
31533153
metadata: vec![],
31543154
only_account: None,
31553155
deadline: 100000,
3156+
mint_price: None,
31563157
};
31573158
let encoded_data = Encode::encode(&mint_data);
31583159
let signature = MultiSignature::Sr25519(user_1_pair.sign(&encoded_data));
@@ -3182,6 +3183,7 @@ fn pre_signed_mints_should_work() {
31823183
metadata: vec![0, 1],
31833184
only_account: None,
31843185
deadline: 10000000,
3186+
mint_price: Some(10),
31853187
};
31863188
let message = Encode::encode(&mint_data);
31873189
let signature = MultiSignature::Sr25519(user_1_pair.sign(&message));
@@ -3198,7 +3200,7 @@ fn pre_signed_mints_should_work() {
31983200

31993201
assert_ok!(Nfts::mint_pre_signed(
32003202
RuntimeOrigin::signed(user_2.clone()),
3201-
mint_data.clone(),
3203+
Box::new(mint_data.clone()),
32023204
signature.clone(),
32033205
user_1.clone(),
32043206
));
@@ -3228,21 +3230,21 @@ fn pre_signed_mints_should_work() {
32283230
assert_eq!(deposit.account, Some(user_2.clone()));
32293231
assert_eq!(deposit.amount, 3);
32303232

3231-
assert_eq!(Balances::free_balance(&user_0), 100 - 2); // 2 - collection deposit
3232-
assert_eq!(Balances::free_balance(&user_2), 100 - 1 - 3 - 6); // 1 - item deposit, 3 - metadata, 6 - attributes
3233+
assert_eq!(Balances::free_balance(&user_0), 100 - 2 + 10); // 2 - collection deposit, 10 - mint price
3234+
assert_eq!(Balances::free_balance(&user_2), 100 - 1 - 3 - 6 - 10); // 1 - item deposit, 3 - metadata, 6 - attributes, 10 - mint price
32333235

32343236
assert_noop!(
32353237
Nfts::mint_pre_signed(
32363238
RuntimeOrigin::signed(user_2.clone()),
3237-
mint_data,
3239+
Box::new(mint_data),
32383240
signature.clone(),
32393241
user_1.clone(),
32403242
),
32413243
Error::<Test>::AlreadyExists
32423244
);
32433245

32443246
assert_ok!(Nfts::burn(RuntimeOrigin::signed(user_2.clone()), 0, 0));
3245-
assert_eq!(Balances::free_balance(&user_2), 100 - 6);
3247+
assert_eq!(Balances::free_balance(&user_2), 100 - 6 - 10);
32463248

32473249
// validate the `only_account` field
32483250
let mint_data = PreSignedMint {
@@ -3252,13 +3254,14 @@ fn pre_signed_mints_should_work() {
32523254
metadata: vec![],
32533255
only_account: Some(account(2)),
32543256
deadline: 10000000,
3257+
mint_price: None,
32553258
};
32563259

32573260
// can't mint with the wrong signature
32583261
assert_noop!(
32593262
Nfts::mint_pre_signed(
32603263
RuntimeOrigin::signed(user_2.clone()),
3261-
mint_data.clone(),
3264+
Box::new(mint_data.clone()),
32623265
signature.clone(),
32633266
user_1.clone(),
32643267
),
@@ -3271,7 +3274,7 @@ fn pre_signed_mints_should_work() {
32713274
assert_noop!(
32723275
Nfts::mint_pre_signed(
32733276
RuntimeOrigin::signed(user_3),
3274-
mint_data.clone(),
3277+
Box::new(mint_data.clone()),
32753278
signature.clone(),
32763279
user_1.clone(),
32773280
),
@@ -3283,7 +3286,7 @@ fn pre_signed_mints_should_work() {
32833286
assert_noop!(
32843287
Nfts::mint_pre_signed(
32853288
RuntimeOrigin::signed(user_2.clone()),
3286-
mint_data,
3289+
Box::new(mint_data),
32873290
signature,
32883291
user_1.clone(),
32893292
),
@@ -3299,14 +3302,15 @@ fn pre_signed_mints_should_work() {
32993302
metadata: vec![],
33003303
only_account: Some(account(2)),
33013304
deadline: 10000000,
3305+
mint_price: None,
33023306
};
33033307
let message = Encode::encode(&mint_data);
33043308
let signature = MultiSignature::Sr25519(user_1_pair.sign(&message));
33053309

33063310
assert_noop!(
33073311
Nfts::mint_pre_signed(
33083312
RuntimeOrigin::signed(user_2.clone()),
3309-
mint_data,
3313+
Box::new(mint_data),
33103314
signature,
33113315
user_1.clone(),
33123316
),
@@ -3321,13 +3325,14 @@ fn pre_signed_mints_should_work() {
33213325
metadata: vec![0, 1],
33223326
only_account: None,
33233327
deadline: 10000000,
3328+
mint_price: None,
33243329
};
33253330
let message = Encode::encode(&mint_data);
33263331
let signature = MultiSignature::Sr25519(user_1_pair.sign(&message));
33273332
assert_noop!(
33283333
Nfts::mint_pre_signed(
33293334
RuntimeOrigin::signed(user_2),
3330-
mint_data,
3335+
Box::new(mint_data),
33313336
signature,
33323337
user_1.clone(),
33333338
),

frame/nfts/src/types.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub(super) type PreSignedMintOf<T, I = ()> = PreSignedMint<
6666
<T as Config<I>>::ItemId,
6767
<T as SystemConfig>::AccountId,
6868
<T as SystemConfig>::BlockNumber,
69+
BalanceOf<T, I>,
6970
>;
7071
pub(super) type PreSignedAttributesOf<T, I = ()> = PreSignedAttributes<
7172
<T as Config<I>>::CollectionId,
@@ -506,7 +507,7 @@ impl CollectionRoles {
506507
impl_codec_bitflags!(CollectionRoles, u8, CollectionRole);
507508

508509
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
509-
pub struct PreSignedMint<CollectionId, ItemId, AccountId, Deadline> {
510+
pub struct PreSignedMint<CollectionId, ItemId, AccountId, Deadline, Balance> {
510511
/// A collection of the item to be minted.
511512
pub(super) collection: CollectionId,
512513
/// Item's ID.
@@ -519,6 +520,8 @@ pub struct PreSignedMint<CollectionId, ItemId, AccountId, Deadline> {
519520
pub(super) only_account: Option<AccountId>,
520521
/// A deadline for the signature.
521522
pub(super) deadline: Deadline,
523+
/// An optional price the claimer would need to pay for the mint.
524+
pub(super) mint_price: Option<Balance>,
522525
}
523526

524527
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]

0 commit comments

Comments
 (0)