Skip to content

Instantly share code, notes, and snippets.

@amplicity
Created January 7, 2022 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amplicity/2e5af1b9299cb5be7c9667ba405361a1 to your computer and use it in GitHub Desktop.
Save amplicity/2e5af1b9299cb5be7c9667ba405361a1 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.1+commit.df193b15.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;
// i think the problem here is that the base uri will change the base uri for all tokens.
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract HeavyMellow is ERC721, Ownable {
using Strings for uint256;
// Optional mapping for token URIs
mapping(uint256 => string) private _tokenURIs;
// Base URI
string public _baseURIextended;
constructor(
string memory _name,
string memory _symbol,
string memory baseTokenURI
) ERC721(_name, _symbol) {
_baseURIextended = baseTokenURI;
}
function setBaseURI(string memory baseTokenURI) external onlyOwner {
_baseURIextended = baseTokenURI;
}
function baseURI() external view returns (string memory) {
return _baseURIextended;
}
function _setTokenURI(uint256 tokenId, string memory _tokenURI)
internal
virtual
{
require(
_exists(tokenId),
"ERC721Metadata: URI set of nonexistent token"
);
_tokenURIs[tokenId] = _tokenURI;
}
function _baseURI() internal view virtual override returns (string memory) {
return _baseURIextended;
}
function tokenURI(uint256 tokenId)
public
view
virtual
override
returns (string memory)
{
require(
_exists(tokenId),
"ERC721Metadata: URI query for nonexistent token"
);
string memory _tokenURI = _tokenURIs[tokenId];
string memory base = _baseURI();
// If there is no base URI, return the token URI.
if (bytes(base).length == 0) {
return _tokenURI;
}
// If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked).
if (bytes(_tokenURI).length > 0) {
return string(abi.encodePacked(base, _tokenURI));
}
// If there is a baseURI but no tokenURI, concatenate the tokenID to the baseURI.
return string(abi.encodePacked(base, tokenId.toString()));
}
function burn(uint256 tokenId) external onlyOwner returns (bool) {
require(
_exists(tokenId),
"ERC721Metadata: Cannot burn nonexistent token"
);
_burn(tokenId);
return true;
}
function mint(
address _to,
uint256 _tokenId,
string memory tokenURI_
) external onlyOwner {
_mint(_to, _tokenId);
_setTokenURI(_tokenId, tokenURI_);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment