Last active
March 4, 2022 16:41
-
-
Save kikoncuo/51b95d3cb34a5ad5654343aa6f14b367 to your computer and use it in GitHub Desktop.
Collectible remix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity ^0.8.0; | |
import "@openzeppelin/contracts/utils/Address.sol"; | |
import "@openzeppelin/contracts/utils/Strings.sol"; | |
import "@openzeppelin/contracts/utils/math/SafeMath.sol"; | |
import "@openzeppelin/contracts/proxy/utils/Initializable.sol"; | |
import "@openzeppelin/contracts/proxy/beacon/BeaconProxy.sol"; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
import "@openzeppelin/contracts/token/ERC721/IERC721.sol"; | |
import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol"; | |
import "@openzeppelin/contracts/token/ERC721/extensions/IERC721Metadata.sol"; | |
import "@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol"; | |
import "@openzeppelin/contracts/utils/introspection/IERC165.sol"; | |
import "./EthereumClaimRegistry.sol"; | |
import "./CollectibleClaimRegistry.sol"; | |
import "./ContributorRegistry.sol"; | |
contract Collectible is Ownable, Initializable, IERC165, IERC721, IERC721Metadata, IERC721Enumerable { | |
using Strings for uint256; | |
using Address for address; | |
using SafeMath for uint256; | |
using SafeMath for uint32; | |
// *** ERC721 *** | |
// Token name | |
string private _name; | |
// Token symbol | |
string private _symbol; | |
// Mapping from token ID to owner address | |
mapping(uint256 => address) private _owners; | |
// Mapping owner address to token count | |
mapping(address => uint256) private _balances; | |
// Mapping from token ID to approved address | |
mapping(uint256 => address) private _tokenApprovals; | |
// Mapping from owner to operator approvals | |
mapping(address => mapping(address => bool)) private _operatorApprovals; | |
// Base token URI | |
string private _baseTokenURI; | |
// Contract level metadata URI | |
string private _contractURI; | |
// *** ERC721Enumerable *** | |
// Mapping from owner to list of owned token IDs | |
mapping(address => mapping(uint256 => uint256)) private _ownedTokens; | |
// Mapping from token ID to index of the owner tokens list | |
mapping(uint256 => uint256) private _ownedTokensIndex; | |
// Array with all token ids, used for enumeration | |
uint256[] private _allTokens; | |
// Mapping from token id to position in the allTokens array | |
mapping(uint256 => uint256) private _allTokensIndex; | |
// *** Business logic *** | |
// Maping from collectible ID to IPFS CID | |
mapping(uint256 => string) private _collectibleCID; | |
// Mapping from collectible ID to list of minted token IDs | |
mapping(uint256 => uint256[]) private _collectibleTokens; | |
// Mapping from token ID to index of the collectible tokens list | |
mapping(uint256 => uint256) private _collectibleTokensIndex; | |
// Ethereum Claim Registry contract | |
EthereumClaimRegistry private _ethereumClaims; | |
// Collectible Claim Registry contract | |
CollectibleClaimRegistry private _collectibleClaims; | |
// Authorization claim issuer address | |
address private _authClaimIssuer; | |
// Claim name for user role | |
bytes32 constant CL_COLLECTIBLE_CREATOR = keccak256("X-Beasy-Collectible-Creator"); | |
// ContributorRegistry contract | |
ContributorRegistry private _contributors; | |
// Operator address | |
address private _operator; | |
/****************************************************************************** | |
* | |
* Implementation of ERC165 | |
* | |
*****************************************************************************/ | |
/** | |
* @dev See {IERC165-supportsInterface}. | |
*/ | |
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) { | |
return interfaceId == type(IERC721).interfaceId | |
|| interfaceId == type(IERC721Metadata).interfaceId | |
|| interfaceId == type(IERC721Enumerable).interfaceId | |
|| interfaceId == type(IERC165).interfaceId | |
; | |
} | |
/****************************************************************************** | |
* | |
* Implementation of Initializable | |
* | |
*****************************************************************************/ | |
/** | |
* @dev Constructor replacement | |
* @param name_ string Token name | |
* @param symbol_ string Token symbol | |
* @param baseURI_ string Token base URI | |
* @param contractURI_ string Token contract level metadata URI | |
* @param ethereumClaimRegistry_ address Ethereum Claim Registry address | |
* @param authClaimIssuer_ address Authorization claim issuer address | |
*/ | |
function initialize( | |
string memory name_, | |
string memory symbol_, | |
string memory baseURI_, | |
string memory contractURI_, | |
address ethereumClaimRegistry_, | |
address collectibleClaimRegistry_, | |
address authClaimIssuer_ | |
) /*external onlyOwner*/ public initializer { | |
_name = name_; | |
_symbol = symbol_; | |
_baseTokenURI = baseURI_; | |
_contractURI = contractURI_; | |
_ethereumClaims = EthereumClaimRegistry(ethereumClaimRegistry_); | |
_collectibleClaims = CollectibleClaimRegistry(collectibleClaimRegistry_); | |
_authClaimIssuer = authClaimIssuer_; | |
transferOwnership(tx.origin); | |
} | |
/* | |
constructor( | |
string memory name_, | |
string memory symbol_, | |
string memory URI_, | |
address ethereumClaimRegistry_, | |
address collectibleClaimRegistry_, | |
address authClaimIssuer_ | |
) { | |
initialize(name_, symbol_, URI_, ethereumClaimRegistry_, collectibleClaimRegistry_, authClaimIssuer_); | |
} | |
*/ | |
/****************************************************************************** | |
* | |
* Implementation of ERC721 | |
* | |
*****************************************************************************/ | |
/** | |
* @dev See {IERC721-balanceOf}. | |
*/ | |
function balanceOf(address owner) public view virtual override returns (uint256) { | |
require(owner != address(0), "ERC721: balance query for the zero address"); | |
return _balances[owner]; | |
} | |
/** | |
* @dev See {IERC721-ownerOf}. | |
*/ | |
function ownerOf(uint256 tokenId) public view virtual override returns (address) { | |
address owner = _owners[tokenId]; | |
require(owner != address(0), "ERC721: owner query for nonexistent token"); | |
return owner; | |
} | |
/** | |
* @dev Base URI for computing {tokenURI}. If set, the resulting URI for each | |
* token will be the concatenation of the `baseURI` and the `tokenId`. Empty | |
* by default, can be overriden in child contracts. | |
*/ | |
function _baseURI() internal view virtual returns (string memory) { | |
return _baseTokenURI; | |
} | |
/** | |
* @dev See {IERC721-approve}. | |
*/ | |
function approve(address to, uint256 tokenId) public virtual override { | |
address owner = ownerOf(tokenId); | |
require(to != owner, "ERC721: approval to current owner"); | |
require( | |
_msgSender() == owner || isApprovedForAll(owner, _msgSender()), | |
"ERC721: approve caller is not owner nor approved for all" | |
); | |
_approve(to, tokenId); | |
} | |
/** | |
* @dev See {IERC721-getApproved}. | |
*/ | |
function getApproved(uint256 tokenId) public view virtual override returns (address) { | |
require(_exists(tokenId), "ERC721: approved query for nonexistent token"); | |
return _tokenApprovals[tokenId]; | |
} | |
/** | |
* @dev See {IERC721-setApprovalForAll}. | |
*/ | |
function setApprovalForAll(address operator, bool approved) public virtual override { | |
require(operator != tx.origin, "ERC721: approve to caller"); | |
_operatorApprovals[tx.origin][operator] = approved; | |
emit ApprovalForAll(tx.origin, operator, approved); | |
} | |
/** | |
* @dev See {IERC721-isApprovedForAll}. | |
*/ | |
function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) { | |
return _operatorApprovals[owner][operator]; | |
} | |
/** | |
* @dev See {IERC721-transferFrom}. | |
*/ | |
function transferFrom( | |
address from, | |
address to, | |
uint256 tokenId | |
) public virtual override { | |
//solhint-disable-next-line max-line-length | |
require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved"); | |
_transfer(from, to, tokenId); | |
} | |
/** | |
* @dev See {IERC721-safeTransferFrom}. | |
*/ | |
function safeTransferFrom( | |
address from, | |
address to, | |
uint256 tokenId | |
) public virtual override { | |
safeTransferFrom(from, to, tokenId, ""); | |
} | |
/** | |
* @dev See {IERC721-safeTransferFrom}. | |
*/ | |
function safeTransferFrom( | |
address from, | |
address to, | |
uint256 tokenId, | |
bytes memory _data | |
) public virtual override { | |
require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved"); | |
_safeTransfer(from, to, tokenId, _data); | |
} | |
/** | |
* @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients | |
* are aware of the ERC721 protocol to prevent tokens from being forever locked. | |
* | |
* `_data` is additional data, it has no specified format and it is sent in call to `to`. | |
* | |
* This internal function is equivalent to {safeTransferFrom}, and can be used to e.g. | |
* implement alternative mechanisms to perform token transfer, such as signature-based. | |
* | |
* Requirements: | |
* | |
* - `from` cannot be the zero address. | |
* - `to` cannot be the zero address. | |
* - `tokenId` token must exist and be owned by `from`. | |
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer. | |
* | |
* Emits a {Transfer} event. | |
*/ | |
function _safeTransfer( | |
address from, | |
address to, | |
uint256 tokenId, | |
bytes memory _data | |
) internal virtual { | |
_transfer(from, to, tokenId); | |
require(_checkOnERC721Received(from, to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer"); | |
} | |
/** | |
* @dev Returns whether `tokenId` exists. | |
* | |
* Tokens can be managed by their owner or approved accounts via {approve} or {setApprovalForAll}. | |
* | |
* Tokens start existing when they are minted (`_mint`), | |
* and stop existing when they are burned (`_burn`). | |
*/ | |
function _exists(uint256 tokenId) internal view virtual returns (bool) { | |
return _owners[tokenId] != address(0); | |
} | |
/** | |
* @dev Returns whether `spender` is allowed to manage `tokenId`. | |
* | |
* Requirements: | |
* | |
* - `tokenId` must exist. | |
*/ | |
function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) { | |
require(_exists(tokenId), "ERC721: operator query for nonexistent token"); | |
address owner = ownerOf(tokenId); | |
return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender)); | |
} | |
/** | |
* @dev Safely mints `tokenId` and transfers it to `to`. | |
* | |
* Requirements: | |
* | |
* - `tokenId` must not exist. | |
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer. | |
* | |
* Emits a {Transfer} event. | |
*/ | |
function _safeMint(address to, uint256 tokenId) internal virtual { | |
_safeMint(to, tokenId, ""); | |
} | |
/** | |
* @dev Same as {xref-ERC721-_safeMint-address-uint256-}[`_safeMint`], with an additional `data` parameter which is | |
* forwarded in {IERC721Receiver-onERC721Received} to contract recipients. | |
*/ | |
function _safeMint( | |
address to, | |
uint256 tokenId, | |
bytes memory _data | |
) internal virtual { | |
_mint(to, tokenId); | |
require( | |
_checkOnERC721Received(address(0), to, tokenId, _data), | |
"ERC721: transfer to non ERC721Receiver implementer" | |
); | |
} | |
/** | |
* @dev Mints `tokenId` and transfers it to `to`. | |
* | |
* WARNING: Usage of this method is discouraged, use {_safeMint} whenever possible | |
* | |
* Requirements: | |
* | |
* - `tokenId` must not exist. | |
* - `to` cannot be the zero address. | |
* | |
* Emits a {Transfer} event. | |
*/ | |
function _mint(address to, uint256 tokenId) internal virtual { | |
require(to != address(0), "ERC721: mint to the zero address"); | |
require(!_exists(tokenId), "ERC721: token already minted"); | |
_beforeTokenTransfer(address(0), to, tokenId); | |
_balances[to] += 1; | |
_owners[tokenId] = to; | |
emit Transfer(address(0), to, tokenId); | |
} | |
/** | |
* @dev Destroys `tokenId`. | |
* The approval is cleared when the token is burned. | |
* | |
* Requirements: | |
* | |
* - `tokenId` must exist. | |
* | |
* Emits a {Transfer} event. | |
*/ | |
function _burn(uint256 tokenId) internal virtual { | |
address owner = ownerOf(tokenId); | |
_beforeTokenTransfer(owner, address(0), tokenId); | |
// Clear approvals | |
_approve(address(0), tokenId); | |
_balances[owner] -= 1; | |
delete _owners[tokenId]; | |
emit Transfer(owner, address(0), tokenId); | |
} | |
/** | |
* @dev Transfers `tokenId` from `from` to `to`. | |
* As opposed to {transferFrom}, this imposes no restrictions on msg.sender. | |
* | |
* Requirements: | |
* | |
* - `to` cannot be the zero address. | |
* - `tokenId` token must be owned by `from`. | |
* | |
* Emits a {Transfer} event. | |
*/ | |
function _transfer( | |
address from, | |
address to, | |
uint256 tokenId | |
) internal virtual { | |
require(ownerOf(tokenId) == from, "ERC721: transfer of token that is not own"); | |
require(to != address(0), "ERC721: transfer to the zero address"); | |
_beforeTokenTransfer(from, to, tokenId); | |
// Clear approvals from the previous owner | |
_approve(address(0), tokenId); | |
_balances[from] -= 1; | |
_balances[to] += 1; | |
_owners[tokenId] = to; | |
emit Transfer(from, to, tokenId); | |
} | |
/** | |
* @dev Approve `to` to operate on `tokenId` | |
* | |
* Emits a {Approval} event. | |
*/ | |
function _approve(address to, uint256 tokenId) internal virtual { | |
_tokenApprovals[tokenId] = to; | |
emit Approval(ownerOf(tokenId), to, tokenId); | |
} | |
/** | |
* @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address. | |
* The call is not executed if the target address is not a contract. | |
* | |
* @param from address representing the previous owner of the given token ID | |
* @param to target address that will receive the tokens | |
* @param tokenId uint256 ID of the token to be transferred | |
* @param _data bytes optional data to send along with the call | |
* @return bool whether the call correctly returned the expected magic value | |
*/ | |
function _checkOnERC721Received( | |
address from, | |
address to, | |
uint256 tokenId, | |
bytes memory _data | |
) private returns (bool) { | |
if (to.isContract()) { | |
try IERC721Receiver(to).onERC721Received(_msgSender(), from, tokenId, _data) returns (bytes4 retval) { | |
return retval == IERC721Receiver(to).onERC721Received.selector; | |
} catch (bytes memory reason) { | |
if (reason.length == 0) { | |
revert("ERC721: transfer to non ERC721Receiver implementer"); | |
} else { | |
assembly { | |
revert(add(32, reason), mload(reason)) | |
} | |
} | |
} | |
} else { | |
return true; | |
} | |
} | |
/****************************************************************************** | |
* | |
* Implementation of ERC721Metadata | |
* | |
*****************************************************************************/ | |
/** | |
* @dev See {IERC721Metadata-name}. | |
*/ | |
function name() public view virtual override returns (string memory) { | |
return _name; | |
} | |
/** | |
* @dev See {IERC721Metadata-symbol}. | |
*/ | |
function symbol() public view virtual override returns (string memory) { | |
return _symbol; | |
} | |
/** | |
* @dev See {IERC721Metadata-tokenURI}. | |
*/ | |
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) { | |
require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token"); | |
return collectibleURI(tokenId & ~uint256(0xffffffff)); | |
} | |
/** | |
* @dev Opensea collection metadata. | |
*/ | |
function contractURI() public view returns (string memory) { | |
return _contractURI; | |
} | |
/****************************************************************************** | |
* | |
* Implementation of ERC721Enumerable | |
* | |
*****************************************************************************/ | |
/** | |
* @dev See {IERC721Enumerable-tokenOfOwnerByIndex}. | |
*/ | |
function tokenOfOwnerByIndex(address owner, uint256 index) public view virtual override returns (uint256) { | |
require(index < balanceOf(owner), "ERC721Enumerable: owner index out of bounds"); | |
return _ownedTokens[owner][index]; | |
} | |
/** | |
* @dev See {IERC721Enumerable-totalSupply}. | |
*/ | |
function totalSupply() public view virtual override returns (uint256) { | |
return _allTokens.length; | |
} | |
/** | |
* @dev See {IERC721Enumerable-tokenByIndex}. | |
*/ | |
function tokenByIndex(uint256 index) public view virtual override returns (uint256) { | |
require(index < totalSupply(), "ERC721Enumerable: global index out of bounds"); | |
return _allTokens[index]; | |
} | |
/** | |
* @dev Hook that is called before any token transfer. This includes minting | |
* and burning. | |
* | |
* Calling conditions: | |
* | |
* - When `from` and `to` are both non-zero, ``from``'s `tokenId` will be | |
* transferred to `to`. | |
* - When `from` is zero, `tokenId` will be minted for `to`. | |
* - When `to` is zero, ``from``'s `tokenId` will be burned. | |
* - `from` cannot be the zero address. | |
* - `to` cannot be the zero address. | |
* | |
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks]. | |
*/ | |
function _beforeTokenTransfer( | |
address from, | |
address to, | |
uint256 tokenId | |
) internal virtual { | |
if (from == address(0)) { | |
_addTokenToAllTokensEnumeration(tokenId); | |
_addTokenToCollectibleTokensEnumeration(tokenId); | |
} else if (from != to) { | |
_removeTokenFromOwnerEnumeration(from, tokenId); | |
} | |
if (to == address(0)) { | |
_removeTokenFromAllTokensEnumeration(tokenId); | |
_removeTokenFromCollectibleEnumeration(tokenId); | |
} else if (to != from) { | |
_addTokenToOwnerEnumeration(to, tokenId); | |
} | |
} | |
/** | |
* @dev Private function to add a token to this extension's ownership-tracking data structures. | |
* @param to address representing the new owner of the given token ID | |
* @param tokenId uint256 ID of the token to be added to the tokens list of the given address | |
*/ | |
function _addTokenToOwnerEnumeration(address to, uint256 tokenId) private { | |
uint256 length = balanceOf(to); | |
_ownedTokens[to][length] = tokenId; | |
_ownedTokensIndex[tokenId] = length; | |
} | |
/** | |
* @dev Private function to add a token to this extension's token tracking data structures. | |
* @param tokenId uint256 ID of the token to be added to the tokens list | |
*/ | |
function _addTokenToAllTokensEnumeration(uint256 tokenId) private { | |
_allTokensIndex[tokenId] = _allTokens.length; | |
_allTokens.push(tokenId); | |
} | |
/** | |
* @dev Private function to remove a token from this extension's ownership-tracking data structures. Note that | |
* while the token is not assigned a new owner, the `_ownedTokensIndex` mapping is _not_ updated: this allows for | |
* gas optimizations e.g. when performing a transfer operation (avoiding double writes). | |
* This has O(1) time complexity, but alters the order of the _ownedTokens array. | |
* @param from address representing the previous owner of the given token ID | |
* @param tokenId uint256 ID of the token to be removed from the tokens list of the given address | |
*/ | |
function _removeTokenFromOwnerEnumeration(address from, uint256 tokenId) private { | |
// To prevent a gap in from's tokens array, we store the last token in the index of the token to delete, and | |
// then delete the last slot (swap and pop). | |
uint256 lastTokenIndex = balanceOf(from) - 1; | |
uint256 tokenIndex = _ownedTokensIndex[tokenId]; | |
// When the token to delete is the last token, the swap operation is unnecessary | |
if (tokenIndex != lastTokenIndex) { | |
uint256 lastTokenId = _ownedTokens[from][lastTokenIndex]; | |
_ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token | |
_ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index | |
} | |
// This also deletes the contents at the last position of the array | |
delete _ownedTokensIndex[tokenId]; | |
delete _ownedTokens[from][lastTokenIndex]; | |
} | |
/** | |
* @dev Private function to remove a token from this extension's token tracking data structures. | |
* This has O(1) time complexity, but alters the order of the _allTokens array. | |
* @param tokenId uint256 ID of the token to be removed from the tokens list | |
*/ | |
function _removeTokenFromAllTokensEnumeration(uint256 tokenId) private { | |
// To prevent a gap in the tokens array, we store the last token in the index of the token to delete, and | |
// then delete the last slot (swap and pop). | |
uint256 lastTokenIndex = _allTokens.length - 1; | |
uint256 tokenIndex = _allTokensIndex[tokenId]; | |
// When the token to delete is the last token, the swap operation is unnecessary. However, since this occurs so | |
// rarely (when the last minted token is burnt) that we still do the swap here to avoid the gas cost of adding | |
// an 'if' statement (like in _removeTokenFromOwnerEnumeration) | |
uint256 lastTokenId = _allTokens[lastTokenIndex]; | |
_allTokens[tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token | |
_allTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index | |
// This also deletes the contents at the last position of the array | |
delete _allTokensIndex[tokenId]; | |
_allTokens.pop(); | |
} | |
/****************************************************************************** | |
* | |
* Various configuration methods | |
* | |
*****************************************************************************/ | |
/** | |
* @dev Sets base token URI | |
* @param URI string base URI | |
*/ | |
function setBaseURI(string memory URI) external virtual onlyOwner { | |
_baseTokenURI = URI; | |
} | |
/** | |
* @dev Sets contract URI for contract level metadata | |
* @param URI string contract URI | |
*/ | |
function setContractURI(string memory URI) external virtual onlyOwner { | |
_contractURI = URI; | |
} | |
/** | |
* @dev Sets authorization claim issuer address | |
* @param address_ address Authorization claim issuer address | |
*/ | |
function setAuthClaimIssuer(address address_) external virtual onlyOwner { | |
_authClaimIssuer = address_; | |
} | |
/** | |
* @dev Sets ContributorRegistry contract address | |
* @param address_ address ContributorRegistry contract address | |
*/ | |
function setContributorRegistry(address address_) external virtual onlyOwner { | |
_contributors = ContributorRegistry(address_); | |
} | |
/** | |
* @dev Sets default operator address | |
* @param address_ address Operator address | |
*/ | |
function setOperator(address address_) external virtual onlyOwner { | |
_operator = address_; | |
} | |
/****************************************************************************** | |
* | |
* Business logic | |
* | |
*****************************************************************************/ | |
event collectible(uint256 indexed collectibleId, string CID); | |
event collectibleDeleted(uint256 indexed collectibleId, string CID); | |
/** | |
* @dev Mints a token within specified collectible | |
* @param to address Initial owner of all them items of the collectible | |
* @param collectibleId uint256 ID of the collectible (only 224 most significat bits are used) | |
* @param data bytes | |
* @return uint256 token ID | |
*/ | |
function safeMint ( | |
address to, | |
uint256 collectibleId, | |
bytes memory data | |
) virtual public returns(uint256) { | |
require((collectibleId & 0xffffffff) == 0, "Beasy: Malformed collectible ID"); | |
require(bytes(_collectibleCID[collectibleId]).length != 0, "Beasy: Minting token of nonexistent collectible"); | |
address collectibleOwner = _collectibleOwner(collectibleId); | |
require(collectibleOwner == tx.origin || isApprovedForAll(collectibleOwner, msg.sender), "Beasy: address is neither a creator of the collecible nor an operator"); | |
uint256 tokenId = collectibleId + _collectibleTokens[collectibleId].length; | |
_safeMint(to, tokenId, data); | |
return tokenId; | |
} | |
/** | |
* @dev Mints a token within specified collectible | |
* @param to address Initial owner of all them items of the collectible | |
* @param collectibleId uint256 ID of the collectible (only 224 most significat bits are used) | |
* @return uint256 token ID | |
*/ | |
function safeMint (address to, uint256 collectibleId) virtual public returns(uint256) { | |
return safeMint(to, collectibleId, ""); | |
} | |
/** | |
* @dev Burns existing token | |
* @param tokenId uint256 token ID to burn | |
*/ | |
function burn(uint256 tokenId) virtual public { | |
_burn(tokenId); | |
} | |
/** | |
* @dev Creates a collectible with specified attributes | |
* @param collectibleId uint256 ID of the collectible (only 224 most significat bits are used) | |
* @param CID string IPFS CID for collectible metadata | |
* @param contributors bytes32[] List of contributor records | |
*/ | |
function createCollectible(uint256 collectibleId, string memory CID, bytes32[] memory contributors) virtual public returns(uint256) { | |
require(_ethereumClaims.getClaim(_authClaimIssuer, tx.origin, CL_COLLECTIBLE_CREATOR) != bytes32(uint256(0)), "Beasy: address is not allowed to create collectibles"); | |
require((collectibleId & 0xffffffff) == 0, "Beasy: Malformed collectible ID"); | |
require(bytes(_collectibleCID[collectibleId]).length == 0, "Beasy: Collectible already exist"); | |
_collectibleCID[collectibleId] = CID; | |
emit collectible(collectibleId, CID); | |
setApprovalForAll(_operator, true); | |
_collectibleClaims.setClaim(collectibleId, CL_COLLECTIBLE_CREATOR, bytes32(bytes20(tx.origin))); | |
_contributors.setContributors(collectibleId, contributors); | |
return collectibleId; | |
} | |
/** | |
* @dev Deletes a collectible with specified attributes | |
* @param collectibleId uint256 ID of the collectible (only 224 most significat bits are used) | |
*/ | |
function deleteCollectible(uint256 collectibleId, string memory CID) virtual public returns(uint256) { | |
require(_collectibleClaims.getClaim(tx.origin, collectibleId, CL_COLLECTIBLE_CREATOR) == bytes32(bytes20(tx.origin)), "Beasy: address is not the original NFT creator"); | |
require((collectibleId & 0xffffffff) == 0, "Beasy: Malformed collectible ID"); | |
require(bytes(_collectibleCID[collectibleId]).length != 0, "Beasy: Collectible does not exist"); | |
_collectibleCID[collectibleId] = ""; | |
emit collectibleDeleted(collectibleId, CID); | |
_collectibleClaims.removeClaim(tx.origin, collectibleId, CL_COLLECTIBLE_CREATOR); | |
_contributors.deleteContributors(collectibleId); | |
return collectibleId; | |
} | |
/** | |
* @dev Returns address of owner for specified collectible | |
* @param collectibleId uint256 Collectibe ID | |
* @return address Address of the owner | |
*/ | |
function _collectibleOwner(uint256 collectibleId) private view returns(address) { | |
return address(bytes20(_collectibleClaims.getClaim(address(this), collectibleId, CL_COLLECTIBLE_CREATOR))); | |
} | |
/** | |
* @dev Private function to add a token to this extension's token tracking data structures. | |
* @param tokenId uint256 ID of the token to be added to the tokens list | |
*/ | |
function _addTokenToCollectibleTokensEnumeration(uint256 tokenId) private { | |
uint256 collectibleId = (tokenId & ~uint256(0xffffffff)); | |
uint256 idx = _collectibleTokens[collectibleId].length; | |
_collectibleTokens[collectibleId].push(tokenId); | |
_collectibleTokensIndex[tokenId] = idx; | |
} | |
/** | |
* @dev Private function to remove a token from this extension's token-tracking data structures. | |
* This has O(1) time complexity, but alters the order of the _collectibleTokens array. | |
* @param tokenId uint256 ID of the token to be removed from the tokens list of the given address | |
*/ | |
function _removeTokenFromCollectibleEnumeration(uint256 tokenId) private { | |
uint256 collectibleId = tokenId & ~uint256(0xffffffff); | |
// To prevent a gap in from's tokens array, we store the last token in the index of the token to delete, and | |
// then delete the last slot (swap and pop). | |
uint256 lastTokenIndex = _collectibleTokens[collectibleId].length; | |
uint256 tokenIndex = _collectibleTokensIndex[tokenId]; | |
// When the token to delete is the last token, the swap operation is unnecessary | |
if (tokenIndex != lastTokenIndex) { | |
uint256 lastTokenId = _collectibleTokens[collectibleId][lastTokenIndex]; | |
_collectibleTokens[collectibleId][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token | |
_collectibleTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index | |
} | |
// This also deletes the contents at the last position of the array | |
delete _collectibleTokensIndex[tokenId]; | |
delete _collectibleTokens[collectibleId][lastTokenIndex]; | |
} | |
/** | |
* @dev Returns total number of tokens of particular collecible | |
* @param collectibleId uint256 ID of the collectible (only 224 most significat bits are used) | |
* @return uint256 number of tokens | |
*/ | |
function tokensOfCollectible(uint256 collectibleId) virtual public view returns(uint256) { | |
return _collectibleTokens[collectibleId].length; | |
} | |
/** | |
* @dev Returns ID of a token by index | |
* @param collectibleId uint256 ID of the collectible (only 224 most significat bits are used) | |
* @param index uint256 token index | |
* @return uint256 token ID | |
*/ | |
function tokenOfCollectibleByIndex(uint256 collectibleId, uint256 index) virtual public view returns(uint256) { | |
return _collectibleTokens[collectibleId][index]; | |
} | |
/** | |
* @dev See {IERC721Metadata-tokenURI}. | |
*/ | |
function collectibleURI(uint256 collectibleId) public view virtual returns (string memory) { | |
string memory baseURI = _baseURI(); | |
return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, _collectibleCID[collectibleId])) : ""; | |
} | |
/** | |
* @dev Changes CID of an existing collectible | |
* @param collectibleId uint256 ID of the collectible (only 224 most significat bits are used) | |
* @param CID string IPFS CID for collectible metadata | |
*/ | |
function updateCID(uint256 collectibleId, string memory CID) public virtual onlyOwner { | |
require((collectibleId & 0xffffffff) == 0, "Beasy: Malformed collectible ID"); | |
require(bytes(_collectibleCID[collectibleId]).length != 0, "Beasy: Collectible not yet exist"); | |
require(bytes(CID).length != 0, "Beasy: Empty CID is not allowed"); | |
_collectibleCID[collectibleId] = CID; | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity ^0.8.0; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
import "@openzeppelin/contracts/proxy/beacon/UpgradeableBeacon.sol"; | |
contract CollectibleBeacon is Ownable, UpgradeableBeacon { | |
/** | |
* @dev See {UpgradeableBeacon-constructor} | |
*/ | |
constructor(address implementation_) UpgradeableBeacon(implementation_) {} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity ^0.8.0; | |
contract CollectibleClaimRegistry { | |
mapping(address => mapping(uint256 => mapping(bytes32 => bytes32))) public registry; | |
event ClaimSet( | |
address indexed issuer, | |
uint256 indexed subject, | |
bytes32 indexed key, | |
bytes32 value, | |
uint updatedAt); | |
event ClaimRemoved( | |
address indexed issuer, | |
uint256 indexed subject, | |
bytes32 indexed key, | |
uint removedAt); | |
// create or update clams | |
function setClaim(uint256 subject, bytes32 key, bytes32 value) public { | |
registry[msg.sender][subject][key] = value; | |
emit ClaimSet(msg.sender, subject, key, value, block.timestamp); | |
} | |
function getClaim(address issuer, uint256 subject, bytes32 key) public view returns(bytes32) { | |
return registry[issuer][subject][key]; | |
} | |
function removeClaim(address issuer, uint256 subject, bytes32 key) public { | |
require(msg.sender == issuer); | |
delete registry[issuer][subject][key]; | |
emit ClaimRemoved(msg.sender, subject, key, block.timestamp); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity ^0.8.0; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
import "@openzeppelin/contracts/proxy/beacon/BeaconProxy.sol"; | |
/** | |
* NOTE | |
* CollectibleProxy has to be derived from Ownable as Collectible does | |
*/ | |
contract CollectibleProxy is Ownable, BeaconProxy { | |
/** | |
* @dev See {BeaconProxy-constructor} | |
*/ | |
constructor(address beacon, bytes memory data) payable BeaconProxy(beacon, data) {} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity ^0.8.0; | |
import "@openzeppelin/contracts/utils/Address.sol"; | |
import "@openzeppelin/contracts/proxy/utils/Initializable.sol"; | |
import "@openzeppelin/contracts/proxy/beacon/BeaconProxy.sol"; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
import "./CollectibleClaimRegistry.sol"; | |
contract ContributorRegistry is Ownable, Initializable { | |
// Contributor registry | |
mapping(uint256 => bytes32[]) private _registry; | |
// Collectible contract | |
address private collectible; | |
// CollectibleClaimRegistry contract | |
CollectibleClaimRegistry private collectibleClaimRegistry; | |
// Claim name for user role | |
bytes32 constant CL_COLLECTIBLE_CREATOR = keccak256("X-Beasy-Collectible-Creator"); | |
event ContributorSet(uint256 indexed collectibleId, bytes32 value); | |
event ContributorDeleted(uint256 indexed collectibleId); | |
function initialize(address collectibleAddress, address collectibleClaimRegistryAddress) public initializer { | |
collectible = collectibleAddress; | |
collectibleClaimRegistry = CollectibleClaimRegistry(collectibleClaimRegistryAddress); | |
} | |
function setContributors(uint256 collectibleId, bytes32[] memory values) virtual external { | |
require(msg.sender == collectible, "Beasy: invalid call chain"); | |
require(collectibleClaimRegistry.getClaim(collectible, collectibleId, CL_COLLECTIBLE_CREATOR) == bytes32(bytes20(tx.origin)), "Beasy: not a collectible creator"); | |
for (uint i = 0; i < values.length; i++) { | |
bytes32 value = values[i]; | |
_registry[collectibleId].push(value); | |
emit ContributorSet(collectibleId, value); | |
} | |
} | |
function deleteContributors(uint256 collectibleId) virtual external { | |
require(msg.sender == collectible, "Beasy: invalid call chain"); | |
require(collectibleClaimRegistry.getClaim(collectible, collectibleId, CL_COLLECTIBLE_CREATOR) == bytes32(bytes20(tx.origin)), "Beasy: not a collectible creator"); | |
for (uint i = 0; i < _registry[collectibleId].length; i++) { | |
_registry[collectibleId][i] == bytes32(0); | |
} | |
emit ContributorDeleted(collectibleId); | |
} | |
function getContributors(uint256 collectibleId) public view returns(bytes32[] memory) { | |
return _registry[collectibleId]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity ^0.8.0; | |
contract ERC780 { | |
mapping(address => mapping(address => mapping(bytes32 => bytes32))) public registry; | |
event ClaimSet( | |
address indexed issuer, | |
address indexed subject, | |
bytes32 indexed key, | |
bytes32 value, | |
uint updatedAt); | |
event ClaimRemoved( | |
address indexed issuer, | |
address indexed subject, | |
bytes32 indexed key, | |
uint removedAt); | |
// create or update clams | |
function setClaim(address subject, bytes32 key, bytes32 value) public { | |
registry[msg.sender][subject][key] = value; | |
emit ClaimSet(msg.sender, subject, key, value, block.timestamp); | |
} | |
function setSelfClaim(bytes32 key, bytes32 value) public { | |
setClaim(msg.sender, key, value); | |
} | |
function getClaim(address issuer, address subject, bytes32 key) public view returns(bytes32) { | |
return registry[issuer][subject][key]; | |
} | |
function removeClaim(address issuer, address subject, bytes32 key) public { | |
require(msg.sender == issuer); | |
delete registry[issuer][subject][key]; | |
emit ClaimRemoved(msg.sender, subject, key, block.timestamp); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity ^0.8.0; | |
import "./ERC780.sol"; | |
contract EthereumClaimRegistry is ERC780 {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment