2022-08-18 10:54:42 +08:00

164 lines
4.3 KiB
Solidity

// SPDX-License-Identifier: MIT
pragma solidity 0.8.10;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";
abstract contract BEBase is ERC721, AccessControl, ERC721Enumerable, Ownable {
using Strings for uint256;
mapping(uint256 => bool) public lockedTokens;
string private _baseTokenURI = "https://market.cebg.games/api/nft/info/";
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant BURN_ROLE = keccak256("BURN_ROLE");
bytes32 public constant LOCK_ROLE = keccak256("LOCK_ROLE");
function _baseURI() internal view virtual override returns (string memory) {
return _baseTokenURI;
}
/**
* @dev Creates a new token for `to`. Its token ID will be automatically
* assigned (and available on the emitted {IERC721-Transfer} event), and the token
* URI autogenerated based on the base URI passed at construction.
*
* See {ERC721-_mint}.
*
* Requirements:
*
* - the caller must have the `MINTER_ROLE`.
*/
function mint(address to, uint256 tokenId)
external
virtual
onlyRole(MINTER_ROLE)
{
require(!_exists(tokenId), "Must have unique tokenId");
// We cannot just use balanceOf to create the new tokenId because tokens
// can be burned (destroyed), so we need a separate counter.
_mint(to, tokenId);
}
/**
* @dev Add factory to mint item
*/
function setMintFactory(address factory) external onlyOwner {
_grantRole(MINTER_ROLE, factory);
}
/**
* @dev Remove factory
*/
function removeMintFactory(address factory) external onlyOwner {
_revokeRole(MINTER_ROLE, factory);
}
/**
* @dev Add factory to burn item
*/
function grantBurnRole(address proxy) external onlyOwner {
_grantRole(BURN_ROLE, proxy);
}
/**
* @dev Remove proxy
*/
function revokeBurnProxy(address proxy) external onlyOwner {
_revokeRole(BURN_ROLE, proxy);
}
/**
* @dev Add address to lock item
*/
function grantLockRole(address account) external onlyOwner {
_grantRole(LOCK_ROLE, account);
}
/**
* @dev Remove address for lock item
*/
function revokeLockRole(address account) external onlyOwner {
_revokeRole(LOCK_ROLE, account);
}
/**
* @dev Lock token to use in game or for rental
*/
function lock(uint256 tokenId) external onlyRole(LOCK_ROLE) {
require(_exists(tokenId), "Must be valid tokenId");
require(!lockedTokens[tokenId], "Token has already locked");
lockedTokens[tokenId] = true;
}
/**
* @dev Unlock token to use blockchain or sale on marketplace
*/
function unlock(uint256 tokenId) external onlyRole(LOCK_ROLE) {
require(_exists(tokenId), "Must be valid tokenId");
require(lockedTokens[tokenId], "Token has already unlocked");
lockedTokens[tokenId] = false;
}
/**
* @dev Get lock status
*/
function isLocked(uint256 tokenId) external view returns (bool) {
return lockedTokens[tokenId];
}
/**
* @dev Set token URI
*/
function updateBaseURI(string calldata baseTokenURI) external onlyOwner {
_baseTokenURI = baseTokenURI;
}
/**
* @dev See {IERC165-_beforeTokenTransfer}.
*/
function _beforeTokenTransfer(
address from,
address to,
uint256 tokenId
) internal virtual override(ERC721, ERC721Enumerable) {
require(!lockedTokens[tokenId], "Can not transfer locked token");
super._beforeTokenTransfer(from, to, tokenId);
}
/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId)
public
view
virtual
override(AccessControl, ERC721, ERC721Enumerable)
returns (bool)
{
return super.supportsInterface(interfaceId);
}
/**
* @dev Burns `tokenId`.
*
* Requirements:
*
* - The caller must own `tokenId` or be an approved operator.
*/
function burn(address owner, uint256 tokenId)
external
virtual
onlyRole(BURN_ROLE)
{
require(_exists(tokenId), "TokenId not exists");
require(!lockedTokens[tokenId], "Can not burn locked token");
require(
ownerOf(tokenId) == owner,
"current address is not owner of this item now"
);
_burn(tokenId);
}
}