Skip to content

Commit

Permalink
Deploy hooks with HooksFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
kingster-will committed Dec 7, 2023
1 parent 8f0b80b commit e199f0e
Show file tree
Hide file tree
Showing 5 changed files with 913 additions and 691 deletions.
1,495 changes: 841 additions & 654 deletions broadcast/Main.s.sol/11155111/run-latest.json

Large diffs are not rendered by default.

29 changes: 15 additions & 14 deletions deployment-11155111.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
{
"main": {
"AccessControlSingleton-Impl": "0x4Dc9653D6A21A44ca322474f81B41Dc964a422DD",
"AccessControlSingleton-Proxy": "0x7EDacD4d53b5A626471c6728f04D92125CcF647F",
"IPAssetRegistry": "0xaD2377c8f9f0ca6382fEf166b08aeDe8318fE49C",
"IPOrgController-Impl": "0xdc4A589eB82A7a7dDF026F3713da3338cFa59465",
"IPOrgController-Proxy": "0xE3C522921A16c692E664D06d53a251f91323a06B",
"LicenseRegistry": "0xCD28eb9D0D2AFcf4354F2c93dE5ce9Bbf88257ae",
"LicensingFrameworkRepo": "0xA30bB959Bc298e244aCE0402B5c0609CC8745134",
"LicensingModule": "0x6a3E5eB4CE11aE3B063903AB6530c34D6daf9Dc0",
"MockERC721": "0x9F9772cd93715A0cb512b3Eb997088a0D0b4a5A3",
"ModuleRegistry": "0xedBd4907A7F98de932e635e9076B4E0C36D0E797",
"RegistrationModule": "0xfb45e1274D9f0A2DF5C0851a705EEB0665171C5B",
"RelationshipModule": "0xf7081c5ff418ddB6C68A3cCc3CC68A4c21803460",
"StoryProtocol": "0x336BC9128fFFb7869381E2D0c4A156981cA5d240",
"TokenGatedHook": "0x1fDD8955a6D70c84A71Ebb69dE88f3Ad0AAc50d6"
"AccessControlSingleton-Impl": "0x60a56aB6360572bCFFB7d724a79A9C6cE868c756",
"AccessControlSingleton-Proxy": "0xEFfd9E4194aE9C1313A5297a572D70C70343Ed04",
"IPAssetRegistry": "0x468a2220f6b1dCaAe138142C64cd643179893C6e",
"IPOrgController-Impl": "0x7DE573872B80D2a2Dc67dC3e3E7ea0F4DA95c23d",
"IPOrgController-Proxy": "0xB8F98568FA4dc1122096f250468B16194Af102b7",
"LicenseRegistry": "0xFFdB0db0EA5e646dD50BFfc7A53f85e198a5118a",
"LicensingFrameworkRepo": "0x7bF69727760d1d68D5Ea8ecd983dd2d520C78cbf",
"LicensingModule": "0x5c19113eBb0C944A023A8B37265FDdB186362872",
"MockERC721": "0x70603c7413A634ae34fAF0903FC9F8F60D218dA4",
"ModuleRegistry": "0xCa3296538010aad4733bD80ED426dcFbeDe3183C",
"PolygonTokenHook": "0x7799333d6C2eea0119a221665996C08065A5E34a",
"RegistrationModule": "0xfA0B488cD7a07794085aB4BB22969BEcE97DEB2F",
"RelationshipModule": "0xbB0db413b72b90503Aa465Ed1d2b5ABDBF7949B0",
"StoryProtocol": "0x5B4FE4484fb347cA3aA4F47B02197F019fdde18E",
"TokenGatedHook": "0xBE0e7f37eaa32c9a5D727af48D70Eed6155071B0"
}
}
6 changes: 3 additions & 3 deletions deployment-31337.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
"LicenseRegistry": "0xa1833d78f09e2198774E612ca3d9879597ACc859",
"LicensingFrameworkRepo": "0xC06CBc0B7A136bda48865BE24228396Dea4094Ec",
"LicensingModule": "0xb9162E7d63aDAC660f987755D45f55710EB5542a",
"MockERC721": "0x4E4FBD0bd713D1A4417E943B0ec924E7535c824B",
"MockERC721": "0x409c6194d48B0D78E59dE8923C0b55e31A6EaA5C",
"ModuleRegistry": "0x446ce25bff9fe7049Aaf3339124F51F301e9c220",
"PolygonTokenHook": "0x7b3ee952cbcf4ff40f1A213E8fEFDDe6E9Bf4A7C",
"PolygonTokenHook": "0x5f9eC9287D55a50B6C34e564272eD5F3e540A8b5",
"RegistrationModule": "0x812d70D430362c56E56F7Cfc73464FE2F1670ec9",
"RelationshipModule": "0x0e0C15Fe242628fce34A123A0317C97f1D3256c8",
"StoryProtocol": "0x869b721BbF176D0130d87E3aD2eEA02CEBB883eE",
"TokenGatedHook": "0xB7C45927D8F3F380E6FE3e4607b2E6Ff76c41467"
"TokenGatedHook": "0xEbB09194fd2A6907E1C67DbBcb813F1a99D6B6d1"
},
"relationship-processors": {
"DstOwnerRelationshipProcessor": "0x6E447CeC286dbaBe885fDbD9723DD63Cc2DE0Bd2",
Expand Down
49 changes: 29 additions & 20 deletions script/foundry/deployment/Main.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import "contracts/lib/modules/PIPLicensingTerms.sol";
import { PolygonToken } from "contracts/lib/hooks/PolygonToken.sol";
import { Create2 } from "@openzeppelin/contracts/utils/Create2.sol";
import { Hook } from "contracts/lib/hooks/Hook.sol";
import "script/foundry/utils/HooksFactory.sol";


contract Main is Script, BroadcastManager, JsonDeploymentHandler, ProxyHelper {
Expand All @@ -51,6 +52,7 @@ contract Main is Script, BroadcastManager, JsonDeploymentHandler, ProxyHelper {
address polygonTokenHook;
address mockNFT;
address licensingFrameworkRepo;
address hooksFactory;

constructor() JsonDeploymentHandler("main") {}

Expand Down Expand Up @@ -229,13 +231,22 @@ contract Main is Script, BroadcastManager, JsonDeploymentHandler, ProxyHelper {

licensingModule = newAddress;

/// HOOKS_FACTORY
contractKey = "HooksFactory";

console.log(string.concat("Deploying ", contractKey, "..."));
newAddress = address(new HooksFactory());
console.log(string.concat(contractKey, " deployed to:"), newAddress);

hooksFactory = newAddress;

/// TOKEN_GATED_HOOK
contractKey = "TokenGatedHook";

console.log(string.concat("Deploying ", contractKey, "..."));
bytes memory tokenGatedHookCode = abi.encodePacked(
type(TokenGatedHook).creationCode, abi.encode(address(accessControl)));
newAddress = _deployHook(tokenGatedHookCode, Hook.SYNC_FLAG, 0);
newAddress = HooksFactory(hooksFactory).deploy(tokenGatedHookCode, Hook.SYNC_FLAG, block.timestamp);
_writeAddress(contractKey, newAddress);
console.log(string.concat(contractKey, " deployed to:"), newAddress);

Expand All @@ -252,7 +263,7 @@ contract Main is Script, BroadcastManager, JsonDeploymentHandler, ProxyHelper {
vm.envAddress("POLYGON_TOKEN_ORACLE_CLIENT"),
vm.envAddress("POLYGON_TOKEN_ORACLE_COORDINATOR")
));
newAddress = _deployHook(polygonTokenHookCode, Hook.ASYNC_FLAG, 0);
newAddress = HooksFactory(hooksFactory).deploy(polygonTokenHookCode, Hook.ASYNC_FLAG, block.timestamp);
_writeAddress(contractKey, newAddress);
console.log(string.concat(contractKey, " deployed to:"), newAddress);

Expand Down Expand Up @@ -305,6 +316,22 @@ contract Main is Script, BroadcastManager, JsonDeploymentHandler, ProxyHelper {
AccessControl.MODULE_EXECUTOR_ROLE,
ipOrgController
);
accessControlSingleton.grantRole(
AccessControl.HOOK_CALLER_ROLE,
moduleRegistry
);
accessControlSingleton.grantRole(
AccessControl.HOOK_CALLER_ROLE,
registrationModule
);
accessControlSingleton.grantRole(
AccessControl.HOOK_CALLER_ROLE,
relationshipModule
);
accessControlSingleton.grantRole(
AccessControl.HOOK_CALLER_ROLE,
licensingModule
);

// REGISTER MODULES
ModuleRegistry(moduleRegistry).registerProtocolModule(
Expand Down Expand Up @@ -366,22 +393,4 @@ contract Main is Script, BroadcastManager, JsonDeploymentHandler, ProxyHelper {
_writeDeployment();
_endBroadcast();
}

function _deployHook(bytes memory code_, uint256 hookTypeFlag_, uint256 seed_) internal returns (address hookAddr) {
uint256 randomNumber = uint256(keccak256(abi.encodePacked(seed_)));
for (uint256 i = 0; i < 1500; i++) {
bytes32 salt = bytes32(randomNumber + i);
bytes32 bytecodeHash = keccak256(code_);
address expectedAddress = Create2.computeAddress(salt, bytecodeHash);
uint160 prefix = hookTypeFlag_ == Hook.SYNC_FLAG ? 0x02 : 0x01;
if (_doesAddressStartWith(expectedAddress, prefix)) {
hookAddr = Create2.deploy(0, salt, code_);
return hookAddr;
}
}
}

function _doesAddressStartWith(address address_,uint160 prefix_) private pure returns (bool) {
return uint160(address_) >> (160 - 2) == prefix_;
}
}
25 changes: 25 additions & 0 deletions script/foundry/utils/HooksFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.19;

import { Create2 } from "@openzeppelin/contracts/utils/Create2.sol";
import { Hook } from "contracts/lib/hooks/Hook.sol";

contract HooksFactory {
function deploy(bytes memory code_, uint256 hookTypeFlag_, uint256 seed_) external returns (address hookAddr) {
uint256 randomNumber = uint256(keccak256(abi.encodePacked(seed_)));
for (uint256 i = 0; i < 1500; i++) {
bytes32 salt = bytes32(randomNumber + i);
bytes32 bytecodeHash = keccak256(code_);
address expectedAddress = Create2.computeAddress(salt, bytecodeHash);
uint160 prefix = hookTypeFlag_ == Hook.SYNC_FLAG ? 0x02 : 0x01;
if (_doesAddressStartWith(expectedAddress, prefix)) {
hookAddr = Create2.deploy(0, salt, code_);
return hookAddr;
}
}
}

function _doesAddressStartWith(address address_,uint160 prefix_) private pure returns (bool) {
return uint160(address_) >> (160 - 2) == prefix_;
}
}

0 comments on commit e199f0e

Please sign in to comment.