Skip to content

Commit

Permalink
fix(protocol): use "onlyInitializable" modifier instead of "initializer"
Browse files Browse the repository at this point in the history
refactor(protocol): mark init function as "internal" in OwnableUpgradeable
refactor(protocol): rename "__OwnableUpgradeable_init" to "__Ownable_init"
test(protocol): new GodModeOwnableUpgradeable to expose init function

See releated discussion in OpenZeppelin/openzeppelin-contracts#3006
  • Loading branch information
PaulRBerg committed Mar 3, 2022
1 parent f3cb543 commit ce70409
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/protocol/contracts/access/OwnableUpgradeable.sol
Expand Up @@ -32,7 +32,7 @@ contract OwnableUpgradeable is

/// @notice The upgradeability variant of the contract constructor.
/// @dev Sets the deployer as the initial owner.
function __OwnableUpgradeable__init() public initializer {
function __Ownable_init() internal initializer {
owner = msg.sender;
emit TransferOwnership(address(0), msg.sender);
}
Expand Down
Expand Up @@ -30,7 +30,7 @@ contract BalanceSheetV2 is
/// @param oracle_ The address of the oracle contract.
function initialize(IFintroller fintroller_, IChainlinkOperator oracle_) public initializer {
// Initialize the owner.
OwnableUpgradeable.__OwnableUpgradeable__init();
__Ownable_init();

// Set the Fintroller contract.
fintroller = fintroller_;
Expand Down
13 changes: 13 additions & 0 deletions packages/protocol/contracts/test/GodModeOwnableUpgradeable.sol
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
pragma solidity >=0.8.4;

import "../access/OwnableUpgradeable.sol";

/// @title GodModeOwnableUpgradeable
/// @author Hifi
/// @dev Strictly for test purposes. Do not use in production.
contract GodModeOwnableUpgradeable is OwnableUpgradeable {
function __godMode_Ownable_init() public {
__Ownable_init();
}
}
28 changes: 16 additions & 12 deletions packages/protocol/test/shared/deployers.ts
@@ -1,5 +1,5 @@
import type { Signer } from "@ethersproject/abstract-signer";
import { BigNumber } from "@ethersproject/bignumber";
import type { BigNumber } from "@ethersproject/bignumber";
import { artifacts, ethers, upgrades, waffle } from "hardhat";
import type { Artifact } from "hardhat/types";

Expand All @@ -14,14 +14,16 @@ import {
WBTC_SYMBOL,
} from "@hifi/constants";
import { getHTokenName, getHTokenSymbol, price } from "@hifi/helpers";
import { GodModeBalanceSheet__factory, OwnableUpgradeable, OwnableUpgradeable__factory } from "../../src/types";
import { ChainlinkOperator } from "../../src/types/ChainlinkOperator";
import { Fintroller } from "../../src/types/Fintroller";
import { GodModeBalanceSheet } from "../../src/types/GodModeBalanceSheet";
import { GodModeErc20 } from "../../src/types/GodModeErc20";
import { GodModeHToken } from "../../src/types/GodModeHToken";
import { HToken } from "../../src/types/HToken";
import { SimplePriceFeed } from "../../src/types/SimplePriceFeed";
import type { GodModeBalanceSheet__factory } from "../../src/types/factories/GodModeBalanceSheet__factory";
import type { GodModeOwnableUpgradeable__factory } from "../../src/types/factories/GodModeOwnableUpgradeable__factory";
import type { ChainlinkOperator } from "../../src/types/ChainlinkOperator";
import type { Fintroller } from "../../src/types/Fintroller";
import type { GodModeBalanceSheet } from "../../src/types/GodModeBalanceSheet";
import type { GodModeErc20 } from "../../src/types/GodModeErc20";
import type { GodModeHToken } from "../../src/types/GodModeHToken";
import type { GodModeOwnableUpgradeable } from "../../src/types/GodModeOwnableUpgradeable";
import type { HToken } from "../../src/types/HToken";
import type { SimplePriceFeed } from "../../src/types/SimplePriceFeed";

const { deployContract } = waffle;
const overrides = { gasLimit: process.env.CODE_COVERAGE ? GAS_LIMITS.coverage : GAS_LIMITS.hardhat };
Expand Down Expand Up @@ -106,9 +108,11 @@ export async function deployGodModeHToken(
return hToken;
}

export async function deployOwnableUpgradeable(): Promise<OwnableUpgradeable> {
const ownableUpgradeableFactory: OwnableUpgradeable__factory = await ethers.getContractFactory("OwnableUpgradeable");
const ownableUpgradeable: OwnableUpgradeable = <OwnableUpgradeable>(
export async function deployOwnableUpgradeable(): Promise<GodModeOwnableUpgradeable> {
const ownableUpgradeableFactory: GodModeOwnableUpgradeable__factory = await ethers.getContractFactory(
"GodModeOwnableUpgradeable",
);
const ownableUpgradeable: GodModeOwnableUpgradeable = <GodModeOwnableUpgradeable>(
await upgrades.deployProxy(ownableUpgradeableFactory)
);
await ownableUpgradeable.deployed();
Expand Down
6 changes: 3 additions & 3 deletions packages/protocol/test/shared/fixtures.ts
Expand Up @@ -18,7 +18,7 @@ import type { Fintroller } from "../../src/types/Fintroller";
import type { GodModeBalanceSheet } from "../../src/types/GodModeBalanceSheet";
import type { GodModeErc20 } from "../../src/types/GodModeErc20";
import type { GodModeHToken } from "../../src/types/GodModeHToken";
import type { OwnableUpgradeable } from "../../src/types/OwnableUpgradeable";
import type { GodModeOwnableUpgradeable } from "../../src/types/GodModeOwnableUpgradeable";
import type { SimplePriceFeed } from "../../src/types/SimplePriceFeed";
import {
deployChainlinkOperator,
Expand Down Expand Up @@ -190,10 +190,10 @@ export async function unitFixtureHToken(signers: Signer[]): Promise<UnitFixtureH
}

type UnitFixtureOwnableUpgradeable = {
ownableUpgradeable: OwnableUpgradeable;
ownableUpgradeable: GodModeOwnableUpgradeable;
};

export async function unitFixtureOwnableUpgradeable(): Promise<UnitFixtureOwnableUpgradeable> {
const ownableUpgradeable: OwnableUpgradeable = await deployOwnableUpgradeable();
const ownableUpgradeable: GodModeOwnableUpgradeable = await deployOwnableUpgradeable();
return { ownableUpgradeable };
}
4 changes: 2 additions & 2 deletions packages/protocol/test/shared/types.ts
Expand Up @@ -7,7 +7,7 @@ import type { Fintroller } from "../../src/types/Fintroller";
import type { GodModeBalanceSheet } from "../../src/types/GodModeBalanceSheet";
import type { GodModeErc20 } from "../../src/types/GodModeErc20";
import type { GodModeHToken } from "../../src/types/GodModeHToken";
import type { OwnableUpgradeable } from "../../src/types/OwnableUpgradeable";
import type { GodModeOwnableUpgradeable } from "../../src/types/GodModeOwnableUpgradeable";
import type { SimplePriceFeed } from "../../src/types/SimplePriceFeed";

declare module "mocha" {
Expand All @@ -24,7 +24,7 @@ export interface Contracts {
fintroller: Fintroller;
hTokens: GodModeHToken[];
oracle: ChainlinkOperator;
ownableUpgradeable: OwnableUpgradeable;
ownableUpgradeable: GodModeOwnableUpgradeable;
usdc: GodModeErc20;
usdcPriceFeed: SimplePriceFeed;
wbtc: GodModeErc20;
Expand Down
Expand Up @@ -4,7 +4,7 @@ import { expect } from "chai";

export function shouldBehaveLikeRenounceOwnership(): void {
beforeEach(async function () {
await this.contracts.ownableUpgradeable.connect(this.signers.admin).__OwnableUpgradeable__init();
await this.contracts.ownableUpgradeable.connect(this.signers.admin).__godMode_Ownable_init();
});

context("when the caller is not the owner", function () {
Expand Down
Expand Up @@ -5,7 +5,7 @@ import { expect } from "chai";

export function shouldBehaveLikeTransferOwnership(): void {
beforeEach(async function () {
await this.contracts.ownableUpgradeable.connect(this.signers.admin).__OwnableUpgradeable__init();
await this.contracts.ownableUpgradeable.connect(this.signers.admin).__godMode_Ownable_init();
});

context("when the caller is not the owner", function () {
Expand Down
Expand Up @@ -11,7 +11,7 @@ export function shouldBehaveLikeOwnerGetter(): void {

context("when the contract was initialized", function () {
beforeEach(async function () {
await this.contracts.ownableUpgradeable.connect(this.signers.admin).__OwnableUpgradeable__init();
await this.contracts.ownableUpgradeable.connect(this.signers.admin).__godMode_Ownable_init();
});

it("returns the correct address", async function () {
Expand Down

0 comments on commit ce70409

Please sign in to comment.