I’m working on a smart contract for NFT creation and need help with payment integration.
Currently I can mint NFTs for free or charge fees in native tokens like BNB. However, I want to accept BUSD tokens as payment on the BSC network instead.
I can successfully check my BUSD balance using balanceOf, but when I try to mint an NFT, I get this error:
Gas estimation failed with execution reverted error
Returned error: {"jsonrpc":"2.0","error":"execution reverted","id":5075618818565261}
Here’s my current contract code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract ArtToken is ERC721URIStorage, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _itemIds;
IERC20 public paymentToken;
uint256 public creationCost = 2e18;
address stablecoinAddress = 0xeB3Eb991D39Dac92616da64b7c6D5af5cCFf1627;
struct TokenData {
string category;
}
mapping(uint256 => TokenData) private _tokenData;
constructor(uint256 cost) ERC721("ArtToken", "ART") {
paymentToken = IERC20(stablecoinAddress);
creationCost = cost * 1e18;
}
function checkBalance()
public view
returns (uint256)
{
return paymentToken.balanceOf(msg.sender);
}
function createToken(address to, string memory uri, string memory category)
public onlyOwner
returns (uint256)
{
require(paymentToken.balanceOf(msg.sender) >= creationCost, "Not enough tokens");
paymentToken.transferFrom(msg.sender, address(this), creationCost);
_itemIds.increment();
uint256 tokenId = _itemIds.current();
_mint(to, tokenId);
_tokenData[tokenId].category = category;
_setTokenURI(tokenId, uri);
return tokenId;
}
function getCategory(uint256 tokenId)
public view
returns (string memory)
{
return _tokenData[tokenId].category;
}
function updateCost(uint256 newCost)
external onlyOwner
{
creationCost = newCost * 1e18;
}
}
What am I missing to make the ERC20 payment work properly?