Skip to content

Commit 60c895e

Browse files
authored
u128 to i128, and doc improvements (#329)
* u128 to i128 * doc improvements * remove instance ttl extension
1 parent 67abb33 commit 60c895e

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

examples/nft-royalties/src/contract.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl ExampleContract {
4949
token_id
5050
}
5151

52-
pub fn get_royalty_info(e: &Env, token_id: u32, sale_price: u128) -> (Address, u128) {
52+
pub fn get_royalty_info(e: &Env, token_id: u32, sale_price: i128) -> (Address, i128) {
5353
Base::royalty_info(e, token_id, sale_price)
5454
}
5555
}
@@ -83,7 +83,7 @@ impl NonFungibleRoyalties for ExampleContract {
8383
Base::remove_token_royalty(e, token_id);
8484
}
8585

86-
fn royalty_info(e: &Env, token_id: u32, sale_price: u128) -> (Address, u128) {
86+
fn royalty_info(e: &Env, token_id: u32, sale_price: i128) -> (Address, i128) {
8787
Base::royalty_info(e, token_id, sale_price)
8888
}
8989
}

packages/tokens/non-fungible/src/extensions/royalties/mod.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,25 @@ use soroban_sdk::{Address, Env, Symbol};
1212
/// trait is designed to be used in conjunction with the `NonFungibleToken`
1313
/// trait.
1414
///
15-
/// This implementation follows the ERC2981 standard for royalties, allowing:
16-
/// - Setting global royalties for the entire collection
17-
/// - Setting per-token royalties that override the global setting
15+
/// This implementation is inspired by the ERC2981 standard for royalties, and
16+
/// additionally, it allows:
17+
/// - Get the royalty info for a token
18+
/// - Set the global default royalty for the entire collection
19+
/// - Set per-token royalties that override the global setting
20+
/// - Remove per-token royalties to fall-back to the global royalty set for the
21+
/// contract
1822
///
1923
/// `storage.rs` file of this module provides the `NonFungibleRoyalties` trait
2024
/// implementation.
2125
///
2226
/// # Notes
2327
///
28+
/// In most marketplaces, royalty calculations are done in amounts of fungible
29+
/// tokens. For example, if an NFT is sold for 10000 USDC and royalty is 10%,
30+
/// 1000 USDC goes to the creator. To preserve the compatibility across
31+
/// Non-Fungible and Fungible tokens, we are using `i128` instead of `u128` for
32+
/// the `sale_price`, due to SEP-41.
33+
///
2434
/// `#[contractimpl]` macro requires even the default implementations to be
2535
/// present under its scope. To avoid confusion, we do not provide the default
2636
/// implementations here, but we are providing a macro that generates them.
@@ -109,7 +119,7 @@ pub trait NonFungibleRoyalties: NonFungibleToken {
109119
/// * data - `[]`
110120
fn remove_token_royalty(e: &Env, token_id: u32, operator: Address);
111121

112-
/// Returns `(Address, u128)` - A tuple containing the receiver address and
122+
/// Returns `(Address, i128)` - A tuple containing the receiver address and
113123
/// the royalty amount.
114124
///
115125
/// # Arguments
@@ -123,7 +133,7 @@ pub trait NonFungibleRoyalties: NonFungibleToken {
123133
///
124134
/// * [`crate::NonFungibleTokenError::NonExistentToken`] - If the token does
125135
/// not exist.
126-
fn royalty_info(e: &Env, token_id: u32, sale_price: u128) -> (Address, u128);
136+
fn royalty_info(e: &Env, token_id: u32, sale_price: i128) -> (Address, i128);
127137
}
128138

129139
// ################## EVENTS ##################

packages/tokens/non-fungible/src/extensions/royalties/storage.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl Base {
154154
/// * [`NonFungibleTokenError::NonExistentToken`] - If the token does not
155155
/// exist.
156156
/// * refer to [`Base::owner_of`] errors.
157-
pub fn royalty_info(e: &Env, token_id: u32, sale_price: u128) -> (Address, u128) {
157+
pub fn royalty_info(e: &Env, token_id: u32, sale_price: i128) -> (Address, i128) {
158158
// Verify token exists by checking owner
159159
let _ = Base::owner_of(e, token_id);
160160

@@ -166,15 +166,14 @@ impl Base {
166166
OWNER_TTL_THRESHOLD,
167167
OWNER_EXTEND_AMOUNT,
168168
);
169-
let royalty_amount = sale_price * royalty_info.basis_points as u128 / 10000;
169+
let royalty_amount = sale_price * royalty_info.basis_points as i128 / 10000;
170170
return (royalty_info.receiver, royalty_amount);
171171
}
172172

173173
// Fall back to default royalty if no token-specific royalty is set
174174
let default_key = NFTRoyaltiesStorageKey::DefaultRoyalty;
175175
if let Some(royalty_info) = e.storage().instance().get::<_, RoyaltyInfo>(&default_key) {
176-
e.storage().instance().extend_ttl(OWNER_TTL_THRESHOLD, OWNER_EXTEND_AMOUNT);
177-
let royalty_amount = sale_price * royalty_info.basis_points as u128 / 10000;
176+
let royalty_amount = sale_price * royalty_info.basis_points as i128 / 10000;
178177
return (royalty_info.receiver, royalty_amount);
179178
}
180179

0 commit comments

Comments
 (0)