Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api-reference/spl-api/introduction-per.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The Private Ephemeral SPL Token API allows you to build transactions with SPL to

### Health & Configuration

- [**Root**](/api-reference/spl-api/root) - Check API health
- **Root** - Check API health
- [**Get Config**](/api-reference/spl-api/get-config) - Retrieve API configuration settings

### Setup & Initialization
Expand All @@ -23,7 +23,7 @@ The Private Ephemeral SPL Token API allows you to build transactions with SPL to
- [**Initialize Ephemeral ATA**](/api-reference/spl-api/initialize-ephemeral-ata) - Initialize an ephemeral token account required for sender and receiver
- [**Deposit SPL Tokens**](/api-reference/spl-api/deposit-spl-tokens) - Deposit tokens into an ephemeral account
- [**Delegate Ephemeral ATA**](/api-reference/spl-api/delegate-ephemeral-ata) - Delegate an ephemeral account to a Delegation Program
- [**Transfer SPL Tokens**](/api-reference/spl-api/transfer-ephemeral-ata) - Transfer tokens between accounts using SPL checked transfer (**Note:** Ephemeral ATA must be initialized and delegated before transfer)
- [**Transfer SPL Tokens**](/api-reference/spl-api/checked-transfer) - Transfer tokens between accounts using SPL checked transfer (**Note:** Ephemeral ATA must be initialized and delegated before transfer)
- [**Undelegate Ephemeral ATA**](/api-reference/spl-api/undelegate-ephemeral-ata) - Remove delegation from an ephemeral account
- [**Withdraw SPL Tokens**](/api-reference/spl-api/withdraw-spl-tokens) - Withdraw tokens from an ephemeral account

Expand Down
4 changes: 2 additions & 2 deletions cn/api-reference/spl-api/introduction-per.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Private Ephemeral SPL Token API 允许你在 MagicBlock 的 Ephemeral Rollup 上

### 健康检查与配置

- [**Root**](/cn/api-reference/spl-api/root) - 检查 API 健康状态
- **Root** - 检查 API 健康状态
- [**Get Config**](/cn/api-reference/spl-api/get-config) - 获取 API 配置设置

### 设置与初始化
Expand All @@ -23,7 +23,7 @@ Private Ephemeral SPL Token API 允许你在 MagicBlock 的 Ephemeral Rollup 上
- [**Initialize Ephemeral ATA**](/cn/api-reference/spl-api/initialize-ephemeral-ata) - 初始化发送方和接收方所需的临时 token 账户
- [**Deposit SPL Tokens**](/cn/api-reference/spl-api/deposit-spl-tokens) - 将 token 存入临时账户
- [**Delegate Ephemeral ATA**](/cn/api-reference/spl-api/delegate-ephemeral-ata) - 将临时账户委托给 Delegation Program
- [**Transfer SPL Tokens**](/cn/api-reference/spl-api/transfer-ephemeral-ata) - 使用 SPL checked transfer 在账户之间转移 token(**注意:** 转账前必须先初始化并委托 Ephemeral ATA)
- [**Transfer SPL Tokens**](/cn/api-reference/spl-api/checked-transfer) - 使用 SPL checked transfer 在账户之间转移 token(**注意:** 转账前必须先初始化并委托 Ephemeral ATA)
- [**Undelegate Ephemeral ATA**](/cn/api-reference/spl-api/undelegate-ephemeral-ata) - 移除临时账户的委托
- [**Withdraw SPL Tokens**](/cn/api-reference/spl-api/withdraw-spl-tokens) - 从临时账户提取 token

Expand Down
280 changes: 280 additions & 0 deletions cn/pages/ephemeral-rollups-ers/how-to-guide/anchor.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
---
title: "Anchor 示例"
description: "学习如何编写一个在 Solana 上递增计数器的简单 Anchor 程序"
---

import Endpoints from "/cn/snippets/endpoints.mdx";

<div
style={{
position: "relative",
paddingBottom: "56.25%",
height: 0,
overflow: "hidden",
}}
>
<iframe
src="https://www.youtube.com/embed/qwu2RBKyFiw?si=PMg-3UbRfvvbrs7C&list=PLWR_ZQiGMS8mIe1kPZe8OfHIbhvZqaM8V"
title="Build a real-time Anchor Counter on Solana with MagicBlock's Ephemeral Rollup | Tutorial"
style={{
position: "absolute",
top: 0,
left: 0,
width: "100%",
height: "100%",
}}
frameBorder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
allowFullScreen
referrerPolicy="strict-origin-when-cross-origin"
/>
</div>

