freecoding.school100% FREE · NO SIGNUP
The EVM CoreISSUE #46 of 48

factory & registry patterns

SoliaVSThe Reentrancy Reaper
Solia saysA factory deploys new instances and a registry tracks them — together they make per-user contracts discoverable, queryable, and indexable without an off-chain database.

The factory pattern is a contract whose only job is to deploy other contracts. The registry pattern tracks every instance the factory has produced. Combined, you get an on-chain phonebook: "show me every vault Alice owns," "list all marketplaces created last week," etc. Front-ends query the registry instead of replaying every InstanceCreated event.

Real-world examples: Gnosis Safe ProxyFactory + tracker, Uniswap V2 PairFactory, Aave reserve registry. The demo deploys three instances and queries them by owner and by type.

Power-ups you unlock

The Reentrancy Reaper attacks — common mistakes

Boss battleDeploy three instances via a factory, store them in a registry, then query by owner and by type.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>
const registry = [];
function factoryDeploy(owner, type){
  const instance = { id:'inst-' + (registry.length + 1), owner, type };
  registry.push(instance);
  return instance;
}
factoryDeploy('alice', 'Vault');
factoryDeploy('bob',   'Vault');
factoryDeploy('carol', 'Escrow');
factoryDeploy('alice', 'Escrow');
const query = (pred) => registry.filter(pred);
const aliceAll  = query(x => x.owner === 'alice');
const allVaults = query(x => x.type === 'Vault');
document.getElementById('o').textContent = [
  'registry (' + registry.length + ' instances):',
  ...registry.map(i => '  ' + i.id + '  owner=' + i.owner + '  type=' + i.type),
  '',
  'query owner=alice  → ' + aliceAll.map(i => i.id).join(', '),
  'query type=Vault   → ' + allVaults.map(i => i.id).join(', ')
].join('\n');
</script></body></html>
▶ Open the interactive comic issue
‹ Beacon Proxies · Mass UpgradesState Machines On-Chain ›