ERC1155 passing user defined tokenURI on the mint process

Hi im trying to set a new tokenURI per mint. Users are submitting the meta on the front end and im trying to get it back from moralis to use in my contract… Is this even possible? I tried doing this by passing tokenURI in the function call from the front end and passing that into the constructor with no luck.
I tried using _setURI() with the passed in uri and no luck. Im now looking to see if I can set the value in the constructor using the {id} but the hashes are alway different as well. Is there a way to do this?

below is my constructor with a template of the hypothetical string argument. and the mint factory function


) public ERC1155("https://ipfs.moralis.io:2053/ipfs/{moralis ipfs hash}/metadata/{id}.json")VRFConsumerBase(_vrfCoordinator, _linkToken) {
        x_lastTimeStamp = block.timestamp;
        x_keyHash = _keyHash;
        x_chainlinkFee = _chainlinkFee;
        x_ticketFee = _ticketFee;
        x_interval = _interval;
        x_theWinner = 0x0000000000000000000000000000000000000000;
        x_lotteryState = lotteryState.OPEN;
    }
    // Mint factory
    function mintImage(string memory tokenURI)
       public
       payable
        returns (uint256)
    {
        //x_tokenURI = tokenURI;
        x_tokenIds.increment();
        uint256 newItemId = x_tokenIds.current();
        _mint(msg.sender, newItemId, 1, "");
        _setURI(tokenURI);
        x_userMintCount[msg.sender] = x_userMintCount[msg.sender] + 1;
        if(x_userMintCount[msg.sender] % 3 == 0){
        x_usersEntered.push(payable(msg.sender));
        emit enteredDraw(msg.sender);
        }
        return newItemId;
    }

you can read here about how to post code: READ BEFORE POSTING - How to post code in the forum

1 Like

you can not use IPFs link directly like that in constructor if you didn’t upload all that data to IPFS before deploying your smart contract.

you can use that if you uploaded all the data in an IPFS folder, or you can use an url that is specific to a static location that is not on IPFs, or you can compute the uri for every minted token separately.

How would computing uri per token look like? That’s what im having trouble figuring out. I tried a handful of ways with no luck…

I think I found the solution here I didnt see this before… ERC-1155 Get token_uri

an example from someone else, in case that it helps:

pragma solidity ^0.8.0;

//Importing ERC 1155 Token contract from OpenZepplin

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC1155/ERC1155.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Strings.sol";


contract NFTContract is ERC1155 , Ownable  {
    
    uint256 public constant X = 0;
    uint256 public constant Y = 1;
    
    constructor() ERC1155("")  {
        _mint(msg.sender,X,1,"");
        _mint(msg.sender,Y,2,"");
    }
    
    
    function mint(address account,uint256 id,uint256 amount) public onlyOwner {
        _mint(account,id,amount,"");
    }
    
    function burn(address account,uint256 id,uint256 amount) public {
        require(msg.sender == account);
        _burn(account,id,amount);
    }
    
      function uri(uint256 _tokenId) override public view returns (string memory) {
        return string(
            abi.encodePacked(
            "https://[redacted].com/",
            Strings.toString(_tokenId),
            ".json"
           )
        );
      }
}
1 Like