Skip to content

Commit cf4ed09

Browse files
authored
Merge pull request #280 from MoMannn/solidity-update
Make tokenURI overridable.
2 parents 0feedcb + 80599f3 commit cf4ed09

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
import "../tokens/nf-token-metadata.sol";
5+
import "../ownership/ownable.sol";
6+
7+
/**
8+
* @dev This is an example contract implementation of NFToken with metadata extension.
9+
*/
10+
contract NFTokenMetadataBaseUriMock is
11+
NFTokenMetadata,
12+
Ownable
13+
{
14+
15+
/**
16+
* @dev URI base for tokenURI function. Token URI is constructed as baseURI + tokenId.
17+
*/
18+
string public baseURI;
19+
20+
/**
21+
* @dev Contract constructor.
22+
* @param _name A descriptive name for a collection of NFTs.
23+
* @param _symbol An abbreviated name for NFTokens.
24+
* @param _baseURI String representing base RFC 3986 URI.
25+
*/
26+
constructor(
27+
string memory _name,
28+
string memory _symbol,
29+
string memory _baseURI
30+
)
31+
{
32+
nftName = _name;
33+
nftSymbol = _symbol;
34+
baseURI = _baseURI;
35+
}
36+
37+
/**
38+
* @dev Mints a new NFT.
39+
* @param _to The address that will own the minted NFT.
40+
* @param _tokenId of the NFT to be minted by the msg.sender.
41+
*/
42+
function mint(
43+
address _to,
44+
uint256 _tokenId
45+
)
46+
external
47+
onlyOwner
48+
{
49+
super._mint(_to, _tokenId);
50+
}
51+
52+
/**
53+
* @dev Removes a NFT from owner.
54+
* @param _tokenId Which NFT we want to remove.
55+
*/
56+
function burn(
57+
uint256 _tokenId
58+
)
59+
external
60+
onlyOwner
61+
{
62+
super._burn(_tokenId);
63+
}
64+
65+
/**
66+
* @dev A distinct URI (RFC 3986) for a given NFT.
67+
* @param _tokenId Id for which we want uri.
68+
* @return URI of _tokenId.
69+
*/
70+
function _tokenURI(
71+
uint256 _tokenId
72+
)
73+
internal
74+
override
75+
view
76+
returns (string memory)
77+
{
78+
return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, _uint2str(_tokenId))) : "";
79+
}
80+
81+
/**
82+
* @dev Helper function that changes uint to string representation.
83+
* @return str String representation.
84+
*/
85+
function _uint2str(
86+
uint256 _i
87+
)
88+
internal
89+
pure
90+
returns (string memory str)
91+
{
92+
if (_i == 0) {
93+
return "0";
94+
}
95+
uint256 j = _i;
96+
uint256 length;
97+
while (j != 0) {
98+
length++;
99+
j /= 10;
100+
}
101+
bytes memory bstr = new bytes(length);
102+
uint256 k = length;
103+
j = _i;
104+
while (j != 0) {
105+
bstr[--k] = bytes1(uint8(48 + j % 10));
106+
j /= 10;
107+
}
108+
str = string(bstr);
109+
}
110+
}

src/contracts/tokens/nf-token-metadata.sol

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,23 @@ contract NFTokenMetadata is
7575
view
7676
validNFToken(_tokenId)
7777
returns (string memory)
78+
{
79+
return _tokenURI(_tokenId);
80+
}
81+
82+
/**
83+
* @notice This is an internal function that can be overriden if you want to implement a different
84+
* way to generate token URI.
85+
* @param _tokenId Id for which we want uri.
86+
* @return URI of _tokenId.
87+
*/
88+
function _tokenURI(
89+
uint256 _tokenId
90+
)
91+
internal
92+
virtual
93+
view
94+
returns (string memory)
7895
{
7996
return idToUri[_tokenId];
8097
}

0 commit comments

Comments
 (0)