0

The function setActivity always revert due to the address ? How can I get the contract address because it sounds that the address is always all zeros when I get the value fron the (address sate variable) ?

Here is the Smart Contract :

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract Learning {

    address public owner;
    bool public isActive = true;
    uint256 public number;

    modifier checkActivity() {
        require(isActive, "Not Active any more.");
        _;
    }
    modifier onlyOwner() {
        require(msg.sender == owner, "not owner");
        _;
    }

    function setValue(uint x) checkActivity public {
           number = x;
      }

    function setActivity(bool _isActive) onlyOwner public {
          isActive = _isActive;
      }
}
Petr Hejda
  • 40,554
  • 8
  • 72
  • 100

1 Answers1

1

Your owner variable (that is validated against in the onlyOwner modifier) has the default value of 0 because it's not set anywhere.

A usual approach is to set its initial value to the deployer address in the constructor. And to enable the current owner to pass the ownership to someone else.

constructor() {
    // set the deployer as the initial owner
    owner = msg.sender;
}

// the current owner can also transfer the ownership to a different address
function transferOwnership(address newOwner) external onlyOwner {
    owner = newOwner;
}

There's also an existing Ownable library by OpenZeppelin that you can learn more about: https://docs.openzeppelin.com/contracts/4.x/access-control#ownership-and-ownable

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