#solana #python #write #anchor #programs #import #seahorse

bin+lib seahorse-dev

使用 Python 编写兼容 Anchor 的 Solana 程序

3 个不稳定版本

0.2.0 2024 年 1 月 6 日
0.1.1 2023 年 10 月 23 日
0.1.0 2023 年 10 月 18 日

#38 in #anchor

Download history 99/week @ 2024-03-11 20/week @ 2024-03-18 15/week @ 2024-03-25 71/week @ 2024-04-01 17/week @ 2024-04-08 8/week @ 2024-04-15 13/week @ 2024-04-22 14/week @ 2024-04-29 21/week @ 2024-05-06 26/week @ 2024-05-13 15/week @ 2024-05-20 49/week @ 2024-05-27 44/week @ 2024-06-03 23/week @ 2024-06-10 22/week @ 2024-06-17 8/week @ 2024-06-24

每月 105 次下载

MIT 许可证

525KB
11K SLoC

seahorse:使用 Python 编写 Solana 程序

[!IMPORTANT] 这是对 seahorse-lang 的分支。原始存储库已被弃用,并因最新的 anchor 更新而损坏,不再由其创建者维护。如果您在项目中使用 seahorse-lang,我们强烈建议迁移到 seahorse-dev

Python 的易用性与 Rust 的安全性。

Seahorse 允许您使用 Python 编写 Solana 程序。它是一个基于 Anchor 的社区主导项目。

开发者可以享受 Python 的易用性,同时仍享有 Solana 链上每个 Rust 程序的安全保证。默认处理低级内存问题,让您只需关注重要的事情。

功能

  • 编译时类型安全
  • 完全与 Rust 代码兼容
  • 与 Anchor 兼容

Seahorse 编译器生成中间 Rust 艺术品,并使用 Anchor 进行一些繁重的工作。

Seahorse 是测试软件。许多功能尚未实现,并且尚未准备好投入生产。

入门

安装

示例

示例:FizzBuzz

这是一个非常简单的程序,其功能类似于经典的 FizzBuzz 问题。

# fizzbuzz
# Built with Seahorse v0.1.0
#
# On-chain, persistent FizzBuzz!

from seahorse.prelude import *

declare_id('Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS')

class FizzBuzz(Account):
  fizz: bool
  buzz: bool
  n: u64

@instruction
def init(owner: Signer, fizzbuzz: Empty[FizzBuzz]):
  fizzbuzz.init(payer = owner, seeds = ['fizzbuzz', owner])

@instruction
def do_fizzbuzz(fizzbuzz: FizzBuzz, n: u64):
  fizzbuzz.fizz = n % 3 == 0
  fizzbuzz.buzz = n % 5 == 0
  if not fizzbuzz.fizz and not fizzbuzz.buzz:
    fizzbuzz.n = n
  else:
    fizzbuzz.n = 0

这展示了某些基本的 Seahorse 功能,如账户初始化和创建指令。更多内容请查看 计算器:您的第一个 Seahorse 程序 或其他示例 这里

编译器架构在 v0.2.0 中完全改变,这里有一个简要概述 - 更多详情 这里

SEAHORSE CORE: THE COMPILER (v0.2.0)
┌───────────────────────────────────────────┐
│                                           │
│ ┌───────────────────────────────────────┐ │
│ │ PARSE                                 │ │
│ │                                       │ │
│ │ Turn Seahorse source code into Python │ │
│ │ AST. Handled by rustpython.           │ │
│ └───────────────────┬───────────────────┘ │
│                     │                     │
│                    AST                    │
│                     │                     │
│ ┌───────────────────▼───────────────────┐ │
│ │ CLEAN                                 │ │
│ │                                       │ │
│ │ Remove unsupported parts from the AST │ │
│ │ (like yields statements). Does some   │ │
│ │ minor changes to make compilation     │ │
│ │ easier.                               │ │
│ └───────────────────┬───────────────────┘ │
│                     │                     │
│                    AST                    │
│                     │                     │
│ ┌───────────────────▼───────────────────┐ │
│ │ PREPROCESS                            │ │
│ │                                       │ │
│ │ Find the source files for every       │ │
│ │ import - recursively calls the first  │ │
│ │ two steps as well.                    │ │
│ │                                       │ │
│ │ Outputs a "module registry" which has │ │
│ │ every parsed+cleaned source file.     │ │
│ └───────────────────┬───────────────────┘ │
│                     │                     │
│                  registry                 │
│                     │                     │
│ ┌───────────────────▼───────────────────┐ │
│ │ COMPILE                               │ │
│ │ ┌───────────────────────────────────┐ │ │
│ │ │ NAMESPACE                         │ │ │
│ │ │                                   │ │ │
│ │ │ Resolve the location of every     │ │ │
│ │ │ import/export in each module.     │ │ │
│ │ └─────────────────┬─────────────────┘ │ │
│ │                   │                   │ │
│ │         registry & namespaces         │ │
│ │                   │                   │ │
│ │ ┌─────────────────▼─────────────────┐ │ │
│ │ │ SIGN                              │ │ │
│ │ │                                   │ │ │
│ │ │ Find types of everything outside  │ │ │
│ │ │ function bodies - class fields,   │ │ │
│ │ │ function params/return type.      │ │ │
│ │ └─────────────────┬─────────────────┘ │ │
│ │                   │                   │ │
│ │         registry & signatures         │ │
│ │                   │                   │ │
│ │ ┌─────────────────▼─────────────────┐ │ │
│ │ │ CHECK                             │ │ │
│ │ │                                   │ │ │
│ │ │ Type check function bodies. Also  │ │ │
│ │ │ outputs the type of each expres-  │ │ │
│ │ │ sion, used for doing syntactic    │ │ │
│ │ │ transformations later.            │ │ │
│ │ └─────────────────┬─────────────────┘ │ │
│ │                   │                   │ │
│ │         registry & expr. types        │ │
│ │                   │                   │ │
│ │ ┌─────────────────▼─────────────────┐ │ │
│ │ │ BUILD                             │ │ │
│ │ │                                   │ │ │
│ │ │ Turn the original Python AST into │ │ │
│ │ │ a Rust-like AST, assisted by the  │ │ │
│ │ │ type information from CHECK.      │ │ │
│ │ │                                   │ │ │
│ │ │ The new AST includes special      │ │ │
│ │ │ constructs for things native to   │ │ │
│ │ │ Anchor, like ix contexts.         │ │ │
│ │ └───────────────────────────────────┘ │ │
│ │                                       │ │
│ └───────────────────┬───────────────────┘ │
│                     │                     │
│                    AST                    │
│                     │                     │
│ ┌───────────────────▼───────────────────┐ │
│ │ GENERATE                              │ │
│ │                                       │ │
│ │ Finally, turn the Rust-like AST into  │ │
│ │ Rust source code. Generates code for  │ │
│ │ each source file individually, as     │ │
│ │ well as a lib.rs that contains the    │ │
│ │ "real" instruction entrypoints.       │ │
│ └───────────────────────────────────────┘ │
│                                           │
└───────────────────────────────────────────┘

依赖关系

~10–21MB
~325K SLoC