SuperAPI AI 工具集

setup-stellar-contracts

编程与开发

查看项目

更新于 10-16 03:11

技能介绍

用途:帮助搭建 Stellar/Soroban 智能合约项目,并使用 OpenZeppelin Contracts for Stellar 作为依赖基础。

环境准备:安装 Rust 工具链(v1.84.0+)与 Soroban WASM 目标:curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh,然后 rustup target add wasm32v1-none。安装 Stellar CLI:curl -fsSL https://github.com/stellar/stellar-cli/raw/main/install.sh | sh。创建项目:stellar contract init my_project,它会生成包含 contracts/*/ 的 Cargo 工作区。

依赖接入:先到 stellar-contracts 仓库确认当前版本,再用 = 精确锁定版本(库处于活跃开发中)。在根 Cargo.toml 的 [workspace.dependencies] 中声明 stellar-tokens、stellar-access、stellar-contract-utils、stellar-macros 等,并在各合约的 Cargo.toml 中以 workspace = true 引用。可用 crate 还包括 stellar-accounts、stellar-fee-abstraction、stellar-governance;只添加实际使用的 crate,stellar-macros 提供 #[when_not_paused]、#[only_owner]、#[derive(Upgradeable)] 等属性,多数合约需要。

代码模式:导入使用下划线作为 crate 根;合约结构体加 #[contract],impl 块加 #[contractimpl](来自 soroban_sdk);每个 trait 单独 impl 块,守护宏装饰具体函数。

平台注意事项:Stellar 读操作免费,应优化写入;实例存储的 TTL 延长由开发者自己负责,库只处理其他存储条目。

构建与测试:stellar contract build(等价于 cargo build --target wasm32v1-none --release,输出在 target/wasm32v1-none/release/);cargo test 运行测试,soroban-sdk testutils 对 crate 内单元测试自动启用。

限制:资料未提供收费、平台支持范围或完整 API 说明,具体版本号与更多用法需查阅相关仓库。