Contratti aggiornabili
Pattern proxy
Contratti aggiornabili è una lezione Web3 & DApp Development Fundamentals gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Web3 & DApp Development Fundamentals, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Web3 & DApp Development Fundamentals include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
The Immutability Problem
Deployed contract code cannot be changed. If you find a bug or need a new feature, the code is frozen at its address.
Upgradeable contracts work around this using a proxy pattern that lets you swap the logic while keeping the same address and data.
The Proxy Pattern
Upgradeability splits a contract into two parts:
- Proxy — holds the state and a pointer to the logic; users interact with it.
- Implementation — holds the code, no permanent state.
The proxy delegatecalls into the implementation, so logic runs against the proxy's storage.
How delegatecall Works
delegatecall executes another contract's code in the caller's storage context. So when the proxy delegatecalls the implementation:
- Code comes from the implementation.
- Storage read/written is the proxy's.
Upgrading just changes which implementation the proxy points to.
Initializers, Not Constructors
Constructors run at deploy time and do not affect proxy storage. Upgradeable contracts use an initialize function instead:
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
contract Box is Initializable {
uint256 public value;
function initialize(uint256 v) public initializer {
value = v;
}
}import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
contract Box is Initializable {
uint256 public value;
function initialize(uint256 v) public initializer {
value = v;
}
}The Upgradeable Library
OpenZeppelin ships a separate package for this. Its modules use initializers instead of constructors:
npm install @openzeppelin/contracts-upgradeablePair it with the Hardhat Upgrades plugin to deploy and manage proxies safely.
npm install @openzeppelin/contracts-upgradeable @openzeppelin/hardhat-upgradesDeploying a Proxy
The Hardhat Upgrades plugin deploys the implementation and proxy together, calling your initializer:
const { ethers, upgrades } = require("hardhat");
const Box = await ethers.getContractFactory("Box");
const box = await upgrades.deployProxy(Box, [42]);
await box.waitForDeployment();The array holds the initializer arguments.
const { ethers, upgrades } = require("hardhat");
const Box = await ethers.getContractFactory("Box");
const box = await upgrades.deployProxy(Box, [42]);
await box.waitForDeployment();Performing an Upgrade
To upgrade, deploy a new implementation and point the existing proxy at it:
const BoxV2 = await ethers.getContractFactory("BoxV2");
const upgraded = await upgrades.upgradeProxy(
await box.getAddress(),
BoxV2
);The address and stored state are preserved; only the logic changes.
const BoxV2 = await ethers.getContractFactory("BoxV2");
const upgraded = await upgrades.upgradeProxy(
await box.getAddress(),
BoxV2
);Storage Layout Rules
Because the proxy keeps its storage, new versions must preserve the storage layout:
- Never reorder or remove existing state variables.
- Only append new variables at the end.
- Reserve gaps in base contracts for future fields.
The plugin checks for unsafe changes and warns you.
Transparent vs UUPS
Two common proxy styles:
- Transparent — upgrade logic lives in the proxy; simple but slightly more gas.
- UUPS — upgrade logic lives in the implementation (via
UUPSUpgradeable); cheaper, but you must not forget to include it.
UUPS is now the generally recommended default.
Upgradeability Trade-offs
Upgradeability is powerful but adds risk:
- Whoever controls upgrades can change the rules — a centralization concern.
- Storage and initializer mistakes can brick the contract.
Guard the upgrade authority with a multisig or timelock, and test upgrades thoroughly.
Disabling Initializers in the Implementation
An implementation contract should never be initialized directly. Lock it in its constructor:
constructor() {
_disableInitializers();
}This prevents an attacker from taking over the standalone implementation while leaving the proxy's initializer usable.
constructor() {
_disableInitializers();
}Quick Check
Test your understanding of upgradeable contracts.
Recap
You learned how upgradeable contracts work.
- A proxy holds state and
delegatecalls a swappable implementation. - Use
initialize(guarded byinitializer) instead of a constructor. - The Hardhat Upgrades plugin deploys and upgrades proxies safely.
- Preserve storage layout: only append variables; use gaps.
- Choose Transparent or UUPS, and protect upgrade authority with a multisig or timelock.
Impara Web3 & DApp Development Fundamentals con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 29
- Lezioni
- 105
Domande Frequenti
La lezione «Contratti aggiornabili» è gratuita?
Sì — il testo completo di «Contratti aggiornabili» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Web3 & DApp Development Fundamentals, passa a CoddyKit PRO. Il corso Web3 & DApp Development Fundamentals include 4 lezioni in totale.
Cosa imparerò in «Contratti aggiornabili»?
Pattern proxy Eserciti Web3 & DApp Development Fundamentals con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Web3 & DApp Development Fundamentals?
Non è richiesta alcuna esperienza precedente. Web3 & DApp Development Fundamentals su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Contratti aggiornabili»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Web3 & DApp Development Fundamentals?
Sì. Ogni lezione Web3 & DApp Development Fundamentals include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Perché OpenZeppelin
- Controllo degli accessi
- Estensioni dei token
- Contratti aggiornabili