0Pricing
Web3 & DApp Development Fundamentals · 课时

索引参数

可筛选的主题

索引参数 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web3 & DApp Development Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Filtering Logs

Once a contract emits many events, off-chain apps need a way to filter them efficiently, for example to find every Transfer to a specific address.

Solidity supports this with indexed event parameters, which become searchable topics.

The indexed Keyword

Mark a parameter indexed to store it as a log topic rather than in the data section. Topics can be filtered quickly by Ethereum nodes.

<code>event Transfer(
    address indexed from,
    address indexed to,
    uint256 amount
);</code>

Topics vs Data

Each log has up to 4 topics and an arbitrary data blob:

  • Topic 0 is the event signature hash (automatic for non-anonymous events)
  • Each indexed parameter takes one more topic slot
  • Non-indexed parameters go into the data section

The Three-Indexed Limit

A non-anonymous event can have at most three indexed parameters, because topic 0 is reserved for the signature hash.

Adding a fourth indexed parameter is a compile error.

<code>// OK: 3 indexed params
event Trade(
    address indexed maker,
    address indexed taker,
    uint256 indexed orderId,
    uint256 price // not indexed
);</code>

Choosing What to Index

Index the fields you will search by: addresses, ids, categories. Keep large or descriptive values (amounts, strings) non-indexed so they stay readable in the data section.

<code>event ItemSold(
    uint256 indexed itemId,
    address indexed buyer,
    uint256 price
);</code>

Indexing Strings and Bytes

When you index a dynamic type like string or bytes, the topic stores the keccak256 hash of the value, not the value itself.

You can still filter by hashing the search term, but you cannot recover the original text from the topic.

<code>event Tagged(string indexed label, uint256 value);
// topic = keccak256(label), original text not in log</code>

Filtering on the Client

Client libraries use indexed topics to build filters. For example, fetching all transfers received by one address becomes a fast topic match.

<code>// ethers.js (off-chain)
// const filter = token.filters.Transfer(null, myAddress);
// const logs = await token.queryFilter(filter);</code>

The Standard Transfer Event

ERC-20 and ERC-721 define a Transfer event with indexed addresses. This is why wallets can show your incoming and outgoing transfers so quickly.

<code>// ERC-20
event Transfer(address indexed from, address indexed to, uint256 value);
// ERC-721
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);</code>

Gas Considerations

Indexed parameters cost slightly more gas than non-indexed ones because they occupy dedicated topic slots. Index only what you genuinely need to filter on.

Anonymous Events

An anonymous event omits the signature hash from topic 0, freeing a fourth indexed slot. They are rare and harder to decode, so use them only for advanced low-level cases.

<code>event LowLevel(uint256 indexed a, uint256 indexed b,
               uint256 indexed c, uint256 indexed d) anonymous;</code>

Mixing Indexed and Data

A practical event indexes the searchable keys and leaves descriptive payload in data, balancing filterability and readability.

<code>event Deposit(
    address indexed account, // searchable
    uint256 amount,          // readable in data
    uint256 timestamp
);</code>

Quick Check

Test your understanding of indexed parameters.

Recap

You learned about indexed event parameters:

  • indexed turns a parameter into a filterable topic
  • Max 3 indexed params (topic 0 = signature hash)
  • Indexed dynamic types store a keccak256 hash, not the value
  • Index searchable keys; keep descriptive payload in the data section

Good indexing makes off-chain queries fast and cheap.

常见问题解答

「索引参数」课时是免费的吗?

是的 — 「索引参数」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web3 & DApp Development Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。

「索引参数」这节课中我会学到什么?

可筛选的主题 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Web3 & DApp Development Fundamentals 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「索引参数」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?

能。每节 Web3 & DApp Development Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 声明事件
  2. 索引参数
  3. 监听事件
  4. 事件与存储
← 返回 Web3 & DApp Development Fundamentals