我最终试图显示正在被铸造的nft。我可以正确地记录事务,但现在我只是尝试显示tokenURI。这是我的明智之举。
contract EzAsPyNews is ERC721, ERC721Enumerable, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _tokenIdCounter;
uint256 public mintRate= 1.00 ether;
constructor() ERC721("EzAsPyNews", "EAPN") {}
function _baseURI() internal pure override returns (string memory) {
return "https://gateway.pinata.cloud/ipfs/QmdCg91nxaT6Rp2itU8k4jNYaCpNqxFd3fvchVdQsZKvzx#";
}
function safeMint(address to) public payable returns(uint256){
require(msg.value >= mintRate, "Please make sure you are entering atleast one ether.");
uint256 tokenId = _tokenIdCounter.current();
_tokenIdCounter.increment();
_safeMint(to, tokenId);
return tokenId;
}这是我创建nfts的流光应用程序,我也想在其中显示NFT。所以,在创建nft之后,您就可以看到您刚刚创建的nft的实际uri或图像。
st.title("Register for a Certificate of membership token")
accounts = w3.eth.accounts
address = st.selectbox("Select account for membership", options=accounts)
if st.button("Purchase a Certificate of Membership Token"):
tx_hash = contract.functions.safeMint(address).transact({
"from": address,
"gas": 1000000,
"value": 1000000000000000000, #Web3.fromWei(1000000000000000000, "ether")
})
st.write(tx_hash)
receipt = w3.eth.waitForTransactionReceipt(tx_hash)
st.write("Transaction receipt mined:")
st.write(dict(receipt))
tokenId = contract.functions.totalSupply()
st.write(tokenId)
tokenURI = contract.functions.tokenURI(tokenId)
st.write(tokenURI)我试着用这个
tokenId = contract.functions.totalSupply()
st.write(tokenId)
tokenURI = contract.functions.tokenURI(tokenId)
st.write(tokenURI)但是遇到了这个错误

我尝试将.call()添加到这两个函数的末尾,但仍然会出现错误。
发布于 2022-12-05 02:05:39
为了正确地获取NFT的令牌URI,您需要首先获得刚刚创建的NFT的令牌ID。在您的代码中,您调用的是totalSupply()函数,它返回已被铸造的令牌总数,而不是最新创建的令牌的ID。
要获取最新创建的令牌的ID,可以使用lastTokenId()函数,它是ERC721Enumerable接口的一部分。因此,您的代码应该按以下方式进行更新:
tokenId = contract.functions.lastTokenId().call()
st.write(tokenId)
tokenURI = contract.functions.tokenURI(tokenId).call()
st.write(tokenURI)您还需要在.call函数的末尾添加tokenURI() (),以正确地获得令牌URI值。
https://ethereum.stackexchange.com/questions/140516
复制相似问题