Contract Factories
Background
Section titled “Background”For developers building on SKALE, it is important to understand how a SKALE Chain the permission layer permission layer works for contract deployments and it’s impact on your smart contract architecture. SKALE Chains are Layer 1 blockchains with a number of unique enhancements that give businesses and developers maximum flexiblity when desigining an application or ecoystem. The permission layer is enabled by default on all SKALE Chains and should be taken into account when designing smart contracts.
What is a factory?
Section titled “What is a factory?”A factory is a smart contract that has the ability to deploy another smart contract. In some cases, this is done in a permissionless way e.g Uniswap where new Uniswap Pools create a new smart contract. In other cases, this is done in a permissioned way where a project uses a factory to create new versions or releases but it is locked down to their keys.
/// SPDX-License-Identifier: MITpragma solidity ^0.8.9;
/// @title Clone/// @notice A simple contract that stores a clone ID/// @dev This contract is meant to be deployed by the Factory contractcontract Clone { /// @notice The unique identifier for this clone /// @dev Set during contract creation and remains unchanged uint256 public cloneId;
/// @notice Creates a new Clone with the specified ID /// @param _cloneId The unique identifier to assign to this clone constructor(uint256 _cloneId) { cloneId = _cloneId; }}
/// @title Factory/// @notice A factory contract that creates and tracks Clone instances/// @dev Implements a simple factory pattern for deploying Clone contractscontract Factory { /// @notice The total number of clones created by this factory /// @dev Also used to assign unique IDs to new clones uint256 public cloneCount;
/// @notice Array of addresses for all clones created by this factory /// @dev Stores references to all deployed Clone contracts address[] public clones;
/// @notice Creates a new Clone contract instance /// @dev Increments the clone counter, deploys a new Clone with that ID, and stores its address function createClone() external { Clone clone = new Clone(++cloneCount); clones.push(address(clone)); }}
Factory Design Checklist for SKALE
Section titled “Factory Design Checklist for SKALE”The following are key concepts that should be used when designing factories for SKALE:
- Can this be done WITHOUT a factory. If yes, remove the factory
- Have you disabled generic deployment of the factory (i.e removing CREATE2)
- Have you disabled public deployment of clones (i.e using AccessControl)
- Do you have an explicit list of WHO should be able to deploy through the factory