Ephemeral accounts 是只存在于 Ephemeral Rollup 内部的账户。一个 sponsor 账户(已委托到 ER)会以 32 lamports/byte 的成本为 ephemeral accounts 代付租金,这比 Solana 基础租金便宜约 109 倍。 关键特性:Documentation Index
Fetch the complete documentation index at: https://docs.magicblock.gg/llms.txt
Use this file to discover all available pages before exploring further.
- 完全在 ER 中创建、存在并销毁
- 归调用它的程序所有(从 CPI 上下文推断)
- 由 sponsor 账户的 lamports 提供资金
- 可以被创建、调整大小和关闭
#[ephemeral_accounts] Macro
这个 proc-macro attribute 用于 Anchor 的 Accounts struct。它会识别 #[account(...)] 中的两个自定义标记:
| 标记 | 作用 |
|---|---|
sponsor | 标记为 ephemeral accounts 支付租金的账户 |
eph | 将账户标记为 ephemeral(仅存在于 ER) |
校验规则
- 如果存在
eph字段,则至少需要一个sponsor - 每个 struct 只允许 一个
sponsor eph不能与init或init_if_needed一起使用(请改用生成的方法)- 如果 sponsor 是 PDA(而不是
Signer),则必须提供用于 PDA 签名的seeds
生成的方法
对于名为conversation 的字段,macro 会生成:
| 方法 | 签名 | 说明 |
|---|---|---|
create_ephemeral_conversation | (data_len: u32) -> Result<()> | 创建 ephemeral account |
init_if_needed_ephemeral_conversation | (data_len: u32) -> Result<()> | 仅在 data_len == 0 时创建 |
resize_ephemeral_conversation | (new_data_len: u32) -> Result<()> | 扩大或缩小账户 |
close_ephemeral_conversation | () -> Result<()> | 关闭账户,并将租金退还给 sponsor |
签名要求
- 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
调整 Ephemeral Account 大小
关闭一个 Ephemeral Account
使用钱包作为 Sponsor
可以直接使用Signer 作为 sponsor,而不是 PDA:
TypeScript 客户端用法
所有 ephemeral account 操作都发送到 ER connection,而不是 base layer:常见坑点
eph fields must use AccountInfo, not Account
eph fields must use AccountInfo, not Account
eph fields must use AccountInfo<'info>, not Account<'info, T>. The account doesn’t exist yet at validation time, so Anchor cannot deserialize it.Manual serialization is required after create
Manual serialization is required after create
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.Cannot combine eph with init
Cannot combine eph with init
The macro enforces this at compile time. Use the generated
create_ephemeral_* method instead of Anchor’s init constraint.Sponsor must be delegated first
Sponsor must be delegated first
The sponsor account needs lamports on the ER to pay ephemeral rent. It must be delegated before creating ephemeral accounts.
Top up the sponsor before delegation
Top up the sponsor before delegation
Transfer extra SOL to the sponsor account before delegating it, so it has enough lamports to fund ephemeral accounts on the ER.
vault and magic_program are auto-injected
vault and magic_program are auto-injected
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

