How to transfer owner's fund after changing ownership

I am transferring ownership to a new address. After changing ownership, I wanted to move all funds to the new address. Writing the function like the following is correct ? I have used ```
_transfer(_owner,newOwner,balanceOf(_owner)); statement inside transferOwnership method.

     contract Ownable is Context, ERC20 {

        address private _owner;

        event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

        constructor () public {
            address msgSender = _msgSender();
            _owner = msgSender;
            emit OwnershipTransferred(address(0), msgSender);

        function owner() public view  returns (address) {
            return _owner;
        modifier onlyOwner() {
            require(owner() == _msgSender(), "Ownable: caller is not the owner");

        function transferOwnership(address newOwner) public onlyOwner {
            require(newOwner != address(0), "Ownable: new owner is the zero address");
            emit OwnershipTransferred(_owner, newOwner);
            _owner = newOwner;
1 Like

I think by this code, you can achieve what you want, although I am not sure why do you want to do this.

1 Like