Royalities of a NFT collection #117
-
Please How do I get the royalities fee of an NFT collection? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi, royalty can exist at both collection level and token level. We recommend you to always read it from the token level. Here's how you can get it with a view function. #[view]
public fun get_token_royalty(token_addr: address): (Option<u64>, Option<u64>, Option<address>) {
let token_obj = object::address_to_object<token::Token>(token_addr);
// royalty is optional, so you need to check if it exists before extract the value of it
let maybe_royalty = royalty::get(token_obj);
if (option::is_none(&maybe_royalty)) {
(option::none(), option::none(), option::none())
} else {
let royalty = option::extract(&mut maybe_royalty);
let numerator = royalty::numerator(&royalty);
let denominator = royalty::denominator(&royalty);
let payee_address = royalty::payee_address(&royalty);
(option::some(numerator), option::some(denominator), option::some(payee_address))
}
} Source code of royalty module if you are interested. Source code for the view function code snippet. I also included a unit test calling it. |
Beta Was this translation helpful? Give feedback.
-
How to get token royaltiesThere's an existing view function for this on the token module. You can call this on a token directly. Source: #[view]
public fun royalty<T: key>(token: Object<T>): Option<Royalty> acquires Token {
borrow(&token);
let royalty = royalty::get(token);
if (option::is_some(&royalty)) {
royalty
} else {
let creator = creator(token);
let collection_name = collection_name(token);
let collection_address = collection::create_collection_address(&creator, &collection_name);
let collection = object::address_to_object<collection::Collection>(collection_address);
royalty::get(collection)
}
} How are royalties handled?Royalties have two levels. There's a token royalty, and a collection royalty. The token one takes precedence over the collection one. |
Beta Was this translation helpful? Give feedback.
How to get token royalties
There's an existing view function for this on the token module. You can call this on a token directly.
https://github.com/aptos-labs/aptos-core/blob/84433ec8ffc448f1a99478720a697c645ee08411/aptos-move/framework/aptos-token-objects/sources/token.move#L464-L477
Source: