2

i set a new contract with eRC 1155 on ethereum with 3 categories of 3 tokens (Id 1,2,3) and deployed it in the blockchain. To have the possibility to mint new token i create a override function for _mint method and i works, i can mint new token (with new id) even after deployed. The problem is that i set images and json metadata doc in ipfs for the token generated on contract deployment and now i'm searching a way to set metadata for contract deployed after. here is my code :

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/utils/Strings.sol";

contract lktoken1155 is ERC1155 {
    uint256 public constant Token1Index = 1;
    uint256 public constant Token2Index = 2;
    uint256 public constant Token3Index = 3;

    constructor() ERC1155("https://ipfs.io/ipfs/bafkreicbpwho6rklhm7j2wd5ltrbjowc6xtdlzv33tf4xu72ofmlba3isq") {
        _mint(msg.sender, Token1Index, 1, "");
        _mint(msg.sender, Token2Index, 1, "");
        _mint(msg.sender, Token3Index, 1, "");
    }

    function GenerateToken(address to, uint256 id, uint256 amount, bytes memory data) public virtual
    {
        _mint(to, id, amount, data);
    }

    function uri(uint256 _tokenid) override public pure returns (string memory) {
        return string(
            abi.encodePacked(
                "https://ipfs.io/ipfs/bafybeieylamp3dpfk4q7jgmllqig42xa6xx5t5ydu5zfyjegdpccwanyie/",
                Strings.toString(_tokenid),".json"
            )
        );
    }
}

1 Answer 1

0

Adding metadata to an ERC1155 contract after it is deployed may not be possible, as the contract code cannot be modified once it has been deployed to the blockchain.

However, you can create a separate contract or storage system that references the original ERC1155 contract and stores the metadata. This can be done by creating a mapping between the token IDs and their respective metadata. And storing this mapping in a separate contract or off-chain storage solution.

Here is an example of how you can implement a metadata storage contract that references an existing ERC1155 contract:

Create a new Solidity contract that has a mapping between token IDs and metadata:

contract MyMetadataContract {
    mapping (uint256 => string) private _tokenMetadata;
    
    function setTokenMetadata(uint256 tokenId, string memory metadata) public {
        _tokenMetadata[tokenId] = metadata;
    }
    
    function getTokenMetadata(uint256 tokenId) public view returns (string memory) {
        return _tokenMetadata[tokenId];
    }
}

Within the same contract, add a reference to the existing ERC1155 contract and set its address:

contract MyMetadataContract {
    ERC1155 private _erc1155Contract;
    mapping (uint256 => string) private _tokenMetadata;
    
    constructor(address erc1155Address) {
        _erc1155Contract = ERC1155(erc1155Address);
    }
    
    // rest of the contract code
}

In the setTokenMetadata function, call the setURI function on the ERC1155 contract to set the metadata URI for the specified token ID:

function setTokenMetadata(uint256 tokenId, string memory metadata) public {
    _tokenMetadata[tokenId] = metadata;
    _erc1155Contract.setURI(tokenId, metadata);
}

This will allow you to add metadata to an existing ERC1155 contract by creating a separate metadata storage contract that references the original contract and stores the metadata.

You will need to deploy this new contract to the blockchain and set its address in the constructor of the ERC1155 contract.

Your Answer

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

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