Skip to content

Commit

Permalink
feat: add upgradeable deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
superical committed Mar 21, 2024
1 parent 2e92076 commit 9f381ad
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 31 deletions.
24 changes: 24 additions & 0 deletions script/DocumentStoreUpgradeable.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity >=0.8.23 <0.9.0;

import "./DeployBase.s.sol";
import "../src/upgradeables/DocumentStoreUpgradeable.sol";
import {DeployUtils} from "../src/libraries/DeployUtils.sol";

contract DocumentStoreUpgradeableScript is DeployBaseScript {
function run(string memory name, address admin) public returns (DocumentStoreUpgradeable ds) {
_requireParams(name, admin);

console2.log("DocumentStore Name: ", name);
console2.log("DocumentStore Admin: ", admin);

vm.broadcast();
(address pAddr, ) = DeployUtils.deployDocumentStoreUpgradeable(name, admin);
ds = DocumentStoreUpgradeable(pAddr);
}

function _requireParams(string memory name, address admin) private pure {
require(bytes(name).length > 0, "Name is required");
require(admin != address(0), "Admin address is required");
}
}
30 changes: 30 additions & 0 deletions script/OwnableDocumentStoreUpgradeable.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity >=0.8.23 <0.9.0;

import "./DeployBase.s.sol";
import "../src/upgradeables/OwnableDocumentStoreUpgradeable.sol";
import {DeployUtils} from "../src/libraries/DeployUtils.sol";

