Saturday, October 23, 2021

solidity 4 inheritance


get secret from guest
//logs
call to MyContract.getSecret errored: VM error: revert.
revert
The transaction has been reverted to the initial state.
Reason provided by the contract: "must be owner".
Debug the transaction to get more information.

------------------
//MyContract.sol

pragma solidity ^0.6.0;

contract Ownable{
    address owner;
    
    constructor() public{
        owner = msg.sender;
    }
    
    modifier onlyOwner(){
        require(msg.sender == owner, 'must be owner');
        _;
    }
}

contract MyContract is Ownable{
    string secret;
    
    constructor(string memory _secret) public{
        secret = _secret;
        super;
    }
    
    function getSecret() public view onlyOwner returns(string memory){
        return secret;
    }
}

-----------------------------
//MyContract2.sol

pragma solidity ^0.6.0;

contract Ownable{
    address owner;
    
    constructor() public{
        owner = msg.sender;
    }
    
    modifier onlyOwner(){
        require(msg.sender == owner, 'must be owner');
        _;
    }
}

contract SecrectVault{
    string secrect;
    
    constructor(string memory _secrect) public{
        secrect = _secrect;
    }
    
    function getSecret() public view returns(string memory){
        return secrect;
    }
}

contract MyContract is Ownable{
    address secrectVault;
    
    constructor(string memory _secrect) public{
        SecrectVault _secrectVault = new SecrectVault(_secrect);
        secrectVault = address(_secrectVault);
        super;
    }
    
    function getSecret() public view onlyOwner returns(string memory){
        SecrectVault _secrectVault = SecrectVault(secrectVault);
        return _secrectVault.getSecret();
    }
}

reference:

No comments:

Post a Comment