Skip to content

Commit

Permalink
More abstraction on the Proxy Factory
Browse files Browse the repository at this point in the history
  • Loading branch information
kyzia551 committed Jul 26, 2024
1 parent 3202af5 commit 2263e6b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,67 +18,33 @@ contract TransparentProxyFactoryZkSync is
TransparentProxyFactoryBase,
ITransparentProxyFactoryZkSync
{
/// @inheritdoc ITransparentProxyFactoryZkSync
bytes32 public immutable TRANSPARENT_UPGRADABLE_PROXY_INIT_CODE_HASH;

/// @inheritdoc ITransparentProxyFactoryZkSync
bytes32 public immutable PROXY_ADMIN_INIT_CODE_HASH;

/// @inheritdoc ITransparentProxyFactoryZkSync
bytes32 public constant ZKSYNC_CREATE2_PREFIX = keccak256('zksyncCreate2');

constructor() {
// to get the bytecode-hash in zkSync, we sanatize the bytes returned from the creationCode
TRANSPARENT_UPGRADABLE_PROXY_INIT_CODE_HASH = bytes32(_sliceBytes(type(TransparentUpgradeableProxy).creationCode, 36, 32));
PROXY_ADMIN_INIT_CODE_HASH = bytes32(_sliceBytes(type(ProxyAdmin).creationCode, 36, 32));
}

/// @inheritdoc ITransparentProxyFactory
function predictCreateDeterministic(
address logic,
address admin,
bytes calldata data,
bytes32 salt
) public view override returns (address) {
return
_predictCreate2Address(
address(this),
salt,
TRANSPARENT_UPGRADABLE_PROXY_INIT_CODE_HASH,
abi.encode(logic, admin, data)
);
}

/// @inheritdoc ITransparentProxyFactory
function predictCreateDeterministicProxyAdmin(bytes32 salt)
public
view
override
returns (address)
{
return _predictCreate2Address(address(this), salt, PROXY_ADMIN_INIT_CODE_HASH, abi.encode());
}

function _predictCreate2Address(
address sender,
bytes32 salt,
bytes32 creationCodeHash,
bytes memory creationCode,
bytes memory constructorInput
) internal pure returns (address) {
) internal pure override returns (address) {
bytes32 addressHash = keccak256(
bytes.concat(
ZKSYNC_CREATE2_PREFIX,
bytes32(uint256(uint160(sender))),
salt,
creationCodeHash,
bytes32(_sliceBytes(creationCode, 36, 32)),
keccak256(constructorInput)
)
);

return address(uint160(uint256(addressHash)));
}

function _sliceBytes(bytes memory data, uint256 start, uint256 length) internal pure returns (bytes memory) {
function _sliceBytes(
bytes memory data,
uint256 start,
uint256 length
) internal pure returns (bytes memory) {
require(start + length <= data.length, 'Slice out of bounds');

bytes memory result = new bytes(length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@
pragma solidity >=0.8.0;

interface ITransparentProxyFactoryZkSync {
/**
* @notice method to get the hash of creation bytecode of the TransparentUpgradableProxy contract
* @return hashed of creation bytecode of the TransparentUpgradableProxy contract
*/
function TRANSPARENT_UPGRADABLE_PROXY_INIT_CODE_HASH() external returns (bytes32);

/**
* @notice method to get the hash of creation bytecode of the ProxyAdmin contract
* @return hashed of creation bytecode of the ProxyAdmin contract
*/
function PROXY_ADMIN_INIT_CODE_HASH() external returns (bytes32);

/**
* @notice method to get the zksync create2 prefix used for create2 address derivation in zksync
* @return create2 prefix used for create2 address derivation
Expand Down
32 changes: 3 additions & 29 deletions src/contracts/transparent-proxy/TransparentProxyFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,18 @@ import {TransparentProxyFactoryBase, ITransparentProxyFactory, ProxyAdmin, Trans
* @dev Highly recommended to pass as `admin` on creation an OZ ProxyAdmin instance
**/
contract TransparentProxyFactory is TransparentProxyFactoryBase {
/// @inheritdoc ITransparentProxyFactory
function predictCreateDeterministic(
address logic,
address admin,
bytes calldata data,
bytes32 salt
) public view override returns (address) {
return
_predictCreate2Address(
address(this),
salt,
type(TransparentUpgradeableProxy).creationCode,
abi.encode(logic, admin, data)
);
}

/// @inheritdoc ITransparentProxyFactory
function predictCreateDeterministicProxyAdmin(bytes32 salt)
public
view
override
returns (address)
{
return _predictCreate2Address(address(this), salt, type(ProxyAdmin).creationCode, abi.encode());
}

function _predictCreate2Address(
address creator,
bytes32 salt,
bytes memory creationCode,
bytes memory contructorArgs
) internal pure returns (address) {
bytes memory constructorArgs
) internal pure override returns (address) {
bytes32 hash = keccak256(
abi.encodePacked(
bytes1(0xff),
creator,
salt,
keccak256(abi.encodePacked(creationCode, contructorArgs))
keccak256(abi.encodePacked(creationCode, constructorArgs))
)
);

Expand Down
26 changes: 24 additions & 2 deletions src/contracts/transparent-proxy/TransparentProxyFactoryBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,30 @@ abstract contract TransparentProxyFactoryBase is ITransparentProxyFactory {
address admin,
bytes calldata data,
bytes32 salt
) public view virtual returns (address);
) public view override returns (address) {
return
_predictCreate2Address(
address(this),
salt,
type(TransparentUpgradeableProxy).creationCode,
abi.encode(logic, admin, data)
);
}

/// @inheritdoc ITransparentProxyFactory
function predictCreateDeterministicProxyAdmin(bytes32 salt) public view virtual returns (address);
function predictCreateDeterministicProxyAdmin(bytes32 salt)
public
view
override
returns (address)
{
return _predictCreate2Address(address(this), salt, type(ProxyAdmin).creationCode, abi.encode());
}

function _predictCreate2Address(
address creator,
bytes32 salt,
bytes memory creationCode,
bytes memory constructorArgs
) internal pure virtual returns (address);
}

0 comments on commit 2263e6b

Please sign in to comment.