كيف تعمل العقود الذكية من الداخل؟ | How Smart Contracts Work Under the Hood
تعلّم ببساطة، خطوة بخطوة
Learn simply, step by step
معلومة واضحة، ثم خطوة جديدة.
Clear knowledge, one step at a time.
كيف تعمل العقود الذكية من الداخل؟ How Smart Contracts Work Under the Hood
لفهم كيف تعمل العقود الذكية من الداخل، يمكن النظر إليها كبرامج يتم نشرها على شبكة البلوكتشين، ثم يستطيع المستخدمون والتطبيقات استدعاء وظائفها لتنفيذ قواعد محددة مسبقًا.

1. تبدأ العقود الذكية بالكود
يكتب المطور العقد الذكي باستخدام لغة مخصصة لهذا النوع من البرامج. على شبكات متوافقة مع Ethereum تُعد Solidity من أشهر اللغات المستخدمة.
يحتوي العقد عادةً على مجموعة من الوظائف Functions والبيانات State Variables والقواعد التي تحدد ما يمكن للمستخدمين أو العقود الأخرى القيام به.
2. ترجمة العقد Compile
قبل نشر العقد، يتم تحويل الكود إلى صيغة تستطيع البيئة التنفيذية للبلوكتشين التعامل معها.
في Ethereum ينتج عن عملية الترجمة Bytecode يمكن تنفيذه بواسطة Ethereum Virtual Machine أو EVM، بالإضافة إلى معلومات تساعد التطبيقات على معرفة الوظائف التي يوفرها العقد.
3. نشر العقد على البلوكتشين
بعد الترجمة يتم إرسال معاملة لنشر العقد على الشبكة.
عند نجاح عملية النشر يصبح للعقد عنوان Contract Address يمكن استخدامه للتفاعل معه.
نشر العقد واستخدام الوظائف التي تغير حالة الشبكة يتطلب موارد حسابية، ولذلك ترتبط هذه العمليات برسوم Gas.
4. ماذا يوجد داخل العقد الذكي؟
لفهم كيف تعمل العقود الذكية، من المفيد معرفة بعض مكوناتها الأساسية:
Variables: بيانات يحتفظ بها العقد، مثل الأرصدة أو العناوين أو الإعدادات.
Functions: وظائف تحدد العمليات التي يستطيع العقد تنفيذها.
Events: سجلات يمكن للعقد إصدارها عندما تحدث عمليات معينة، وتستطيع التطبيقات مراقبتها.
Modifiers وشروط الوصول: قواعد يمكن استخدامها لتحديد من يستطيع تنفيذ وظيفة معينة وتحت أي شروط.
5. كيف يتفاعل المستخدم مع العقد؟
عادةً لا يتعامل المستخدم مع الكود مباشرة. تطبيق dApp يعرض واجهة سهلة مثل زر Swap أو Deposit أو Mint.
عندما يضغط المستخدم على زر يتطلب تغيير حالة البلوكتشين، يقوم التطبيق بإعداد طلب معاملة يتضمن الوظيفة المطلوبة والبيانات اللازمة لتنفيذها.
تظهر المعاملة في المحفظة حتى يستطيع المستخدم مراجعتها وتوقيعها قبل إرسالها إلى الشبكة.
6. ماذا يحدث بعد إرسال المعاملة؟
بعد إرسال المعاملة إلى الشبكة، تقوم البنية التحتية للشبكة بمعالجتها والتحقق منها، ثم يتم تنفيذ التعليمات الخاصة بالعقد وفق قواعد الشبكة.
إذا استوفت المعاملة شروط العقد وتم تنفيذها بنجاح، يمكن أن تتغير حالة العقد، مثل تحديث رصيد أو تسجيل ملكية أو تنفيذ عملية داخل بروتوكول DeFi.
القراءة تختلف عن الكتابة
ليست كل عملية تفاعل مع العقد معاملة تغير البلوكتشين.
يمكن قراءة بعض البيانات مثل رصيد أو قيمة مخزنة دون تغيير حالة العقد. أما تنفيذ وظيفة تقوم بتغيير البيانات على الشبكة فيحتاج عادةً إلى معاملة.
ما هي حالة العقد State؟
State هي البيانات الدائمة التي يحتفظ بها العقد على البلوكتشين.
على سبيل المثال، قد يحتفظ عقد توكن بأرصدة العناوين، بينما يحتفظ عقد آخر بمعلومات ملكية أصل رقمي أو بيانات بروتوكول معين.
عندما تنفذ معاملة وظيفة تغير هذه البيانات، يتم تحديث حالة العقد بعد نجاح التنفيذ.
ما هي Events؟
يمكن للعقد إصدار Events لتسجيل أن حدثًا معينًا وقع.
على سبيل المثال، قد يصدر عقد توكن حدث Transfer عند انتقال التوكنات من عنوان إلى آخر، ويمكن لواجهات التطبيقات والخدمات متابعة هذه الأحداث والاستجابة لها.
أين تدخل رسوم Gas؟
تنفيذ العمليات على Ethereum يحتاج إلى موارد حسابية، ويتم قياس هذا العمل باستخدام Gas.
العمليات الأكثر تعقيدًا أو التي تتطلب كتابة بيانات أكبر على الشبكة يمكن أن تستهلك Gas أكثر من العمليات الأبسط.
لهذا السبب يجب مراجعة رسوم الشبكة التي تظهر في المحفظة قبل تأكيد المعاملة.
هل يمكن تغيير العقد بعد نشره؟
كود العقد المنشور نفسه مصمم ليكون غير قابل للتعديل مباشرة. لكن توجد أنماط تطوير متقدمة مثل Upgradeable Contracts تستخدم عدة عقود وآليات مثل Proxy لتسمح للمشروع بتوجيه التفاعل إلى منطق جديد.
لذلك عبارة “العقد لا يمكن تغييره” تحتاج إلى فهم بنية المشروع كاملة، وليس النظر إلى عنوان واحد فقط.
لماذا يجب فهم العقد قبل التفاعل معه؟
توقيع معاملة قد يؤدي إلى تنفيذ وظيفة داخل عقد ذكي وتغيير حالة البلوكتشين، ولذلك من المهم معرفة العقد الذي تتفاعل معه والصلاحيات التي تمنحها.
إذا كنت تتعامل مع توكن غير معروف، يمكنك استخدام
Token Security Checker من TheCrypTechAI
كإحدى خطوات البحث الأولية قبل التفاعل معه.
وإذا وصلك رابط dApp غير معروف، يمكنك فحصه أولًا باستخدام
Suspicious Link Checker
.
مصدر إضافي
للتعمق أكثر في الوظائف والمتغيرات والأحداث وطريقة بناء العقود، يمكنك قراءة
دليل Ethereum الرسمي لبنية العقود الذكية
.
الخلاصة
لفهم كيف تعمل العقود الذكية من الداخل، تذكر التسلسل الأساسي:
كتابة الكود ← Compile ← نشر العقد ← عنوان العقد ← معاملة المستخدم ← تنفيذ الوظيفة ← تحديث الحالة ← تسجيل الأحداث.
العقد الذكي ليس مجرد اتفاق رقمي، بل برنامج يعمل وفق قواعد محددة على البلوكتشين، وله وظائف وبيانات وحالة يمكن أن تتغير نتيجة المعاملات.
في الدرس التالي سنتعرف على Oracles، ولماذا تحتاج العقود الذكية أحيانًا إلى طريقة للحصول على معلومات من خارج البلوكتشين.
To understand How Smart Contracts Work under the hood, think of them as programs deployed on a blockchain. Users and applications can interact with these programs by calling predefined functions that execute according to the contract's rules.

