Skip to content

Instantly share code, notes, and snippets.

@taycaldwell
Created January 28, 2024 18:38
Show Gist options
  • Save taycaldwell/2496abd5dac8138f3773a56bcb2f6db0 to your computer and use it in GitHub Desktop.
Save taycaldwell/2496abd5dac8138f3773a56bcb2f6db0 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyNFT is ERC721, Ownable {
using Strings for uint256;
// Base URI
string private _baseURIextended;
constructor(string memory name, string memory symbol)
ERC721(name, symbol)
{}
function setBaseURI(string memory baseURI_) external onlyOwner {
_baseURIextended = baseURI_;
}
function _baseURI() internal view virtual override returns (string memory) {
return _baseURIextended;
}
// Function to set/update the URI of a specific token
function setTokenURI(uint256 tokenId, string memory tokenURI) public onlyOwner {
_setTokenURI(tokenId, tokenURI);
}
// Override function to return the full URI of a token
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
string memory base = _baseURI();
return bytes(base).length > 0 ? string(abi.encodePacked(base, tokenId.toString())) : "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment