Verified contractExactUnlicense
CheapMintNFT
Address0x4ef77d2992d555ce3ce84a243732710dc2aee1d1
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.12
- Same code
Only this contract
Details
- Compiler
- solc 0.8.12+commit.f00d7308
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- london
- License
- Unlicense
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 15,093,805
- Creation transaction
- 0x8432a66f1f…bee06f6392
contracts/CheapMintNFT.sol
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.12;
import "./lib/CheapMintNFTBytesLib.sol";
import "./interfaces/ICheapMintNFT.sol";
import "./interfaces/ICheapSwapAddress.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
contract CheapMintNFT is ICheapMintNFT {
using CheapMintNFTBytesLib for bytes;
constructor() {}
/* =================== VIEW FUNCTIONS =================== */
function getMintData(bytes calldata msgData, uint256 msgValue)
public
pure
override
returns (
uint80 callMsgValue,
// 目标地址
address target,
// value
uint80 value,
// 选择器
uint32 selector,
// mint数量
uint8 mintAmount
)
{
callMsgValue = msgData.toUint80(4);
target = msgData.toAddress(14);
if (msgValue > 0) {
value = msgData.toUint80(34);
selector = msgData.toUint32(44);
mintAmount = msgData.toUint8(48);
} else {
selector = msgData.toUint32(34);
mintAmount = msgData.toUint8(38);
}
}
/* ================ TRANSACTION FUNCTIONS ================ */
function mint() external payable override {
unchecked {
(, address target, uint80 value, uint32 selector, uint8 mintAmount) = getMintData(msg.data, msg.value);
// 兼容 msg.sender
address owner = msg.sender;
if (msg.sender != tx.origin) {
ICheapSwapAddress cheapSwapAddress = ICheapSwapAddress(msg.sender);
owner = cheapSwapAddress.owner();
}
// 模拟 mint,获取 startTokenId
(bool success, bytes memory returnData) = target.call{value: value}(
abi.encodePacked(selector, uint256(mintAmount))
);
require(!success, "CheapMintNFT: can not get startTokenId");
uint256 startTokenId = returnData.toUint32(returnData.length - 4);
uint256 thisGas = gasleft();
uint256 beforeGas;
uint256 useGas;
// 检查 gas 和 balance
while (thisGas >= useGas && address(this).balance >= value) {
beforeGas = thisGas;
new MintNFT{value: value}(owner, startTokenId, target, selector, mintAmount);
startTokenId += mintAmount;
thisGas = gasleft();
useGas = beforeGas - thisGas;
}
// 返回 value
if (address(this).balance > 0) {
payable(owner).transfer(address(this).balance);
}
}
}
// 收到 ERC721,抛出tokenId
function onERC721Received(
address,
address,
uint256 tokenId,
bytes calldata
) external pure override returns (bytes4) {
revert(string(abi.encodePacked(tokenId)));
}
}
contract MintNFT {
constructor(
address owner,
uint256 startTokenId,
address target,
uint32 selector,
uint256 mintAmount
) payable {
unchecked {
// 执行 mint
(bool success, ) = target.call{value: msg.value}(abi.encodePacked(selector, mintAmount));
require(success, "cheapMintNFT: mint NFT error");
// 发送 ERC721 给 owner
IERC721 nft = IERC721(target);
uint256 maxTokenId = startTokenId + mintAmount;
for (; startTokenId < maxTokenId; ++startTokenId) {
nft.transferFrom(address(this), owner, startTokenId);
}
// 合约自毁
selfdestruct(payable(msg.sender));
}
}
}
@openzeppelin/contracts/token/ERC721/IERC721.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../../utils/introspection/IERC165.sol";
/**
* @dev Required interface of an ERC721 compliant contract.
*/
interface IERC721 is IERC165 {
/**
* @dev Emitted when `tokenId` token is transferred from `from` to `to`.
*/
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
*/
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
*/
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
/**
* @dev Returns the number of tokens in ``owner``'s account.
*/
function balanceOf(address owner) external view returns (uint256 balance);
/**
* @dev Returns the owner of the `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function ownerOf(uint256 tokenId) external view returns (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.
*/
function safeTransferFrom(
address from,
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.
*/
function transferFrom(
address from,
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.
*/
function approve(address to, uint256 tokenId) external;
/**
* @dev Returns the account approved for `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function getApproved(uint256 tokenId) external view returns (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.
*/
function setApprovalForAll(address operator, bool _approved) external;
/**
* @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
*
* See {setApprovalForAll}
*/
function isApprovedForAll(address owner, address operator) external view returns (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.
*/
function safeTransferFrom(
address from,
address to,
uint256 tokenId,
bytes calldata data
) external;
}
@openzeppelin/contracts/utils/introspection/IERC165.sol
// SPDX-License-Identifier: MIT
pragma solidity ^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}.
*/
interface IERC165 {
/**
* @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.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
contracts/interfaces/ICheapMintNFT.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;
interface ICheapMintNFT {
/* =================== VIEW FUNCTIONS =================== */
function getMintData(bytes calldata msgData, uint256 msgValue)
external
pure
returns (
uint80 callMsgValue,
address target,
uint80 value,
uint32 selector,
uint8 mintAmount
);
/* ================ TRANSACTION FUNCTIONS ================ */
function mint() external payable;
function onERC721Received(
address operator,
address from,
uint256 tokenId,
bytes calldata data
) external returns (bytes4);
}
contracts/interfaces/ICheapSwapAddress.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;
interface ICheapSwapAddress {
/* ==================== EVENTS =================== */
event SetTargetData(uint256 indexed value, bytes targetData);
event SetPause(bool isPause);
/* ==================== VIEW FUNCTIONS =================== */
function owner() external view returns (address);
function getTargetData(uint256 msgValue)
external
view
returns (
uint8 runTime,
uint8 maxRunTime,
uint40 deadline,
address target,
uint80 value,
bytes memory data
);
/* ================ TRANSACTION FUNCTIONS ================ */
function doReceive() external payable;
function call(
uint256 callMsgValue,
address target,
bytes calldata data
) external payable;
/* ===================== ADMIN FUNCTIONS ==================== */
function setPause(bool isPause) external;
function setTargetData(
uint256 msgValue,
uint8 maxRunTime,
uint40 deadline,
address target,
uint80 value,
bytes calldata data
) external;
}
contracts/lib/CheapMintNFTBytesLib.sol
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* @title Solidity Bytes Arrays Utils
* @author Gonçalo Sá <[email protected]>
*
* @dev Bytes tightly packed arrays utility library for ethereum contracts written in Solidity.
* The library lets you concatenate, slice and type cast bytes arrays both in memory and storage.
*/
pragma solidity >=0.5.0;
library CheapMintNFTBytesLib {
function slice(
bytes memory _bytes,
uint256 _start,
uint256 _length
) internal pure returns (bytes memory) {
require(_length + 31 >= _length, "slice_overflow");
require(_start + _length >= _start, "slice_overflow");
require(_bytes.length >= _start + _length, "slice_outOfBounds");
bytes memory tempBytes;
assembly {
switch iszero(_length)
case 0 {
// Get a location of some free memory and store it in tempBytes as
// Solidity does for memory variables.
tempBytes := mload(0x40)
// The first word of the slice result is potentially a partial
// word read from the original array. To read it, we calculate
// the length of that partial word and start copying that many
// bytes into the array. The first word we copy will start with
// data we don't care about, but the last `lengthmod` bytes will
// land at the beginning of the contents of the new array. When
// we're done copying, we overwrite the full first word with
// the actual length of the slice.
let lengthmod := and(_length, 31)
// The multiplication in the next line is necessary
// because when slicing multiples of 32 bytes (lengthmod == 0)
// the following copy loop was copying the origin's length
// and then ending prematurely not copying everything it should.
let mc := add(add(tempBytes, lengthmod), mul(0x20, iszero(lengthmod)))
let end := add(mc, _length)
for {
// The multiplication in the next line has the same exact purpose
// as the one above.
let cc := add(add(add(_bytes, lengthmod), mul(0x20, iszero(lengthmod))), _start)
} lt(mc, end) {
mc := add(mc, 0x20)
cc := add(cc, 0x20)
} {
mstore(mc, mload(cc))
}
mstore(tempBytes, _length)
//update free-memory pointer
//allocating the array padded to 32 bytes like the compiler does now
mstore(0x40, and(add(mc, 31), not(31)))
}
//if we want a zero-length slice let's just return a zero-length array
default {
tempBytes := mload(0x40)
//zero out the 32 bytes slice we are about to return
//we need to do it because Solidity does not garbage collect
mstore(tempBytes, 0)
mstore(0x40, add(tempBytes, 0x20))
}
}
return tempBytes;
}
function toAddress(bytes memory _bytes, uint256 _start) internal pure returns (address) {
require(_start + 20 >= _start, "toAddress_overflow");
require(_bytes.length >= _start + 20, "toAddress_outOfBounds");
address tempAddress;
assembly {
tempAddress := div(mload(add(add(_bytes, 0x20), _start)), 0x1000000000000000000000000)
}
return tempAddress;
}
function toUint8(bytes memory _bytes, uint256 _start) internal pure returns (uint8) {
require(_start + 1 >= _start, "toUint8_overflow");
require(_bytes.length >= _start + 1, "toUint8_outOfBounds");
uint8 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0x1), _start))
}
return tempUint;
}
function toUint32(bytes memory _bytes, uint256 _start) internal pure returns (uint32) {
require(_start + 4 >= _start, "toUint32_overflow");
require(_bytes.length >= _start + 4, "toUint32_outOfBounds");
uint32 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0x4), _start))
}
return tempUint;
}
function toUint80(bytes memory _bytes, uint256 _start) internal pure returns (uint80) {
require(_start + 10 >= _start, "toUint80_overflow");
require(_bytes.length >= _start + 10, "toUint80_outOfBounds");
uint80 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0xa), _start))
}
return tempUint;
}
}
Compiler settingsShow
{
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "london",
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.