1. Smart Contracts Start with Code
Developers write smart contracts using programming languages designed for blockchain applications. On Ethereum-compatible networks, Solidity is one of the most widely used languages.
A contract can contain functions, state variables, events, and rules that define what users or other contracts are allowed to do.
2. Compiling the Contract
Before deployment, the source code must be converted into a format that the blockchain execution environment can understand.
On Ethereum, compilation produces bytecode that can be executed by the Ethereum Virtual Machine (EVM), along with information that applications can use to understand and interact with the contract's functions.
3. Deploying the Contract
After compilation, a deployment transaction is submitted to the blockchain.
Once deployment succeeds, the contract receives a Contract Address that users, applications, and other contracts can use to interact with it.
Deploying a contract and executing functions that change blockchain state require computational resources, which is why these operations involve gas fees.
4. What Is Inside a Smart Contract?
Understanding How Smart Contracts Work becomes easier when you know some of their main components:
Variables: Data stored by the contract, such as balances, addresses, or configuration values.
Functions: Blocks of code that define operations the contract can perform.
Events: Logs emitted by a contract when certain actions occur. Applications can monitor these events.
Modifiers and Access Rules: Conditions that can restrict who may execute certain functions and under what circumstances.
5. How Does a User Interact with a Contract?
Users normally do not interact directly with smart contract code. Instead, a dApp provides an interface with actions such as Swap, Deposit, Mint, or Borrow.
When a user chooses an action that changes blockchain state, the application prepares a transaction containing the required function call and parameters.
The wallet then displays the transaction so the user can review and sign it before sending it to the network.
6. What Happens After a Transaction Is Sent?
After the transaction is submitted, the blockchain network processes and validates it. The smart contract instructions are then executed according to the network and contract rules.
If the transaction satisfies the contract's conditions and executes successfully, the contract state may change. This could involve updating a balance, recording ownership, or performing an operation inside a DeFi protocol.
Reading Is Different from Writing
Not every interaction with a smart contract changes the blockchain.
Applications can read certain contract data without modifying its state. However, executing a function that changes on-chain data generally requires a transaction.
What Is Contract State?
State refers to persistent data maintained by a smart contract on the blockchain.
For example, a token contract may keep track of balances associated with addresses, while another contract may store ownership records or protocol-related information.
When a successful transaction executes a function that modifies this information, the contract state is updated.
What Are Events?
Smart contracts can emit Events to record that specific actions have occurred.
For example, a token contract may emit a Transfer event when tokens move between addresses. Applications and other services can monitor these events and respond to them.
Where Do Gas Fees Come In?
Executing operations on Ethereum requires computational resources, and this work is measured using Gas.
More complex operations or transactions that require more computation or storage can consume more gas than simpler operations.
For this reason, users should review the estimated network fee shown by their wallet before confirming a transaction.
Can a Smart Contract Be Changed After Deployment?
The code of an already deployed contract is designed to remain immutable. However, developers can build advanced architectures known as Upgradeable Contracts.
These systems may use multiple contracts and mechanisms such as proxies to redirect interactions to new logic while the previously deployed contracts themselves remain on the blockchain.
Therefore, understanding whether a protocol can be upgraded requires examining its complete architecture rather than looking at only one contract address.
Why Should You Understand a Contract Before Interacting?
Signing a transaction may execute a smart contract function and change blockchain state. It is therefore important to understand which contract you are interacting with and what permissions you are granting.
If you are dealing with an unfamiliar token, you can use the
TheCrypTechAI Token Security Checker
as one step in your research before interacting with it.
If you receive an unfamiliar dApp link, you can also check it first using the
Suspicious Link Checker
.
Learn More
For more information about functions, variables, events, and smart contract structure, read the
official Ethereum guide to smart contract anatomy
.
Key Takeaway
To remember How Smart Contracts Work, keep this basic sequence in mind:
Write Code → Compile → Deploy → Contract Address → User Transaction → Function Execution → State Update → Events.
A smart contract is not simply a digital agreement. It is a blockchain program containing functions, data, and rules that can execute when users or applications interact with it.
In the next lesson, we will explore Blockchain Oracles and learn why smart contracts sometimes need a way to access information that originates outside the blockchain.
اختبر فهمك
Check Your Understanding
سؤالان سريعان لتثبيت أهم ما تعلمته.
Two quick questions to reinforce the key ideas.