From 69eadde57851e39bd549df426d4cfb282cba76b4 Mon Sep 17 00:00:00 2001 From: Dan Oved Date: Tue, 19 Sep 2023 13:09:17 -0700 Subject: [PATCH] Can mine for a determinstic address for the proxy shim in a script, and have a test that shows we can determinstically deploy proxy to that address with that salt and deployer account --- package.json | 1 + remappings.txt | 3 +- script/GetDeterminsticParam.s.sol | 55 ++++++++++++++++++++++++ test/deployer/DeterminsticDeployer.t.sol | 28 ++++++++++++ yarn.lock | 5 +++ 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 script/GetDeterminsticParam.s.sol create mode 100644 test/deployer/DeterminsticDeployer.t.sol diff --git a/package.json b/package.json index 77c6c57b1..d39ae8293 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "listr2": "^6.4.2", "prettier": "^2.8.8", "prettier-plugin-solidity": "^1.1.3", + "solady": "^0.0.123", "tsup": "^6.7.0", "typescript": "^5.0.4", "viem": "^1.6.0", diff --git a/remappings.txt b/remappings.txt index 76854be3f..6985dcb8b 100644 --- a/remappings.txt +++ b/remappings.txt @@ -4,4 +4,5 @@ forge-std/=node_modules/forge-std/src/ @zoralabs/protocol-rewards/src/=node_modules/@zoralabs/protocol-rewards/src/ @openzeppelin/contracts/=node_modules/@openzeppelin/contracts/ _imagine=_imagine/ -solemate/=/node_modules/solemate/src/ \ No newline at end of file +solemate/=/node_modules/solemate/src/ +solady/=node_modules/solady/src/ \ No newline at end of file diff --git a/script/GetDeterminsticParam.s.sol b/script/GetDeterminsticParam.s.sol new file mode 100644 index 000000000..f06455509 --- /dev/null +++ b/script/GetDeterminsticParam.s.sol @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.17; + +import "forge-std/Script.sol"; +import "forge-std/console2.sol"; + +import {ZoraDeployerBase} from "./ZoraDeployerBase.sol"; +import {Zora1155Factory} from "../src/proxies/Zora1155Factory.sol"; +import {ProxyShim} from "../src/utils/ProxyShim.sol"; +import {LibString} from "solady/utils/LibString.sol"; + +contract GetDeterminsticParam is ZoraDeployerBase { + // copied from: https://github.com/karmacoma-eth/foundry-playground/blob/main/script/MineSaltScript.sol#L17C1-L36C9 + function mineSalt(address deployer, bytes32 initCodeHash, string memory startsWith) public returns (string memory saltStr, address expectedAddress) { + string[] memory args = new string[](8); + args[0] = "cast"; + args[1] = "create2"; + args[2] = "--starts-with"; + args[3] = startsWith; + args[4] = "--deployer"; + args[5] = LibString.toHexString(deployer); + args[6] = "--init-code-hash"; + args[7] = LibString.toHexStringNoPrefix(uint256(initCodeHash), 32); + string memory result = string(vm.ffi(args)); + + uint256 addressIndex = LibString.indexOf(result, "Address: "); + string memory addressStr = LibString.slice(result, addressIndex + 9, addressIndex + 9 + 42); + expectedAddress = vm.parseAddress(addressStr); + + uint256 saltIndex = LibString.indexOf(result, "Salt: "); + saltStr = LibString.slice(result, saltIndex + 6, bytes(result).length); + } + + function run() public { + address deployerAddress = makeAddr("determinsticDeployer"); + + console2.log(deployerAddress); + + address factoryShimAddress = address(new ProxyShim(deployerAddress)); + console2.log(factoryShimAddress); + + bytes memory initCode = abi.encodePacked(type(Zora1155Factory).creationCode, abi.encode(factoryShimAddress, "")); + + bytes32 creationCodeHash = keccak256(initCode); + + console.log("init code hash: ", LibString.toHexStringNoPrefix(uint256(creationCodeHash), 32)); + + console2.log("mining for salt"); + + (string memory saltString, address expectedAddress) = mineSalt(deployerAddress, creationCodeHash, "0x7777777"); + + console.log("salt: ", saltString); + console.log("expected address: ", expectedAddress); + } +} diff --git a/test/deployer/DeterminsticDeployer.t.sol b/test/deployer/DeterminsticDeployer.t.sol new file mode 100644 index 000000000..ef576c34d --- /dev/null +++ b/test/deployer/DeterminsticDeployer.t.sol @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: MIT +pragma solidity 0.8.17; + +import "forge-std/Test.sol"; +import {Zora1155Factory} from "../../src/proxies/Zora1155Factory.sol"; +import {ProxyShim} from "../../src/utils/ProxyShim.sol"; +import {Create2} from "@openzeppelin/contracts/utils/Create2.sol"; + +contract ZoraCreator1155FactoryTest is Test { + function test_proxyCanByDeployedAtDesiredAddress() external { + // the values in this test can be determined by running the script GetDeterminsticParam.s.sol, + // then using the output of that to pipe into cast create2 to get a determinstic address that matches + // 0x7777777 + + address deployerAddress = makeAddr("determinsticDeployer"); + + address factoryShimAddress = address(new ProxyShim(deployerAddress)); + + assertEq(factoryShimAddress, 0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f, "factory shim"); + + bytes32 salt = bytes32(uint256(58621195373076836725248570099637242069749185457981134621891359830769662503721)); + + vm.startPrank(deployerAddress); + address factoryProxyAddress = address(new Zora1155Factory{salt: salt}(factoryShimAddress, "")); + + assertEq(factoryProxyAddress, 0x7777777CC7d51bDfa57B1afC08Cea482AFfb40cf, "factory proxy address"); + } +} diff --git a/yarn.lock b/yarn.lock index 814d325d9..74ed87789 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3298,6 +3298,11 @@ snake-case@^3.0.4: dot-case "^3.0.4" tslib "^2.0.3" +solady@^0.0.123: + version "0.0.123" + resolved "https://registry.yarnpkg.com/solady/-/solady-0.0.123.tgz#7ef95767c1570e3efde7550da2a8b439b2f413d2" + integrity sha512-F/B8OMCplGsS4FrdPrnEG0xdg8HKede5PwC+Rum8soj/LWxfKckA0p+Uwnlbgey2iI82IHvmSOCNhsdbA+lUrw== + solidity-comments-extractor@^0.0.7: version "0.0.7" resolved "https://registry.yarnpkg.com/solidity-comments-extractor/-/solidity-comments-extractor-0.0.7.tgz#99d8f1361438f84019795d928b931f4e5c39ca19"