Home > Enterprise >  getting parsererror I write inheritance solidity code on remix i got error
getting parsererror I write inheritance solidity code on remix i got error

Time:08-13

from solidity: contracts/inheritance add two contract.sol:32:1: ParserError: Function, variable, struct or modifier declaration expected. contract Mycontract is Ownable{ ^------^

//code pragma solidity ^0.6.0;

contract Ownable{

address public owner;

constructor( string memory _secret)public{
    owner = msg.sender;
}

modifier addcontract(string memory _secret){
    require(owner == msg.sender, "only owner can accesss");
    _;

}

}

contract secretvault { string secret;

constructor() public{
    secret = _secret;

}

function getsecret() public view returns(string memory){
    return secret;

}

contract Mycontract is Ownable{ //error is in this line

address vault;

constructor(string memory _secret)  public{
    secretvault _Vault = new Vault(_secret);
    secretvault = address(_Vault);
    super;

}
function getsecret() public view addcontract returns(string memory){
    secretvault _Vault = Vault(secretvault);
    return _Vault.getsecret();
}

}

CodePudding user response:

If the contract you are inheriting from expects arguments to its constructor, then you should pass those arguments like Ownable("secret value") in one of two places:

On the line here we declare the inheritance:

contract MyContract is Ownable("secret value") {

    address vault;

    constructor(string memory _secret) public {
        secretvault _Vault = new Vault(_secret);
        secretvault = address(_Vault);
        super;
    }
    function getsecret() public view addcontract returns(string memory){
        secretvault _Vault = Vault(secretvault);
        return _Vault.getsecret();
    }
}

Or on the child constructor:

contract MyContract is Ownable {

    address vault;

    constructor(string memory _secret) Ownable("secret value") public {
        secretvault _Vault = new Vault(_secret);
        secretvault = address(_Vault);
        super;
    }
    function getsecret() public view addcontract returns(string memory){
        secretvault _Vault = Vault(secretvault);
        return _Vault.getsecret();
    }
}

By the way, I'm not sure what you are trying to do or if you are simply playout around with some concepts, but there is no such thing as "private" or "secret" data in smart contracts and blockchain. All the data in a smart contract can be accessed directly from the blockchain, using something like web.eth.getStorageAt(contractAddress, storageIndex).

Also, I recommend you to ask these Ethereum/Solidity related question in the dedicated community: https://ethereum.stackexchange.com/

By the way, trying to guess what you are trying to do, I refactored your code to look like this:


//code
pragma solidity ^0.6.0;

contract Ownable {

   address public owner;

    constructor() public {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(owner == msg.sender, "Only owner can accesss");
        _;
    }
}

contract Vault {
    // There is nothing actually private in a smart contract or blockchain.
    string private secret;

    constructor(string memory _secret) public {
        secret = _secret;
    }

    function getSecret() public view returns(string memory){
        return secret;
    }
}

contract MyContract is Ownable {

    Vault vault;

    constructor(string memory _secret) public {
        vault = new Vault(_secret);
    }
    function getSecret() public view onlyOwner returns(string memory){
        return vault.getSecret();
    }
}

CodePudding user response:

In secretvault smart contract declaration, you must close curly brackets at the end (after getsecret() function) in this way:

contract secretvault { 
  string secret;

  constructor() public {
    secret = _secret;
  }

  function getsecret() public view returns(string memory){
    return secret;
  }

}
  • Related