Skip to content

Instantly share code, notes, and snippets.

@shobhitic
Last active April 12, 2023 11:38
Show Gist options
  • Save shobhitic/9e1e8b9c274088a993c254ef2cfb9cb7 to your computer and use it in GitHub Desktop.
Save shobhitic/9e1e8b9c274088a993c254ef2cfb9cb7 to your computer and use it in GitHub Desktop.
Mint NFT using ERC20 token - https://www.youtube.com/watch?v=dbsmJ-fbgxQ
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract MyNFT is ERC721, Ownable {
using Counters for Counters.Counter;
IERC20 public tokenAddress;
uint256 public rate = 100 * 10 ** 18;
Counters.Counter private _tokenIdCounter;
constructor(address _tokenAddress) ERC721("MyNFT", "MTK") {
tokenAddress = IERC20(_tokenAddress);
}
function safeMint() public {
tokenAddress.transferFrom(msg.sender, address(this), rate);
uint256 tokenId = _tokenIdCounter.current();
_tokenIdCounter.increment();
_safeMint(msg.sender, tokenId);
}
function withdrawToken() public onlyOwner {
tokenAddress.transfer(msg.sender, tokenAddress.balanceOf(address(this)));
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/[email protected]/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor() ERC20("MyToken", "MTK") {
_mint(msg.sender, 10000 * 10 ** decimals());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment