Add ERC1155URIStorage (#3210)

* Add ERC721URIStorage-like extension for ERC1155

* Add tests for ERC1155URIStorage extension

* add changelog entry for ERC721URIStorage

* Fix linting errors

* Emit URI event in ERC1155URIStorage

* Remove exists check and ERC1155Supply dependency

* Fix lint error

* Overwrite ERC1155 uri method

* Update ERC1155URIStorage specs

* Fix ERC1155URIStorageMock

* Rename _setTokenURI => _setURI in ERC1155URIStorage

* Add baseURI to ERC1155URIStorage

* Move super.uri call in ERC1155URIStorage

* Clearify ERC1155URIStorage description in change log

* reorder changelog & add documentation

* improve documentation

* fix typo

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
This commit is contained in:
S E R A Y A
2022-03-29 11:15:43 +02:00
committed by GitHub
parent ae270b0d89
commit 02fcc75bb7
5 changed files with 157 additions and 4 deletions

View File

@ -0,0 +1,22 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./ERC1155Mock.sol";
import "../token/ERC1155/extensions/ERC1155URIStorage.sol";
contract ERC1155URIStorageMock is ERC1155Mock, ERC1155URIStorage {
constructor(string memory _uri) ERC1155Mock(_uri) {}
function uri(uint256 tokenId) public view virtual override(ERC1155, ERC1155URIStorage) returns (string memory) {
return ERC1155URIStorage.uri(tokenId);
}
function setURI(uint256 tokenId, string memory _tokenURI) public {
_setURI(tokenId, _tokenURI);
}
function setBaseURI(string memory baseURI) public {
_setBaseURI(baseURI);
}
}

View File

@ -36,6 +36,8 @@ NOTE: This core set of contracts is designed to be unopinionated, allowing devel
{{ERC1155Supply}}
{{ERC1155URIStorage}}
== Presets
These contracts are preconfigured combinations of the above features. They can be used through inheritance or as models to copy and paste their source code.

View File

@ -0,0 +1,62 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../../../utils/Strings.sol";
import "../ERC1155.sol";
/**
* @dev ERC1155 token with storage based token URI management.
* Inspired by the ERC721URIStorage extension
*
* _Available since v4.6._
*/
abstract contract ERC1155URIStorage is ERC1155 {
using Strings for uint256;
// Optional base URI
string private _baseURI = "";
// Optional mapping for token URIs
mapping(uint256 => string) private _tokenURIs;
/**
* @dev See {IERC1155MetadataURI-uri}.
*
* This implementation returns the concatenation of the `_baseURI`
* and the token-specific uri if the latter is set
*
* This enables the following behaviors:
*
* - if `_tokenURIs[tokenId]` is set, then the result is the concatenation
* of `_baseURI` and `_tokenURIs[tokenId]` (keep in mind that `_baseURI`
* is empty per default);
*
* - if `_tokenURIs[tokenId]` is NOT set then we fallback to `super.uri()`
* which in most cases will contain `ERC1155._uri`;
*
* - if `_tokenURIs[tokenId]` is NOT set, and if the parents do not have a
* uri value set, then the result is empty.
*/
function uri(uint256 tokenId) public view virtual override returns (string memory) {
string memory tokenURI = _tokenURIs[tokenId];
// If token URI is set, concatenate base URI and tokenURI (via abi.encodePacked).
return bytes(tokenURI).length > 0 ? string(abi.encodePacked(_baseURI, tokenURI)) : super.uri(tokenId);
}
/**
* @dev Sets `tokenURI` as the tokenURI of `tokenId`.
*/
function _setURI(uint256 tokenId, string memory tokenURI) internal virtual {
_tokenURIs[tokenId] = tokenURI;
emit URI(uri(tokenId), tokenId);
}
/**
* @dev Sets `baseURI` as the `_baseURI` for all tokens
*/
function _setBaseURI(string memory baseURI) internal virtual {
_baseURI = baseURI;
}
}