Last active
March 18, 2025 04:58
-
-
Save Jayke770/8eb37316932101aca7f2e75d533e0b01 to your computer and use it in GitHub Desktop.
ERC721 Contract
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SPDX-License-Identifier: MIT | |
| pragma solidity ^0.8.9; | |
| import "@openzeppelin/contracts@4.8.3/token/ERC721/ERC721.sol"; | |
| import "@openzeppelin/contracts@4.8.3/token/ERC20/IERC20.sol"; | |
| import "@openzeppelin/contracts@4.8.3/token/ERC721/extensions/ERC721URIStorage.sol"; | |
| import "@openzeppelin/contracts@4.8.3/access/Ownable.sol"; | |
| import "@openzeppelin/contracts@4.8.3/utils/Counters.sol"; | |
| contract $PEPE is ERC721, ERC721URIStorage, Ownable { | |
| using Counters for Counters.Counter; | |
| Counters.Counter private _tokenIdCounter; | |
| address Erc20tokenAddress; | |
| struct NFTData { | |
| uint256 price; | |
| address erc20Address; | |
| string uri; | |
| bool isSold; | |
| } | |
| mapping (uint256 => NFTData) public nftData; | |
| constructor(address tokenAddress) ERC721("$PEPE NFT", "PEPE NFT") { | |
| Erc20tokenAddress = tokenAddress; | |
| } | |
| function buyNft(uint256 tokenid) public { | |
| uint256 erc20Balance = IERC20(nftData[tokenid].erc20Address).balanceOf(msg.sender); | |
| uint256 allowance = IERC20(nftData[tokenid].erc20Address).allowance(msg.sender, address(this)); | |
| require(!nftData[tokenid].isSold, "Tokenid already sold"); | |
| require(erc20Balance >= nftData[tokenid].price, "Insufficient ERC20 token balance"); | |
| require(allowance >= nftData[tokenid].price, "Insufficient ERC20 token allowance"); | |
| IERC20(nftData[tokenid].erc20Address).transferFrom(msg.sender, address(this), nftData[tokenid].price); | |
| _tokenIdCounter.increment(); | |
| _safeMint(msg.sender, tokenid); | |
| _setTokenURI(tokenid, nftData[tokenid].uri); | |
| nftData[tokenid].isSold = true; | |
| } | |
| function addNft(uint256 price, string memory uri) public onlyOwner { | |
| uint256 tokenId = _tokenIdCounter.current(); | |
| nftData[tokenId] = NFTData(price, Erc20tokenAddress, uri, false); | |
| } | |
| function getTokenPrice(uint256 tokenid) public view returns (uint256) { | |
| return nftData[tokenid].price; | |
| } | |
| function getTokenErcAddress(uint256 tokenid) public view returns (address) { | |
| return nftData[tokenid].erc20Address; | |
| } | |
| function isTokenSold(uint256 tokenid) public view returns (bool) { | |
| return nftData[tokenid].isSold; | |
| } | |
| function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) { | |
| super._burn(tokenId); | |
| } | |
| function tokenURI(uint256 tokenId) | |
| public | |
| view | |
| override(ERC721, ERC721URIStorage) | |
| returns (string memory) | |
| { | |
| return super.tokenURI(tokenId); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment