-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathuri_storage.rs
184 lines (159 loc) · 5.25 KB
/
uri_storage.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! ERC-721 token with storage-based token URI management.
//!
//! It also implements IERC4096, which is an ERC-721 Metadata Update Extension.
use alloc::string::String;
use alloy_primitives::U256;
use alloy_sol_types::sol;
use stylus_sdk::{evm, stylus_proc::sol_storage};
use crate::token::erc721::{extensions::Erc721Metadata, Error, IErc721};
sol! {
/// This event gets emitted when the metadata of a token is changed.
///
/// The event comes from IERC4096.
#[allow(missing_docs)]
event MetadataUpdate(uint256 token_id);
/// This event gets emitted when the metadata of a range of tokens
/// is changed.
///
/// The event comes from IERC4096.
#[allow(missing_docs)]
event BatchMetadataUpdate(uint256 from_token_id, uint256 to_token_id);
}
sol_storage! {
/// Uri Storage.
pub struct Erc721UriStorage {
/// Optional mapping for token URIs.
mapping(uint256 => string) _token_uris;
}
}
impl Erc721UriStorage {
/// Sets `token_uri` as the tokenURI of `token_id`.
///
/// # Arguments
///
/// * `&mut self` - Write access to the contract's state.
/// * `token_id` - Id of a token.
/// * `token_uri` - URI for the token.
///
/// # Events
/// Emits a [`MetadataUpdate`] event.
pub fn _set_token_uri(&mut self, token_id: U256, token_uri: String) {
self._token_uris.setter(token_id).set_str(token_uri);
evm::log(MetadataUpdate { token_id });
}
/// 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.
/// * `erc721` - Read access to a contract providing [`IErc721`] interface.
/// * `metadata` - Read access to a [`Erc721Metadata`] contract.
///
/// # Errors
///
/// If the token does not exist, then the error
/// [`Error::NonexistentToken`] is returned.
///
/// NOTE: In order to have [`Erc721UriStorage::token_uri`] exposed in ABI,
/// you need to do this manually.
///
/// # Examples
///
/// ```rust,ignore
/// #[selector(name = "tokenURI")]
/// pub fn token_uri(&self, token_id: U256) -> Result<String, Vec<u8>> {
/// Ok(self.uri_storage.token_uri(
/// token_id,
/// &self.erc721,
/// &self.metadata,
/// )?)
/// }
pub fn token_uri(
&self,
token_id: U256,
erc721: &impl IErc721<Error = Error>,
metadata: &Erc721Metadata,
) -> Result<String, Error> {
let _owner = erc721.owner_of(token_id)?;
let token_uri = self._token_uris.getter(token_id).get_string();
let base = metadata.base_uri();
// If there is no base URI, return the token URI.
if base.is_empty() {
return Ok(token_uri);
}
// If both are set, concatenate the `base_uri` and `token_uri`.
let uri = if !token_uri.is_empty() {
base + &token_uri
} else {
metadata.token_uri(token_id, erc721)?
};
Ok(uri)
}
}
/*
#[cfg(all(test, feature = "std"))]
mod tests {
use alloy_primitives::U256;
use stylus_sdk::{msg, stylus_proc::sol_storage};
use super::Erc721UriStorage;
use crate::token::erc721::{extensions::Erc721Metadata, Erc721};
fn random_token_id() -> U256 {
let num: u32 = rand::random();
U256::from(num)
}
sol_storage! {
struct Erc721MetadataExample {
Erc721 erc721;
Erc721Metadata metadata;
Erc721UriStorage uri_storage;
}
}
#[motsu::test]
fn get_token_uri_works(contract: Erc721MetadataExample) {
let alice = msg::sender();
let token_id = random_token_id();
contract
.erc721
._mint(alice, token_id)
.expect("should mint a token for Alice");
let token_uri = String::from("https://docs.openzeppelin.com/contracts/5.x/api/token/erc721#Erc721URIStorage");
contract
.uri_storage
._token_uris
.setter(token_id)
.set_str(token_uri.clone());
assert_eq!(
token_uri,
contract
.uri_storage
.token_uri(token_id, &contract.erc721, &contract.metadata)
.expect("should return token URI")
);
}
#[motsu::test]
fn set_token_uri_works(contract: Erc721MetadataExample) {
let alice = msg::sender();
let token_id = random_token_id();
contract
.erc721
._mint(alice, token_id)
.expect("should mint a token for Alice");
let initial_token_uri = String::from("https://docs.openzeppelin.com/contracts/5.x/api/token/erc721#Erc721URIStorage");
contract
.uri_storage
._token_uris
.setter(token_id)
.set_str(initial_token_uri);
let token_uri = String::from("Updated Token URI");
contract.uri_storage._set_token_uri(token_id, token_uri.clone());
assert_eq!(
token_uri,
contract
.uri_storage
.token_uri(token_id, &contract.erc721, &contract.metadata)
.expect("should return token URI")
);
}
}
*/