Overview
A factory contract:- Stores the WASM code of contracts it can deploy
- Creates new contract accounts
- Deploys code to those accounts
- Optionally initializes the deployed contracts
GitHub Repository
View the complete factory contract example
How it works
Use cases
Token factories
Deploy new FT or NFT contracts on demand for users or projects
DAO factories
Create DAO contracts with custom governance rules
Game instances
Spawn isolated game session contracts
User contracts
Deploy personal contracts for each user (e.g., wallets, vaults)
Best practices
Account naming
Account naming
Choose a clear naming convention for deployed contracts:
- Use meaningful prefixes:
token.factory.near - Include version numbers if needed:
v1.token.factory.near - Consider uniqueness: append timestamps or counters
- Sub-accounts cost storage, factor into deposit requirements
Storage costs
Storage costs
Creating accounts and deploying contracts requires NEAR deposits:
- Account creation: ~0.00182 NEAR minimum
- Code storage: ~0.0001 NEAR per byte
- State storage: depends on contract initialization
- Always require sufficient deposit in
create_contract
Code updates
Code updates
Plan for updating the contract template:
- Implement
update_codewith proper access control - Consider versioning: maintain multiple templates
- Test thoroughly before updating production code
- Document breaking changes
Security considerations
Security considerations
Protect your factory from abuse:
- Limit who can create contracts (whitelist, fees, etc.)
- Validate contract initialization parameters
- Consider rate limiting
- Monitor for suspicious patterns
Advanced patterns
Versioned deployments
Maintain multiple contract versions:Access control
Restrict who can create contracts:Callback tracking
Track deployed contracts:Testing factory contracts
- Rust
- JavaScript
Next steps
Update contracts
Learn how to update deployed contracts
Cross-contract calls
Interact with deployed contracts
DAO primitives
Use factories to create DAOs
Contract standards
Implement standard interfaces