Help with ERC20 token integration for NFT purchases
I’m working on a smart contract that should accept both ETH and USDT for minting NFTs. However I keep running into compilation issues when trying to import the ERC20 interface.
The error I get is:
DeclarationError: Identifier already declared
Here’s my contract code:
// 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/token/ERC20/IERC20.sol";
contract GameCard is ERC721, ERC721Enumerable, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _cardIdCounter;
IERC20 public paymentToken;
uint256 public ethCost = 0;
uint256 public tokenCost = 0;
uint256 public totalCards;
bool public salesActive;
mapping(address => uint256) public userMints;
constructor(address _paymentToken) ERC721("GameCard", "GCD") {
totalCards = 50;
paymentToken = IERC20(_paymentToken);
}
function updatePaymentToken(address _newToken) external onlyOwner {
paymentToken = IERC20(_newToken);
}
function updatePricing(uint256 _ethCost, uint256 _tokenCost) external onlyOwner {
ethCost = _ethCost;
tokenCost = _tokenCost;
}
function purchaseWithToken() external {
require(tokenCost > 0, "Invalid price");
paymentToken.transferFrom(msg.sender, owner(), tokenCost);
createCard();
}
function purchaseWithEth() external payable {
require(msg.value >= ethCost, "Insufficient payment");
createCard();
}
function createCard() internal {
require(salesActive, "Sales paused");
require(totalSupply() < totalCards, "No cards left");
uint256 newCardId = _cardIdCounter.current();
_cardIdCounter.increment();
_safeMint(msg.sender, newCardId);
}
function toggleSales(bool _active) external onlyOwner {
salesActive = _active;
}
function supportsInterface(bytes4 interfaceId)
public
view
override(ERC721, ERC721Enumerable)
returns (bool)
{
return super.supportsInterface(interfaceId);
}
function _beforeTokenTransfer(
address from,
address to,
uint256 cardId,
uint256 batchSize
) internal override(ERC721, ERC721Enumerable) {
super._beforeTokenTransfer(from, to, cardId, batchSize);
}
}
I’ve tried using both IERC20 and ERC20 imports but keep getting the same declaration error. What am I doing wrong with the token integration? Any help would be great since I’m still learning Solidity basics.