Skip to main content
Ephemeral accounts 是只存在于 Ephemeral Rollup 内部的账户。一个 sponsor 账户(已委托到 ER)会以 32 lamports/byte 的成本为 ephemeral accounts 代付租金,这比 Solana 基础租金便宜约 109 倍。 关键特性:
  • 完全在 ER 中创建、存在并销毁
  • 归调用它的程序所有(从 CPI 上下文推断)
  • 由 sponsor 账户的 lamports 提供资金
  • 可以被创建、调整大小和关闭

#[ephemeral_accounts] Macro

这个 proc-macro attribute 用于 Anchor 的 Accounts struct。它会识别 #[account(...)] 中的两个自定义标记:

校验规则

  • 如果存在 eph 字段,则至少需要一个 sponsor
  • 每个 struct 只允许 一个 sponsor
  • eph 不能与 initinit_if_needed 一起使用(请改用生成的方法)
  • 如果 sponsor 是 PDA(而不是 Signer),则必须提供用于 PDA 签名的 seeds

生成的方法

对于名为 conversation 的字段,macro 会生成:

签名要求

  • Sponsor:在所有操作(create、resize、close)中都必须是 signer
  • Ephemeral仅在 create 时 必须是 signer(防止 pubkey 抢注)。resize 或 close 时不要求。
  • 对于 PDA 账户,macro 会通过 find_program_address 自动推导 signer seeds

租金模型

  • 扩容:sponsor 向 vault 支付额外租金
  • 缩容:vault 将多余租金退还给 sponsor
  • 关闭:所有租金都从 vault 退还给 sponsor

创建一个 Ephemeral Account

在调用 create_ephemeral_* 之后,你必须手动将数据 struct 序列化到原始账户数据中。该 macro 只会分配空间,并不会初始化数据。

调整 Ephemeral Account 大小


关闭一个 Ephemeral Account


使用钱包作为 Sponsor

可以直接使用 Signer 作为 sponsor,而不是 PDA:

TypeScript 客户端用法

所有 ephemeral account 操作都发送到 ER connection,而不是 base layer:

常见坑点

eph fields must use AccountInfo<'info>, not Account<'info, T>. The account doesn’t exist yet at validation time, so Anchor cannot deserialize it.
After calling create_ephemeral_*, you must serialize your data struct into the raw account data yourself. The macro allocates space but does not write any data.
The macro enforces this at compile time. Use the generated create_ephemeral_* method instead of Anchor’s init constraint.
Transfer extra SOL to the sponsor account before delegating it, so it has enough lamports to fund ephemeral accounts on the ER.
You don’t need to declare them in your struct, but they appear in the IDL and must be passed from the client. Anchor resolves them automatically if named correctly.

Learn More

Ephemeral Accounts Demo

Full example program on GitHub

Delegation & Undelegation

How delegation and state synchronization work

Quickstart

Build your first program with Ephemeral Rollups