Skip to content

Commit

Permalink
fix: patch also on zksync
Browse files Browse the repository at this point in the history
  • Loading branch information
sakulstra committed Jan 22, 2025
1 parent e702cdc commit 8af7fd7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ abstract contract TransparentProxyFactoryBase is ITransparentProxyFactory {
) internal pure virtual returns (address);

function _storeProxyInRegistry(address proxy) internal {
_proxyToAdmin[proxy] = _predictCreate1Address(proxy);
_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 _predictCreate1Address(address proxy) internal virtual returns (address) {
function _predictProxyAdminAddress(address proxy) internal pure virtual returns (address) {
return
address(
uint160(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ contract TransparentProxyFactoryZkSync is
{
/// @inheritdoc ITransparentProxyFactoryZkSync
bytes32 public constant ZKSYNC_CREATE2_PREFIX = keccak256('zksyncCreate2');
bytes32 public constant ZKSYNC_CREATE_PREFIX = keccak256('zksyncCreate');

function _predictCreate2Address(
address sender,
Expand Down Expand Up @@ -57,4 +58,13 @@ contract TransparentProxyFactoryZkSync is
}
return result;
}

// on zksync nonces start with 0 https://docs.zksync.io/zksync-protocol/differences/nonces
function _predictProxyAdminAddress(address proxy) internal pure override returns (address) {
bytes32 addressHash = keccak256(
bytes.concat(ZKSYNC_CREATE_PREFIX, bytes32(uint256(uint160(proxy))), bytes32(uint256(0)))
);

return address(uint160(uint256(addressHash)));
}
}
23 changes: 20 additions & 3 deletions zksync/test/TransparentProxyFactoryZkSync.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,38 @@
pragma solidity ^0.8.24;

import {Test} from 'forge-std/Test.sol';
import {TransparentProxyFactoryZkSync} from '../src/contracts/transparent-proxy/TransparentProxyFactoryZkSync.sol';
import {TransparentUpgradeableProxy} from 'openzeppelin-contracts/contracts/proxy/transparent/TransparentUpgradeableProxy.sol';
import {TransparentUpgradeableProxy, ProxyAdmin} from 'openzeppelin-contracts/contracts/proxy/transparent/TransparentUpgradeableProxy.sol';
import {Ownable} from 'openzeppelin-contracts/contracts/access/Ownable.sol';
import {TransparentProxyFactoryZkSync} from '../src/contracts/transparent-proxy/TransparentProxyFactoryZkSync.sol';
import {MockImpl} from '../../src/mocks/MockImpl.sol';

contract TestTransparentProxyFactoryZkSync is Test {
TransparentProxyFactoryZkSync internal factory;
MockImpl internal mockImpl;
address internal owner = makeAddr('owner');
address internal owner = address(1234);

function setUp() public {
factory = new TransparentProxyFactoryZkSync();
mockImpl = new MockImpl();
}

function test_createProxy() external {
uint256 FOO = 2;
bytes memory data = abi.encodeWithSelector(mockImpl.initialize.selector, FOO);
{
address proxy = factory.create(address(mockImpl), owner, data);

address proxyAdmin = factory.getProxyAdmin(proxy);
assertEq(ProxyAdmin(proxyAdmin).owner(), owner);
}
{
address proxy = factory.create(address(mockImpl), owner, data);

address proxyAdmin = factory.getProxyAdmin(proxy);
assertEq(ProxyAdmin(proxyAdmin).owner(), owner);
}
}

function testCreate() public {
uint256 FOO = 2;
bytes memory data = abi.encodeWithSelector(mockImpl.initialize.selector, FOO);
Expand Down

0 comments on commit 8af7fd7

Please sign in to comment.