-
Notifications
You must be signed in to change notification settings - Fork 938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1.4.0: Improve ProxyFactory
#468
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7e2da4b
remove createProxy, add createNetworkSpecificProxy, check that single…
mmv08 1f8f495
Merge branch 'main' of https://github.com/safe-global/safe-contracts …
mmv08 4cb2283
fix typos
mmv08 8abdcdd
add a function for getting proxy code from solc artifacts
mmv08 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,79 +4,79 @@ pragma solidity >=0.7.0 <0.9.0; | |
import "./GnosisSafeProxy.sol"; | ||
import "./IProxyCreationCallback.sol"; | ||
|
||
/// @title Proxy Factory - Allows to create new proxy contact and execute a message call to the new proxy within one transaction. | ||
/// @title Proxy Factory - Allows to create a new proxy contract and execute a message call to the new proxy within one transaction. | ||
/// @author Stefan George - <[email protected]> | ||
contract GnosisSafeProxyFactory { | ||
event ProxyCreation(GnosisSafeProxy proxy, address singleton); | ||
|
||
/// @dev Allows to create new proxy contact and execute a message call to the new proxy within one transaction. | ||
/// @param singleton Address of singleton contract. | ||
/// @param data Payload for message call sent to new proxy contract. | ||
function createProxy(address singleton, bytes memory data) public returns (GnosisSafeProxy proxy) { | ||
proxy = new GnosisSafeProxy(singleton); | ||
if (data.length > 0) | ||
// solhint-disable-next-line no-inline-assembly | ||
assembly { | ||
if eq(call(gas(), proxy, 0, add(data, 0x20), mload(data), 0, 0), 0) { | ||
revert(0, 0) | ||
} | ||
} | ||
emit ProxyCreation(proxy, singleton); | ||
} | ||
|
||
/// @dev Allows to retrieve the runtime code of a deployed Proxy. This can be used to check that the expected Proxy was deployed. | ||
function proxyRuntimeCode() public pure returns (bytes memory) { | ||
return type(GnosisSafeProxy).runtimeCode; | ||
} | ||
|
||
/// @dev Allows to retrieve the creation code used for the Proxy deployment. With this it is easily possible to calculate predicted address. | ||
function proxyCreationCode() public pure returns (bytes memory) { | ||
return type(GnosisSafeProxy).creationCode; | ||
} | ||
|
||
/// @dev Allows to create new proxy contact using CREATE2 but it doesn't run the initializer. | ||
/// @dev Allows to create a new proxy contract using CREATE2. Optionally executes an initializer call to a new proxy. | ||
/// This method is only meant as an utility to be called from other methods | ||
/// @param _singleton Address of singleton contract. | ||
/// @param initializer Payload for message call sent to new proxy contract. | ||
/// @param saltNonce Nonce that will be used to generate the salt to calculate the address of the new proxy contract. | ||
function deployProxyWithNonce( | ||
/// @param _singleton Address of singleton contract. Must be deployed at the time of execution. | ||
/// @param initializer Payload for a message call to be sent to a new proxy contract. | ||
/// @param salt Create2 salt to use for calculating the address of the new proxy contract. | ||
function deployProxy( | ||
address _singleton, | ||
bytes memory initializer, | ||
uint256 saltNonce | ||
bytes32 salt | ||
) internal returns (GnosisSafeProxy proxy) { | ||
// If the initializer changes the proxy address should change too. Hashing the initializer data is cheaper than just concatinating it | ||
bytes32 salt = keccak256(abi.encodePacked(keccak256(initializer), saltNonce)); | ||
require(isContract(_singleton), "Singleton contract not deployed"); | ||
|
||
bytes memory deploymentData = abi.encodePacked(type(GnosisSafeProxy).creationCode, uint256(uint160(_singleton))); | ||
// solhint-disable-next-line no-inline-assembly | ||
assembly { | ||
proxy := create2(0x0, add(0x20, deploymentData), mload(deploymentData), salt) | ||
} | ||
require(address(proxy) != address(0), "Create2 call failed"); | ||
|
||
if (initializer.length > 0) { | ||
// solhint-disable-next-line no-inline-assembly | ||
assembly { | ||
if eq(call(gas(), proxy, 0, add(initializer, 0x20), mload(initializer), 0, 0), 0) { | ||
revert(0, 0) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// @dev Allows to create new proxy contact and execute a message call to the new proxy within one transaction. | ||
/// @param _singleton Address of singleton contract. | ||
/// @param initializer Payload for message call sent to new proxy contract. | ||
/// @dev Allows to create a new proxy contract and execute a message call to the new proxy within one transaction. | ||
/// @param _singleton Address of singleton contract. Must be deployed at the time of execution. | ||
/// @param initializer Payload for a message call to be sent to a new proxy contract. | ||
/// @param saltNonce Nonce that will be used to generate the salt to calculate the address of the new proxy contract. | ||
function createProxyWithNonce( | ||
address _singleton, | ||
bytes memory initializer, | ||
uint256 saltNonce | ||
) public returns (GnosisSafeProxy proxy) { | ||
proxy = deployProxyWithNonce(_singleton, initializer, saltNonce); | ||
if (initializer.length > 0) | ||
// solhint-disable-next-line no-inline-assembly | ||
assembly { | ||
if eq(call(gas(), proxy, 0, add(initializer, 0x20), mload(initializer), 0, 0), 0) { | ||
revert(0, 0) | ||
} | ||
} | ||
// If the initializer changes the proxy address should change too. Hashing the initializer data is cheaper than just concatinating it | ||
bytes32 salt = keccak256(abi.encodePacked(keccak256(initializer), saltNonce)); | ||
proxy = deployProxy(_singleton, initializer, salt); | ||
emit ProxyCreation(proxy, _singleton); | ||
} | ||
|
||
/// @dev Allows to create new proxy contact, execute a message call to the new proxy and call a specified callback within one transaction | ||
/// @param _singleton Address of singleton contract. | ||
/// @param initializer Payload for message call sent to new proxy contract. | ||
/// @dev Allows to create a new proxy contract that should exist only on 1 network (e.g. specific governance or admin accounts) | ||
/// by including the chain id in the create2 salt. Such proxies cannot be created on other networks by replaying the transaction. | ||
/// @param _singleton Address of singleton contract. Must be deployed at the time of execution. | ||
/// @param initializer Payload for a message call to be sent to a new proxy contract. | ||
/// @param saltNonce Nonce that will be used to generate the salt to calculate the address of the new proxy contract. | ||
function createChainSpecificProxyWithNonce( | ||
address _singleton, | ||
bytes memory initializer, | ||
uint256 saltNonce | ||
) public returns (GnosisSafeProxy proxy) { | ||
// If the initializer changes the proxy address should change too. Hashing the initializer data is cheaper than just concatinating it | ||
bytes32 salt = keccak256(abi.encodePacked(keccak256(initializer), saltNonce, getChainId())); | ||
proxy = deployProxy(_singleton, initializer, salt); | ||
emit ProxyCreation(proxy, _singleton); | ||
} | ||
|
||
/// @dev Allows to create a new proxy contract, execute a message call to the new proxy and call a specified callback within one transaction | ||
/// @param _singleton Address of singleton contract. Must be deployed at the time of execution. | ||
/// @param initializer Payload for a message call to be sent to a new proxy contract. | ||
/// @param saltNonce Nonce that will be used to generate the salt to calculate the address of the new proxy contract. | ||
/// @param callback Callback that will be invoked after the new proxy contract has been successfully deployed and initialized. | ||
function createProxyWithCallback( | ||
|
@@ -90,18 +90,24 @@ contract GnosisSafeProxyFactory { | |
if (address(callback) != address(0)) callback.proxyCreated(proxy, _singleton, initializer, saltNonce); | ||
} | ||
|
||
/// @dev Allows to get the address for a new proxy contact created via `createProxyWithNonce` | ||
/// This method is only meant for address calculation purpose when you use an initializer that would revert, | ||
/// therefore the response is returned with a revert. When calling this method set `from` to the address of the proxy factory. | ||
/// @param _singleton Address of singleton contract. | ||
/// @param initializer Payload for message call sent to new proxy contract. | ||
/// @param saltNonce Nonce that will be used to generate the salt to calculate the address of the new proxy contract. | ||
function calculateCreateProxyWithNonceAddress( | ||
address _singleton, | ||
bytes calldata initializer, | ||
uint256 saltNonce | ||
) external returns (GnosisSafeProxy proxy) { | ||
proxy = deployProxyWithNonce(_singleton, initializer, saltNonce); | ||
revert(string(abi.encodePacked(proxy))); | ||
/// @dev Returns true if `account` is a contract. | ||
/// @param account The address being queried | ||
function isContract(address account) internal view returns (bool) { | ||
uint256 size; | ||
// solhint-disable-next-line no-inline-assembly | ||
assembly { | ||
size := extcodesize(account) | ||
} | ||
return size > 0; | ||
} | ||
|
||
/// @dev Returns the chain id used by this contract. | ||
function getChainId() public view returns (uint256) { | ||
uint256 id; | ||
// solhint-disable-next-line no-inline-assembly | ||
assembly { | ||
id := chainid() | ||
} | ||
return id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pinned some versions because of bugs/incompatibilities. Please see description for a more detailed explanation