0

I am ultimately trying to display the nft that is being minted. I can record the transaction properly but now I am just trying to display the tokenURI. This is my smart contract.

    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;
        
    }



This is my streamlit application to mint the nfts, where i also want to display the NFT. So right after you mint an nft you can see the actual uri or image of the nft that you just minted.

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)

I tried to use this

    tokenId = contract.functions.totalSupply()
    st.write(tokenId)

    tokenURI = contract.functions.tokenURI(tokenId)
    st.write(tokenURI)

but was met with this error

enter image description here

I tried adding .call() to the end of the two functions but I would still get an error.

1
  • Seems like you don't have an accessible tokenURI() function; make sure that its implemented and is either public or external;
    – Casareafer
    Dec 4, 2022 at 5:47

1 Answer 1

1

In order to properly get the token URI of an NFT, you need to first get the token ID of the NFT that was just minted. In your code, you are calling the totalSupply() function, which returns the total number of tokens that have been minted, not the ID of the latest token that was minted.

To get the ID of the latest token that was minted, you can use the lastTokenId() function, which is part of the ERC721Enumerable interface. So, your code should be updated as follows:

tokenId = contract.functions.lastTokenId().call()
st.write(tokenId)

tokenURI = contract.functions.tokenURI(tokenId).call()
st.write(tokenURI)

You also need to add the .call() at the end of the tokenURI() function to properly get the token URI value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.