I’m working on a smart contract that should accept both ETH and ERC20 tokens like USDT for NFT purchases. However, I keep running into compilation issues with my imports. Here’s what I’m trying to build:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "@openzeppelin/contracts@4.8.0/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts@4.8.0/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts@4.8.0/access/Ownable.sol";
import "@openzeppelin/contracts@4.8.0/utils/Counters.sol";
import "@openzeppelin/contracts@4.8.0/token/ERC20/IERC20.sol";
contract GameCards is ERC721, ERC721Enumerable, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _cardIdCounter;
IERC20 public paymentToken;
uint256 public ethCost = 0.01 ether;
uint256 public tokenCost = 50 * 10**18;
uint256 public totalCards;
bool public saleActive;
constructor(address _paymentToken) ERC721("GameCards", "GC") {
totalCards = 500;
paymentToken = IERC20(_paymentToken);
}
function updatePaymentToken(address _newToken) external onlyOwner {
paymentToken = IERC20(_newToken);
}
function setCosts(uint256 _ethCost, uint256 _tokenCost) external onlyOwner {
ethCost = _ethCost;
tokenCost = _tokenCost;
}
function purchaseWithToken() external {
require(paymentToken.balanceOf(msg.sender) >= tokenCost, "Insufficient tokens");
paymentToken.transferFrom(msg.sender, owner(), tokenCost);
createCard();
}
function purchaseWithEth() external payable {
require(msg.value >= ethCost, "Not enough ETH sent");
createCard();
}
function createCard() internal {
require(saleActive, "Sale not active");
require(totalSupply() < totalCards, "Max supply reached");
uint256 newCardId = _cardIdCounter.current();
_cardIdCounter.increment();
_safeMint(msg.sender, newCardId);
}
function toggleSale(bool _active) external onlyOwner {
saleActive = _active;
}
}
The main issue seems to be with importing the right ERC20 interface. Should I use IERC20 instead of ERC20 for token transfers? What’s the proper way to handle multiple payment methods in a single NFT contract?