-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented PermanentOwnable contract with the test coverage (#82)
* Implemented PermanentOwnable contract with the test coverage * Refactored the code * Integrated PermanentOwnable into ProxyBeacon and TransparentProxyUpgrader contracts * Updated the patch version and refactored imports
- Loading branch information
Showing
10 changed files
with
123 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.4; | ||
|
||
/** | ||
* @notice The PermanentOwnable module | ||
* | ||
* Contract module which provides a basic access control mechanism, where there is | ||
* an account (an owner) that can be granted exclusive access to specific functions. | ||
* | ||
* The owner is set to the address provided by the deployer. The ownership cannot be further changed. | ||
* | ||
* This module will make available the modifier `onlyOwner`, which can be applied | ||
* to your functions to restrict their use to the owners. | ||
*/ | ||
abstract contract PermanentOwnable { | ||
address private immutable _OWNER; | ||
|
||
/** | ||
* @dev Throws if called by any account other than the owner. | ||
*/ | ||
modifier onlyOwner() { | ||
_onlyOwner(); | ||
_; | ||
} | ||
|
||
/** | ||
* @notice Initializes the contract setting the address provided by the deployer as the owner. | ||
* @param owner_ the address of the permanent owner. | ||
*/ | ||
constructor(address owner_) { | ||
require(owner_ != address(0), "PermanentOwnable: zero address can not be the owner"); | ||
|
||
_OWNER = owner_; | ||
} | ||
|
||
/** | ||
* @notice Returns the address of the owner. | ||
* @return the permanent owner. | ||
*/ | ||
function owner() public view virtual returns (address) { | ||
return _OWNER; | ||
} | ||
|
||
function _onlyOwner() internal view virtual { | ||
require(_OWNER == msg.sender, "PermanentOwnable: caller is not the owner"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.4; | ||
|
||
import {PermanentOwnable} from "../../access-control/PermanentOwnable.sol"; | ||
|
||
contract PermanentOwnableMock is PermanentOwnable { | ||
event ValidOwner(); | ||
|
||
constructor(address _owner) PermanentOwnable(_owner) {} | ||
|
||
function onlyOwnerMethod() external onlyOwner { | ||
emit ValidOwner(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { ethers } from "hardhat"; | ||
import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers"; | ||
import { expect } from "chai"; | ||
import { Reverter } from "@/test/helpers/reverter"; | ||
import { ZERO_ADDR } from "@/scripts/utils/constants"; | ||
|
||
import { PermanentOwnableMock } from "@ethers-v6"; | ||
|
||
describe("PermanentOwnable", () => { | ||
const reverter = new Reverter(); | ||
|
||
let OWNER: SignerWithAddress; | ||
let OTHER: SignerWithAddress; | ||
|
||
let permanentOwnable: PermanentOwnableMock; | ||
|
||
before("setup", async () => { | ||
[OWNER, OTHER] = await ethers.getSigners(); | ||
|
||
const permanentOwnableMock = await ethers.getContractFactory("PermanentOwnableMock"); | ||
permanentOwnable = await permanentOwnableMock.deploy(OWNER); | ||
|
||
await reverter.snapshot(); | ||
}); | ||
|
||
afterEach(reverter.revert); | ||
|
||
describe("PermanentOwnable", () => { | ||
it("should set the correct owner", async () => { | ||
expect(await permanentOwnable.owner()).to.equal(OWNER.address); | ||
}); | ||
|
||
it("should reject zero address during the owner initialization", async () => { | ||
const permanentOwnableMock = await ethers.getContractFactory("PermanentOwnableMock"); | ||
|
||
await expect(permanentOwnableMock.deploy(ZERO_ADDR)).to.be.revertedWith( | ||
"PermanentOwnable: zero address can not be the owner", | ||
); | ||
}); | ||
|
||
it("only owner should call this function", async () => { | ||
expect(await permanentOwnable.connect(OWNER).onlyOwnerMethod()).to.emit(permanentOwnable, "ValidOwner"); | ||
await expect(permanentOwnable.connect(OTHER).onlyOwnerMethod()).to.be.revertedWith( | ||
"PermanentOwnable: caller is not the owner", | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters