本文介绍了在Solidity中处理保存和管理NFT的mint功能,包括代码示例和相应的Gas费用分析。文章结构清晰,提供了合约代码和背景信息,适合具备一定编程基础的读者,尤其是希望深入了解区块链和NFT开发的开发者。
照片由 Gene Gallin 提供,来源于 Unsplash
(如果你看到付费墙,可以在 这里 阅读。 如果你想继续,请查看这个 github 仓库: https://github.com/DonkeVerse/GasContest)
如果开发者来自 C 类语言(Java、C、JavaScript 等),那么 Solidity 是一种很容易学习的语言,但在区块链的背景下管理 gas 成本是很少出现的事情。
让我们看一下下面摘要中的铸造函数的典型工作流。铸造函数是 web3 应用程序区块链后端的一部分,用户通过点击铸造按钮并同意发送一定数量的加密货币来铸造 NFT。
以下是一个典型的 Solidity 工作流,它反映了许多在生产中使用的 NFT 智能合约,并具有一些非常好的功能。
Medium
mapping (address => uint256) public alreadyMinted;
bool public enablePublicMint = true;
uint256 constant public PRICE = 0.06 ether;
address private publicMintingAddress;
function publicMint(bytes calldata _signature) external payable {
require(totalSupply() < MAX_SUPPLY, "max supply"); // 限制 NFT 系列的总供应量
require(enablePublicMint, "public mint enabled"); // 仅在所有者启用时允许公开铸造
require(msg.sender == tx.origin, "no bots"); // 阻止智能合约铸造
require(publicMintingAddress == // 检查合约所有者是否签署了买家的地址
bytes32(uint256(uint160(msg.sender)))
.toEthSignedMessageHash()
.recover(_signature),
"not allowed"
);
require(alreadyMinted[msg.sender] < 2, "too many");// 限制每个地址的铸造量为 2
require(msg.value == PRICE, "wrong price"); // 要求发送的以太币等于设定的价格
alreadyMinted[msg.sender]++;
_safeMint(msg.sender, totalSupply());
}
| 方法 ·
···············|························|··············|·············|
| 合约 · 方法 · 最小 · 最大 ·
···············|························|··············|·············|
| GasContest · publicMint · 132695 · 160995 ·
查看原文 Base Case Minting.md 托管由 ❤ 提供 GitHub
这段代码是在 hardhat 环境中执行的,该环境报告 gas 成本。为了简洁起见,合约的其他部分没有显示,但它继承了 OpenZeppelin 的 ERC721Enumerable 合约。如果你希望继续,可以使用这个仓库 这里 作为入门。
- 原文链接: medium.com/donkeverse/ha...
- 登链社区 AI 助手,为大家转译优秀英文文章,如有翻译不通的地方,还请包涵~
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!