5分钟了解 LUMOS:你的第一个 Solana Schema
发布: (2025年12月18日 GMT+8 07:12)
3 min read
原文: Dev.to
Source: Dev.to

想要在 Solana 应用中获得类型安全的 Rust + TypeScript 吗?让我们在 恰好 5 分钟 内帮你从零开始拥有可用的 schema。
第 1 分钟:安装
cargo install lumos-cli
验证:
lumos --version
# Output: lumos-cli 0.1.0
没有 Rust? 使用 npm 代替:
npx @getlumos/cli --version
第 2 分钟:初始化项目
lumos init my-game
cd my-game
这会创建:
my-game/
├── schema.lumos ← 你的 schema 定义
├── lumos.toml ← 配置文件
└── README.md ← 入门指南
第 3 分钟:编写你的模式
打开 schema.lumos 并添加:
#[solana]
#[account]
struct PlayerAccount {
wallet: PublicKey,
level: u16,
experience: u64,
equipped_items: [PublicKey],
}
#[solana]
struct MatchResult {
player: PublicKey,
opponent: Option,
score: u64,
timestamp: i64,
}
发生了什么
#[solana]– Solana 特定类型#[account]– Anchor 账户(使用 Anchor 宏)PublicKey– 在 Rust 中映射为Pubkey,在 TS 中为PublicKey[PublicKey]– 向量/数组Option– 可选值
第 4 分钟:生成代码
lumos generate schema.lumos
输出
Generating Rust code
Wrote ./generated.rs
Generating TypeScript code
Wrote ./generated.ts
Finished generated 2 type definitions
Rust 输出 (generated.rs)
use anchor_lang::prelude::*;
#[account]
pub struct PlayerAccount {
pub wallet: Pubkey,
pub level: u16,
pub experience: u64,
pub equipped_items: Vec,
}
TypeScript 输出 (generated.ts)
import { PublicKey } from '@solana/web3.js';
import * as borsh from '@coral-xyz/borsh';
export interface PlayerAccount {
wallet: PublicKey;
level: number;
experience: number;
equipped_items: PublicKey[];
}
export const PlayerAccountBorshSchema = borsh.struct([
borsh.publicKey('wallet'),
borsh.u16('level'),
borsh.u64('experience'),
borsh.vec(borsh.publicKey(), 'equipped_items'),
]);
第 5 分钟:在项目中使用
Anchor 程序
mod generated;
use generated::*;
#[program]
pub mod my_game {
use super::*;
pub fn create_player(ctx: Context) -> Result {
let player = &mut ctx.accounts.player;
player.wallet = *ctx.accounts.user.key;
player.level = 1;
player.experience = 0;
Ok(())
}
}
TypeScript 客户端
import { PlayerAccount, PlayerAccountBorslSchema } from './generated';
import { PublicKey, Connection } from '@solana/web3.js';
async function getPlayer(pubkey: PublicKey, connection: Connection): Promise {
const accountInfo = await connection.getAccountInfo(pubkey);
if (!accountInfo) throw new Error('Account not found');
return PlayerAccountBorshSchema.deserialize(accountInfo.data);
}
完成!
在 5 分钟内,你:
- ✅ 安装了 LUMOS CLI
- ✅ 创建了第一个
.lumosschema - ✅ 生成了类型安全的 Rust + TypeScript
- ✅ 学会了完整的工作流
你得到的
- 在多语言之间共享的类型安全 schema
- 自动的 Borsh 序列化
- 零手动样板代码
- 可直接用于 Anchor 的代码