Questions on initiating constructor in DecentralizedStableCoin.sol, when it inherits Ownable contract. #286
-
in DecentralisedStableCoin.sol, it inherits the 1) ERC20Burnable, which inherits the ERC20, and also the 2) Ownable contract. contract DecentralizedStableCoin is ERC20Burnable, Ownable {
error DecentralizedStableCoin__MustBeMoreThanZero();
error DecentralizedStableCoin__BurnAmountExceedsBalance();
error DecentralizedStableCoin__NotZeroAddress();
constructor () ERC20 ("DecentralizedStableCoin","DSC") {
} why is it not something like: constructor () ERC20("DecentralizedStableCoin","DSC") Ownable(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266){
} since under "Ownable.sol", there is a constructor, which is:
My underrstanding is that the child contract inheriting the parent contract, will need to also include the parent's constructor. Whis is the example: it inherits both parents, but only instantiate the ERC20 token, and does not include the Ownable's constructor. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
EDIT: There was an update to OpenZeppelin's code two months ago that changed from the code I wrote below, to what you have. So yes, you should pass a parameter when initiating Ownable contract. Changes in the code can be seen in OpenZeppelin's repo, or specifically in this change's commit. It's because the constructor in Ownable contract doesn't take any parameters. As you can see in the following code snipper (I've only copied the relevant part), when Ownable is called, whatever is in the constructor will be automatically run - which is the abstract contract Ownable is Context {
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor() {
_transferOwnership(_msgSender());
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
} On the other hand, in the ERC20 constructor, you can see that there's two parameters in the constructor, which must be entered when calling ERC20 contract: contract ERC20 is Context, IERC20, IERC20Metadata {
string private _name;
string private _symbol;
/**
* @dev Sets the values for {name} and {symbol}.
*
* All two of these values are immutable: they can only be set once during
* construction.
*/
constructor(string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
} |
Beta Was this translation helpful? Give feedback.
-
In my (and the course) "Ownable.sol" from the OpenZeppelin contracts there is this constructor: /**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor() {
_transferOwnership(_msgSender());
} Where are you getting that "Ownable.sol"? |
Beta Was this translation helpful? Give feedback.
-
Well i have tried this |
Beta Was this translation helpful? Give feedback.
EDIT: There was an update to OpenZeppelin's code two months ago that changed from the code I wrote below, to what you have. So yes, you should pass a parameter when initiating Ownable contract. Changes in the code can be seen in OpenZeppelin's repo, or specifically in this change's commit.
So when this course was filmed, Patrick used the version that had no parameters in the constructor, but there was an update since so now you must specify it :)
It's because the constructor in Ownable contract doesn't take any parameters.
As you can see in the following code snipper (I've only copied the relevant part), when Ownable is called, whatever is in the constructor will be automatically run …