荒唐的NFT(二) - Solidity 写合约

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// SPDX-License-Identifier: GPL-3.0

//Developer : FazelPejmanfar , Twitter :@Pejmanfarfazel

/*
This a Free Contract, DO NOT SELL IT
*/

pragma solidity >=0.7.0 <0.9.0;

import "erc721a/contracts/ERC721A.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
// 有助于防止对函数的可重入调用
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
// 白名单
// import "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";
import "@openzeppelin/contracts/utils/Strings.sol";

contract WorldCupRabbitNFT is ERC721A, Ownable, ReentrancyGuard {
// uint256 类型 可调用 String 的方法 .toString之类的
using Strings for uint256;

string public baseURI;
string public baseExtension = ".json";
// 未开售图片
// string public notRevealedUri;
// 单价
uint256 public cost = 0.041 ether;
// 最大供应量
uint256 public maxSupply = 400;
// 免费供应量
// uint256 public FreeSupply = 2500;
// 最大付费mint
// uint256 public MaxperWallet = 10;
// 最大免费mint
// uint256 public MaxperWalletFree = 3;
bool public paused = false;
// bool public revealed = false;

modifier callerIsUser() {
require(tx.origin == msg.sender, "The caller is another contract");
_;
}

// constructor(string memory _initBaseURI, string memory _notRevealedUri)
constructor(string memory _initBaseURI)
ERC721A("WorldCupRabbitNFT", "Rabbit")
{
// change the name and symbol for your collection
setBaseURI(_initBaseURI);
// setNotRevealedURI(_notRevealedUri);
}

// internal
function _baseURI() internal view virtual override returns (string memory) {
return baseURI;
}

function _startTokenId() internal view virtual override returns (uint256) {
return 1;
}

// public
/// @dev Public mint
function mint(uint256 tokens) public payable nonReentrant callerIsUser {
require(!paused, "oops contract is paused");
// require(tokens <= MaxperWallet, "max mint amount per tx exceeded");
require(totalSupply() + tokens <= maxSupply, "We Soldout");
// require(
// _numberMinted(_msgSenderERC721A()) + tokens <= MaxperWallet,
// "Max NFT Per Wallet exceeded"
// );
require(msg.value >= cost * tokens, "insufficient funds");

_safeMint(_msgSenderERC721A(), tokens);
}

/// @dev free mint
// function freemint(uint256 tokens) public nonReentrant callerIsUser {
// require(!paused, "oops contract is paused");
// require(totalSupply() + tokens <= maxSupply, "We Soldout");
// require(
// _numberMinted(_msgSenderERC721A()) + tokens <= MaxperWalletFree,
// "Max NFT Per Wallet exceeded"
// );
// require(tokens <= MaxperWalletFree, "max mint per Tx exceeded");
// require(
// totalSupply() + tokens <= FreeSupply,
// "Whitelist MaxSupply exceeded"
// );

// _safeMint(_msgSenderERC721A(), tokens);
// }

/// @dev use it for giveaway and team mint
// function airdrop(uint256 _mintAmount, address destination)
// public
// onlyOwner
// nonReentrant
// callerIsUser
// {
// require(
// totalSupply() + _mintAmount <= maxSupply,
// "max NFT limit exceeded"
// );

// _safeMint(destination, _mintAmount);
// }

/// @notice returns metadata link of tokenid 重写内部 ERC721A 的 tokenURI 方法
function tokenURI(uint256 tokenId)
public
view
virtual
override
returns (string memory)
{
require(
_exists(tokenId),
"ERC721AMetadata: URI query for nonexistent token"
);

// if (revealed == false) {
// return notRevealedUri;
// }

string memory currentBaseURI = _baseURI();
return
bytes(currentBaseURI).length > 0
? string(
abi.encodePacked(
currentBaseURI,
tokenId.toString(),
baseExtension
)
)
: "";
}

/// @notice return the number minted by an address
// function numberMinted(address owner) public view returns (uint256) {
// return _numberMinted(owner);
// }

/// @notice return the tokens owned by an address
// function tokensOfOwner(address owner)
// public
// view
// returns (uint256[] memory)
// {
// unchecked {
// uint256 tokenIdsIdx;
// address currOwnershipAddr;
// uint256 tokenIdsLength = balanceOf(owner);
// uint256[] memory tokenIds = new uint256[](tokenIdsLength);
// TokenOwnership memory ownership;
// for (
// uint256 i = _startTokenId();
// tokenIdsIdx != tokenIdsLength;
// ++i
// ) {
// ownership = _ownershipAt(i);
// if (ownership.burned) {
// continue;
// }
// if (ownership.addr != address(0)) {
// currOwnershipAddr = ownership.addr;
// }
// if (currOwnershipAddr == owner) {
// tokenIds[tokenIdsIdx++] = i;
// }
// }
// return tokenIds;
// }
// }

//only owner
// function reveal(bool _state) public onlyOwner {
// revealed = _state;
// }

/// @dev change the public max per wallet
// function setMaxPerWallet(uint256 _limit) public onlyOwner {
// MaxperWallet = _limit;
// }

/// @dev change the free max per wallet
// function setFreeMaxPerWallet(uint256 _limit) public onlyOwner {
// MaxperWalletFree = _limit;
// }

/// @dev change the public price(amount need to be in wei)
// function setCost(uint256 _newCost) public onlyOwner {
// cost = _newCost;
// }

/// @dev cut the supply if we dont sold out
// function setMaxsupply(uint256 _newsupply) public onlyOwner {
// maxSupply = _newsupply;
// }

/// @dev cut the free supply
// function setFreesupply(uint256 _newsupply) public onlyOwner {
// FreeSupply = _newsupply;
// }

/// @dev set base extension(default is .json)
// function setBaseExtension(string memory _newBaseExtension)
// public
// onlyOwner
// {
// baseExtension = _newBaseExtension;
// }

/// @dev set hidden uri
// function setNotRevealedURI(string memory _notRevealedURI) public onlyOwner {
// notRevealedUri = _notRevealedURI;
// }

// Admin functions ----

function isOwnerCall() public view onlyOwner returns (bool) {
unchecked {
return owner() == _msgSenderERC721A();
}
}

/// @dev set your baseuri
function setBaseURI(string memory _newBaseURI) public onlyOwner {
_checkOwner();
unchecked {
baseURI = _newBaseURI;
}
}

function showBalance() external view onlyOwner returns (uint256) {
_checkOwner();
unchecked {
return address(this).balance;
}
}

/// @dev to pause and unpause your contract(use booleans true or false)
function pause(bool _state) external onlyOwner nonReentrant {
_checkOwner();
unchecked {
paused = _state;
}
}

/// @dev withdraw funds from contract
function withdraw() external payable onlyOwner nonReentrant {
_checkOwner();
uint256 balance = address(this).balance;
payable(owner()).transfer(balance);
}
}

看看这个 API:
modifier callerIsUser: 校验器,是否是用户调用
constructor(_initBaseUri): _initBaseUri 初始化时输入上传的答辩 json 的文件夹名
_startTokenId:所以叫你从 1.json 开始命名
mint: 让你交钱,交易 nft 的方法
tokenURI: 就是 json 地址的全路径 readonly
isOwnerCall: 是否是合约部署者调用
showBalance: 显示你这个合约里收了多少钱了 ETH
pause: 暂停合约 mint 功能
withdraw: 取钱跑路 🏃

源码

部署合约

去别的地方找教程…

荒唐的NFT(二) - Solidity 写合约

http://example.com/2023/03/03/教程/荒唐的NFT-合约解析/

作者

Huasun47

发布于

2023-03-03

更新于

2023-03-03

许可协议