本指南将带你编写一个简单的 Anchor 程序,用来递增计数器。你将学习如何把这个程序部署到 Solana 上,并通过 React 客户端与之交互。

## 软件包

该程序使用以下软件包开发和测试。其他软件版本也可能兼容。

| 软件 | 版本 | 安装指南 |
| ---------- | ------ | --------------------------------------------------------------- |
| **Solana** | 3.1.9 | [安装 Solana](https://docs.anza.xyz/cli/install) |
| **Rust** | 1.89.0 | [安装 Rust](https://www.rust-lang.org/tools/install) |
| **Anchor** | 1.0.2 | [安装 Anchor](https://www.anchor-lang.com/docs/installation) |

## 快速查看源代码

如果你想直接进入代码:

<CardGroup cols={2}>
<Card
title="源代码:Anchor Counter 程序"
icon="anchor"
href="https://github.com/magicblock-labs/magicblock-engine-examples/tree/main/anchor-counter"
iconType="duotone"
></Card>
<Card
title="在线示例应用"
icon="react"
href="https://counter-example.magicblock.app/"
iconType="duotone"
></Card>
</CardGroup>

## 编写 Anchor 程序

下面我们来拆解这个计数器程序的关键组成部分:

## 核心功能

该程序实现了两个主要 instruction:

1. `initialize`:将计数器设为 0
2. `increment`:将计数器加 1

下面是程序的核心结构:

```rust
#[ephemeral]
#[program]
pub mod public_counter {
use super::*;

/// Initialize the counter.
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
let counter = &mut ctx.accounts.counter;
counter.count = 0;
Ok(())
}

/// Increment the counter.
pub fn increment(ctx: Context<Increment>) -> Result<()> {
let counter = &mut ctx.accounts.counter;
counter.count += 1;
Ok(())
}

// ... Additional instructions will be added here
}
```

这里没有什么特别之处,只是一个简单的、用于递增计数器的 Anchor 程序。唯一的区别是我们引入了 `delegate` 宏,用来注入与委托程序交互所需的逻辑。

### 委托 Counter PDA

为了将 counter PDA 委托出去,使其在 Ephemeral Rollup 会话中变为可写,我们需要添加一个内部调用 `delegate_account` 函数的 instruction。`delegate_account` 会通过 CPI 调用委托程序,验证通过后会接管该账户的所有权。
完成这一步后,临时验证者就可以开始处理 counter PDA 上的交易,并通过委托程序提交状态差异。

<Card
title="交易(Base Layer):Delegate"
icon="magnifying-glass"
href="https://solscan.io/tx/5tyEk8gkdP88xe8GL7X12f9WGZfYZtmDbuXozvqkpea4rphXPiNp91vAzmSjsyUR399wimBYazEszKSH35sidPRH?cluster=devnet"
iconType="duotone"
>
在 Solana Explorer 上查看交易详情
</Card>
```rust
/// Add delegate function to the context
#[delegate]
#[derive(Accounts)]
pub struct DelegateInput<'info> {
pub payer: Signer<'info>,
/// CHECK The pda to delegate
#[account(mut, del)]
pub pda: AccountInfo<'info>,
}
````

```rust
/// Delegate the account to the delegation program
/// Set specific validator based on ER, see https://docs.magicblock.gg/pages/ephemeral-rollups-ers/how-to-guide/local-development
pub fn delegate(ctx: Context<DelegateInput>) -> Result<()> {
ctx.accounts.delegate_pda(
&ctx.accounts.payer,
&[COUNTER_SEED],
DelegateConfig {
// Optionally set a specific validator from the first remaining account
validator: ctx.remaining_accounts.first().map(|acc| acc.key()),
..Default::default()
},
)?;
Ok(())
}
```

### 在 PDA 已被委托时进行 commit

Ephemeral 运行时允许在 PDA 处于委托状态时提交其状态。这通过构建一个带有 `commit` intent 的 `MagicIntentBundleBuilder` 并执行它来完成。

<CardGroup cols={2}>
<Card
title="交易(ER):Commit"
icon="magnifying-glass"
href="https://solscan.io/tx/335F5UcgAW3P8wSaVoEPiBoxeykw9wR1kcJLWUnd9sHmxymCgGvLtxaGDy6ZKDgJw2ku9jtRqwVNY85DuAHvs9GT?cluster=custom&customUrl=https://devnet.magicblock.app"
iconType="duotone"
>
在 Solana Explorer 上查看交易详情
</Card>
<Card
title="交易(Base Layer):Commit"
icon="magnifying-glass"
href="https://solscan.io/tx/3xzMGbj2fwFCnGp6r7wyMv5LExmL6SXaEbt52DmxA3Wx1LyksuXpPJS4AAPtqoSeJe3aF7oY3jFNiEhBQKwhzRGN?cluster=devnet"
iconType="duotone"
>
在 Solana Explorer 上查看交易详情
</Card>
</CardGroup>
```rust
use ephemeral_rollups_sdk::ephem::MagicIntentBundleBuilder;

/// Increment the counter and manually commit the account in the Ephemeral Rollup session.
pub fn increment_and_commit(ctx: Context<IncrementAndCommit>) -> Result<()> {
let counter = &mut ctx.accounts.counter;
counter.count += 1;
// Serialize the Anchor account before the CPI sees it
counter.exit(&crate::ID)?;
MagicIntentBundleBuilder::new(
ctx.accounts.payer.to_account_info(),
ctx.accounts.magic_context.to_account_info(),
ctx.accounts.magic_program.to_account_info(),
)
.commit(&[ctx.accounts.counter.to_account_info()])
.build_and_invoke()?;
Ok(())
}
````

### 解除 PDA 的委托

解除 PDA 委托是通过构建一个带有 `commit_and_undelegate` intent 的 `MagicIntentBundleBuilder` 来完成的。
该操作会提交最新状态,并将 PDA 的所有权交还给所属程序。

<CardGroup cols={2}>
<Card
title="交易(ER):Undelegate"
icon="magnifying-glass"
href="https://solscan.io/tx/3MN6Avygk3FwsuQPoKHzpLo9kBBommHwRHTdgxBooDwsaNHgSYr1yaDXYEe63Eii5DCVbtaK89Yyrhy3dSGhHmvw?cluster=custom&customUrl=https://devnet.magicblock.app"
iconType="duotone"
>
在 Solana Explorer 上查看交易详情
</Card>
<Card
title="交易(Base Layer):Undelegate"
icon="magnifying-glass"
href="https://solscan.io/tx/2YwzNQh632bJu8TSP9GPsXmh7o78KgMMM9QPNbdaAzamtjGZfmT4o7bkG2PtpCWp7aJmmxpxTXr4Qo36k5EjgPab?cluster=devnet"
iconType="duotone"
>
在 Solana Explorer 上查看交易详情
</Card>
</CardGroup>

```rust
use ephemeral_rollups_sdk::ephem::MagicIntentBundleBuilder;

/// Undelegate the account from the delegation program
pub fn undelegate(ctx: Context<IncrementAndCommit>) -> Result<()> {
MagicIntentBundleBuilder::new(
ctx.accounts.payer.to_account_info(),
ctx.accounts.magic_context.to_account_info(),
ctx.accounts.magic_program.to_account_info(),
)
.commit_and_undelegate(&[ctx.accounts.counter.to_account_info()])
.build_and_invoke()?;
Ok(())
}
```

## 接入 React 客户端

React 客户端是一个简单的界面,让你可以与 Anchor 程序进行交互。
它使用 Anchor 绑定与程序通信,并通过 MagicBlock SDK 与 Ephemeral Rollup 会话交互。源代码与程序放在一起,位于 [`anchor-counter/app`](https://github.com/magicblock-labs/magicblock-engine-examples/tree/main/anchor-counter/app)。

{" "}

<Card
title="在线示例应用"
icon="react"
href="https://counter-example.magicblock.app/"
iconType="duotone"
></Card>

<iframe
src="https://counter-example.magicblock.app/"
width="100%"
height="500"
frameborder="0"
></iframe>

<Warning>
iframe 仅适用于部分钱包(如 Backpack)。如果遇到问题,可在此直接体验在线 demo:https://counter-example.magicblock.app/
</Warning>

### Ephemeral 端点配置

要与 Ephemeral Rollup 会话进行交互,你需要配置正确的端点:

- 对于 devnet,使用以下 ephemeral 端点:

https://devnet.magicblock.app

- 对于 mainnet,请联系 MagicBlock 团队获取相应端点。
- 对于 <a href="/cn/pages/ephemeral-rollups-ers/how-to-guide/local-development">localhost</a>,请下载并安装 ephemeral validator,并使用相应的环境变量在本地运行。

请确保根据你的开发或生产环境,将客户端配置更新为正确的端点。

<Endpoints />

<CardGroup cols={2}>
<Card
title="源代码:Anchor Counter 程序"
icon="anchor"
href="https://github.com/magicblock-labs/magicblock-engine-examples/tree/main/anchor-counter"
iconType="duotone"
></Card>
<Card
title="在线示例应用"
icon="react"
href="https://counter-example.magicblock.app/"
iconType="duotone"
></Card>
</CardGroup>
4 changes: 2 additions & 2 deletions cn/pages/get-started/introduction/ephemeral-rollup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ Ephemeral rollup 作为一个 **专用 SVM runtime** 运行,能够显著提升
<Card
title="Magic Router"
icon="router"
href="/cn/pages/get-started/introduction/smart-router"
href="/cn/pages/ephemeral-rollups-ers/introduction/magic-router"
iconType="duotone"
>
了解 Magic Router 的工作方式
</Card>
<Card
title="构建"
icon="hammer"
href="/cn/pages/get-started/how-integrate-your-program/overview"
href="/cn/pages/ephemeral-rollups-ers/how-to-guide/quickstart"
iconType="duotone"
>
使用 Rust、Anchor 和 Typescript 进行实践
Expand Down
4 changes: 2 additions & 2 deletions cn/pages/get-started/introduction/why-magicblock.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ MagicBlock 以专用的 **Solana Virtual Machine (SVM) runtime** 形式运行,
<Card
title="Magic Router"
icon="router"
href="/cn/pages/get-started/introduction/magic-router"
href="/cn/pages/ephemeral-rollups-ers/introduction/magic-router"
iconType="duotone"
>
了解 Magic Router 的工作方式
</Card>
<Card
title="构建"
icon="hammer"
href="/cn/pages/get-started/how-integrate-your-program/overview"
href="/cn/pages/ephemeral-rollups-ers/how-to-guide/quickstart"
iconType="duotone"
>
使用 Rust、Anchor 和 Typescript 进行实践
Expand Down
4 changes: 2 additions & 2 deletions cn/pages/tools/solana-unity-sdk/guides/host-your-game.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ git push origin <branch>

- 你的仓库现在应与 SDK 的 [gp-pages](https://github.com/garbles-labs/Solana.Unity-SDK/tree/gh-pages) 分支类似
- 在仓库设置中启用 gh-pages 部署
![](images/gh-pages-deply.png)
![](/images/gh-pages-deply.png)

Github 会为线上部署提供一个 URL: [garbles-labs.github.io/Solana.Unity-SDK](https://garbles-labs.github.io/Solana.Unity-SDK/)

Expand All @@ -45,7 +45,7 @@ Github 会为线上部署提供一个 URL: [garbles-labs.github.io/Solana.Unit
</Card>

<Card title="将你的游戏发布为 xNFT" icon="lightbulb" iconType="duotone">
按照[此指南](/Build/SolanaUnitySDK/guides/publishing-a-game)操作,你可以在不到 2 分钟内将游戏发布为 xNFT。
按照[此指南](/cn/pages/tools/solana-unity-sdk/guides/publishing-a-game)操作,你可以在不到 2 分钟内将游戏发布为 xNFT。
</Card>

祝你游戏开发顺利,也别忘了给仓库点个 ⭐
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ title: "将你的游戏发布为 xNFT"
icon="lightbulb"
iconType="duotone"
>
按照[这份指南](/Build/SolanaUnitySDK/guides/host-your-game)将游戏编译为 WebGL,并把构建结果托管到 Github Pages
按照[这份指南](/cn/pages/tools/solana-unity-sdk/guides/host-your-game)将游戏编译为 WebGL,并把构建结果托管到 Github Pages
</Card>

### (可选)使用 xNFT WebGL 模板
Expand Down
Loading