0

I'm new in Solidity and trying to allow purchasing using ETH and other token such as USDT but I keep getting this error no matter what I tried.

    DeclarationError: Identifier already declared. --> contract-9b5b02c5de.sol:9:1:
|
9 | import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

This is my code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "@openzeppelin/[email protected]/token/ERC721/ERC721.sol";
import "@openzeppelin/[email protected]/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/[email protected]/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/[email protected]/access/Ownable.sol";
import "@openzeppelin/[email protected]/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";


contract Combat is ERC721, ERC721Enumerable, ERC721URIStorage, Ownable {
    using Counters for Counters.Counter;

    Counters.Counter private _tokenIdCounter;

    ERC20 public tokenUsdt;
    uint256 public ethPrice = 0;
    uint256 public usdtPrice = 0;
    uint256 public maxSupply; //Maximum amount token
    bool public isMintEnabled; //default : false
    mapping(address=>uint256) public mintedWallets;


    constructor(address _tokenUsdt) ERC721("XXX", "QAZ") {
        maxSupply = 100;
        tokenUsdt = ERC20(_tokenUsdt);
    }

    function setToken(address _tokenUsdt) external onlyOwner {
        tokenUsdt = _tokenUsdt;
    }

    function setPrice(uint256 _ethPrice, uint256 _usdtPrice) external onlyOwner {
        ethPrice = _ethPrice;
        usdtPrice = _usdtPrice;
    }

    function buyMembershipUsdt() external payable {
        require(msg.value >= usdtPrice, "Price Error");
        tokenUsdt.safeTransferFrom(msg.sender, owner(), _amount);
        mintMembership();
    }

    function buyMembershipEth() external payable {
        require(msg.value >= ethPrice, "Price Error");
        mintMembership();
    }

    function mintMembership() internal {
        require(isMintEnabled, "Not For Sale");
        require(totalSupply() < maxSupply, "Sold Out");

        uint256 tokenId = _tokenIdCounter.current();
        _tokenIdCounter.increment();
        _safeMint(msg.sender, tokenId);
    }

    function setMintEnabled(bool isMintEnabled_) external onlyOwner {
        isMintEnabled = isMintEnabled_;
    }

    function _baseURI() internal pure override returns (string memory) {
        return "ipfs://bafybeiebgbvibloa3p3vge7ecxobwxxnuyg4pdozbcfjigfglhz2ogidq4/";
    }

    function setMaxSupply(uint256 maxSupply_) external onlyOwner{
        maxSupply = maxSupply_;
    }

    function withdraw() public onlyOwner {
        require(address(this).balance > 0, "Balance is 0");
        payable(owner()).transfer(address(this).balance);
    }

    function withdrawToken() external onlyOwner {
        uint256 tokenBalance = tokenUsdt.balanceOf(address(this));
        require(tokenBalance > 0, "Insufficient balance");
        tokenUsdt.safeTransfer(msg.sender, tokenBalance);
  }

  // The following functions are overrides required by Solidity.

    function tokenURI(uint256 tokenId)
        public
        view
        override(ERC721, ERC721URIStorage)
        returns (string memory)
    {
        return super.tokenURI(tokenId);
    }
}

I seen some guides our there but none seems to help because it uses custom token which I'm not using it. Regardless whether I'm using IERC20 or ERC20, I will keep getting error above I mentioned. Am I missing something or what?

2
  • Looks like some conflict of imported files, I usually see this when trying to import two contracts that have the same name but different implementation. See what happens if you try running the import statement by themselves? Aka, delete everything except for the imports, contract declaration, and an empty constructor to call the erc721 constructor and see if it compiles.
    – Bruce
    Commented Dec 27, 2022 at 6:07
  • Then you can manually add/remove import statements to see which ones are conflicting (if that turns out to be the issue). If you find two imports that are conflicting, I recommend looking at their implementations on the open zeppelin github to see if they are importing anything the the same names, and also generally examine them for what might be causing the problem.
    – Bruce
    Commented Dec 27, 2022 at 6:09

1 Answer 1

0

This problem might be arising since you are using a specific version of openzeppelin, (4.8.0) for all of your contracts but the erc20 implementation. Perhaps you should also try grabbing the erc20 implementation from that version (import "@openzeppelin/[email protected]/token/ERC20/ERC20.sol";) and see if that fixes it. You might be trying to import 2 different version of the same openzeppelin file, with different implementations since you are specifying a specific version with some imports but not with others.

1
  • tried, it doesn't work too, same error.
    – MuthaFury
    Commented Dec 27, 2022 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.