1

I want to override transferOwnership from Ownable However, the internal function, _transferOwnership, is not being recognized. I either have to have a way to call this or to alter _transferOwnership directly.

function transferOwnership(address newOwner) public virtual onlyOwner {
    require(newOwner != address(0), "Ownable: new owner is the zero address");
    _transferOwnership(newOwner);
}
daveaneo
  • 29
  • 3

1 Answers1

1

The only thing you're missing is the override keyword.

Docs: https://docs.soliditylang.org/en/v0.8.9/contracts.html#function-overriding

pragma solidity ^0.8;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";

contract MyContract is Ownable {
    function transferOwnership(address newOwner) public virtual override onlyOwner {
        // TODO your own implementation
    }
}

Note: You can also remove the virtual keyword if you don't want/need this function to be further overwritable (by contracts deriving from the MyContract).

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100