Skip to content

Commit 9907a94

Browse files
committed
Added tokenURI to ERC721Enumeration implementation
1 parent 782b51f commit 9907a94

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

src/ERC721/ERC721/ERC721Facet.sol

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface IERC721Receiver {
1818
returns (bytes4);
1919
}
2020

21-
/// @title ERC-721 Token (Zero-Dependency Implementation)
21+
/// @title ERC-721 Token
2222
/// @notice A complete, dependency-free ERC-721 implementation using the diamond storage pattern.
2323
/// @dev This facet provides metadata, ownership, approvals, safe transfers, minting, burning, and helpers.
2424
contract ERC721Facet {
@@ -90,6 +90,23 @@ contract ERC721Facet {
9090
return getStorage().symbol;
9191
}
9292

93+
/// @notice Provide the metadata URI for a given token ID.
94+
/// @param _tokenId tokenID of the NFT to query the metadata from
95+
/// @return the URI providing the detailed metadata of the specified tokenID
96+
function tokenURI(uint256 _tokenId) external view returns (string memory) {
97+
ERC721Storage storage s = getStorage();
98+
address owner = s.ownerOf[_tokenId];
99+
if (owner == address(0)) {
100+
revert ERC721NonexistentToken(_tokenId);
101+
}
102+
103+
if (bytes(s.baseURI).length == 0) {
104+
return "";
105+
}
106+
107+
return string.concat(s.baseURI, LibUtils.toString(_tokenId));
108+
}
109+
93110
/// @notice Returns the number of tokens owned by a given address.
94111
/// @param _owner The address to query the balance of.
95112
/// @return The balance (number of tokens) owned by `_owner`.
@@ -242,21 +259,4 @@ contract ERC721Facet {
242259
}
243260
}
244261
}
245-
246-
/// @notice Provide the metadata URI for a given token ID.
247-
/// @param _tokenId tokenID of the NFT to query the metadata from
248-
/// @return the URI providing the detailed metadata of the specified tokenID
249-
function tokenURI(uint256 _tokenId) external view returns (string memory) {
250-
ERC721Storage storage s = getStorage();
251-
address owner = s.ownerOf[_tokenId];
252-
if (owner == address(0)) {
253-
revert ERC721NonexistentToken(_tokenId);
254-
}
255-
256-
if (bytes(s.baseURI).length == 0) {
257-
return "";
258-
}
259-
260-
return string.concat(s.baseURI, LibUtils.toString(_tokenId));
261-
}
262262
}

src/ERC721/ERC721Enumerable/ERC721EnumerableFacet.sol

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity >=0.8.30;
33

4+
import {LibUtils} from "../../Libraries/LibUtils.sol";
5+
46
/// @title ERC721 Receiver Interface
57
/// @notice Interface for contracts that want to support safe ERC721 token transfers.
68
/// @dev Implementers must return the function selector to confirm token receipt.
@@ -16,7 +18,7 @@ interface IERC721Receiver {
1618
returns (bytes4);
1719
}
1820

19-
/// @title ERC-721 Enumerable Token (Zero-dependency Implementation)
21+
/// @title ERC-721 Enumerable Token
2022
/// @notice A complete, dependency-free ERC-721 implementation with enumeration support using a custom storage layout.
2123
/// @dev Provides metadata, ownership, approvals, enumeration, safe transfers, minting, and burning features.
2224
contract ERC721EnumerableFacet {
@@ -52,6 +54,7 @@ contract ERC721EnumerableFacet {
5254
struct ERC721EnumerableStorage {
5355
string name;
5456
string symbol;
57+
string baseURI;
5558
mapping(uint256 tokenId => string tokenURI) tokenURIOf;
5659
mapping(uint256 tokenId => address owner) ownerOf;
5760
mapping(address owner => uint256[] ownedTokens) ownedTokensOf;
@@ -83,6 +86,23 @@ contract ERC721EnumerableFacet {
8386
return getStorage().symbol;
8487
}
8588

89+
/// @notice Provide the metadata URI for a given token ID.
90+
/// @param _tokenId tokenID of the NFT to query the metadata from
91+
/// @return the URI providing the detailed metadata of the specified tokenID
92+
function tokenURI(uint256 _tokenId) external view returns (string memory) {
93+
ERC721EnumerableStorage storage s = getStorage();
94+
address owner = s.ownerOf[_tokenId];
95+
if (owner == address(0)) {
96+
revert ERC721NonexistentToken(_tokenId);
97+
}
98+
99+
if (bytes(s.baseURI).length == 0) {
100+
return "";
101+
}
102+
103+
return string.concat(s.baseURI, LibUtils.toString(_tokenId));
104+
}
105+
86106
/// @notice Returns the total number of tokens in existence.
87107
/// @return The total supply of tokens.
88108
function totalSupply() external view returns (uint256) {

src/ERC721/ERC721Enumerable/libraries/LibERC721Enumerable.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ library LibERC721 {
4444
struct ERC721EnumerableStorage {
4545
string name;
4646
string symbol;
47+
string baseURI;
4748
mapping(uint256 tokenId => address owner) ownerOf;
4849
mapping(address owner => uint256[] ownedTokens) ownedTokensOf;
4950
mapping(uint256 tokenId => uint256 ownedTokensIndex) ownedTokensIndexOf;

0 commit comments

Comments
 (0)