Solidity Solidity Advanced Patterns and Architecture 1 — Questions and Answers
Question 1: What is the purpose of the Proxy Upgrade Pattern (UUPS or Transparent Proxy) in Solidity?
- It allows replacing a contract's logic while preserving its storage state and address (Correct answer)
- It allows a contract to pay its own gas fees
- It enables a contract to call itself recursively without a stack limit
- It provides automatic circuit-breaker functionality
Correct answer: It allows replacing a contract's logic while preserving its storage state and address
Proxy upgrade patterns separate storage (proxy) from logic (implementation), enabling developers to fix bugs or add features without migrating user data to a new address.
Question 2: What is the key difference between UUPS and Transparent Proxy upgrade patterns?
- UUPS places upgrade logic in the implementation contract; Transparent Proxy places it in the proxy contract (Correct answer)
- UUPS uses delegatecall while Transparent Proxy uses staticcall
- Transparent Proxy supports ERC-20 while UUPS does not
- UUPS requires a multisig while Transparent Proxy does not
Correct answer: UUPS places upgrade logic in the implementation contract; Transparent Proxy places it in the proxy contract
In UUPS, the upgrade function lives in the implementation contract (smaller proxy, lower deployment cost); in Transparent Proxy, the proxy itself handles upgrades.
Question 3: What is the Factory pattern in Solidity and what problem does it solve?
- A contract that deploys other contracts programmatically, enabling on-chain contract creation with tracked addresses (Correct answer)
- A pattern that caches expensive computations across multiple calls
- A pattern that batches multiple ERC-20 transfers into one transaction
- A contract that generates random salt values for CREATE2 deployments
Correct answer: A contract that deploys other contracts programmatically, enabling on-chain contract creation with tracked addresses
The Factory pattern lets a single contract deploy and register many instances of another contract, enabling on-chain tracking of all deployed children.
Question 4: What does `CREATE2` opcode allow that `CREATE` does not?
- Deterministic contract address calculation before deployment, based on deployer + salt + bytecode hash (Correct answer)
- Deploying contracts without a constructor
- Deploying contracts that can be self-destructed and redeployed
- Deploying to a different chain with the same address automatically
Correct answer: Deterministic contract address calculation before deployment, based on deployer + salt + bytecode hash
CREATE2 lets you compute a contract's address before deployment using the deployer address, a salt, and the init code hash, enabling counterfactual contracts.
Question 5: What is the Diamond Pattern (EIP-2535) in Solidity?
- A proxy pattern that routes function calls to multiple implementation contracts (facets) based on function selectors (Correct answer)
- A multi-sig pattern where approvals form a diamond graph
- A storage pattern using diamond inheritance for cheaper reads
- A token standard combining ERC-20 and ERC-721 in one contract
Correct answer: A proxy pattern that routes function calls to multiple implementation contracts (facets) based on function selectors
The Diamond Pattern allows a single proxy contract to delegate to multiple logic contracts (facets), bypassing the 24KB contract size limit and enabling modular upgrades.
Question 6: What is the purpose of the `initializer` modifier in OpenZeppelin's upgradeable contracts?
- It ensures the initialization function can only be called once, replacing the constructor for proxy-based contracts (Correct answer)
- It marks a function as payable during deployment only
- It automatically calls the parent contract's constructor
- It prevents the contract from being paused during initialization
Correct answer: It ensures the initialization function can only be called once, replacing the constructor for proxy-based contracts
Upgradeable contracts cannot use constructors (which run at deploy time in the implementation, not the proxy), so `initializer` gates a setup function to run exactly once.
What is the purpose of the Proxy Upgrade Pattern (UUPS or Transparent Proxy) in Solidity?