Inheritance Assignment

Hey Guys,

I have those warnings in the Destroy contract for the selfdestruct, so what that’s meaning?

Hi @Ahmad_Abd_Allah

The security warning says about what can happen if you call selfdestruct function. You can ignore it as it does not have any effect in compiling the code.

Not sure about the second warning. It is a warning about assert function, which is not present in your code :thinking: .
Maybe check for more details by clicking more on the warning.

1 Like

Used multi-level inheritance, where Bank is Destroyable, which is Ownable.

pragma solidity 0.7.5;

import "./destroyable.sol";

contract Bank is Destroyable {
     ...
}

import "./ownable.sol";

contract Destroyable is Ownable{
    function close() onlyOwner public {
        selfdestruct(owner);
    }
}

contract Ownable{
    address payable owner;

    constructor() {
    owner = msg.sender;
    }

    modifier onlyOwner {
    require(msg.sender == owner, "you are not the owner");
    _;
    }
    
}

Hope this is right.

Ownable code

// SPDX-License-Identifier: MIT

pragma solidity 0.7.5;

contract Ownable    {
    address public owner;

    constructor()   {
        owner = msg.sender;
    }

    modifier onlyOwner  {
        require(msg.sender == owner, "You are not the owner!");
        _;
    }
}

Destroyable code:

//SPDX-License-Identifier: MIT
pragma solidity 0.7.5;

import "./ownable.sol";

contract Destroyable is Ownable {
    function destroy() public onlyOwner{
        selfdestruct(msg.sender);
    }
}

Here is my bank code

//SPDX-License-Identifier: MIT

pragma solidity 0.7.5;

import "./ownable.sol";
import "./destroyable.sol";

contract Bank is Ownable, Destroyable {

    mapping(address => uint) balance;

    event depositDone(uint amount, address indexed depositedTo);

    function deposit() public payable returns(uint) {
        balance[msg.sender] += msg.value;
        emit depositDone(msg.value, msg.sender);
        return balance[msg.sender];
    }

    function getBalance() public view returns (uint) {
        return balance[msg.sender];
    }

    function transfer(address receipient, uint _transferAmount) public returns(uint) {
        balance[msg.sender] -= _transferAmount;
        balance[receipient] += _transferAmount;
        return balance[receipient];
    }

    function withdraw(uint amount) public onlyOwner returns (uint)   {
        require (balance[msg.sender] >= amount, "You don't have enough funds!");
        uint previousBalance = balance[msg.sender];
        balance[msg.sender] -= amount;
        msg.sender.transfer (amount);
        assert (balance[msg.sender] == previousBalance - amount);
        return balance[msg.sender];
    }

}
1 Like