العقود القابلة للترقية
أنماط Proxy
العقود القابلة للترقية درس مجاني في Web3 & DApp Development Fundamentals على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Web3 & DApp Development Fundamentals، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Web3 & DApp Development Fundamentals 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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.
الأسئلة الشائعة
هل درس «العقود القابلة للترقية» مجاني؟
نعم — نص درس «العقود القابلة للترقية» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Web3 & DApp Development Fundamentals، انتقل إلى CoddyKit PRO. تتضمن دورة Web3 & DApp Development Fundamentals 4 دروس في المجموع.
ماذا ستتعلم في «العقود القابلة للترقية»؟
أنماط Proxy تتمرن على Web3 & DApp Development Fundamentals مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Web3 & DApp Development Fundamentals؟
لا تُشترط خبرة سابقة. Web3 & DApp Development Fundamentals على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «العقود القابلة للترقية»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Web3 & DApp Development Fundamentals هذا؟
نعم. كل درس في Web3 & DApp Development Fundamentals يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- لماذا OpenZeppelin
- التحكم في الوصول
- امتدادات الرموز
- العقود القابلة للترقية