From cf637cb10299ff6b665f6c7c8e4b68618ca01ec2 Mon Sep 17 00:00:00 2001 From: Albert Date: Wed, 27 Mar 2024 17:37:54 +0800 Subject: [PATCH] feat: minor refactor --- src/ProxyAdminMultiSig.sol | 30 +++++++++++++----------------- test/ProxyAdminMultiSig.t.sol | 11 +++++++++++ 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/ProxyAdminMultiSig.sol b/src/ProxyAdminMultiSig.sol index 9a96ae6..2705e08 100644 --- a/src/ProxyAdminMultiSig.sol +++ b/src/ProxyAdminMultiSig.sol @@ -19,7 +19,7 @@ contract ProxyAdminMultiSig is IErrors { string status; } - uint256 internal constant MAX_UINT256 = type(uint256).max; + uint256 public constant MAX_UINT256 = type(uint256).max; // multi-sig wallet mapping(address => address) internal _owners; @@ -190,13 +190,7 @@ contract ProxyAdminMultiSig is IErrors { function _deletePendingProposalId(uint256 proposalId) internal { // find index to be deleted - uint256 index = MAX_UINT256; - for (uint256 i = 0; i < _pendingProposalIds.length; i++) { - if (proposalId == _pendingProposalIds[i]) { - index = i; - break; - } - } + uint256 index = _getPendingProposalIndex(proposalId); // if index not equal to MAX_UINT256, it means the proposalId is found if (index != MAX_UINT256) { @@ -225,9 +219,9 @@ contract ProxyAdminMultiSig is IErrors { function _hasApproved(address owner, uint256 proposalId) internal view returns (bool) { uint256 index = MAX_UINT256; - Proposal memory proposal = _proposals[proposalId]; - for (uint256 i = 0; i < proposal.approvals.length; i++) { - if (owner == proposal.approvals[i]) { + address[] memory approvals = _proposals[proposalId].approvals; + for (uint256 i = 0; i < approvals.length; i++) { + if (owner == approvals[i]) { index = i; break; } @@ -237,17 +231,19 @@ contract ProxyAdminMultiSig is IErrors { } function _isPendingProposal(uint256 proposalId) internal view returns (bool) { - uint256 index = MAX_UINT256; + uint256 index = _getPendingProposalIndex(proposalId); + return index != MAX_UINT256; + } + + /// @dev get the index of a pending proposal, return MAX_UINT256 if not found, + function _getPendingProposalIndex(uint256 proposalId) internal view returns (uint256 index) { + index = MAX_UINT256; for (uint256 i = 0; i < _pendingProposalIds.length; i++) { if (proposalId == _pendingProposalIds[i]) { - // plus 1 because index 0 - // means a value is not in the array. - index = i + 1; + index = i; break; } } - - return index != MAX_UINT256; } /** diff --git a/test/ProxyAdminMultiSig.t.sol b/test/ProxyAdminMultiSig.t.sol index 6a44e52..4e8207f 100644 --- a/test/ProxyAdminMultiSig.t.sol +++ b/test/ProxyAdminMultiSig.t.sol @@ -47,6 +47,17 @@ contract MultiSigTest is IErrors, Test, Utils { proxy = address(transparentProxy); } + function testSetupStatus() public view { + assertEq(_getImplementation(proxy), address(implementationV1)); + assertEq(_getAdmin(proxy), address(multiSig)); + + _checkWalletDetail(2, 3, ownersArr3); + + for (uint256 i = 0; i < ownersArr3.length; i++) { + assertEq(multiSig.isOwner(ownersArr3[i]), true); + } + } + function testConstruct() public { multiSig = new ProxyAdminMultiSig(ownersArr3, 2); _checkWalletDetail(2, 3, ownersArr3);