Web3 & DApp Development Fundamentals · 강의

이벤트와 스토리지 비교

가스 측면의 장단점

레슨 4/413개 단계

이벤트와 스토리지 비교은(는) CoddyKit의 무료 Web3 & DApp Development Fundamentals 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Web3 & DApp Development Fundamentals 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Web3 & DApp Development Fundamentals 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

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.

무료로 시작

AI 튜터와 함께 Web3 & DApp Development Fundamentals을(를) 배우세요 — 무료

브라우저에서 실제 코드를 작성하고 실행하며, 24/7 AI 튜터로부터 즉각적인 도움을 받고, 웹이나 앱에서 중단한 부분부터 계속 학습하세요.

코스
29
레슨
105

자주 묻는 질문

“이벤트와 스토리지 비교” 강의는 무료인가요?

네 — “이벤트와 스토리지 비교” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Web3 & DApp Development Fundamentals 강의 전체를 잠금 해제할 수 있습니다. Web3 & DApp Development Fundamentals 강의에는 총 4개의 강의가 포함되어 있습니다.

“이벤트와 스토리지 비교”에서 뭘 배우나요?

가스 측면의 장단점 브라우저에서 직접 실행하는 실습 코드로 Web3 & DApp Development Fundamentals을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Web3 & DApp Development Fundamentals을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Web3 & DApp Development Fundamentals은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.

“이벤트와 스토리지 비교” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Web3 & DApp Development Fundamentals 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Web3 & DApp Development Fundamentals 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 이벤트 선언하기
  2. 인덱싱된 매개변수
  3. 이벤트 수신하기
  4. 이벤트와 스토리지 비교
← Web3 & DApp Development Fundamentals(으)로 돌아가기