I already saw the issue open which would make view functions such as uri virtual so that we could modify for our use case. However, I wanted to ask if you know when (an estimation of time) this will get implemented and would be possible to use? So that we don’t copy the whole ERC1155 locally?
This is planned to be looked at for the next release of OpenZeppelin Contracts.
In the mean time, you could clone the current implementation and add the URI functionality that you need. You will need to appropriately test and audit this.
Wondering the same. Can someone share how they modify the uri function to match the exact path of the metadata json file (including the 0 padding). Thanks in Advance!
As a contract 1155 actually what it does is create serialized tokens with a balance, so for me strictly speaking they are not NFTs, we must create a mapping to be able to create unique NFTs.
mapping(uint => string) private _URIS;
So we create a token id with a unique balance and we already have an NFT uint256 public constant NFT = 1;
The uri function will first check if there is a specific uri for that token, and if not, it will concatenate the id to receive the metada associated with the semi-fungible token, as shown above.
Thanks for sharing this. I've heard a lazier approach. Restrict who can mint, and simply don't mint more than 1. Ie. check is not mandatory but good to have.
Regarding the URI, I'm confused which of the following are compliant with ERC1155 standards if id=250.
ipfs://uri/250.json
ipfs://uri/fa.json
ipfs://uri/00000000000000000000000000000000000000000000000000000000000000fa.json
i tried what frangio said but never got it working. i'm totally new to solidity/javascript, but im pretty sure what he said is right.
starting to go off topic now, but when i tried i didnt know how to use "padStart" function. im compiling with remix solidity and that gave me an error. maybe missing some imports. i also tried importing the toString but that didnt fix it. hope you had better luck
@NothingBurger26, It did not work for you because what you have shared @frangio is javascript code for your front.
It is not solidity code.
Think of your smart contract as the back of a web application. The data returned by the uri function must be manipulated so that they generate the recommended uri in the EIP
What you have to do is create the front of a dapp to receive the response from the uri function connecting to the blockchain through one of the most used javascript libraries: Ethers.js or Web3.js
You can also use either of the two most popular development environments:
Sharing a solution to getting URI function to output tokenURI with the padding and in hex without further modification on the front end! Let me know if you guys like it
bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef";
function toHexString(uint256 value, uint256 length) public pure returns (string memory) {
bytes memory buffer = new bytes(length+2);
for (uint256 i = length + 1; i > 1; --i) {
buffer[i] = _HEX_SYMBOLS[value & 0xf];
value >>= 4;
}
require(value == 0, "Strings: hex length insufficient");
return string(buffer);
}
function uri(uint256 _tokenID) override public view returns (string memory) {
return string(abi.encodePacked(baseURI,toHexString(_tokenID,64),".json"));
}