第3节:ERC1155(半同质化Token)

  1. 1155是每个id为一个token组,每个组里面可以包含多个erc20

  2. mint(to, id, amount)

  3. setURI(id, uri)

  4. setURI的时候,填写的是metadata.json,不是图片的url,使用pinata服务存储图片和metadata.json:

    {
        "description": "this is a nft1155 metadata json desc",
        //"image": "ipfs://QmTfK2CeRBkRqSZHmnekdZYSBrsKLQ6U5Px8MWtGf1Eqta",
        "image": "https://gateway.pinata.cloud/ipfs/QmTfK2CeRBkRqSZHmnekdZYSBrsKLQ6U5Px8MWtGf1Eqta",
        "name": "duke nft"
    }
    
  5. opensea测试网是rinkeby,链接为:https://testnets.opensea.io

  6. opensea会自动将当前链接的地址账户的nft扫描出来展示在页面上。

  7. 自己部署(示例):https://testnets.opensea.io/assets/rinkeby/0x54f1a0ad311e00b68fa940bc969c191863ac84fd/1

更好的合约:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155URIStorage.sol";

contract MockErc1155Token is ERC1155URIStorage {
    string public name;
    string public symbol;
    uint256 public tokenCounter;

    constructor(string memory _name, string memory _symbol) ERC1155("") {
        name = _name;
        symbol = _symbol;
    }

    function createCollectible(string memory tokenURI, uint256 _amount)
        external
        returns (uint256)
    {
        uint256 newItemId = tokenCounter;
        _mint(_msgSender(), newItemId, _amount, "");
        _setURI(newItemId, tokenURI);

        tokenCounter = tokenCounter + 1;
        return newItemId;
    }

    // function mint(uint256 _id, uint256 _amount) external {
    //     _mint(_msgSender(), _id, _amount, "");
    // }

    // function mintBatch(uint256[] memory _ids, uint256[] memory _amounts)
    //     external
    // {
    //     _mintBatch(_msgSender(), _ids, _amounts, "");
    // }

    function burn(uint256 _id, uint256 _amount) external {
        _burn(msg.sender, _id, _amount);
    }

    function burnBatch(uint256[] memory _ids, uint256[] memory _amounts)
        external
    {
        _burnBatch(msg.sender, _ids, _amounts);
    }

    function burnForMint(
        address _from,
        uint256[] memory _burnIds,
        uint256[] memory _burnAmounts,
        uint256[] memory _mintIds,
        uint256[] memory _mintAmounts
    ) external {
        _burnBatch(_from, _burnIds, _burnAmounts);
        _mintBatch(_from, _mintIds, _mintAmounts, "");
    }

    function setURI(uint256 _id, string memory _uri) external {
        _setURI(_id, _uri);
    }
}