Solidity
ERC1155 總供應量未顯示?
我希望能夠看到每個代幣的總供應量。我已經導入了擴展並遵循了關於 totalSupply 的 erc1155 文件,但是當在 Remix 上部署並呼叫總供應量時,即使在鑄造後它也顯示為 0?我無法弄清楚如何能夠顯示每個 id 已經鑄造了多少?這是我的合約程式碼,謝謝。
// SPDX-License-Identifier: MIT pragma solidity ^0.8.4; import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; import "@openzeppelin/contracts/utils/Strings.sol"; import "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Supply.sol"; contract MyToken is ERC1155, Ownable { uint256 public constant Poloy = 1; uint256 public constant Leafy = 2; mapping (uint256 => uint256) public tokenSupply; constructor() ERC1155("https://ipfs.io/ipfs/3bz4r7c6q3fjmt2uoicyibx2nhf7q6gtsdgze277wzlnadbvfme/{id}.json") {} //make sure opensea can read the link function uri(uint256 _tokenId) override public pure returns (string memory){ return string( abi.encodePacked( "https://ipfs.io/ipfs/bafybeid3bz4r7c6q3fjmt2uoicyibx2nhf7q6gtsdgze277wzlnadbvfme/", Strings.toString(_tokenId), ".json" ) ); } // Minting - If you do not have any poloy the contract will let you buy one function mintPoloy() public{ require(balanceOf(msg.sender,Poloy) == 0,"you already have a polo "); require(balanceOf(msg.sender,Leafy) == 0,"you already have a token cheaty cheater "); _mint(msg.sender, Poloy, 1, "0x000"); } // Minting - If you do not have any leafy the contract will let you buy one function mintLeafy() public { require(balanceOf(msg.sender,Leafy) == 0,"you already have a leaf "); require(balanceOf(msg.sender,Poloy) == 0,"you already have a token cheaty cheater "); _mint(msg.sender, Leafy, 1, "0x000"); } function totalSupply(uint256 id) public view returns (uint256) { return tokenSupply[id]; } }
你
tokenSupply
的從來沒有增加。我對您的契約做了一些更改:
- 我在每個函式上添加了一個增量
- 由於您的程式碼被重複,我將其全部移到了一個
private
函式中- 我減少了
error
消息的數量(花費很多)。這將優化程式碼的大小盡量確保您的錯誤消息不超過 32 個字元(32 字節),否則它將儲存在 64 字節上。
function mintPoloy() public { require(_hasMinted(), "Already minted cheater!"); tokenSupply[Poloy]++; _mint(msg.sender, Poloy, 1, "0x000"); } function mintLeafy() public { require(_hasMinted(), "Already minted cheater!"); tokenSupply[Leafy]++; _mint(msg.sender, Leafy, 1, "0x000"); } function _hasMinted() private view returns (bool) { return ((balanceOf(msg.sender, Poloy) + balanceOf(msg.sender, Leafy)) > 0) ? false : true; }