ERC1155
ERC1155
是以太坊上的一个标准协议,用于创建可批量铸造、可批量传输的数字资产,如游戏中的物品、收藏品、证书等。每个 ERC1155
合约可以铸造多种类型的资产,每种资产都有一个唯一的 ID
,并且每种资产可以有多个实例。这个标准协议定义了一组功能和事件,使得 ERC1155
资产可以被安全地转移、拥有、交易和检索。与 ERC721
不同,ERC1155
允许多种类型的资产被打包在一个交易中,从而使得交易更加高效,并且减少了需要进行的交易数量。这个标准协议具有可扩展性,并且在许多不同的场景下都可以使用,例如游戏、NFT 市场、证券发行等。
Mint
可以在以下测试网络自己mint,具体步骤可以参考 Github
Network | Contract Address |
---|---|
Ethereum Goerli | 0xb1ecef869cd9382ae17efb07bd813d49832ea6c1 |
开发
在 Openzeppelin 的帮助下可以很快的入手开发ERC1155
标准的NFT合约。参考之前测试用的NFT代码,自行编译并且发布到了更多的测试网络上,可以很方便的进行ERC1155
NFT测试。
引入ERC1155
库之后,自己只需要实现mint
等自定义方法即可,非常简单。
合约开发使用的是VSCode和hardhat,合约验证使用hardhat flattening。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract TestToadz is ERC1155, Ownable {
uint256 public constant maxTokens = 6969;
uint256 public numAvailableTokens = 6969;
uint256 public constant maxMintsPerTx = 5;
bool public devMintLocked = false;
uint256[10000] private _availableTokens;
constructor() ERC1155("ipfs://QmWEFSMku6yGLQ9TQr66HjSd9kay8ZDYKbBEfjNi4pLtrr/{id}") {}
function getNumAvailableTokens() public view returns (uint256) {
return numAvailableTokens;
}
//Minting
function mint(uint256 quantity, uint256 amount) public {
uint256 updatedNumAvailableTokens = numAvailableTokens;
require(
block.timestamp >= 1337133769,
"Sale starts at whatever this time is"
);
require(
quantity <= maxMintsPerTx,
"There is a limit on minting too many at a time!"
);
require(
updatedNumAvailableTokens - quantity >= 0,
"Minting this many would exceed supply!"
);
require(msg.sender == tx.origin, "No contracts!");
uint256[] memory ids = new uint256[](quantity);
uint256[] memory amounts = new uint256[](quantity);
for (uint256 i = 0; i < quantity; i++) {
uint256 tokenId = getRandomSerialToken(quantity, i);
ids[i] = tokenId;
amounts[i] = amount;
updatedNumAvailableTokens--;
}
_mintBatch(msg.sender, ids, amounts, "");
numAvailableTokens = updatedNumAvailableTokens;
}
//Dev mint special tokens
function mintSpecial(uint256[] memory specialIds, uint256[] memory amounts ) external onlyOwner {
require(!devMintLocked, "Dev Mint Permanently Locked");
_mintBatch(msg.sender, specialIds, amounts, "");
}
function getRandomSerialToken(uint256 _numToFetch, uint256 _i)
internal
returns (uint256)
{
uint256 randomNum = uint256(
keccak256(
abi.encode(
msg.sender,
tx.gasprice,
block.number,
block.timestamp,
blockhash(block.number - 1),
_numToFetch,
_i
)
)
);
uint256 randomIndex = randomNum % numAvailableTokens;
uint256 valAtIndex = _availableTokens[randomIndex];
uint256 result;
if (valAtIndex == 0) {
result = randomIndex;
} else {
result = valAtIndex;
}
uint256 lastIndex = numAvailableTokens - 1;
if (randomIndex != lastIndex) {
uint256 lastValInArray = _availableTokens[lastIndex];
if (lastValInArray == 0) {
_availableTokens[randomIndex] = lastIndex;
} else {
_availableTokens[randomIndex] = lastValInArray;
}
}
numAvailableTokens--;
return result;
}
function lockDevMint() public onlyOwner {
devMintLocked = true;
}
}
网友评论