Skip to content

Commit

Permalink
fix(Create3): handling edge cases (#24)
Browse files Browse the repository at this point in the history
* fix(Create3): handling edge cases

* Update Create3Deployer comments

Co-authored-by: Milap Sheth <milap.sheth2010@gmail.com>

* fix(Deploy3): update Create3Deployer comments

* Update contracts/deploy/Create3Deployer.sol

---------

Co-authored-by: Milap Sheth <milap.sheth2010@gmail.com>
  • Loading branch information
re1ro and milapsheth authored Feb 22, 2023
1 parent f1aafa8 commit 750df47
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
11 changes: 8 additions & 3 deletions contracts/deploy/Create3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ error DeployFailed();

contract CreateDeployer {
function deploy(bytes memory bytecode) external {
address deployed;
// solhint-disable-next-line no-inline-assembly
assembly {
pop(create(0, add(bytecode, 32), mload(bytecode)))
deployed := create(0, add(bytecode, 32), mload(bytecode))
if iszero(deployed) {
revert(0, 0)
}
}

selfdestruct(payable(msg.sender));
Expand All @@ -23,7 +27,7 @@ library Create3 {
function deploy(bytes32 salt, bytes memory bytecode) internal returns (address deployed) {
deployed = deployedAddress(salt, address(this));

if (deployed.code.length != 0) revert AlreadyDeployed();
if (deployed.codehash != bytes32(0)) revert AlreadyDeployed();
if (bytecode.length == 0) revert EmptyBytecode();

// CREATE2
Expand All @@ -33,7 +37,8 @@ library Create3 {

deployer.deploy(bytecode);

if (deployed.code.length == 0) revert DeployFailed();
// checking for codehash instead of code length to support contracts that selfdestruct in constructor
if (deployed.codehash == bytes32(0)) revert DeployFailed();
}

function deployedAddress(bytes32 salt, address host) internal pure returns (address deployed) {
Expand Down
6 changes: 3 additions & 3 deletions contracts/deploy/Create3Deployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract Create3Deployer {
* Requirements:
*
* - `bytecode` must not be empty.
* - `salt` must have not been used for `bytecode` already by the same `msg.sender`.
* - `salt` must not have been used already by the same `msg.sender`.
*/
function deploy(bytes calldata bytecode, bytes32 salt) external returns (address deployedAddress_) {
bytes32 deploySalt = keccak256(abi.encode(msg.sender, salt));
Expand All @@ -38,7 +38,7 @@ contract Create3Deployer {
* Requirements:
*
* - `bytecode` must not be empty.
* - `salt` must have not been used for `bytecode` already by the same `msg.sender`.
* - `salt` must not have been used already by the same `msg.sender`.
* - `init` is used to initialize the deployed contract
*/
function deployAndInit(
Expand All @@ -56,7 +56,7 @@ contract Create3Deployer {

/**
* @dev Returns the address where a contract will be stored if deployed via {deploy} or {deployAndInit} by `sender`.
* Any change in the `bytecode`, `sender`, or `salt` will result in a new destination address.
* Any change in `salt` or `sender` will result in a new destination address.
*/
function deployedAddress(address sender, bytes32 salt) external view returns (address) {
bytes32 deploySalt = keccak256(abi.encode(sender, salt));
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@axelar-network/axelar-gmp-sdk-solidity",
"version": "3.2.0",
"version": "3.2.1",
"description": "Solidity GMP SDK and utilities provided by Axelar for cross-chain development",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 750df47

Please sign in to comment.