definition of base has to precede definition of derived contract
Asked Answered
S

1

6

I have two different files which are Project1.sol and Project2.sol

Project2.sol is like:

import "./Project1.sol";
        
contract Project2{
    address newProject1Address =address(new Project1());
}

Project1.sol is like:

import "./Project2.sol";

contract Project1 is Project2{

}

I have deployed Project1 inside of Project2.sol file. And also I have been using a struct which is in Project2 from Project1.sol file.

I got an error which is "definition of base has to precede definition of derived contract." for this line: contract Project1 is Project2{

After I checked the error on the internet, there were solutions for two contracts and one file. However, I had two files.

I merged these two contracts in a file.

This is what I did:

pragma solidity >=0.7.0 <0.9.0;

contract Project2{

        Apple[] public applepies;
        
        struct Apple{
             string name;
             mapping (address => bool) applepie;
        }

        function createProject() external{
            
             address newProject1Address =address(new Project1(msg.sender));

             uint idx = applepies.length;
             applepies.push();
             Apple storage newProject = applepies[idx];
        }

        

    }


contract Project1 is Project2{
        address public creator;

        constructor (address creator1){
            
             creator= creator1;
        }

        function getDetails(uint index) public{
             Apple storage newv= applepies[index];
             //require(newv.applepie = msg.sender);
        }
}

Then, I could not deploy Project1 from Project2. When I do that, this is the error which I got, "circular reference for contract creation(cannot create instance of derived or same contract)" on this part address newProject1Address =address(new Project1());

What should I do? What is your suggestions?

Stites answered 4/5, 2021 at 13:2 Comment(3)
Can you clarify what you mean by "I have deployed Project1 inside of Project2.sol file"? ... It doesn't make much sense: 1) A contract is deployed to a network, not to a file. 2) The error message and "Project1 inside of Project2.sol" suggest that you have both contracts defined in one file, but your code examples explicitly define two files - "Project1.sol" and "Project2.sol".Cotangent
@PetrHejda 1) I fixed the part of what I was trying to say on the post 2) And added the parts which you said. Could you check it again? I hope it is more clear right now. Sorry for my english.Stites
Few more things: 1) What version of Solidity are you using? 2) function createProject{ throws syntax error. It's valid either in some very old version, or you didn't copy-paste correctly the parenthesis (old Solidity version) and visibility modifier (public, external, ...). Can you also correct this?Cotangent
F
4

You are instantiating a contract inside another contract which is inhering the callee. Kinda circular reference.

When you inherit a contract(Project1) from a base contract (Project 2), the inheriting (Project1) should be the one that makes the calls. Your (Project2) contract acts like a base contract. The inheriting contract (Project1) can call or override all the functions in the base contract if needed. So here my suggestions (it's one way among many to solve your problem). See below. I compiled it and deployed and it works. Don't forget to provide an address when deploying. I hope this would help. If you have other questions you reach to me.

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
pragma experimental ABIEncoderV2;

contract Project2{

       
        
        struct Apple{
             string name;
             bool applepie;
             //mapping (address => bool) applepie;
        }
        
        Apple[] public applepies;
        Apple public newProject;
        
        constructor() {
            
             //address newProject1Address = address(new Project1(msg.sender));

             //uint idx = applepies.length;
             newProject = Apple("superApple", true);
             applepies.push(newProject);
        }

         function getNewProject() public view returns(string memory){
             return newProject.name;
        }

    }


contract Project1 is Project2{
        address public creator;

        constructor (address creator1){
            
             creator= creator1;
        }

        function getDetails(uint index) public view returns (string memory){
             Apple storage newv= applepies[index];
             //require(newv.applepie = msg.sender);
             return newv.name;
        }
}
Footling answered 7/5, 2021 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.