Introduction
On Ethereum, smart contracts can dynamically create new contracts, enabling complex decentralized applications like Uniswap's factory-generated liquidity pools. This guide explores contract creation using Solidity's create
method, with practical examples from a simplified Uniswap V2 implementation.
Key Concepts: Contract Creation with create
How create
Works
The create
method deploys new contracts using the new
keyword:
Contract x = new Contract{value: _value}(params)
Contract
: Target contract namex
: Address of the deployed contract_value
: Optional ETH transfer (for payable constructors)params
: Constructor arguments
Factory Contract Pattern
Decentralized exchanges like Uniswap use factory contracts to:
- Deploy multiple instances of template contracts (e.g., token pairs)
- Manage contract addresses efficiently
Practical Implementation: Minimal Uniswap Clone
1. Pair Contract (Token Pair Manager)
contract Pair {
address public factory; // Factory contract address
address public token0; // First token
address public token1; // Second token
constructor() payable {
factory = msg.sender;
}
function initialize(address _token0, address _token1) external {
require(msg.sender == factory, 'Unauthorized');
token0 = _token0;
token1 = _token1;
}
}
Key Features:
- Immutable factory address set during deployment
initialize()
configures token pairs post-creation
2. PairFactory Contract (Deployment Manager)
contract PairFactory {
mapping(address => mapping(address => address)) public getPair;
address[] public allPairs;
function createPair(address tokenA, address tokenB) external returns (address pairAddr) {
Pair pair = new Pair(); // Deploy new Pair contract
pair.initialize(tokenA, tokenB);
// Update address mappings
pairAddr = address(pair);
allPairs.push(pairAddr);
getPair[tokenA][tokenB] = pairAddr;
getPair[tokenB][tokenA] = pairAddr;
}
}
Core Functions:
- Tracks all deployed pairs via
allPairs
array - Enables O(1) address lookup with
getPair
mapping
👉 Explore advanced Solidity patterns for scalable DApp architectures.
Testing on Remix: Step-by-Step
- Deploy
PairFactory
Call
createPair
with:- WBNB:
0x2c44b726ADF1963cA47Af88B284C06f30380fC78
- PEOPLE:
0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c
- WBNB:
Verify:
- New Pair address generated (e.g.,
0xD3e200...
) - Token addresses stored correctly in Pair contract
- New Pair address generated (e.g.,
Debug Tip: Use Remix's debugger to inspect the CREATE
opcode execution.
FAQs
Q1: Why use factory contracts instead of direct deployment?
Factory contracts enable:
- Mass production of standardized contracts
- Centralized address management
- Gas efficiency through template reuse
Q2: Can created contracts create other contracts?
Yes! Contracts can recursively deploy others, but monitor gas limits and complexity.
Q3: How does create
differ from create2
?
create
generates addresses unpredictably, while create2
allows deterministic address pre-calculation.
👉 Learn about gas optimization techniques for high-frequency contract deployments.
Best Practices
- Access Control: Restrict
initialize
calls to factory contracts - Event Logging: Emit events for deployment tracking
- Security Audits: Verify contract templates before mass deployment
This pattern powers DeFi ecosystems—master it to build scalable blockchain solutions.