Skip to content

Commit

Permalink
Can mine for a determinstic address for the proxy shim in a script, a…
Browse files Browse the repository at this point in the history
…nd have a test that shows we can determinstically deploy proxy to that address with that salt and deployer account
  • Loading branch information
oveddan committed Sep 19, 2023
1 parent 8648f6a commit 69eadde
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion remappings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/
solemate/=/node_modules/solemate/src/
solady/=node_modules/solady/src/
55 changes: 55 additions & 0 deletions script/GetDeterminsticParam.s.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}
28 changes: 28 additions & 0 deletions test/deployer/DeterminsticDeployer.t.sol
Original file line number Diff line number Diff line change
@@ -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");
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 69eadde

Please sign in to comment.