Olaylar ve Depolama Karşılaştırması
Gas ödünleşimleri
Olaylar ve Depolama Karşılaştırması, CoddyKit'te ücretsiz bir Web3 & DApp Development Fundamentals dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Web3 & DApp Development Fundamentals öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Web3 & DApp Development Fundamentals kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
Two Ways to Record Data
When something happens in a contract you can record it in storage or in an event log. Choosing wisely is a core gas-optimization skill.
This lesson compares the trade-offs so you know when to use each.
Storage Recap
Storage is the contract's permanent state. It is the only data the contract itself can read back later, but writing to it is very expensive.
<code>mapping(address => uint256) public balances; // contract can read this</code>Events Recap
Events write to the transaction log. They are far cheaper, but the contract cannot read them back on-chain - only off-chain clients can.
<code>event BalanceChanged(address indexed who, uint256 newBalance);
// off-chain apps read this; the contract cannot</code>Gas Cost Difference
Writing a fresh storage slot (an SSTORE from zero) costs around 20,000 gas. Emitting a log topic or word costs only a few hundred gas. Events can be an order of magnitude cheaper.
The Golden Rule
A simple guideline:
- Store data the contract needs to read or use in future logic
- Emit data only the outside world needs (history, UI, analytics)
Do not store data purely for display - emit it instead.
What Must Be Storage
Balances, ownership, allowances, and any value used in require checks or future calculations must live in storage, because the contract reads them.
<code>function transfer(address to, uint256 amt) public {
require(balances[msg.sender] >= amt); // needs storage
balances[msg.sender] -= amt;
balances[to] += amt;
}</code>What Can Be Events Only
A transaction history, audit trail, or notification feed is perfect for events. The contract never needs to revisit those records, so storing them would waste gas.
<code>event Logged(address indexed user, string action, uint256 time);
function act(string memory a) public {
emit Logged(msg.sender, a, block.timestamp); // no storage
}</code>Use Both Together
The most common pattern is to store the new state and emit an event describing the change. The storage value drives logic; the event keeps the UI in sync.
<code>function deposit() public payable {
balances[msg.sender] += msg.value; // storage for logic
emit BalanceChanged(msg.sender, balances[msg.sender]); // event for UI
}</code>Logs Cannot Be Queried On-Chain
A critical limitation: another contract cannot read your emitted logs. If contract B needs a value, contract A must expose it via storage or a return value, never via an event.
Avoid Storing for History
A common beginner mistake is pushing every action into a storage array to build a history. This grows unbounded and becomes hugely expensive. Emit events instead and let an off-chain indexer rebuild the history.
<code>// Anti-pattern: unbounded, costly
// historyArray.push(record);
// Better:
emit ActionRecorded(msg.sender, block.timestamp);</code>Decision Checklist
Ask: Does any on-chain code read this value later?
- Yes - storage (and optionally emit too)
- No, it is only for display or audit - event only
This single question resolves most design choices.
Quick Check
Test your understanding of events versus storage.
Recap
You learned the storage vs events trade-off:
- Storage is readable on-chain but expensive (~20k gas per fresh slot)
- Events are cheap but only off-chain clients can read them
- Store what the contract needs; emit what only the outside world needs
- The common pattern: update storage AND emit an event
Never store data purely for history or display.
Yapay zeka eğitmeniyle Web3 & DApp Development Fundamentals öğren — ücretsiz
Tarayıcında gerçek kod yaz ve çalıştır, 7/24 yapay zeka eğitmeninden anında yardım al; web'de ya da uygulamada kaldığın yerden devam et.
- Kurslar
- 29
- Dersler
- 105
Sıkça Sorulan Sorular
“Olaylar ve Depolama Karşılaştırması” dersi ücretsiz mi?
Evet — “Olaylar ve Depolama Karşılaştırması” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Web3 & DApp Development Fundamentals kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Web3 & DApp Development Fundamentals kursu toplamda 4 dersten oluşur.
“Olaylar ve Depolama Karşılaştırması” dersinde ne öğreneceğim?
Gas ödünleşimleri Web3 & DApp Development Fundamentals ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Web3 & DApp Development Fundamentals öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Web3 & DApp Development Fundamentals, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.
“Olaylar ve Depolama Karşılaştırması” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Web3 & DApp Development Fundamentals dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Web3 & DApp Development Fundamentals dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Olay Tanımlama
- Dizinlenmiş Parametreler
- Olayları Dinleme
- Olaylar ve Depolama Karşılaştırması