ERC-1155 Get token_uri

Hi guys, guys I need my metadata(or token_uri) to be dynamic but I see that ERC-1155 is different than ERC-721 and I don’t know exactly how can I send this information to have this property dynamic with the ipfs pathfile registered on EthNFTOwners table.

Result: (I have a cloud function getting all the tokens from EthNFTOwners table)

My contract:

Moralis Table:

you may have to override this uri function:

1 Like

Hi there,

I am having the same issue. @cryptokid I am wondering why would I have to override the uri function. I mean it works fine when I call as it is, it returns the uri but when I get all the nfts with moralis sdk I get the same empty gateway link :frowning:

@degueba, @chas13, can you give more details about this case, examples that I could check if possible?

Hey @chas13 and @cryptokid, let me share what was my solution.

1 step - Add metadata with type bytes on your struct
image

2 step - Add metadata on the function that you are using to mint with type bytes memory.
image

3 step - Hopefully, you are using OpenZeppelin and you can override and add the function setURI to your smart contract
image

4 step - On the front-end you can call the function exactly like this and pass the response from cid_hash with the parameter to to the method that you are using to mint.

1 Like

Hi @cryptokid,

Of course, thank you for your quick reply.

I have the following contract

the uri functions returns ipfs://hash/{id}.json
as it’s explained in https://forum.openzeppelin.com/t/create-an-erc1155/4433

when I use moralis sdk I get the following token_uri

(see next post)

I would expect it either to return https://ipfs.moralis.io:2053/ipfs/hash/tokenId.json or at least the original uri ?

I am not sure if I am doing something wrong, thank you for your help!

1 Like

Hey @chas13 check my post above.

Thank you! Your use case is a bit more complex than what I am doing right now, I can call the uri method directly for each token on the front end but I would love if moralis would return the token uri as expected, again maybe I did something wrong but in the contract my uri function returns ipfs://hash/{id}.json

@chas13, can you also paste code that is selectable, and maybe give example of a contract address that is already deployed?

Sure, thank you !

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/security/Pausable.sol";

contract ContractName is ERC1155, Ownable, Pausable {
    constructor() ERC1155("ipfs://hash/{id}.json") {
        _mint(msg.sender, 0, 10000000000 * 10 ** 18, "something");
        _mint(msg.sender, 1, 10000000 * 10 ** 18, "somethingelse");
        _mint(msg.sender, 2, 10000 * 10 ** 18, "rainbows");
    }

    function setURI(string memory newuri) public onlyOwner {
        _setURI(newuri);
    }

    function pause() public onlyOwner {
        _pause();
    }

    function unpause() public onlyOwner {
        _unpause();
    }

    function _beforeTokenTransfer(address operator, address from, address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data)
        internal
        whenNotPaused
        override
    {
        super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
    }
    

}```

Was your uri available on IPFS, that one from ipfs://QmT3BLd343TtH2rPb7EbWQXAtsk8qe7oeHc7ctWjoCWxqr/{id}.jso with particular ids replaced?
How did you make that work?

I think that Moralis tried to access that link in order to extract the metadata, and maybe it couldn’t access that link.

2 Likes

Oh that would make a lot of sense, there was just the string “ipfs://” in the constructor when I first created the contract and I later called the setURI function.

I will redeploy it with the URI in the constructor.

Thank you for your help @cryptokid

1 Like