// SPDX-License-Identifier: UNLICENSED pragma solidity =0.8.18; import "@openzeppelin/contracts/proxy/Clones.sol"; import "./ITemplateFactory.sol"; library TemplateFactoryLibrary { ITemplateFactory internal constant FACTORY = ITemplateFactory(0x812DD6988CA35BE1BA27095d90864298722f915A); /** * @notice See {ITemplateFactory-predictDeployAddress}. */ function predictDeployAddress(ITemplateFactory factory, bytes32 templateId, uint256 version, bytes memory args) internal view returns (address) { return factory.predictDeployAddress(templateId, version, args, defaultSalt(templateId, version)); } /** * @notice See {ITemplateFactory-predictCloneAddress}. */ function predictCloneAddress(ITemplateFactory factory, bytes32 templateId, uint256 version) internal view returns (address) { return factory.predictCloneAddress(templateId, version, defaultSalt(templateId, version)); } // ------- DEPLOY METHODS /** * @notice See {TemplateFactoryLibrary-deployTemplateLATEST}. */ function deployTemplateLATEST(ITemplateFactory factory, bytes32 templateId, bytes memory args) internal returns (address instance) { return deployTemplate(factory, templateId, factory.latestVersion(templateId), args); } /** * @notice See {TemplateFactoryLibrary-deployTemplateLATEST}. */ function deployTemplateLATEST(ITemplateFactory factory, bytes32 templateId, bytes memory args, bytes32 salt) internal returns (address instance) { return deployTemplate(factory, templateId, factory.latestVersion(templateId), args, salt); } /** * @notice See {TemplateFactoryLibrary-deployTemplateLATEST}. */ function deployTemplateLATEST( ITemplateFactory factory, bytes32 templateId, bytes memory args, bytes[] memory functionCalls ) internal returns (address instance) { return deployTemplate(factory, templateId, factory.latestVersion(templateId), args, functionCalls); } /** * @notice Deploys the latest version of a template. * @param templateId The id of the template to deploy the latest template of. * @param args The abi-encoded constructor arguments. * @param functionCalls The abi-encoded function calls. * @param salt The unique hash to identify the contract. * @custom:emits TemplateDeployed * @custom:requirement The latest version's number of parts must be equal to the version's number of total parts uploaded. * @custom:requirement The length of the latest version's creation code must be greater than zero. * @return instance The instance of the deployed template. */ function deployTemplateLATEST( ITemplateFactory factory, bytes32 templateId, bytes memory args, bytes[] memory functionCalls, bytes32 salt ) internal returns (address instance) { return deployTemplate(factory, templateId, factory.latestVersion(templateId), args, functionCalls, salt); } /** * @notice See {ITemplateFactory-deployTemplate}. */ function deployTemplate(ITemplateFactory factory, bytes32 templateId, uint256 version, bytes memory args) internal returns (address instance) { return deployTemplate(factory, templateId, version, args, new bytes[](0)); } /** * @notice See {ITemplateFactory-deployTemplate}. */ function deployTemplate( ITemplateFactory factory, bytes32 templateId, uint256 version, bytes memory args, bytes32 salt ) internal returns (address instance) { return deployTemplate(factory, templateId, version, args, new bytes[](0), salt); } /** * @notice See {ITemplateFactory-deployTemplate}. */ function deployTemplate( ITemplateFactory factory, bytes32 templateId, uint256 version, bytes memory args, bytes[] memory functionCalls ) internal returns (address instance) { return deployTemplate(factory, templateId, version, args, functionCalls, defaultSalt(templateId, version)); } /** * @notice See {ITemplateFactory-deployTemplate}. */ function deployTemplate( ITemplateFactory factory, bytes32 templateId, uint256 version, bytes memory args, bytes[] memory functionCalls, bytes32 salt ) internal returns (address instance) { return factory.deployTemplate(templateId, version, args, functionCalls, salt); } // ------------ /** * @notice See {TemplateFactoryLibrary-cloneTemplateLATEST}. */ function cloneTemplateLATEST(ITemplateFactory factory, bytes32 templateId) internal returns (address instance) { return cloneTemplate(factory, templateId, factory.latestVersion(templateId)); } /** * @notice See {TemplateFactoryLibrary-cloneTemplateLATEST}. */ function cloneTemplateLATEST(ITemplateFactory factory, bytes32 templateId, bytes32 salt) internal returns (address instance) { return cloneTemplate(factory, templateId, factory.latestVersion(templateId), salt); } /** * @notice See {TemplateFactoryLibrary-cloneTemplateLATEST}. */ function cloneTemplateLATEST(ITemplateFactory factory, bytes32 templateId, bytes[] memory functionCalls) internal returns (address instance) { return cloneTemplate(factory, templateId, factory.latestVersion(templateId), functionCalls); } /** * @notice Clones the latest version of a template. * @param templateId The id of the template to clone. * @param functionCalls The abi-encoded function calls. * @param salt The unique hash to identify the contract. * @custom:emits TemplateCloned * @custom:requirement The version's implementation must not equal `address(0)`. * @return instance The instance of the cloned template. */ function cloneTemplateLATEST( ITemplateFactory factory, bytes32 templateId, bytes[] memory functionCalls, bytes32 salt ) internal returns (address instance) { return cloneTemplate(factory, templateId, factory.latestVersion(templateId), functionCalls, salt); } /** * @notice See {ITemplateFactory-cloneTemplate}. */ function cloneTemplate(ITemplateFactory factory, bytes32 templateId, uint256 version) internal returns (address instance) { return cloneTemplate(factory, templateId, version, new bytes[](0)); } /** * @notice See {ITemplateFactory-cloneTemplate}. */ function cloneTemplate(ITemplateFactory factory, bytes32 templateId, uint256 version, bytes32 salt) internal returns (address instance) { return cloneTemplate(factory, templateId, version, new bytes[](0), salt); } /** * @notice See {ITemplateFactory-cloneTemplate}. */ function cloneTemplate(ITemplateFactory factory, bytes32 templateId, uint256 version, bytes[] memory functionCalls) internal returns (address instance) { return cloneTemplate(factory, templateId, version, functionCalls, defaultSalt(templateId, version)); } /** * @notice See {ITemplateFactory-cloneTemplate}. */ function cloneTemplate( ITemplateFactory factory, bytes32 templateId, uint256 version, bytes[] memory functionCalls, bytes32 salt ) internal returns (address instance) { return factory.cloneTemplate(templateId, version, functionCalls, salt); } // ----- /** * @notice See {ITemplateFactory-initCodeHash}. */ function initCodeHash(ITemplateFactory factory, bytes32 templateId, uint256 version) internal view returns (bytes32) { return factory.initCodeHash(templateId, version, ""); } // ----- function defaultSalt(bytes32 templateId, uint256 version) internal view returns (bytes32) { return keccak256(abi.encodePacked(templateId, version, block.number, msg.sender)); } }