// SPDX-License-Identifier: MITpragmasolidity >=0.6.2 <0.8.0;/**
* @dev Collection of functions related to the address type
*/libraryAddress{
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
* ====
*/functionisContract(address account) internalviewreturns (bool) {
// This method relies on extcodesize, which returns 0 for contracts in// construction, since the code is only stored at the end of the// constructor execution.uint256 size;
// solhint-disable-next-line no-inline-assemblyassembly { size :=extcodesize(account) }
return size >0;
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/functionsendValue(addresspayable recipient, uint256 amount) internal{
require(address(this).balance>= amount, "Address: insufficient balance");
// solhint-disable-next-line avoid-low-level-calls, avoid-call-value
(bool success, ) = recipient.call{ value: amount }("");
require(success, "Address: unable to send value, recipient may have reverted");
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain`call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason, it is bubbled up by this
* function (like regular Solidity function calls).
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*
* _Available since v3.1._
*/functionfunctionCall(address target, bytesmemory data) internalreturns (bytesmemory) {
return functionCall(target, data, "Address: low-level call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
* `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/functionfunctionCall(address target, bytesmemory data, stringmemory errorMessage) internalreturns (bytesmemory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*
* _Available since v3.1._
*/functionfunctionCallWithValue(address target, bytesmemory data, uint256 value) internalreturns (bytesmemory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
/**
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
* with `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/functionfunctionCallWithValue(address target, bytesmemory data, uint256 value, stringmemory errorMessage) internalreturns (bytesmemory) {
require(address(this).balance>= value, "Address: insufficient balance for call");
require(isContract(target), "Address: call to non-contract");
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytesmemory returndata) = target.call{ value: value }(data);
return _verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/functionfunctionStaticCall(address target, bytesmemory data) internalviewreturns (bytesmemory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/functionfunctionStaticCall(address target, bytesmemory data, stringmemory errorMessage) internalviewreturns (bytesmemory) {
require(isContract(target), "Address: static call to non-contract");
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytesmemory returndata) = target.staticcall(data);
return _verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/functionfunctionDelegateCall(address target, bytesmemory data) internalreturns (bytesmemory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/functionfunctionDelegateCall(address target, bytesmemory data, stringmemory errorMessage) internalreturns (bytesmemory) {
require(isContract(target), "Address: delegate call to non-contract");
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytesmemory returndata) = target.delegatecall(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function_verifyCallResult(bool success, bytesmemory returndata, stringmemory errorMessage) privatepurereturns(bytesmemory) {
if (success) {
return returndata;
} else {
// Look for revert reason and bubble it up if presentif (returndata.length>0) {
// The easiest way to bubble the revert reason is using memory via assembly// solhint-disable-next-line no-inline-assemblyassembly {
let returndata_size :=mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
Contract Source Code
File 2 of 11: Context.sol
// SPDX-License-Identifier: MITpragmasolidity >=0.6.0 <0.8.0;/*
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with GSN meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/abstractcontractContext{
function_msgSender() internalviewvirtualreturns (addresspayable) {
returnmsg.sender;
}
function_msgData() internalviewvirtualreturns (bytesmemory) {
this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691returnmsg.data;
}
}
Contract Source Code
File 3 of 11: ERC165.sol
// SPDX-License-Identifier: MITpragmasolidity >=0.6.0 <0.8.0;import"IERC165.sol";
/**
* @dev Implementation of the {IERC165} interface.
*
* Contracts may inherit from this and call {_registerInterface} to declare
* their support of an interface.
*/abstractcontractERC165isIERC165{
/*
* bytes4(keccak256('supportsInterface(bytes4)')) == 0x01ffc9a7
*/bytes4privateconstant _INTERFACE_ID_ERC165 =0x01ffc9a7;
/**
* @dev Mapping of interface ids to whether or not it's supported.
*/mapping(bytes4=>bool) private _supportedInterfaces;
constructor () internal{
// Derived contracts need only register support for their own interfaces,// we register support for ERC165 itself here
_registerInterface(_INTERFACE_ID_ERC165);
}
/**
* @dev See {IERC165-supportsInterface}.
*
* Time complexity O(1), guaranteed to always use less than 30 000 gas.
*/functionsupportsInterface(bytes4 interfaceId) publicviewvirtualoverridereturns (bool) {
return _supportedInterfaces[interfaceId];
}
/**
* @dev Registers the contract as an implementer of the interface defined by
* `interfaceId`. Support of the actual ERC165 interface is automatic and
* registering its interface id is not required.
*
* See {IERC165-supportsInterface}.
*
* Requirements:
*
* - `interfaceId` cannot be the ERC165 invalid interface (`0xffffffff`).
*/function_registerInterface(bytes4 interfaceId) internalvirtual{
require(interfaceId !=0xffffffff, "ERC165: invalid interface id");
_supportedInterfaces[interfaceId] =true;
}
}
Contract Source Code
File 4 of 11: IERC165.sol
// SPDX-License-Identifier: MITpragmasolidity >=0.6.0 <0.8.0;/**
* @dev Interface of the ERC165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[EIP].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/interfaceIERC165{
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/functionsupportsInterface(bytes4 interfaceId) externalviewreturns (bool);
}
Contract Source Code
File 5 of 11: IERC721.sol
// SPDX-License-Identifier: MITpragmasolidity >=0.6.2 <0.8.0;import"IERC165.sol";
/**
* @dev Required interface of an ERC721 compliant contract.
*/interfaceIERC721isIERC165{
/**
* @dev Emitted when `tokenId` token is transferred from `from` to `to`.
*/eventTransfer(addressindexedfrom, addressindexed to, uint256indexed tokenId);
/**
* @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
*/eventApproval(addressindexed owner, addressindexed approved, uint256indexed tokenId);
/**
* @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
*/eventApprovalForAll(addressindexed owner, addressindexed operator, bool approved);
/**
* @dev Returns the number of tokens in ``owner``'s account.
*/functionbalanceOf(address owner) externalviewreturns (uint256 balance);
/**
* @dev Returns the owner of the `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/functionownerOf(uint256 tokenId) externalviewreturns (address owner);
/**
* @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.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must be have been allowed to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
*
* Emits a {Transfer} event.
*/functionsafeTransferFrom(addressfrom, address to, uint256 tokenId) external;
/**
* @dev Transfers `tokenId` token from `from` to `to`.
*
* WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
*
* Emits a {Transfer} event.
*/functiontransferFrom(addressfrom, address to, uint256 tokenId) external;
/**
* @dev Gives permission to `to` to transfer `tokenId` token to another account.
* The approval is cleared when the token is transferred.
*
* Only a single account can be approved at a time, so approving the zero address clears previous approvals.
*
* Requirements:
*
* - The caller must own the token or be an approved operator.
* - `tokenId` must exist.
*
* Emits an {Approval} event.
*/functionapprove(address to, uint256 tokenId) external;
/**
* @dev Returns the account approved for `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/functiongetApproved(uint256 tokenId) externalviewreturns (address operator);
/**
* @dev Approve or remove `operator` as an operator for the caller.
* Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
*
* Requirements:
*
* - The `operator` cannot be the caller.
*
* Emits an {ApprovalForAll} event.
*/functionsetApprovalForAll(address operator, bool _approved) external;
/**
* @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
*
* See {setApprovalForAll}
*/functionisApprovedForAll(address owner, address operator) externalviewreturns (bool);
/**
* @dev Safely transfers `tokenId` token from `from` to `to`.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
*
* Emits a {Transfer} event.
*/functionsafeTransferFrom(addressfrom, address to, uint256 tokenId, bytescalldata data) external;
}
// SPDX-License-Identifier: MITpragmasolidity >=0.6.0 <0.8.0;/**
* @title ERC721 token receiver interface
* @dev Interface for any contract that wants to support safeTransfers
* from ERC721 asset contracts.
*/interfaceIERC721Receiver{
/**
* @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
* by `operator` from `from`, this function is called.
*
* It must return its Solidity selector to confirm the token transfer.
* If any other value is returned or the interface is not implemented by the recipient, the transfer will be reverted.
*
* The selector can be obtained in Solidity with `IERC721.onERC721Received.selector`.
*/functiononERC721Received(address operator, addressfrom, uint256 tokenId, bytescalldata data) externalreturns (bytes4);
}
Contract Source Code
File 8 of 11: IStarNFT.sol
/*
Copyright 2021 Project Galaxy.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache License, Version 2.0
*/pragmasolidity 0.7.6;/**
* @title IStarNFT
* @author Galaxy Protocol
*
* Interface for operating with StarNFTs.
*/interfaceIStarNFT{
/* ============ Events =============== *//* ============ Functions ============ */functionisOwnerOf(address, uint256) externalviewreturns (bool);
functiongetNumMinted() externalviewreturns (uint256);
// mintfunctionmint(address account, uint256 powah) externalreturns (uint256);
functionmintBatch(address account, uint256 amount, uint256[] calldata powahArr) externalreturns (uint256[] memory);
functionburn(address account, uint256 id) external;
functionburnBatch(address account, uint256[] calldata ids) external;
}
Contract Source Code
File 9 of 11: Ownable.sol
// SPDX-License-Identifier: MITpragmasolidity >=0.6.0 <0.8.0;import"Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/abstractcontractOwnableisContext{
addressprivate _owner;
eventOwnershipTransferred(addressindexed previousOwner, addressindexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/constructor () internal{
address msgSender = _msgSender();
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
}
/**
* @dev Returns the address of the current owner.
*/functionowner() publicviewvirtualreturns (address) {
return _owner;
}
/**
* @dev Throws if called by any account other than the owner.
*/modifieronlyOwner() {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
_;
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/functionrenounceOwnership() publicvirtualonlyOwner{
emit OwnershipTransferred(_owner, address(0));
_owner =address(0);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/functiontransferOwnership(address newOwner) publicvirtualonlyOwner{
require(newOwner !=address(0), "Ownable: new owner is the zero address");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}
Contract Source Code
File 10 of 11: StarNFTV4.sol
/*
Copyright 2022 Project Galaxy.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache License, Version 2.0
*/pragmasolidity 0.7.6;import"IERC721.sol";
import"IERC721Receiver.sol";
import"IERC721Metadata.sol";
import"Address.sol";
import"Strings.sol";
import"Ownable.sol";
import"ERC165.sol";
import"IStarNFT.sol";
/**
* @dev Fork https://github.com/generalgalactic/ERC721S and implement IStarNFT interface
*/contractStarNFTV4isOwnable, ERC165, IERC721, IERC721Metadata, IStarNFT{
usingAddressforaddress;
usingStringsforuint256;
/* ============ State Variables ============ */structStar {
uint160 owner;
uint96 cid; // Max value is 7.9E28, enough to store campaign id
}
// Token namestringprivate _name;
// Token symbolstringprivate _symbol;
// Total number of tokens burneduint256private _burnCount;
// Array of all tokens storing the owner's address and the campaign id
Star[] private _tokens;
// Mapping owner address to token countmapping(address=>uint256) private _balances;
// Mapping from token ID to approved addressmapping(uint256=>address) private _tokenApprovals;
// Mapping from owner to operator approvalsmapping(address=>mapping(address=>bool)) private _operatorApprovals;
// Mint and burn star.mapping(address=>bool) private _minters;
// Default allow transferboolprivate _transferable =true;
// Base token URIstringprivate _baseURI;
/* ============ Events ============ */// Add new mintereventEventMinterAdded(addressindexed newMinter);
// Remove old mintereventEventMinterRemoved(addressindexed oldMinter);
/* ============ Modifiers ============ *//**
* Only minter.
*/modifieronlyMinter() {
require(_minters[msg.sender], "StarNFT: must be minter");
_;
}
/**
* Only allow transfer.
*/modifieronlyTransferable() {
require(_transferable, "StarNFT: must transferable");
_;
}
/**
* @dev Initializes the contract
*/constructor() {
// Initialize zero index value
Star memory _star = Star(0, 0);
_tokens.push(_star);
}
/**
* @dev See {IERC165-supportsInterface}.
*/functionsupportsInterface(bytes4 interfaceId)
publicviewoverride(ERC165, IERC165)
returns (bool)
{
return
interfaceId ==type(IERC721).interfaceId||
interfaceId ==type(IERC721Metadata).interfaceId||
interfaceId ==type(IStarNFT).interfaceId||super.supportsInterface(interfaceId);
}
/**
* @dev Is this address a minters.
*/functionminters(address account) publicviewreturns (bool) {
return _minters[account];
}
/**
* @dev Is this contract allow nft transfer.
*/functiontransferable() publicviewreturns (bool) {
return _transferable;
}
/**
* @dev Returns the base URI for nft.
*/functionbaseURI() publicviewreturns (stringmemory) {
return _baseURI;
}
/**
* @dev Get Star NFT CID.
*/functioncid(uint256 tokenId) publicviewreturns (uint256) {
require(_exists(tokenId), "StarNFT: StarNFT does not exist");
return _tokens[tokenId].cid;
}
/**
* @dev Get Star NFT owner
*/functiongetNumMinted() publicviewoverridereturns (uint256) {
return _tokens.length-1;
}
/**
* @dev See {IERC721Enumerable-totalSupply}.
*/functiontotalSupply() publicviewreturns (uint256) {
return getNumMinted() - _burnCount;
}
/**
* @dev See {IERC721Enumerable-tokenOfOwnerByIndex}.
* This is implementation is O(n) and should not be
* called by other contracts.
*/functiontokenOfOwnerByIndex(address owner, uint256 index)
publicviewreturns (uint256)
{
uint256 currentIndex =0;
for (uint256 i =1; i < _tokens.length; i++) {
if (isOwnerOf(owner, i)) {
if (currentIndex == index) {
return i;
}
currentIndex +=1;
}
}
revert("ERC721Enumerable: owner index out of bounds");
}
/**
* @dev See {IERC721-balanceOf}.
*/functionbalanceOf(address owner)
publicviewoverridereturns (uint256)
{
require(
owner !=address(0),
"ERC721: balance query for the zero address"
);
return _balances[owner];
}
/**
* @dev See {IERC721-ownerOf}.
*/functionownerOf(uint256 tokenId)
publicviewoverridereturns (address)
{
require(
_exists(tokenId),
"ERC721: owner query for nonexistent token"
);
returnaddress(_tokens[tokenId].owner);
}
/**
* @dev See {IStarNFT-isOwnerOf}.
*/functionisOwnerOf(address account, uint256 id)
publicviewoverridereturns (bool)
{
address owner = ownerOf(id);
return owner == account;
}
/**
* @dev See {IERC721Metadata-name}.
*/functionname() publicviewvirtualoverridereturns (stringmemory) {
return _name;
}
/**
* @dev See {IERC721Metadata-symbol}.
*/functionsymbol() publicviewvirtualoverridereturns (stringmemory) {
return _symbol;
}
/**
* @dev See {IERC721Metadata-tokenURI}.
*/functiontokenURI(uint256 tokenId)
publicviewoverridereturns (stringmemory)
{
require(
_exists(tokenId),
"ERC721Metadata: URI query for nonexistent token"
);
returnbytes(_baseURI).length>0
? string(abi.encodePacked(_baseURI, tokenId.toString(), ".json"))
: "";
}
/**
* @dev See {IERC721-approve}.
*/functionapprove(address to, uint256 tokenId) publicoverride{
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}.
*/functiongetApproved(uint256 tokenId)
publicviewoverridereturns (address)
{
require(
_exists(tokenId),
"ERC721: approved query for nonexistent token"
);
return _tokenApprovals[tokenId];
}
/**
* @dev See {IERC721-setApprovalForAll}.
*/functionsetApprovalForAll(address operator, bool approved)
publicoverride{
require(operator != _msgSender(), "ERC721: approve to caller");
_operatorApprovals[_msgSender()][operator] = approved;
emit ApprovalForAll(_msgSender(), operator, approved);
}
/**
* @dev See {IERC721-isApprovedForAll}.
*/functionisApprovedForAll(address owner, address operator)
publicviewoverridereturns (bool)
{
return _operatorApprovals[owner][operator];
}
/**
* @dev See {IERC721-transferFrom}.
*/functiontransferFrom(addressfrom,
address to,
uint256 tokenId
) publiconlyTransferableoverride{
//solhint-disable-next-line max-line-lengthrequire(
_isApprovedOrOwner(_msgSender(), tokenId),
"ERC721: transfer caller is not owner nor approved"
);
_transfer(from, to, tokenId);
}
/**
* @dev See {IERC721-safeTransferFrom}.
*/functionsafeTransferFrom(addressfrom,
address to,
uint256 tokenId
) publiconlyTransferableoverride{
safeTransferFrom(from, to, tokenId, "");
}
/**
* @dev See {IERC721-safeTransferFrom}.
*/functionsafeTransferFrom(addressfrom,
address to,
uint256 tokenId,
bytesmemory _data
) publiconlyTransferableoverride{
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(addressfrom,
address to,
uint256 tokenId,
bytesmemory _data
) internal{
_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) internalviewreturns (bool) {
return tokenId >0&& tokenId <= getNumMinted() && _tokens[tokenId].owner !=0x0;
}
/**
* @dev Returns whether `spender` is allowed to manage `tokenId`.
*
* Requirements:
*
* - `tokenId` must exist.
*/function_isApprovedOrOwner(address spender, uint256 tokenId)
internalviewreturns (bool)
{
address owner = ownerOf(tokenId);
return (spender == owner ||
getApproved(tokenId) == spender ||
isApprovedForAll(owner, spender));
}
/* ============ External Functions ============ */functionmint(address account, uint256 cid)
externaloverrideonlyMinterreturns (uint256)
{
require(account !=address(0), "StarNFT: mint to the zero address");
uint256 tokenId = _tokens.length;
Star memory star = Star(uint160(account), uint96(cid));
_balances[account] +=1;
_tokens.push(star);
require(
_checkOnERC721Received(address(0), account, tokenId, ""),
"StarNFT: transfer to non ERC721Receiver implementer"
);
emit Transfer(address(0), account, tokenId);
return tokenId;
}
functionmintBatch(address account,
uint256 amount,
uint256[] calldata cidArr
) externaloverrideonlyMinterreturns (uint256[] memory) {
require(account !=address(0), "StarNFT: mint to the zero address");
uint256[] memory ids =newuint256[](amount);
_balances[account] += amount;
for (uint256 i =0; i < ids.length; i++) {
uint256 tokenId = _tokens.length;
Star memory star = Star(uint160(account), uint96(cidArr[i]));
ids[i] = tokenId;
_tokens.push(star);
require(
_checkOnERC721Received(address(0), account, tokenId, ""),
"StarNFT: transfer to non ERC721Receiver implementer"
);
emit Transfer(address(0), account, tokenId);
}
return ids;
}
functionburn(address account, uint256 id) externaloverrideonlyMinter{
require(
_isApprovedOrOwner(_msgSender(), id),
"StarNFT: caller is not approved or owner"
);
require(isOwnerOf(account, id), "StarNFT: not owner");
// Clear approvals
_approve(address(0), id);
_burnCount++;
_balances[account] -=1;
_tokens[id].owner =0;
_tokens[id].cid =0;
emit Transfer(account, address(0), id);
}
functionburnBatch(address account, uint256[] calldata ids)
externaloverrideonlyMinter{
_burnCount += ids.length;
_balances[account] -= ids.length;
for (uint256 i =0; i < ids.length; i++) {
uint256 tokenId = ids[i];
require(
_isApprovedOrOwner(_msgSender(), tokenId),
"StarNFT: caller is not approved or owner"
);
require(isOwnerOf(account, tokenId), "StarNFT: not owner");
// Clear approvals
_approve(address(0), tokenId);
_tokens[tokenId].owner =0;
_tokens[tokenId].cid =0;
emit Transfer(account, 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(addressfrom,
address to,
uint256 tokenId
) internalvirtual{
require(
isOwnerOf(from, tokenId),
"ERC721: transfer of token that is not own"
);
require(to !=address(0), "ERC721: transfer to the zero address");
// Clear approvals from the previous owner
_approve(address(0), tokenId);
_balances[from] -=1;
_balances[to] +=1;
_tokens[tokenId].owner =uint160(to);
emit Transfer(from, to, tokenId);
}
/**
* @dev Approve `to` to operate on `tokenId`
*
* Emits a {Approval} event.
*/function_approve(address to, uint256 tokenId) internalvirtual{
_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(addressfrom,
address to,
uint256 tokenId,
bytesmemory _data
) privatereturns (bool) {
if (to.isContract()) {
try
IERC721Receiver(to).onERC721Received(
_msgSender(),
from,
tokenId,
_data
)
returns (bytes4 retval) {
return retval == IERC721Receiver.onERC721Received.selector;
} catch (bytesmemory reason) {
if (reason.length==0) {
revert(
"ERC721: transfer to non ERC721Receiver implementer"
);
} else {
assembly {
revert(add(32, reason), mload(reason))
}
}
}
}
returntrue;
}
/* ============ Util Functions ============ *//**
* @dev Sets a new baseURI for all token types.
*/functionsetURI(stringcalldata newURI) externalonlyOwner{
_baseURI = newURI;
}
/**
* @dev Sets a new transferable for all token types.
*/functionsetTransferable(bool transferable) externalonlyOwner{
_transferable = transferable;
}
/**
* @dev Sets a new name for all token types.
*/functionsetName(stringcalldata newName) externalonlyOwner{
_name = newName;
}
/**
* @dev Sets a new symbol for all token types.
*/functionsetSymbol(stringcalldata newSymbol) externalonlyOwner{
_symbol = newSymbol;
}
/**
* @dev Add a new minter.
*/functionaddMinter(address minter) externalonlyOwner{
require(minter !=address(0), "minter must not be null address");
require(!_minters[minter], "minter already added");
_minters[minter] =true;
emit EventMinterAdded(minter);
}
/**
* @dev Remove a old minter.
*/functionremoveMinter(address minter) externalonlyOwner{
require(_minters[minter], "minter does not exist");
delete _minters[minter];
emit EventMinterRemoved(minter);
}
}
Contract Source Code
File 11 of 11: Strings.sol
// SPDX-License-Identifier: MITpragmasolidity >=0.6.0 <0.8.0;/**
* @dev String operations.
*/libraryStrings{
/**
* @dev Converts a `uint256` to its ASCII `string` representation.
*/functiontoString(uint256 value) internalpurereturns (stringmemory) {
// Inspired by OraclizeAPI's implementation - MIT licence// https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.solif (value ==0) {
return"0";
}
uint256 temp = value;
uint256 digits;
while (temp !=0) {
digits++;
temp /=10;
}
bytesmemory buffer =newbytes(digits);
uint256 index = digits -1;
temp = value;
while (temp !=0) {
buffer[index--] =bytes1(uint8(48+ temp %10));
temp /=10;
}
returnstring(buffer);
}
}