【问题标题】:Identifier already declared for Context.sol in Openzeppelin在 Openzeppelin 中已经为 Context.sol 声明了标识符
【发布时间】:2021-04-05 21:20:06
【问题描述】:

我正在做一个 NFT 项目。我的合同文件夹中有我的 NFT 文件,要从中导入 nodemodules 中的 openzeppelin 文件。但是,编译器似乎建议 Context.sol 已在我的文件中声明。我检查了 Context.sol 文件,给定的文件夹中似乎有很多,我该如何解决。

Error: DeclarationError: Identifier already declared.
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
^-------------------------------------------------------^
@openzeppelin/contracts/utils/Context.sol:15:2: The previous declaration is here
:
contract Context {
^ (Relevant source part starts here and spans across multiple lines)

NFT.sol

pragma solidity ^0.5.0;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract NFT is ERC721, Ownable {
     address payable public _owner;
     mapping (uint => bool) public sold;
     mapping (uint => uint) public price;

     event Purchase(address owner, uint price, uint id, string uri);

     constructor() ERC721("Dapp University", "DAPPU") public{
        _owner = msg.sender;
     }

     function mint(string memory _tokenURI, uint _price) public onlyOwner returns (bool) {
         uint _tokenId = totalSupply() + 1;
         price[_tokenId] = _price;

         _mint(address(this), _tokenId);
         _setTokenURI(_tokenId, _tokenURI);
    
         return true;
     }

     function buy(uint _id) external payable {
         _validate(_id); //check req. for trade
         _trade(_id); //swap nft for eth
    
         emit Purchase(msg.sender, price[_id], _id, tokenURI(_id));
     }

     function _validate(uint _id) internal {
         require(_exists(_id), "Error, wrong Token id"); //not exists
         require(!sold[_id], "Error, Token is sold"); //already sold
         require(msg.value >= price[_id], "Error, Token costs more"); //costs more
     }

    function _trade(uint _id) internal {
        _transfer(address(this), msg.sender, _id); //nft to user
        _owner.transfer(msg.value); //eth to owner
        sold[_id] = true; //nft is sold
     }
}

Context.sol

pragma solidity ^0.5.0;

contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
        return msg.data;
    }
    
}

【问题讨论】:

    标签: node.js solidity smartcontracts openzeppelin nft


    【解决方案1】:

    我找到了答案。无论您使用@O​​penZeppilin 合约做什么类型的工作,都会面临这个问题。每个子文件夹中都有一个 Context.sol 文件来帮助独立项目。但是,大量的 Context.sol 文件相互冲突,从编译器引发错误。从抛出的错误中可以看出,我不得不追溯导入并意识到 Context.sol 被跟踪并从 GSN 文件夹而不是 Utils 文件夹导入,所以我重新更改了导入以从 utils 文件夹中获取它在这里看到。虚拟关键字也必须清理以防您使用

    pragma solidity ^0.5.0;
    
    import "@openzeppelin/contracts/utils/Context.sol";
    // Change to utils folder instead of GSN folder and possibly for all clashing 
    // Context.sol files
     import "./IERC721.sol";
     import "./IERC721Receiver.sol";
     import "../../math/SafeMath.sol";
     import "../../utils/Address.sol";
     import "../../drafts/Counters.sol";
     import "../../introspection/ERC165.sol";
    
    
     contract ERC721 is  Context, ERC165, IERC721 {
     using SafeMath for uint256;
     using Address for address;
     using Counters for Counters.Counter;
    
     // Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`
     // which can be also obtained as `IERC721Receiver(0).onERC721Received.selector`
     bytes4 private constant _ERC721_RECEIVED = 0x150b7a02;
    

    【讨论】:

      猜你喜欢
      • 2019-10-10
      • 2020-02-13
      • 1970-01-01
      • 1970-01-01
      • 2023-01-22
      • 2021-11-17
      • 2021-11-18
      • 2018-08-09
      • 2019-11-15
      相关资源
      最近更新 更多