contract OwnableDocumentStoreUpgradeableScript is DeployBaseScript {
function run(
string memory name,
string memory symbol,
address admin
) public returns (OwnableDocumentStoreUpgradeable ds) {
_requireParams(name, symbol, admin);

console2.log("OwnableDocumentStore Name: ", name);
console2.log("OwnableDocumentStore Symbol: ", symbol);
console2.log("OwnableDocumentStore Admin: ", admin);

vm.broadcast();
(address pAddr, ) = DeployUtils.deployOwnableDocumentStoreUpgradeable(name, symbol, admin);
ds = OwnableDocumentStoreUpgradeable(pAddr);
}

function _requireParams(string memory name, string memory symbol, address admin) private pure {
require(bytes(name).length > 0, "Name is required");
require(bytes(symbol).length > 0, "Symbol is required");
require(admin != address(0), "Admin address is required");
}
}
2 changes: 1 addition & 1 deletion src/initializables/DocumentStoreInitializable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract DocumentStoreInitializable is BaseDocumentStore {
* @param _name The name of the contract
* @param initAdmin The owner of the contract
*/
function initialize(string memory _name, address initAdmin) public initializer {
function initialize(string memory _name, address initAdmin) external initializer {
__BaseDocumentStore_init(_name, initAdmin);
}
}
2 changes: 1 addition & 1 deletion src/initializables/OwnableDocumentStoreInitializable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "../base/BaseOwnableDocumentStore.sol";
contract OwnableDocumentStoreInitializable is BaseOwnableDocumentStore {
constructor() initializer {}

function initialize(string memory name_, string memory symbol_, address initAdmin) public initializer {
function initialize(string memory name_, string memory symbol_, address initAdmin) external initializer {
__OwnableDocumentStore_init(name_, symbol_, initAdmin);
}
}
4 changes: 2 additions & 2 deletions src/utils/DeployUtils.sol → src/libraries/DeployUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "../upgradeables/OwnableDocumentStoreUpgradeable.sol";

library DeployUtils {
function deployDocumentStoreUpgradeable(string memory name, address initAdmin) internal returns (address, address) {
bytes memory initData = abi.encodeCall(DocumentStoreUpgradeable.initialize, (name, initAdmin));
bytes memory initData = abi.encodeCall(DocumentStoreInitializable.initialize, (name, initAdmin));

DocumentStoreUpgradeable documentStore = new DocumentStoreUpgradeable();
address dsAddr = address(documentStore);
Expand All @@ -23,7 +23,7 @@ library DeployUtils {
string memory symbol,
address initAdmin
) internal returns (address, address) {
bytes memory initData = abi.encodeCall(OwnableDocumentStoreUpgradeable.initialize, (name, symbol, initAdmin));
bytes memory initData = abi.encodeCall(OwnableDocumentStoreInitializable.initialize, (name, symbol, initAdmin));

OwnableDocumentStoreUpgradeable documentStore = new OwnableDocumentStoreUpgradeable();
address dsAddr = address(documentStore);
Expand Down
19 changes: 3 additions & 16 deletions src/upgradeables/DocumentStoreUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,13 @@
pragma solidity >=0.8.23 <0.9.0;

import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "../base/BaseDocumentStore.sol";

import "../initializables/DocumentStoreInitializable.sol";

/**
* @title DocumentStore
* @notice A contract for storing and revoking documents with access control
*/
contract DocumentStoreUpgradeable is UUPSUpgradeable, BaseDocumentStore {
/**
* @notice Initialises the contract with a name and initial admin
*/
constructor() initializer {}

/**
* @notice Internally initialises the contract with a name and owner
* @param _name The name of the contract
* @param initAdmin The owner of the contract
*/
function initialize(string memory _name, address initAdmin) public initializer {
__BaseDocumentStore_init(_name, initAdmin);
}

contract DocumentStoreUpgradeable is UUPSUpgradeable, DocumentStoreInitializable {
function _authorizeUpgrade(address) internal view virtual override onlyRole(DEFAULT_ADMIN_ROLE) {}
}
9 changes: 2 additions & 7 deletions src/upgradeables/OwnableDocumentStoreUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
pragma solidity >=0.8.23 <0.9.0;

import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "../base/BaseOwnableDocumentStore.sol";

contract OwnableDocumentStoreUpgradeable is UUPSUpgradeable, BaseOwnableDocumentStore {
constructor() initializer {}

function initialize(string memory name_, string memory symbol_, address initAdmin) public initializer {
__OwnableDocumentStore_init(name_, symbol_, initAdmin);
}
import "../initializables/OwnableDocumentStoreInitializable.sol";

contract OwnableDocumentStoreUpgradeable is UUPSUpgradeable, OwnableDocumentStoreInitializable {
function _authorizeUpgrade(address) internal view virtual override onlyRole(DEFAULT_ADMIN_ROLE) {}
}
4 changes: 2 additions & 2 deletions test/DocumentStoreUpgradeable.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

import "../src/upgradeables/DocumentStoreUpgradeable.sol";
import {CommonTest} from "./CommonTest.t.sol";
import {DeployUtils} from "../src/utils/DeployUtils.sol";
import {DeployUtils} from "../src/libraries/DeployUtils.sol";

contract DocumentStoreUpgradeable_Test is CommonTest {
DocumentStoreUpgradeable public dsProxy;
Expand Down Expand Up @@ -70,7 +70,7 @@ contract DocumentStoreUpgradeable_Test is CommonTest {

function testUpgradeToAndCallReinitialiseFail() public {
address newImplementation = address(new DocumentStoreUpgradeable());
bytes memory initData = abi.encodeCall(DocumentStoreUpgradeable.initialize, (initialName, owner));
bytes memory initData = abi.encodeCall(DocumentStoreInitializable.initialize, (initialName, owner));

vm.expectRevert(abi.encodeWithSelector(Initializable.InvalidInitialization.selector));

Expand Down
4 changes: 2 additions & 2 deletions test/OwnableDocumentStoreUpgradeable.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

import "../src/upgradeables/OwnableDocumentStoreUpgradeable.sol";
import {CommonTest} from "./CommonTest.t.sol";
import {DeployUtils} from "../src/utils/DeployUtils.sol";
import {DeployUtils} from "../src/libraries/DeployUtils.sol";

contract OwnableDocumentStoreUpgradeable_Test is CommonTest {
OwnableDocumentStoreUpgradeable public dsProxy;
Expand Down Expand Up @@ -78,7 +78,7 @@ contract OwnableDocumentStoreUpgradeable_Test is CommonTest {
function testUpgradeToAndCallReinitialiseFail() public {
address newImplementation = address(new OwnableDocumentStoreUpgradeable());
bytes memory initData = abi.encodeCall(
OwnableDocumentStoreUpgradeable.initialize,
OwnableDocumentStoreInitializable.initialize,
(initialName, initialSymbol, owner)
);

Expand Down

0 comments on commit 9f381ad

Please sign in to comment.