At first it is impossible to recover a balance from a contract that does not have a withdrawal.
But, theoretically, if a contract inherited this. Being the same owner, would it be possible to implement a withdraw function for the inherited contract?
The main problem I see is that an ethers transfer cannot be done from another account. That is, you can't set a "from" other than the address.this, right?
I was thinking the same until I saw the following problem. All transfers are made from the original contract. Therefore, the transfer is made from the main contract and not from the inherited contract. How can you order a transfer from another contract? I think here is the key.
//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/Ownable.sol";
contract Receive is Ownable
{
constructor() {}
function getBalance() public view onlyOwner returns(uint256)
{
return address(this).balance;
}
receive() external payable {}
}
If we inherit this contract we can access to balance without problems but the withdraw function does not come out from it. And without it leaving from that inherited address the contract cannot be emptied.
Withdraw Contract:
//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import "./Receive.sol";
contract Withdraw is Ownable
{
// The token contract
Receive public token;
constructor(Receive _token)
{
token = _token;
}
function getBalance() public view onlyOwner returns(uint256)
{
return address(token).balance;
}
function withdraw() public onlyOwner payable
{
uint256 amount = address(token).balance;
token.payable(owner()).transfer(amount); // this is impossible!!
}
}
And in this way it also fails, reverts the transaction. Although I logically think that you will only access the balance of the contract withdraw.sol that is at zero.
This is not inheritance. Inheritance would be if you wrote contract Withdraw is Receive. In what you have written, it is correct that you will not be able to withdraw the balance of Receive.