Skip to content

Commit

Permalink
paginator & EIP-6224 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Arvolear committed Apr 19, 2023
1 parent 07d1bc4 commit 1e8cf86
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 25 deletions.
20 changes: 14 additions & 6 deletions contracts/contracts-registry/AbstractContractsRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ abstract contract AbstractContractsRegistry is Initializable {
mapping(string => address) private _contracts;
mapping(address => bool) private _isProxy;

event AddedContract(string name, address contractAddress, bool isProxy);
event RemovedContract(string name);
event ContractAdded(string name, address contractAddress);
event ProxyContractAdded(string name, address contractAddress, address implementation);
event ProxyContractUpgraded(string name, address newImplementation);
event ContractRemoved(string name);

/**
* @notice The proxy initializer function
Expand Down Expand Up @@ -147,6 +149,8 @@ abstract contract AbstractContractsRegistry is Initializable {
require(_isProxy[contractToUpgrade_], "ContractsRegistry: not a proxy contract");

_proxyUpgrader.upgrade(contractToUpgrade_, newImplementation_, data_);

emit ProxyContractUpgraded(name_, newImplementation_);
}

/**
Expand All @@ -160,7 +164,7 @@ abstract contract AbstractContractsRegistry is Initializable {

_contracts[name_] = contractAddress_;

emit AddedContract(name_, contractAddress_, false);
emit ContractAdded(name_, contractAddress_);
}

/**
Expand All @@ -179,7 +183,7 @@ abstract contract AbstractContractsRegistry is Initializable {
_contracts[name_] = proxyAddr_;
_isProxy[proxyAddr_] = true;

emit AddedContract(name_, proxyAddr_, true);
emit ProxyContractAdded(name_, proxyAddr_, contractAddress_);
}

/**
Expand All @@ -195,7 +199,11 @@ abstract contract AbstractContractsRegistry is Initializable {
_contracts[name_] = contractAddress_;
_isProxy[contractAddress_] = true;

emit AddedContract(name_, contractAddress_, true);
emit ProxyContractAdded(
name_,
contractAddress_,
_proxyUpgrader.getImplementation(contractAddress_)
);
}

/**
Expand All @@ -210,6 +218,6 @@ abstract contract AbstractContractsRegistry is Initializable {
delete _isProxy[contractAddress_];
delete _contracts[name_];

emit RemovedContract(name_);
emit ContractRemoved(name_);
}
}
13 changes: 7 additions & 6 deletions contracts/contracts-registry/proxy/ProxyUpgrader.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ contract ProxyUpgrader {

address private immutable _OWNER;

event Upgraded(address proxy, address implementation);

modifier onlyOwner() {
require(_OWNER == msg.sender, "ProxyUpgrader: not an owner");
_onlyOwner();
_;
}

Expand All @@ -32,15 +30,18 @@ contract ProxyUpgrader {
} else {
TransparentUpgradeableProxy(payable(what_)).upgradeTo(to_);
}

emit Upgraded(what_, to_);
}

function getImplementation(address what_) external view onlyOwner returns (address) {
// bytes4(keccak256("implementation()")) == 0x5c60da1b
(bool success_, bytes memory returndata_) = address(what_).staticcall(hex"5c60da1b");
require(success_);

require(success_, "ProxyUpgrader: not a proxy");

return abi.decode(returndata_, (address));
}

function _onlyOwner() internal view {
require(_OWNER == msg.sender, "ProxyUpgrader: not an owner");
}
}
19 changes: 9 additions & 10 deletions contracts/libs/arrays/Paginator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {StringSet} from "../data-structures/StringSet.sol";
*
* Supports the following data types `uin256[]`, `address[]`, `bytes32[]`, `UintSet`,
* `AddressSet`, `BytesSet`, `StringSet`.
*
*/
library Paginator {
using EnumerableSet for *;
Expand All @@ -34,7 +33,7 @@ library Paginator {
uint256 offset_,
uint256 limit_
) internal view returns (uint256[] memory list_) {
uint256 to_ = _handleIncomingParametersForPart(arr.length, offset_, limit_);
uint256 to_ = getTo(arr.length, offset_, limit_);

list_ = new uint256[](to_ - offset_);

Expand All @@ -48,7 +47,7 @@ library Paginator {
uint256 offset_,
uint256 limit_
) internal view returns (address[] memory list_) {
uint256 to_ = _handleIncomingParametersForPart(arr.length, offset_, limit_);
uint256 to_ = getTo(arr.length, offset_, limit_);

list_ = new address[](to_ - offset_);

Expand All @@ -62,7 +61,7 @@ library Paginator {
uint256 offset_,
uint256 limit_
) internal view returns (bytes32[] memory list_) {
uint256 to_ = _handleIncomingParametersForPart(arr.length, offset_, limit_);
uint256 to_ = getTo(arr.length, offset_, limit_);

list_ = new bytes32[](to_ - offset_);

Expand All @@ -76,7 +75,7 @@ library Paginator {
uint256 offset_,
uint256 limit_
) internal view returns (uint256[] memory list_) {
uint256 to_ = _handleIncomingParametersForPart(set.length(), offset_, limit_);
uint256 to_ = getTo(set.length(), offset_, limit_);

list_ = new uint256[](to_ - offset_);

Expand All @@ -90,7 +89,7 @@ library Paginator {
uint256 offset_,
uint256 limit_
) internal view returns (address[] memory list_) {
uint256 to_ = _handleIncomingParametersForPart(set.length(), offset_, limit_);
uint256 to_ = getTo(set.length(), offset_, limit_);

list_ = new address[](to_ - offset_);

Expand All @@ -104,7 +103,7 @@ library Paginator {
uint256 offset_,
uint256 limit_
) internal view returns (bytes32[] memory list_) {
uint256 to_ = _handleIncomingParametersForPart(set.length(), offset_, limit_);
uint256 to_ = getTo(set.length(), offset_, limit_);

list_ = new bytes32[](to_ - offset_);

Expand All @@ -118,7 +117,7 @@ library Paginator {
uint256 offset_,
uint256 limit_
) internal view returns (string[] memory list_) {
uint256 to_ = _handleIncomingParametersForPart(set.length(), offset_, limit_);
uint256 to_ = getTo(set.length(), offset_, limit_);

list_ = new string[](to_ - offset_);

Expand All @@ -127,11 +126,11 @@ library Paginator {
}
}

function _handleIncomingParametersForPart(
function getTo(
uint256 length_,
uint256 offset_,
uint256 limit_
) private pure returns (uint256 to_) {
) internal pure returns (uint256 to_) {
to_ = offset_ + limit_;

if (to_ > length_) {
Expand Down
2 changes: 1 addition & 1 deletion contracts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dlsl/dev-modules",
"version": "2.3.4",
"version": "2.4.0",
"license": "MIT",
"author": "Distributed Lab",
"readme": "README.md",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dlsl",
"version": "2.3.4",
"version": "2.4.0",
"license": "MIT",
"author": "Distributed Lab",
"description": "Solidity Development Modules by Distributed Lab",
Expand Down
2 changes: 1 addition & 1 deletion test/contracts-registry/ProxyUpgrader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("ProxyUpgrader", () => {
});

it("should not get implementation", async () => {
await truffleAssert.reverts(proxyUpgrader.getImplementation(token.address));
await truffleAssert.reverts(proxyUpgrader.getImplementation(token.address), "ProxyUpgrader: not a proxy");
});

it("only owner should get implementation", async () => {
Expand Down

0 comments on commit 1e8cf86

Please sign in to comment.