Skip to content

Commit 894ed07

Browse files
author
RECTOR
committed
docs: Add Seahorse integration documentation (#56)
1 parent 1fb86c4 commit 894ed07

File tree

1 file changed

+306
-0
lines changed

1 file changed

+306
-0
lines changed
Lines changed: 306 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,306 @@
1+
---
2+
title: Seahorse Integration
3+
description: Generate Seahorse-compatible Python code for Solana programs from LUMOS schemas
4+
---
5+
6+
LUMOS supports generating code for Seahorse, a framework that lets you write Solana programs in Python.
7+
8+
## What is Seahorse?
9+
10+
[Seahorse](https://seahorse-lang.org/) is a Python framework that compiles to Anchor-compatible Rust code, enabling Python developers to write Solana programs. LUMOS generates Seahorse-compatible Python classes from your schemas.
11+
12+
## When to Use Seahorse Mode
13+
14+
Use Seahorse generation when:
15+
16+
- Writing Solana programs in Python using Seahorse
17+
- Want Python-native type annotations
18+
- Building with the Seahorse framework ecosystem
19+
- Need shared schemas between Python and TypeScript/Rust
20+
21+
## Schema Syntax
22+
23+
Seahorse schemas use the same LUMOS syntax as Anchor:
24+
25+
```rust
26+
#[solana]
27+
#[account]
28+
struct PlayerAccount {
29+
wallet: PublicKey,
30+
level: u16,
31+
experience: u64,
32+
username: String,
33+
}
34+
35+
#[solana]
36+
enum GameState {
37+
Active,
38+
Paused,
39+
Ended,
40+
}
41+
```
42+
43+
## Generated Code
44+
45+
### Python Output
46+
47+
```python
48+
# Auto-generated by LUMOS for Seahorse
49+
# DO NOT EDIT - Changes will be overwritten
50+
51+
from seahorse.prelude import *
52+
53+
@account
54+
class PlayerAccount:
55+
wallet: Pubkey
56+
level: u16
57+
experience: u64
58+
username: str
59+
60+
61+
class GameState(IntEnum):
62+
Active = 0
63+
Paused = 1
64+
Ended = 2
65+
```
66+
67+
### Key Features
68+
69+
- Uses `from seahorse.prelude import *` for Seahorse types
70+
- `@account` decorator for account structs
71+
- Native Seahorse types (`u8`, `u16`, `u64`, `Pubkey`)
72+
- Python `IntEnum` for unit enums
73+
- No explicit Borsh schemas (Seahorse handles serialization)
74+
75+
---
76+
77+
## Usage
78+
79+
### Generate Seahorse Code
80+
81+
```bash
82+
# Generate Seahorse Python code
83+
lumos generate schema.lumos --lang seahorse
84+
85+
# Generate multiple languages including Seahorse
86+
lumos generate schema.lumos --lang rust,typescript,seahorse
87+
88+
# Dry-run to preview output
89+
lumos generate schema.lumos --lang seahorse --dry-run
90+
```
91+
92+
### Output Structure
93+
94+
```
95+
project/
96+
├── schema.lumos # Source schema
97+
├── generated.py # Generated Seahorse code
98+
└── programs/
99+
└── my_program/
100+
└── src/
101+
└── lib.rs # Use generated types
102+
```
103+
104+
### Using in Seahorse Program
105+
106+
```python
107+
# programs_py/my_program.py
108+
from seahorse.prelude import *
109+
from generated import PlayerAccount, GameState
110+
111+
declare_id('...')
112+
113+
@instruction
114+
def initialize_player(
115+
owner: Signer,
116+
player: Empty[PlayerAccount],
117+
):
118+
player = player.init(
119+
payer=owner,
120+
seeds=['player', owner],
121+
)
122+
player.wallet = owner.key()
123+
player.level = 1
124+
player.experience = 0
125+
player.username = ""
126+
```
127+
128+
---
129+
130+
## Type Mapping
131+
132+
| LUMOS Type | Seahorse Type | Notes |
133+
|------------|---------------|-------|
134+
| `u8`, `u16`, `u32`, `u64` | `u8`, `u16`, `u32`, `u64` | Native Seahorse types |
135+
| `i8`, `i16`, `i32`, `i64` | `i8`, `i16`, `i32`, `i64` | Signed integers |
136+
| `u128`, `i128` | `u128`, `i128` | Large integers |
137+
| `bool` | `bool` | Python boolean |
138+
| `String` | `str` | Python string |
139+
| `PublicKey` | `Pubkey` | Seahorse Pubkey type |
140+
| `[T]` | `List[T]` | Dynamic arrays |
141+
| `[T; N]` | `Array[T, N]` | Fixed-size arrays |
142+
| `Option<T>` | `T \| None` | Optional fields |
143+
144+
---
145+
146+
## Comparison: Seahorse vs Standard Python
147+
148+
| Feature | Seahorse | Standard Python |
149+
|---------|----------|-----------------|
150+
| Import | `seahorse.prelude` | `dataclasses`, `solders` |
151+
| Decorator | `@account` | `@dataclass` |
152+
| Types | `u16`, `u64`, `Pubkey` | `int`, `Pubkey` |
153+
| Serialization | Automatic | `borsh-construct` |
154+
| Use case | Solana programs | Client-side code |
155+
156+
### When to Choose Seahorse
157+
158+
- **Writing programs**: Building on-chain logic in Python
159+
- **Type safety**: Native Rust-like types in Python
160+
- **Seahorse ecosystem**: Using Seahorse's decorators and patterns
161+
162+
### When to Choose Standard Python
163+
164+
- **Client code**: Building Python backends for Solana dApps
165+
- **Data analysis**: Processing blockchain data
166+
- **Testing**: Writing test scripts for Solana programs
167+
168+
---
169+
170+
## Advanced Features
171+
172+
### Complex Enums
173+
174+
```rust
175+
#[solana]
176+
enum GameEvent {
177+
Started,
178+
PlayerJoined { player: PublicKey },
179+
ScoreUpdate { player: PublicKey, score: u64 },
180+
}
181+
```
182+
183+
Generates:
184+
185+
```python
186+
from seahorse.prelude import *
187+
from dataclasses import dataclass
188+
189+
# Variant types
190+
@dataclass
191+
class GameEventStarted:
192+
pass
193+
194+
@dataclass
195+
class GameEventPlayerJoined:
196+
field0: Pubkey
197+
198+
@dataclass
199+
class GameEventScoreUpdate:
200+
player: Pubkey
201+
score: u64
202+
203+
GameEvent = GameEventStarted | GameEventPlayerJoined | GameEventScoreUpdate
204+
```
205+
206+
### Optional Fields
207+
208+
```rust
209+
#[solana]
210+
#[account]
211+
struct Profile {
212+
wallet: PublicKey,
213+
nickname: Option<String>,
214+
}
215+
```
216+
217+
Generates:
218+
219+
```python
220+
@account
221+
class Profile:
222+
wallet: Pubkey
223+
nickname: str | None
224+
```
225+
226+
### Fixed Arrays
227+
228+
```rust
229+
#[solana]
230+
#[account]
231+
struct Vault {
232+
authorities: [PublicKey; 3],
233+
balances: [u64; 10],
234+
}
235+
```
236+
237+
Generates:
238+
239+
```python
240+
@account
241+
class Vault:
242+
authorities: Array[Pubkey, 3]
243+
balances: Array[u64, 10]
244+
```
245+
246+
---
247+
248+
## Dependencies
249+
250+
Add Seahorse to your project:
251+
252+
```bash
253+
# Install Seahorse CLI
254+
cargo install seahorse-dev
255+
256+
# Initialize Seahorse project
257+
seahorse init my-project
258+
```
259+
260+
Your `pyproject.toml` or project structure should include Seahorse:
261+
262+
```toml
263+
[tool.seahorse]
264+
program_name = "my_program"
265+
```
266+
267+
---
268+
269+
## Best Practices
270+
271+
### 1. Keep Schemas in Sync
272+
273+
Generate code whenever schemas change:
274+
275+
```bash
276+
lumos generate schema.lumos --lang seahorse,typescript
277+
```
278+
279+
### 2. Use Version Control
280+
281+
Add generated files to `.gitignore` or commit them based on your workflow:
282+
283+
```gitignore
284+
# Option 1: Ignore generated files
285+
generated.py
286+
287+
# Option 2: Commit generated files for visibility
288+
# (No ignore needed)
289+
```
290+
291+
### 3. Validate Before Compiling
292+
293+
```bash
294+
lumos validate schema.lumos
295+
lumos generate schema.lumos --lang seahorse
296+
seahorse build
297+
```
298+
299+
---
300+
301+
## See Also
302+
303+
- [Standard Python Generator](/api/generators#python) - For client-side Python code
304+
- [Anchor Integration](/frameworks/anchor) - For Anchor Rust programs
305+
- [Type System](/api/types) - Complete type reference
306+
- [CLI Reference](/cli/generate) - Full CLI documentation

0 commit comments

Comments
 (0)