How to remove zeros in tokenID in Solidity?

439 views Asked by At

I'm learning Solidity, there's a problem that keeps me busy for days

Basically, I'm going to mint a bunch of NFTs.

The images are 1.png, 2.png, 3.png

The corresponding metadata has the same number as image, so it's 1.json, 2.json, 3.json etc.

And the link to the metadata in Pinata is ipfs://blahblahblah/{id}.json

However, due to the clients interpretation, on OpenSea the URI becomes ipfs://blahblahblah/00000000000000000000000000000000000000000000000000000000000000xx.json

Which means:

"1.json" becomes 0000000000000000000000000000000000000000000000000000000000000001.json

"2.json" becomes 0000000000000000000000000000000000000000000000000000000000000002.json ....

As a result, OpenSea can't get the data of the NFT, because the link is not correct

Is there a way to convert the tokenID to strip off all the zeros, and leave only the number?

Thank you so much

Here's the code

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

import '@openzeppelin/contracts/token/ERC1155/ERC1155.sol';
import '@openzeppelin/contracts/access/Ownable.sol';

contract ArtCollectibleV2 is Ownable, ERC1155 {
// Base URI
string private baseURI;
string public name;

constructor()
    ERC1155(
        'ipfs://blahblahblah/{id}.json'
    )
{
    setName('Collection Name');
    
}

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

function setName(string memory _name) public onlyOwner {
    name = _name;
}

function mintBatch(uint256[] memory ids, uint256[] memory amounts)
    public
    onlyOwner
{
    _mintBatch(msg.sender, ids, amounts, '');
}

function mint(uint256 id, uint256 amount) public onlyOwner {
    _mint(msg.sender, id, amount, '');
}
1

There are 1 answers

4
keser On

In your imported ERC1155.sol contract, there is a virtual function that returns the uri of the token:

    /**
     * @dev See {IERC1155MetadataURI-uri}.
     *
     * This implementation returns the same URI for *all* token types. It relies
     * on the token type ID substitution mechanism
     * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
     *
     * Clients calling this function must replace the `\{id\}` substring with the
     * actual token type ID.
     */
    function uri(uint256) public view virtual override returns (string memory) {
        return _uri;
    }

You can override it and prefix 00..00 to the token id to be compatible with the OpenSea