0

Our base function:

pragma solidity ^0.8.0;

import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract Token is ERC20{
    address private m_owner;

    uint256 private m_delay = 1 days;

    mapping(address => uint256) private lockTime;

    constructor(string memory name, string memory symbol) ERC20(name, symbol){
        m_owner = msg.sender;
    }

    function faucet(address to, uint256 amount) external virtual{
        require(block.timestamp > lockTime[msg.sender], "lock time has not ended");

        _mint(to, amount);

        lockTime[to] = block.timestamp + m_delay; 
    }

    function setDelay(uint256 time) public virtual{
        m_delay = time;
    }

    function setOwner(address account) public virtual{
        m_owner = account;
    }
}

Our Derived class:

pragma solidity ^0.8.0;

import "./Token.sol";
contract Dai is Token{
    
    constructor() Token("Dai", "DAI") {
    }
}

How would I be able to use faucet() and setOwner() for the Dai contract specifically to set the value for Dai contract. Would I need to create it's own faucet() and setOwner()?? If yes, then there would be no use of making a base Token class?

Such as if I were to do

Dai d = new Dai();
dai.faucet(msg.sender, 1 ether);
dai.setOwner(address(0));

I believe it would not work as stated. It would set the Owner of Token contract instead. Which wouldn't be so useful as if I were to create multiple Token derived contracts, every time I set the owner for that specific contract, it'd change the owner for the base class instead.

How do I alter the Dai contract so it changes specifically for Dai contract while not affecting other derived contracts.

1
  • Such things you don't need to believe, you can check. For example go to any environment such as RemixIDE, create 2 tokens that inherit the same contract, call .setOwner and see what happens. Jan 20, 2022 at 15:06

1 Answer 1

0

In solidity inheritance works slightly different than other languages.

Let's suppose you have a Base contract and a Derived contract.

contract Base {
    uint256 public x;

    function foo(uint256 a) public {
        x = 1000 + a;
    }

    function bar() public view returns (uint256) {
        return x;
    }
}

contract Derived is Base {
    function bar() public view returns (uint256) {
        return 123 + super.bar();
    }
}

When Derived is compiled the resulting bytecode will be similar to DerivedFlattened:

contract DerivedFlattened {
    uint256 public x;

    function foo(uint256 a) public {
        x = 1000 + a;
    }

    function Base_bar() internal view returns (uint256) {
        return x;
    }

    function bar() public view returns (uint256) {
        return 123 + Base_bar();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.