From 4b402b9fc34ab860884d3b944f65c6f491b6a3c2 Mon Sep 17 00:00:00 2001 From: Quazia Date: Tue, 24 Sep 2024 15:07:54 -0400 Subject: [PATCH] feat(evm): skip deployment if already deployed to address --- packages/evm/script/solidity/Util.s.sol | 28 ++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/evm/script/solidity/Util.s.sol b/packages/evm/script/solidity/Util.s.sol index bc85fa16..a2a91839 100644 --- a/packages/evm/script/solidity/Util.s.sol +++ b/packages/evm/script/solidity/Util.s.sol @@ -14,11 +14,29 @@ contract ScriptUtils is Script { function _deploy2(bytes memory deployCode, bytes memory args) internal { bytes32 salt = keccak256(bytes(vm.envString("BOOST_DEPLOYMENT_SALT"))); - bytes memory payload = abi.encodePacked(salt, deployCode, args); - // deploy using address configured at the CLI level - vm.broadcast(); - (bool success,) = CREATE2_FACTORY.call(payload); - if (!success) revert("create2 failed"); + bytes32 bytecodeHash = keccak256(abi.encodePacked(deployCode, args)); + address computedAddress = address(uint160(uint256(keccak256(abi.encodePacked( + bytes1(0xff), + CREATE2_FACTORY, + salt, + bytecodeHash + ))))); + + // Check if the address already has code deployed + uint256 codeSize; + assembly { + codeSize := extcodesize(computedAddress) + } + + if (codeSize == 0) { + bytes memory payload = abi.encodePacked(salt, deployCode, args); + // deploy using address configured at the CLI level + vm.broadcast(); + (bool success,) = CREATE2_FACTORY.call(payload); + if (!success) revert("create2 failed"); + } else { + console.log("Address already deployed at: ", computedAddress); + } } function _buildJsonDeployPath() internal virtual view returns (string memory) {