ethereum / solc-js

Javascript bindings for the Solidity compiler
https://soliditylang.org
MIT License
1.45k stars 474 forks source link

Identifier already declared #696

Closed C0D3O closed 1 year ago

C0D3O commented 1 year ago

hi, I can't figure out how should I use openzeppelin contract with solc for it not to throw the error... (the screenshot is more obvious in the last message)

{ errors: [ { component: 'general', errorCode: '2333', formattedMessage: 'DeclarationError: Identifier already declared.\n' + ' --> CryptoDevs.sol:6:1:\n' + ' |\n' + formattedMessage: 'DeclarationError: Identifier already declared.\n' + ' --> CryptoDevs.sol:7:1:\n' + ' |\n' + '7 | import "./IWhitelist.sol";\n' + ' | ^^^^^^^^^^^^^^^^^^^^^^^^^^\n' + 'Note: The previous declaration is here:\n' + ' --> @openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol:16:1:\n' + ' |\n' + '16 | abstract contract Context {\n' + ' | ^ (Relevant source part starts here and spans across multiple lines).\n' + '\n', message: 'Identifier already declared.', secondarySourceLocations: [Array], severity: 'error', sourceLocation: [Object], type: 'DeclarationError' } ], sources: { '@openzeppelin/contracts/access/Ownable.sol': { id: 0 }, '@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol': { id: 1 }, 'CryptoDevs.sol': { id: 2 }, 'IWhitelist.sol': { id: 3 } } }

the code

` // SPDX-License-Identifier: MIT pragma solidity 0.8.19;

// import "@openzeppelin/contracts/utils/Context.sol"; import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; import "./IWhitelist.sol";

contract CryptoDevs is ERC721, Ownable { string _baseTokenURI;

uint256 public _price = 0.01 ether;

bool public _paused;
uint8 public tokenIds;
uint8 _maxTokenIds = 10;

IWhitelist whitelist;

bool public presaleStarted;
uint256 public presaleEnded;

modifier onlyWhenNotPaused{
    require(!_paused, "The presale is paused rn");
    _;
}

constructor (string memory baseURI, address whitelistContract) ERC721('CryptoDevs', "CD"){
    _baseTokenURI = baseURI;
    whitelist = IWhitelist(whitelistContract);
}

function startPresale() public onlyOwner {
    presaleStarted = true;
    presaleEnded = block.timestamp + 5 minutes;
}

function presaleMint() public payable onlyWhenNotPaused{
    require(presaleStarted && block.timestamp < presaleEnded, "Presale is not active rn");
    require(whitelist.whitelistedAddresses(msg.sender), "You are not whitelisted");
    require(msg.value >= _price, "Wrong amount of ether sent");
    require(tokenIds < _maxTokenIds, "No NFTs left to mint");
    tokenIds += 1;

    _safeMint(msg.sender, tokenIds);
}

function mint() public payable onlyWhenNotPaused {
    require(presaleStarted && block.timestamp >=  presaleEnded, "Presale has not ended yet");
    require(tokenIds < _maxTokenIds, "Exceed maximum Crypto Devs supply");
    require(msg.value >= _price, "Ether sent is not correct");
    tokenIds += 1;
    _safeMint(msg.sender, tokenIds);
}

function _baseURI() internal view virtual override returns(string memory) {
    return _baseTokenURI;
}

function setPaused(bool val) public onlyOwner{
    _paused = val;
}

function withdraw() public onlyOwner  {
    address _owner = owner();
    uint256 amount = address(this).balance;
    (bool sent, ) =  _owner.call{value: amount}("");
    require(sent, "Failed to send Ether");
}

receive() external payable {}

fallback() external payable {}

} `

index.ts `import fs from 'fs'; //@ts-ignore import solc from 'solc'; import { JsonRpcProvider, ContractFactory, Wallet } from 'ethers'; import 'dotenv/config'; import { METADATA_URL, WHITELIST_CONTRACT_ADDRESS } from './config';

(async () => { const source = fs.readFileSync('./contracts/CryptoDevs.sol', 'utf-8');

const findImports = () => {
    const lines = source.split('\n');
    for (let line of lines) {
        if (/import "/i.test(line)) {
            const neededPart = line.split('"')[1];

            if (/@/.test(neededPart)) {
                return {
                    contents: fs.readFileSync('./node_modules/' + neededPart, 'utf-8'),
                };
            } else {
                return {
                    contents: fs.readFileSync('./contracts' + neededPart.slice(1)),
                };
            }
        }
    }
};

const input = {
    language: 'Solidity',
    sources: {
        'CryptoDevs.sol': {
            content: source,
        },
    },
    settings: {
        outputSelection: {
            '*': {
                '*': ['*'],
            },
        },
    },
};

const contractsData = JSON.parse(solc.compile(JSON.stringify(input), { import: findImports }));
console.log(contractsData);

})(); `

could you please tell how to make in the right way? everybody just advicing me to use hardhat or smth else... and telling that solc is really bad for anything.... but is it?)) thank you

C0D3O commented 1 year ago

image

r0qs commented 1 year ago

Hi @moneyDev1111, please post questions in our forum or Matrix channel #solidity.

Related with your problem, as the error suggests, it seems that you have a redeclaration of Context, most probably in this file IWhitelist.sol.