I’m working on a website where people can mint NFTs. Most sites I’ve seen use ETH for transactions on Ethereum. But I was wondering if it’s possible to use my own ERC20 token instead.
Has anyone tried this before? Are there any technical challenges I should be aware of? I’m not sure if smart contracts can handle this kind of setup.
contract MyNFT is ERC721 {
IERC20 public paymentToken;
constructor(address _paymentToken) ERC721("MyNFT", "MNFT") {
paymentToken = IERC20(_paymentToken);
}
function mint(uint256 tokenId) public {
// How to implement payment with custom ERC20?
}
}
Any advice or examples would be really helpful. Thanks!
Yes, it’s absolutely possible to use your custom ERC20 token for NFT transactions. I’ve implemented this in a recent project. The key is to modify your mint function to accept and transfer the ERC20 tokens.
Here’s a basic implementation:
function mint(uint256 tokenId, uint256 price) public {
require(paymentToken.transferFrom(msg.sender, address(this), price), "Transfer failed");
_safeMint(msg.sender, tokenId);
}
Make sure to set an appropriate allowance for your contract to spend the user’s tokens. Also, consider adding a function to withdraw the collected ERC20 tokens. One challenge to be aware of is liquidity. If your token isn’t widely traded, users might find it difficult to acquire. Consider providing a swap function or partnering with a DEX to ensure accessibility.
ya, ur token works fine for nft sales if the contract uses transferFrom correctly. make sure liquidity is sorted so ppl can get the token. good luck wit ur project!