Skip to content

Commit 1150f0d

Browse files
authored
Merge pull request #129 from rmrk-team/bug/121-add-royalty-struct
implement royalty struct
2 parents e42d798 + 989f277 commit 1150f0d

File tree

5 files changed

+83
-13
lines changed

5 files changed

+83
-13
lines changed

pallets/rmrk-core/src/functions.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ where
270270
_sender: T::AccountId,
271271
owner: T::AccountId,
272272
collection_id: CollectionId,
273-
recipient: Option<T::AccountId>,
274-
royalty: Option<Permill>,
273+
royalty_recipient: Option<T::AccountId>,
274+
royalty_amount: Option<Permill>,
275275
metadata: StringLimitOf<T>,
276276
) -> sp_std::result::Result<(CollectionId, NftId), DispatchError> {
277277
let nft_id = Self::get_next_nft_id(collection_id)?;
@@ -282,14 +282,23 @@ where
282282
ensure!(nft_id < max, Error::<T>::CollectionFullOrLocked);
283283
}
284284

285-
let recipient = recipient.unwrap_or_else(|| owner.clone());
286-
let royalty = royalty.unwrap_or_default();
285+
let mut royalty: Option<RoyaltyInfo::<T::AccountId>> = None;
286+
287+
if let Some(amount) = royalty_amount {
288+
match royalty_recipient {
289+
Some(recipient) => {
290+
royalty = Some(RoyaltyInfo::<T::AccountId> { recipient, amount });
291+
},
292+
None => {
293+
royalty = Some(RoyaltyInfo::<T::AccountId> { recipient: owner.clone(), amount });
294+
}
295+
}
296+
};
287297

288298
let owner_as_maybe_account = AccountIdOrCollectionNftTuple::AccountId(owner.clone());
289299

290300
let nft = NftInfo {
291301
owner: owner_as_maybe_account,
292-
recipient,
293302
royalty,
294303
metadata,
295304
equipped: false,

pallets/rmrk-core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use sp_std::convert::TryInto;
1212

1313
use rmrk_traits::{
1414
primitives::*, AccountIdOrCollectionNftTuple, Collection, CollectionInfo, Nft, NftInfo,
15-
Priority, Property, Resource, ResourceInfo,
15+
Priority, Property, Resource, ResourceInfo, RoyaltyInfo
1616
};
1717
use sp_std::result::Result;
1818

pallets/rmrk-core/src/tests.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,59 @@ fn mint_collection_max_logic_works() {
274274
});
275275
}
276276

277+
/// NFT: Royalty defaults to self when amount provided but no recipient
278+
#[test]
279+
fn royalty_recipient_default_works() {
280+
ExtBuilder::default().build().execute_with(|| {
281+
// Create a basic collection
282+
assert_ok!(basic_collection());
283+
// Mint an NFT
284+
assert_ok!(RMRKCore::mint_nft(
285+
Origin::signed(ALICE),
286+
ALICE,
287+
COLLECTION_ID_0,
288+
None, // No royalty recipient
289+
Some(Permill::from_float(20.525)),
290+
bvec![0u8; 20]
291+
));
292+
// Royalty recipient should default to issuer (ALICE)
293+
assert_eq!(RmrkCore::nfts(0, 0).unwrap().royalty.unwrap().recipient, ALICE);
294+
// Mint another NFT
295+
assert_ok!(RMRKCore::mint_nft(
296+
Origin::signed(ALICE),
297+
ALICE,
298+
COLLECTION_ID_0,
299+
Some(BOB), // Royalty recipient is BOB
300+
Some(Permill::from_float(20.525)),
301+
bvec![0u8; 20]
302+
));
303+
// Royalty recipient should be BOB
304+
assert_eq!(RmrkCore::nfts(0, 1).unwrap().royalty.unwrap().recipient, BOB);
305+
// Mint another NFT
306+
assert_ok!(RMRKCore::mint_nft(
307+
Origin::signed(ALICE),
308+
ALICE,
309+
COLLECTION_ID_0,
310+
None, // No royalty recipient is BOB
311+
None, // No royalty amount
312+
bvec![0u8; 20]
313+
));
314+
// Royalty should not exist
315+
assert!(RmrkCore::nfts(0, 2).unwrap().royalty.is_none());
316+
// Mint another NFT
317+
assert_ok!(RMRKCore::mint_nft(
318+
Origin::signed(ALICE),
319+
ALICE,
320+
COLLECTION_ID_0,
321+
Some(ALICE), // Royalty recipient is ALICE
322+
None, // No royalty amount
323+
bvec![0u8; 20]
324+
));
325+
// Royalty should not exist
326+
assert!(RmrkCore::nfts(0, 3).unwrap().royalty.is_none());
327+
});
328+
}
329+
277330
/// NFT: Send tests (RMRK2.0 spec: SEND)
278331
#[test]
279332
fn send_nft_to_minted_nft_works() {

traits/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub use part::{EquippableList, FixedPart, PartType, SlotPart};
1414
pub use theme::{Theme, ThemeProperty};
1515
// pub use part::{PartInfo};
1616
pub use collection::{Collection, CollectionInfo};
17-
pub use nft::{AccountIdOrCollectionNftTuple, Nft, NftInfo};
17+
pub use nft::{AccountIdOrCollectionNftTuple, Nft, NftInfo, RoyaltyInfo};
1818
pub use priority::Priority;
1919
pub use property::Property;
2020
pub use resource::{Resource, ResourceInfo};

traits/src/nft.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,24 @@ pub enum AccountIdOrCollectionNftTuple<AccountId> {
1919
CollectionAndNftTuple(CollectionId, NftId),
2020
}
2121

22+
/// Royalty information (recipient and amount)
23+
#[cfg_attr(feature = "std", derive(PartialEq, Eq))]
24+
#[derive(Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)]
25+
pub struct RoyaltyInfo<AccountId> {
26+
/// Recipient (AccountId) of the royalty
27+
pub recipient: AccountId,
28+
/// Amount (Permill) of the royalty
29+
pub amount: Permill,
30+
}
31+
2232
/// Nft info.
2333
#[cfg_attr(feature = "std", derive(PartialEq, Eq))]
2434
#[derive(Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)]
2535
pub struct NftInfo<AccountId, BoundedString> {
2636
/// The owner of the NFT, can be either an Account or a tuple (CollectionId, NftId)
2737
pub owner: AccountIdOrCollectionNftTuple<AccountId>,
28-
/// The user account which receives the royalty
29-
pub recipient: AccountId,
30-
/// Royalty in per mille (1/1000)
31-
pub royalty: Permill,
38+
/// Royalty (optional)
39+
pub royalty: Option<RoyaltyInfo<AccountId>>,
3240
/// Arbitrary data about an instance, e.g. IPFS hash
3341
pub metadata: BoundedString,
3442
/// Equipped state
@@ -46,8 +54,8 @@ pub trait Nft<AccountId, BoundedString> {
4654
sender: AccountId,
4755
owner: AccountId,
4856
collection_id: CollectionId,
49-
recipient: Option<AccountId>,
50-
royalty: Option<Permill>,
57+
royalty_recipient: Option<AccountId>,
58+
royalty_amount: Option<Permill>,
5159
metadata: BoundedString,
5260
) -> Result<(CollectionId, NftId), DispatchError>;
5361
fn nft_burn(

0 commit comments

Comments
 (0)