0

I´m trying to make a simple ERC721 NFT minting contract. I created an image and its corresponding metadata and I´ve uploaded them to the ipfs. When does this image and metadata link with the token created in the smart contract?. I was trying to use this code generated with the Openzeppelin contract wizard:

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

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract MyToken is ERC721, Ownable {
    using Counters for Counters.Counter;

    Counters.Counter private _tokenIdCounter;

    constructor() ERC721("MyToken", "MTK") {}

    function safeMint(address to) public onlyOwner {
        uint256 tokenId = _tokenIdCounter.current();
        _tokenIdCounter.increment();
        _safeMint(to, tokenId);
    }
}

But I don't see where I can link my nft image and metadata to the contract. I´ve seen that in ERC721 standart by openzeppelin you can set the base URI and tokenURI with the functions tokenURI and _baseURI, but I don't know exactly how to use them. I was planning to create a multiple-item collection (in the ipfs) so I don't know what to use in my case.

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
ArtySaurio
  • 25
  • 3

1 Answers1

0

Replace ERC721 with ERC721URIStorage

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

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract MyToken is ERC721URIStorage, Ownable {
    using Counters for Counters.Counter;

    Counters.Counter private _tokenIdCounter;

    constructor() ERC721("MyToken", "MTK") {}

    function safeMint(string memory tokenURI, address to) public onlyOwner {
        uint256 tokenId = _tokenIdCounter.current();
        _tokenIdCounter.increment();
        _safeMint(to, tokenId);
        _setTokenURI(tokenId, tokenURI);
    }
}