合约示例
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;
/*
* @dev Provides information about the current execution context.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
/**
* @title Roles
* @dev Library for managing addresses assigned to a Role.
*/
library Roles {
struct Role {
mapping(address => bool) bearer;
}
function add(Role storage role, address account) internal {
require(!has(role, account), "Roles: account already has role");
role.bearer[account] = true;
}
function remove(Role storage role, address account) internal {
require(has(role, account), "Roles: account does not have role");
role.bearer[account] = false;
}
function has(Role storage role, address account) internal view returns (bool) {
require(account != address(0), "Roles: account is the zero address");
return role.bearer[account];
}
}
/**
* @dev Collection of functions related to the address type.
*/
library Address {
/**
* @dev Returns true if `account` is a contract.
*/
function isContract(address account) internal view returns (bool) {
return account.code.length > 0;
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the checks-effects-interactions pattern.
*/
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
// [FIX C-01] use {value:} call syntax; no longer vulnerable to the
// legacy .call.value() reentrancy pattern
(bool success, ) = recipient.call{value: amount}("");
require(success, "Address: unable to send value, recipient may have reverted");
}
}
/**
* @title Counters
* @dev Provides counters that can only be incremented or decremented by one.
*/
library Counters {
struct Counter {
uint256 _value;
}
function current(Counter storage counter) internal view returns (uint256) {
return counter._value;
}
function increment(Counter storage counter) internal {
// [FIX C-04] 0.8.x has built-in overflow checks; unchecked is safe
// here because a uint256 counter incrementing by 1 cannot realistically
// overflow in practice, and this saves a small amount of gas.
unchecked {
counter._value += 1;
}
}
function decrement(Counter storage counter) internal {
uint256 value = counter._value;
require(value > 0, "Counter: decrement overflow");
unchecked {
counter._value = value - 1;
}
}
}
contract MinterRole is Context {
using Roles for Roles.Role;
event MinterAdded(address indexed account);
event MinterRemoved(address indexed account);
Roles.Role private _minters;
constructor() {
_addMinter(_msgSender());
}
modifier onlyMinter() {
require(isMinter(_msgSender()), "MinterRole: caller does not have the Minter role");
_;
}
function isMinter(address account) public view returns (bool) {
return _minters.has(account);
}
function addMinter(address account) public onlyMinter {
_addMinter(account);
}
function renounceMinter() public {
_removeMinter(_msgSender());
}
function _addMinter(address account) internal {
_minters.add(account);
emit MinterAdded(account);
}
function _removeMinter(address account) internal {
_minters.remove(account);
emit MinterRemoved(account);
}
}
/**
* @dev Interface of the TRC165 standard.
*/
interface ITRC165 {
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
/**
* @dev Required interface of a TRC721 compliant contract.
*/
interface ITRC721 is ITRC165 {
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
function balanceOf(address owner) external view returns (uint256 balance);
function ownerOf(uint256 tokenId) external view returns (address owner);
function safeTransferFrom(address from, address to, uint256 tokenId) external;
function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
function transferFrom(address from, address to, uint256 tokenId) external;
function approve(address to, uint256 tokenId) external;
function getApproved(uint256 tokenId) external view returns (address operator);
function setApprovalForAll(address operator, bool approved) external;
function isApprovedForAll(address owner, address operator) external view returns (bool);
}
/**
* @title TRC-721 Non-Fungible Token Standard, optional metadata extension
*/
interface ITRC721Metadata is ITRC721 {
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function tokenURI(uint256 tokenId) external view returns (string memory);
}
/**
* @title TRC-721 Non-Fungible Token Standard, optional enumeration extension
*/
interface ITRC721Enumerable is ITRC721 {
function totalSupply() external view returns (uint256);
function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256);
function tokenByIndex(uint256 index) external view returns (uint256);
}
/**
* @title TRC721 token receiver interface
*/
interface ITRC721Receiver {
function onTRC721Received(
address operator,
address from,
uint256 tokenId,
bytes calldata data
) external returns (bytes4);
}
/**
* @dev Implementation of the {ITRC165} interface.
*/
abstract contract TRC165 is ITRC165 {
mapping(bytes4 => bool) private _supportedInterfaces;
constructor() {
_registerInterface(type(ITRC165).interfaceId);
}
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return _supportedInterfaces[interfaceId];
}
function _registerInterface(bytes4 interfaceId) internal {
require(interfaceId != 0xffffffff, "TRC165: invalid interface id");
_supportedInterfaces[interfaceId] = true;
}
}
/**
* @title TRC721 Non-Fungible Token Standard basic implementation
*/
contract TRC721 is Context, TRC165, ITRC721 {
using Address for address;
using Counters for Counters.Counter;
// bytes4(keccak256("onTRC721Received(address,address,uint256,bytes)"))
bytes4 private constant _TRC721_RECEIVED = 0x5175f878;
mapping(uint256 => address) private _tokenOwner;
mapping(uint256 => address) private _tokenApprovals;
mapping(address => Counters.Counter) private _ownedTokensCount;
mapping(address => mapping(address => bool)) private _operatorApprovals;
bytes4 private constant _INTERFACE_ID_TRC721 = 0x80ac58cd;
constructor() {
_registerInterface(_INTERFACE_ID_TRC721);
}
function balanceOf(address owner) public view override returns (uint256) {
require(owner != address(0), "TRC721: balance query for the zero address");
return _ownedTokensCount[owner].current();
}
function ownerOf(uint256 tokenId) public view override returns (address) {
address owner = _tokenOwner[tokenId];
require(owner != address(0), "TRC721: owner query for nonexistent token");
return owner;
}
function approve(address to, uint256 tokenId) public override {
address owner = ownerOf(tokenId);
require(to != owner, "TRC721: approval to current owner");
require(
_msgSender() == owner || isApprovedForAll(owner, _msgSender()),
"TRC721: approve caller is not owner nor approved for all"
);
_tokenApprovals[tokenId] = to;
emit Approval(owner, to, tokenId);
}
function getApproved(uint256 tokenId) public view override returns (address) {
require(_exists(tokenId), "TRC721: approved query for nonexistent token");
return _tokenApprovals[tokenId];
}
function setApprovalForAll(address to, bool approved) public override {
require(to != _msgSender(), "TRC721: approve to caller");
_operatorApprovals[_msgSender()][to] = approved;
emit ApprovalForAll(_msgSender(), to, approved);
}
function isApprovedForAll(address owner, address operator) public view override returns (bool) {
return _operatorApprovals[owner][operator];
}
function transferFrom(address from, address to, uint256 tokenId) public override {
require(_isApprovedOrOwner(_msgSender(), tokenId), "TRC721: transfer caller is not owner nor approved");
_transferFrom(from, to, tokenId);
}
function safeTransferFrom(address from, address to, uint256 tokenId) public override {
safeTransferFrom(from, to, tokenId, "");
}
function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) public override {
require(_isApprovedOrOwner(_msgSender(), tokenId), "TRC721: transfer caller is not owner nor approved");
_safeTransferFrom(from, to, tokenId, _data);
}
function _safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) internal {
_transferFrom(from, to, tokenId);
require(_checkOnTRC721Received(from, to, tokenId, _data), "TRC721: transfer to non TRC721Receiver implementer");
}
function _exists(uint256 tokenId) internal view returns (bool) {
return _tokenOwner[tokenId] != address(0);
}
function _isApprovedOrOwner(address spender, uint256 tokenId) internal view returns (bool) {
require(_exists(tokenId), "TRC721: operator query for nonexistent token");
address owner = ownerOf(tokenId);
return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender));
}
function _safeMint(address to, uint256 tokenId) internal {
_safeMint(to, tokenId, "");
}
function _safeMint(address to, uint256 tokenId, bytes memory _data) internal {
_mint(to, tokenId);
require(_checkOnTRC721Received(address(0), to, tokenId, _data), "TRC721: transfer to non TRC721Receiver implementer");
}
function _mint(address to, uint256 tokenId) internal virtual {
require(to != address(0), "TRC721: mint to the zero address");
require(!_exists(tokenId), "TRC721: token already minted");
_tokenOwner[tokenId] = to;
_ownedTokensCount[to].increment();
emit Transfer(address(0), to, tokenId);
}
function _burn(address owner, uint256 tokenId) internal virtual {
require(ownerOf(tokenId) == owner, "TRC721: burn of token that is not own");
_clearApproval(tokenId);
_ownedTokensCount[owner].decrement();
_tokenOwner[tokenId] = address(0);
emit Transfer(owner, address(0), tokenId);
}
function _burn(uint256 tokenId) internal {
_burn(ownerOf(tokenId), tokenId);
}
function _transferFrom(address from, address to, uint256 tokenId) internal virtual {
require(ownerOf(tokenId) == from, "TRC721: transfer of token that is not own");
require(to != address(0), "TRC721: transfer to the zero address");
_clearApproval(tokenId);
_ownedTokensCount[from].decrement();
_ownedTokensCount[to].increment();
_tokenOwner[tokenId] = to;
emit Transfer(from, to, tokenId);
}
/**
* @dev Internal function to invoke {ITRC721Receiver-onTRC721Received} on a target address.
* The call is not executed if the target address is not a contract.
* [FIX H-08] isContract() is now a proper function call via Address library.
*/
function _checkOnTRC721Received(address from, address to, uint256 tokenId, bytes memory _data)
internal returns (bool)
{
if (!to.isContract()) {
return true;
}
(bool success, bytes memory returndata) = to.call(
abi.encodeWithSelector(
ITRC721Receiver(to).onTRC721Received.selector,
_msgSender(),
from,
tokenId,
_data
)
);
if (!success) {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert("TRC721: transfer to non TRC721Receiver implementer");
}
} else {
bytes4 retval = abi.decode(returndata, (bytes4));
return (retval == _TRC721_RECEIVED);
}
}
function _clearApproval(uint256 tokenId) private {
if (_tokenApprovals[tokenId] != address(0)) {
_tokenApprovals[tokenId] = address(0);
}
}
}
/**
* @title TRC721 with metadata extension
*/
abstract contract TRC721Metadata is Context, TRC165, TRC721, ITRC721Metadata {
string private _name;
string private _symbol;
string private _baseURI;
mapping(uint256 => string) private _tokenURIs;
bytes4 private constant _INTERFACE_ID_TRC721_METADATA = 0x5b5e139f;
constructor(string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
_registerInterface(_INTERFACE_ID_TRC721_METADATA);
}
function name() external view override returns (string memory) {
return _name;
}
function symbol() external view override returns (string memory) {
return _symbol;
}
function tokenURI(uint256 tokenId) external view override returns (string memory) {
require(_exists(tokenId), "TRC721Metadata: URI query for nonexistent token");
string memory _tokenURI = _tokenURIs[tokenId];
if (bytes(_tokenURI).length == 0) {
return "";
}
return string(abi.encodePacked(_baseURI, _tokenURI));
}
function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal {
require(_exists(tokenId), "TRC721Metadata: URI set of nonexistent token");
_tokenURIs[tokenId] = _tokenURI;
}
function _setBaseURI(string memory baseURI_) internal {
_baseURI = baseURI_;
}
function baseURI() external view returns (string memory) {
return _baseURI;
}
function _burn(address owner, uint256 tokenId) internal virtual override {
super._burn(owner, tokenId);
if (bytes(_tokenURIs[tokenId]).length != 0) {
delete _tokenURIs[tokenId];
}
}
}
/**
* @title TRC721MetadataMintable
*/
contract TRC721MetadataMintable is TRC721, TRC721Metadata, MinterRole {
function mintWithTokenURI(address to, uint256 tokenId, string memory tokenURI_)
public onlyMinter returns (bool)
{
_mint(to, tokenId);
_setTokenURI(tokenId, tokenURI_);
return true;
}
}
/**
* @title TRC721Mintable
*/
contract TRC721Mintable is TRC721, MinterRole {
function mint(address to, uint256 tokenId) public onlyMinter returns (bool) {
_mint(to, tokenId);
return true;
}
function safeMint(address to, uint256 tokenId) public onlyMinter returns (bool) {
_safeMint(to, tokenId);
return true;
}
function safeMint(address to, uint256 tokenId, bytes memory _data) public onlyMinter returns (bool) {
_safeMint(to, tokenId, _data);
return true;
}
}
/**
* @title TRC721 with enumeration extension
*/
contract TRC721Enumerable is Context, TRC165, TRC721, ITRC721Enumerable {
mapping(address => uint256[]) private _ownedTokens;
mapping(uint256 => uint256) private _ownedTokensIndex;
uint256[] private _allTokens;
mapping(uint256 => uint256) private _allTokensIndex;
bytes4 private constant _INTERFACE_ID_TRC721_ENUMERABLE = 0x780e9d63;
constructor() {
_registerInterface(_INTERFACE_ID_TRC721_ENUMERABLE);
}
function tokenOfOwnerByIndex(address owner, uint256 index) public view override returns (uint256) {
require(index < balanceOf(owner), "TRC721Enumerable: owner index out of bounds");
return _ownedTokens[owner][index];
}
function totalSupply() public view override returns (uint256) {
return _allTokens.length;
}
function tokenByIndex(uint256 index) public view override returns (uint256) {
require(index < totalSupply(), "TRC721Enumerable: global index out of bounds");
return _allTokens[index];
}
function _transferFrom(address from, address to, uint256 tokenId) internal override {
super._transferFrom(from, to, tokenId);
_removeTokenFromOwnerEnumeration(from, tokenId);
_addTokenToOwnerEnumeration(to, tokenId);
}
function _mint(address to, uint256 tokenId) internal virtual override {
super._mint(to, tokenId);
_addTokenToOwnerEnumeration(to, tokenId);
_addTokenToAllTokensEnumeration(tokenId);
}
function _burn(address owner, uint256 tokenId) internal virtual override {
super._burn(owner, tokenId);
_removeTokenFromOwnerEnumeration(owner, tokenId);
_ownedTokensIndex[tokenId] = 0;
_removeTokenFromAllTokensEnumeration(tokenId);
}
function _tokensOfOwner(address owner) internal view returns (uint256[] storage) {
return _ownedTokens[owner];
}
function _addTokenToOwnerEnumeration(address to, uint256 tokenId) private {
_ownedTokensIndex[tokenId] = _ownedTokens[to].length;
_ownedTokens[to].push(tokenId);
}
function _addTokenToAllTokensEnumeration(uint256 tokenId) private {
_allTokensIndex[tokenId] = _allTokens.length;
_allTokens.push(tokenId);
}
function _removeTokenFromOwnerEnumeration(address from, uint256 tokenId) private {
uint256 lastTokenIndex = _ownedTokens[from].length - 1;
uint256 tokenIndex = _ownedTokensIndex[tokenId];
if (tokenIndex != lastTokenIndex) {
uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];
_ownedTokens[from][tokenIndex] = lastTokenId;
_ownedTokensIndex[lastTokenId] = tokenIndex;
}
_ownedTokens[from].pop();
}
function _removeTokenFromAllTokensEnumeration(uint256 tokenId) private {
uint256 lastTokenIndex = _allTokens.length - 1;
uint256 tokenIndex = _allTokensIndex[tokenId];
uint256 lastTokenId = _allTokens[lastTokenIndex];
_allTokens[tokenIndex] = lastTokenId;
_allTokensIndex[lastTokenId] = tokenIndex;
_allTokens.pop();
_allTokensIndex[tokenId] = 0;
}
}
/**
* @title TRC721Token
* @dev Full TRC-721 token implementation with enumeration and mintable metadata.
* Replace "Your Token Name" and "YTN" with your token's name and symbol.
*/
contract TRC721Token is TRC721Enumerable, TRC721MetadataMintable {
constructor() TRC721Metadata("Your Token Name", "YTN") {}
}Updated 10 days ago