Skip to content

Commit

Permalink
feat: remove custom proxy admin (#54)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Pavel <[email protected]>
  • Loading branch information
sakulstra and pavelvm5 authored Jan 24, 2025
1 parent bfb52fc commit 611e5ad
Show file tree
Hide file tree
Showing 14 changed files with 1,028 additions and 1,112 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
[submodule "lib/openzeppelin-contracts-upgradeable"]
path = lib/openzeppelin-contracts-upgradeable
url = https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable
branch = release-v5.0
branch = release-v5.1
2 changes: 1 addition & 1 deletion lib/forge-std
2 changes: 1 addition & 1 deletion lib/openzeppelin-contracts-upgradeable
45 changes: 0 additions & 45 deletions src/contracts/transparent-proxy/ProxyAdmin.sol

This file was deleted.

59 changes: 49 additions & 10 deletions src/contracts/transparent-proxy/TransparentProxyFactoryBase.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import {TransparentUpgradeableProxy} from 'openzeppelin-contracts/contracts/proxy/transparent/TransparentUpgradeableProxy.sol';
import {ProxyAdmin} from 'openzeppelin-contracts/contracts/proxy/transparent/ProxyAdmin.sol';
import {ITransparentProxyFactory} from './interfaces/ITransparentProxyFactory.sol';
import {TransparentUpgradeableProxy} from './TransparentUpgradeableProxy.sol';
import {ProxyAdmin} from './ProxyAdmin.sol';

/**
* @title TransparentProxyFactory
Expand All @@ -14,11 +14,23 @@ import {ProxyAdmin} from './ProxyAdmin.sol';
* @dev Highly recommended to pass as `admin` on creation an OZ ProxyAdmin instance
**/
abstract contract TransparentProxyFactoryBase is ITransparentProxyFactory {
mapping(address proxy => address admin) internal _proxyToAdmin;

function getProxyAdmin(address proxy) external view returns (address) {
return _proxyToAdmin[proxy];
}

/// @inheritdoc ITransparentProxyFactory
function create(address logic, ProxyAdmin admin, bytes calldata data) external returns (address) {
address proxy = address(new TransparentUpgradeableProxy(logic, admin, data));
function create(
address logic,
address adminOwner,
bytes calldata data
) external returns (address) {
address proxy = address(new TransparentUpgradeableProxy(logic, adminOwner, data));
_storeProxyInRegistry(proxy);

emit ProxyCreated(proxy, logic, adminOwner);

emit ProxyCreated(proxy, logic, address(admin));
return proxy;
}

Expand All @@ -33,13 +45,14 @@ abstract contract TransparentProxyFactoryBase is ITransparentProxyFactory {
/// @inheritdoc ITransparentProxyFactory
function createDeterministic(
address logic,
ProxyAdmin admin,
address adminOwner,
bytes calldata data,
bytes32 salt
) external returns (address) {
address proxy = address(new TransparentUpgradeableProxy{salt: salt}(logic, admin, data));
address proxy = address(new TransparentUpgradeableProxy{salt: salt}(logic, adminOwner, data));
_storeProxyInRegistry(proxy);

emit ProxyDeterministicCreated(proxy, logic, address(admin), salt);
emit ProxyDeterministicCreated(proxy, logic, adminOwner, salt);
return proxy;
}

Expand All @@ -57,7 +70,7 @@ abstract contract TransparentProxyFactoryBase is ITransparentProxyFactory {
/// @inheritdoc ITransparentProxyFactory
function predictCreateDeterministic(
address logic,
ProxyAdmin admin,
address admin,
bytes calldata data,
bytes32 salt
) public view returns (address) {
Expand All @@ -66,7 +79,7 @@ abstract contract TransparentProxyFactoryBase is ITransparentProxyFactory {
address(this),
salt,
type(TransparentUpgradeableProxy).creationCode,
abi.encode(logic, address(admin), data)
abi.encode(logic, admin, data)
);
}

Expand All @@ -90,4 +103,30 @@ abstract contract TransparentProxyFactoryBase is ITransparentProxyFactory {
bytes memory creationCode,
bytes memory constructorArgs
) internal pure virtual returns (address);

function _storeProxyInRegistry(address proxy) internal {
_proxyToAdmin[proxy] = _predictProxyAdminAddress(proxy);
}

/**
* @dev the prediction only depends on the address of the proxy.
* The admin is always the first and only contract deployed by the proxy.
*/
function _predictProxyAdminAddress(address proxy) internal pure virtual returns (address) {
return
address(
uint160(
uint256(
keccak256(
abi.encodePacked(
bytes1(0xd6), // RLP prefix for a list with total length 22
bytes1(0x94), // RLP prefix for an address (20 bytes)
proxy, // 20-byte address
uint8(1) // 1-byte nonce
)
)
)
)
);
}
}
125 changes: 0 additions & 125 deletions src/contracts/transparent-proxy/TransparentUpgradeableProxy.sol

This file was deleted.

15 changes: 15 additions & 0 deletions src/contracts/transparent-proxy/interfaces/IProxyAdminOzV4.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

/**
* The package relies on OZ-v5, some legacy contracts rely on the oz v4 versions of `TransparentUpgradeableProxy` and `ProxyAdmin`.
* While we no longer recommend deploying new instances of these, we expose the interface to allow interacting with existing contracts.
*/

interface IProxyAdminOzV4 {
function changeProxyAdmin(address proxy, address newAdmin) external;

function upgrade(address proxy, address implementation) external;

function upgradeAndCall(address proxy, address implementation, bytes memory data) external;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import {ProxyAdmin} from '../ProxyAdmin.sol';

interface ITransparentProxyFactory {
event ProxyCreated(address proxy, address indexed logic, address indexed proxyAdmin);
Expand All @@ -28,7 +27,7 @@ interface ITransparentProxyFactory {
* for an `initialize` function being `function initialize(uint256 foo) external initializer;`
* @return address The address of the proxy deployed
**/
function create(address logic, ProxyAdmin admin, bytes memory data) external returns (address);
function create(address logic, address admin, bytes memory data) external returns (address);

/**
* @notice Creates a proxyAdmin instance, and transfers ownership to provided owner
Expand All @@ -51,7 +50,7 @@ interface ITransparentProxyFactory {
**/
function createDeterministic(
address logic,
ProxyAdmin admin,
address admin,
bytes memory data,
bytes32 salt
) external returns (address);
Expand Down Expand Up @@ -80,7 +79,7 @@ interface ITransparentProxyFactory {
**/
function predictCreateDeterministic(
address logic,
ProxyAdmin admin,
address admin,
bytes calldata data,
bytes32 salt
) external view returns (address);
Expand Down
Loading

0 comments on commit 611e5ad

Please sign in to comment.