Skip to content

Commit

Permalink
ref: add IErc721UriStorage trait
Browse files Browse the repository at this point in the history
Refers #336
  • Loading branch information
bidzyyys committed Oct 15, 2024
1 parent e24c024 commit 0b97efe
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion contracts/src/token/erc721/extensions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ pub mod uri_storage;
pub use burnable::IErc721Burnable;
pub use enumerable::{Erc721Enumerable, IErc721Enumerable};
pub use metadata::{Erc721Metadata, IErc721Metadata};
pub use uri_storage::Erc721UriStorage;
pub use uri_storage::{Erc721UriStorage, IErc721UriStorage};
36 changes: 21 additions & 15 deletions contracts/src/token/erc721/extensions/uri_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ sol_storage! {
}
}

/// Interface of [`Erc721`] token with storage-based token URI management.
pub trait IErc721UriStorage {
/// Returns the Uniform Resource Identifier (URI) for `token_id` token.
///
/// # Arguments
///
/// * `&self` - Read access to the contract's state.
/// * `token_id` - Id of a token.
#[must_use]
fn token_uri(&self, token_id: U256) -> String;
}

#[public]
impl IErc721UriStorage for Erc721UriStorage {
#[must_use]
fn token_uri(&self, token_id: U256) -> String {
self._token_uris.getter(token_id).get_string()
}
}

impl Erc721UriStorage {
/// Sets `token_uri` as the tokenURI of `token_id`.
///
Expand All @@ -48,25 +68,11 @@ impl Erc721UriStorage {
}
}

#[public]
impl Erc721UriStorage {
/// Returns the Uniform Resource Identifier (URI) for `token_id` token.
///
/// # Arguments
///
/// * `&self` - Read access to the contract's state.
/// * `token_id` - Id of a token.
#[must_use]
pub fn token_uri(&self, token_id: U256) -> String {
self._token_uris.getter(token_id).get_string()
}
}

#[cfg(all(test, feature = "std"))]
mod tests {
use alloy_primitives::U256;

use super::Erc721UriStorage;
use super::{Erc721UriStorage, IErc721UriStorage};

fn random_token_id() -> U256 {
let num: u32 = rand::random();
Expand Down
2 changes: 1 addition & 1 deletion examples/erc721-metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use alloy_primitives::{Address, U256};
use openzeppelin_stylus::token::erc721::{
extensions::{
Erc721Metadata as Metadata, Erc721UriStorage as UriStorage,
IErc721Burnable, IErc721Metadata,
IErc721Burnable, IErc721Metadata, IErc721UriStorage,
},
Erc721, IErc721,
};
Expand Down

0 comments on commit 0b97efe

Please sign in to comment.