Skip to content

Commit

Permalink
Adding support to NFTs metadata (ERC-721 & 1155)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaitor committed Jan 14, 2022
1 parent 285c0d3 commit 0d49fbf
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 84 deletions.
20 changes: 14 additions & 6 deletions contracts/registry/DIDRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ contract DIDRegistry is DIDFactory {
hashDID(_didSeed, msg.sender),
_cap,
_royalties,
_mint
_mint,
_nftMetadata
);
return result;
}
Expand All @@ -101,7 +102,7 @@ contract DIDRegistry is DIDFactory {
* @param _royalties refers to the royalties to reward to the DID creator in the secondary market
* @param _mint if true it mints the ERC-1155 NFTs attached to the asset
* @param _activityId refers to activity
* @param _attributes refers to the provenance attributes
* @param _nftMetadata refers to the url providing the NFT Metadata
* @return size refers to the size of the registry after the register action.
*/
function registerMintableDID721(
Expand All @@ -112,17 +113,18 @@ contract DIDRegistry is DIDFactory {
uint8 _royalties,
bool _mint,
bytes32 _activityId,
string memory _attributes
string memory _nftMetadata
)
public
onlyValidAttributes(_attributes)
onlyValidAttributes(_nftMetadata)
returns (uint size)
{
uint result = registerDID(_didSeed, _checksum, _providers, _url, _activityId, _attributes);
uint result = registerDID(_didSeed, _checksum, _providers, _url, _activityId, '');
enableAndMintDidNft721(
hashDID(_didSeed, msg.sender),
_royalties,
_mint
_mint,
_nftMetadata
);
return result;
}
Expand Down Expand Up @@ -190,6 +192,9 @@ contract DIDRegistry is DIDFactory {
{
didRegisterList.initializeNftConfig(_did, _cap, _royalties);

if (bytes(_nftMetadata).length > 0)
erc1155.setNFTMetadata(uint256(_did), _nftMetadata);

if (_royalties > 0)
erc1155.setTokenRoyalty(uint256(_did), msg.sender, _royalties);

Expand Down Expand Up @@ -225,6 +230,9 @@ contract DIDRegistry is DIDFactory {
{
didRegisterList.initializeNft721Config(_did, _royalties);

if (bytes(_nftMetadata).length > 0)
erc721.setNFTMetadata(uint256(_did), _nftMetadata);

if (_royalties > 0)
erc721.setTokenRoyalty(uint256(_did), msg.sender, _royalties);

Expand Down
51 changes: 40 additions & 11 deletions contracts/token/erc2981/ERC2981.sol → contracts/token/NFTBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,63 @@ pragma solidity ^0.8.0;
// Code is Apache-2.0 and docs are CC-BY-4.0

import '@openzeppelin/contracts-upgradeable/interfaces/IERC2981Upgradeable.sol';
import '@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/token/ERC1155/ERC1155Upgradeable.sol';

/**
*
* @dev Implementation of the Royalties EIP-2981 base contract
* See https://eips.ethereum.org/EIPS/eip-2981
*/
abstract contract ERC2981 is IERC2981Upgradeable {
abstract contract NFTBase is IERC2981Upgradeable, OwnableUpgradeable, AccessControlUpgradeable {

// Mapping from account to proxy approvals
mapping (address => bool) internal _proxyApprovals;

bytes32 public constant MINTER_ROLE = keccak256('MINTER_ROLE');

struct RoyaltyInfo {
address receiver;
uint256 royaltyAmount;
}

struct NFTMetadata {
string nftURI;
}

// Mapping of Royalties per tokenId (DID)
mapping(uint256 => RoyaltyInfo) internal _royalties;

function supportsInterface(bytes4 interfaceId)
public
view
virtual
override
returns (bool)
mapping(uint256 => NFTMetadata) internal _metadata;

/**
* Event for recording proxy approvals.
*/
event ProxyApproval(address sender, address operator, bool approved);


function setProxyApproval(
address operator,
bool approved
)
public
onlyOwner
virtual
{
return
interfaceId == type(IERC2981Upgradeable).interfaceId ||
supportsInterface(interfaceId);
_proxyApprovals[operator] = approved;
emit ProxyApproval(_msgSender(), operator, approved);
}


function _setNFTMetadata(
uint256 tokenId,
string memory tokenURI
)
internal
{
_metadata[tokenId] = NFTMetadata(tokenURI);
}

function _setTokenRoyalty(
uint256 tokenId,
address receiver,
Expand Down
47 changes: 25 additions & 22 deletions contracts/token/erc1155/NFTUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,15 @@
pragma solidity ^0.8.0;

import '@openzeppelin/contracts-upgradeable/token/ERC1155/ERC1155Upgradeable.sol';
import '@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol';
import '../erc2981/ERC2981.sol';
import '../NFTBase.sol';

/**
*
* @dev Implementation of the basic standard multi-token.
* See https://eips.ethereum.org/EIPS/eip-1155
*/
contract NFTUpgradeable is ERC1155Upgradeable, ERC2981, OwnableUpgradeable, AccessControlUpgradeable {

// Mapping from account to proxy approvals
mapping (address => bool) private _proxyApprovals;

bytes32 public constant MINTER_ROLE = keccak256('MINTER_ROLE');
contract NFTUpgradeable is ERC1155Upgradeable, NFTBase {

/**
* Event for recording proxy approvals.
*/
event ProxyApproval(address sender, address operator, bool approved);

/**
* @dev See {_setURI}.
*/
Expand All @@ -37,11 +25,6 @@ contract NFTUpgradeable is ERC1155Upgradeable, ERC2981, OwnableUpgradeable, Acce
AccessControlUpgradeable._setupRole(MINTER_ROLE, msg.sender);
}

function setProxyApproval(address operator, bool approved) public onlyOwner virtual {
_proxyApprovals[operator] = approved;
emit ProxyApproval(_msgSender(), operator, approved);
}

/**
* @dev See {IERC1155-isApprovedForAll}.
*/
Expand All @@ -63,6 +46,26 @@ contract NFTUpgradeable is ERC1155Upgradeable, ERC2981, OwnableUpgradeable, Acce
AccessControlUpgradeable._setupRole(MINTER_ROLE, account);
}

// function uri(uint256 id) external view returns (string memory)
function uri(uint256 tokenId) public view override returns (string memory) {
return _metadata[tokenId].nftURI;
}

/**
* @dev Record some NFT Metadata
* @param tokenId the id of the asset with the royalties associated
* @param nftURI the URI (https, ipfs, etc) to the metadata describing the NFT
*/
function setNFTMetadata(
uint256 tokenId,
string memory nftURI
)
public
{
require(hasRole(MINTER_ROLE, msg.sender), 'only minter');
_setNFTMetadata(tokenId, nftURI);
}

/**
* @dev Record the asset royalties
* @param tokenId the id of the asset with the royalties associated
Expand All @@ -86,12 +89,12 @@ contract NFTUpgradeable is ERC1155Upgradeable, ERC2981, OwnableUpgradeable, Acce
public
view
virtual
override(AccessControlUpgradeable, ERC1155Upgradeable, ERC2981)
override(ERC1155Upgradeable, IERC165Upgradeable)
returns (bool)
{
return AccessControlUpgradeable.supportsInterface(interfaceId)
|| ERC1155Upgradeable.supportsInterface(interfaceId)
|| ERC2981.supportsInterface(interfaceId);
|| ERC1155Upgradeable.supportsInterface(interfaceId)
|| interfaceId == type(IERC2981Upgradeable).interfaceId;
}

}
50 changes: 22 additions & 28 deletions contracts/token/erc721/NFT721Upgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,13 @@
pragma solidity ^0.8.0;

import '@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol';
import '@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol';
import '@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol';
import '../erc2981/ERC2981.sol';
import '../NFTBase.sol';

/**
*
* @dev Implementation of the basic standard multi-token.
*/
contract NFT721Upgradeable is ERC721Upgradeable, ERC2981, OwnableUpgradeable, AccessControlUpgradeable {

// Mapping from account to proxy approvals
mapping (address => bool) private _proxyApprovals;

bytes32 public constant MINTER_ROLE = keccak256('MINTER_ROLE');

/**
* Event for recording proxy approvals.
*/
event ProxyApproval(address sender, address operator, bool approved);
contract NFT721Upgradeable is ERC721Upgradeable, NFTBase {

/**
* @dev See {_setURI}.
Expand All @@ -35,13 +23,7 @@ contract NFT721Upgradeable is ERC721Upgradeable, ERC2981, OwnableUpgradeable, Ac
AccessControlUpgradeable.__AccessControl_init();
AccessControlUpgradeable._setupRole(MINTER_ROLE, msg.sender);
}


function setProxyApproval(address operator, bool approved) public onlyOwner virtual {
_proxyApprovals[operator] = approved;
emit ProxyApproval(_msgSender(), operator, approved);
}

/**
* @dev See {IERC1155-isApprovedForAll}.
*/
Expand All @@ -62,14 +44,26 @@ contract NFT721Upgradeable is ERC721Upgradeable, ERC2981, OwnableUpgradeable, Ac
require(hasRole(MINTER_ROLE, msg.sender), 'only minter can burn');
_burn(id);
}

function tokenURI(uint256 tokenId) public view override returns (string memory) {
require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
return _metadata[tokenId].nftURI;
}

/**
* @dev Record some NFT Metadata
* @param tokenId the id of the asset with the royalties associated
* @param nftURI the URI (https, ipfs, etc) to the metadata describing the NFT
*/
function setNFTMetadata(
uint256 tokenId,
string memory nftURI
)
public
{
require(hasRole(MINTER_ROLE, msg.sender), 'only minter');
_setNFTMetadata(tokenId, nftURI);
}

string memory baseURI = _baseURI();
return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, tokenId.toString())) : "";
}

/**
* @dev Record the asset royalties
* @param tokenId the id of the asset with the royalties associated
Expand All @@ -93,12 +87,12 @@ contract NFT721Upgradeable is ERC721Upgradeable, ERC2981, OwnableUpgradeable, Ac
public
view
virtual
override(AccessControlUpgradeable, ERC721Upgradeable, ERC2981)
override(ERC721Upgradeable, IERC165Upgradeable)
returns (bool)
{
return AccessControlUpgradeable.supportsInterface(interfaceId)
|| ERC721Upgradeable.supportsInterface(interfaceId)
|| ERC2981.supportsInterface(interfaceId);
|| interfaceId == type(IERC2981Upgradeable).interfaceId;
}

}
Loading

0 comments on commit 0d49fbf

Please sign in to comment.