Solidity
我的程式碼有效,但它不應該,我不明白為什麼
所以我有程式碼,我正在鑄造一個 NFT。我的元數據位於一個名為 tokenURI() 的函式中。但是當我執行我的腳本進行部署時,我從未真正呼叫過這個函式,但它仍然以某種方式用正確的元數據鑄造了我的 NFT。我很高興它正在工作,但實際上它根本不應該工作。為什麼我的 tokenURI() 函式會被呼叫?
Github repo 以備不時之需:https ://github.com/jaysnel/js-nft
正在執行的命令:
npx hardhat run scripts/deploy.js --network goerli
我的 .sol 文件:
// SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.17; // OpenZeppelin Contracts and hardhat import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; import "@openzeppelin/contracts/utils/Counters.sol"; import "hardhat/console.sol"; contract JSNFT is ERC721URIStorage { using Counters for Counters.Counter; Counters.Counter private _tokenIds; constructor() ERC721 ('jsNFT', 'JSNFT'){ console.log('NFT contract deployed'); } function makeJSNFT() public { uint256 newItemId = _tokenIds.current(); _safeMint(msg.sender, newItemId); _tokenIds.increment(); } function tokenURI(uint256 _tokenId) public view override returns (string memory) { require(_exists(_tokenId)); console.log("An NFT SVG w/ ID %s has been minted to %s", _tokenId, msg.sender); return string( abi.encodePacked( "data:application/json;base64", "data:application/json;base64,ewogICAgIm5hbWUiOiAiSlMtTkZULUFJLUFSVCIsCiAgICAiZGVzY3JpcHRpb24iOiAiQUkgYXJ0IGNyZWF0ZWQgYnkgSlMtTkZUIiwKICAgICJpbWFnZSI6ICJkYXRhOmltYWdlL3N2Zyt4bWw7YmFzZTY0LFBITjJaeUI0Yld4dWN6MGlhSFIwY0RvdkwzZDNkeTUzTXk1dmNtY3ZNakF3TUM5emRtY2lJSEJ5WlhObGNuWmxRWE53WldOMFVtRjBhVzg5SW5oTmFXNVpUV2x1SUcxbFpYUWlJSFpwWlhkQ2IzZzlJakFnTUNBek5UQWdNelV3SWo0S0lDQWdJRHh6ZEhsc1pUNHVZbUZ6WlNCN0lHWnBiR3c2SUhkb2FYUmxPeUJtYjI1MExXWmhiV2xzZVRvZ2MyVnlhV1k3SUdadmJuUXRjMmw2WlRvZ01UUndlRHNnZlR3dmMzUjViR1UrQ2lBZ0lDQThjbVZqZENCM2FXUjBhRDBpTVRBd0pTSWdhR1ZwWjJoMFBTSXhNREFsSWlCbWFXeHNQU0ppYkdGamF5SWdMejRLSUNBZ0lEeDBaWGgwSUhnOUlqVXdKU0lnZVQwaU5UQWxJaUJqYkdGemN6MGlZbUZ6WlNJZ1pHOXRhVzVoYm5RdFltRnpaV3hwYm1VOUltMXBaR1JzWlNJZ2RHVjRkQzFoYm1Ob2IzSTlJbTFwWkdSc1pTSStTbE10VGtaVUxVRlNWRHd2ZEdWNGRENEtQQzl6ZG1jKyIKfQ==" ) ); } }
部署.js:
const deployContract = async () => { const nftContractFactory = await hre.ethers.getContractFactory('JSNFT'); const nftContract = await nftContractFactory.deploy(); await nftContract.deployed(); console.log("Contract deployed to:", nftContract.address); // Call the function let txn = await nftContract.makeJSNFT() await txn.wait() }; const main = async () => { try { await deployContract(); process.exit(0); } catch (error) { console.log(error); process.exit(1); } }; main();
makeJSNFT
您的部署腳本使用該函式創建 NFT 。tokenURI
與鑄幣本身無關,它只允許您獲取已經存在的元數據