setup-stellar-contracts
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseStellar Setup
Stellar 项目搭建
Soroban/Stellar Development Setup
Soroban/Stellar 开发环境配置
Install the Rust toolchain (v1.84.0+) and the Soroban WASM target:
bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup target add wasm32v1-noneInstall the Stellar CLI:
bash
curl -fsSL https://github.com/stellar/stellar-cli/raw/main/install.sh | shCreate a new Soroban project:
bash
stellar contract init my_projectThis creates a Cargo workspace with contracts in .
contracts/*/安装Rust工具链(v1.84.0及以上版本)以及Soroban WASM目标环境:
bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup target add wasm32v1-none安装Stellar CLI:
bash
curl -fsSL https://github.com/stellar/stellar-cli/raw/main/install.sh | sh创建新的Soroban项目:
bash
stellar contract init my_project这会创建一个Cargo工作区,合约文件位于目录下。
contracts/*/OpenZeppelin Dependencies
OpenZeppelin 依赖配置
Look up the current version from the stellar-contracts repo before adding. Pin exact versions with as the library is under active development.
=Add OpenZeppelin crates to the root under :
Cargo.toml[workspace.dependencies]toml
[workspace.dependencies]
stellar-tokens = "=<VERSION>"
stellar-access = "=<VERSION>"
stellar-contract-utils = "=<VERSION>"
stellar-macros = "=<VERSION>"Then reference them in the per-contract :
contracts/*/Cargo.tomltoml
[dependencies]
soroban-sdk = { workspace = true }
stellar-tokens = { workspace = true }
stellar-access = { workspace = true }
stellar-contract-utils = { workspace = true }
stellar-macros = { workspace = true }Available crates: , , , , , , .
stellar-accessstellar-accountsstellar-contract-utilsstellar-fee-abstractionstellar-governancestellar-macrosstellar-tokensOnly add the crates the contract actually uses.provides proc-macro attributes (for example,stellar-macros,#[when_not_paused],#[only_owner]) and is needed in most contracts.#[derive(Upgradeable)]
添加依赖前,请从stellar-contracts仓库查询当前版本。由于该库正处于活跃开发阶段,请使用固定精确版本。
=将OpenZeppelin包添加到根目录的下:
Cargo.toml[workspace.dependencies]toml
[workspace.dependencies]
stellar-tokens = "=<VERSION>"
stellar-access = "=<VERSION>"
stellar-contract-utils = "=<VERSION>"
stellar-macros = "=<VERSION>"然后在每个合约的中引用这些依赖:
contracts/*/Cargo.tomltoml
[dependencies]
soroban-sdk = { workspace = true }
stellar-tokens = { workspace = true }
stellar-access = { workspace = true }
stellar-contract-utils = { workspace = true }
stellar-macros = { workspace = true }可用包包括:、、、、、、。
stellar-accessstellar-accountsstellar-contract-utilsstellar-fee-abstractionstellar-governancestellar-macrosstellar-tokens仅添加合约实际需要的包。提供过程宏属性(例如stellar-macros、#[when_not_paused]、#[only_owner]),大多数合约都需要该包。#[derive(Upgradeable)]
Code Patterns
代码模式
Imports use underscores as the crate root (Rust convention):
rust
use stellar_tokens::fungible::{Base, FungibleToken};
use stellar_tokens::fungible::burnable::FungibleBurnable;
use stellar_access::ownable::Ownable;
use stellar_contract_utils::pausable::Pausable;
use stellar_macros::when_not_paused;Contracts use on the struct and on the impl block (from ):
#[contract]#[contractimpl]soroban_sdkrust
use soroban_sdk::{contract, contractimpl, Env};
#[contract]
pub struct MyToken;
#[contractimpl]
impl MyToken {
// Implement trait methods here
}Trait implementations are separate blocks per trait (e.g., , ). Guard macros like and decorate individual functions.
implFungibleTokenPausable#[when_not_paused]#[only_owner]导入时以下划线作为包根(Rust约定):
rust
use stellar_tokens::fungible::{Base, FungibleToken};
use stellar_tokens::fungible::burnable::FungibleBurnable;
use stellar_access::ownable::Ownable;
use stellar_contract_utils::pausable::Pausable;
use stellar_macros::when_not_paused;合约需在结构体上使用宏,在实现块上使用宏(来自):
#[contract]#[contractimpl]soroban_sdkrust
use soroban_sdk::{contract, contractimpl, Env};
#[contract]
pub struct MyToken;
#[contractimpl]
impl MyToken {
// Implement trait methods here
}Trait实现需为每个trait单独编写块(例如、)。和等守卫宏用于修饰单个函数。
implFungibleTokenPausable#[when_not_paused]#[only_owner]Platform Notes
平台注意事项
- Read operations are free in Stellar. Optimize for minimizing writes; reads and computation are cheap. Prefer clean, readable code over micro-optimizations.
- Instance storage TTL extension is the developer's responsibility. The OpenZeppelin library handles TTL extension for other storage entries, but contracts must extend their own storage entries to prevent expiration.
instance
- Stellar中的读取操作是免费的。应尽量减少写入操作;读取和计算的成本很低。优先选择清晰、易读的代码,而非微优化。
- 实例存储的TTL续期由开发者负责。OpenZeppelin库会处理其他存储条目的TTL续期,但合约必须自行对存储条目进行续期,以防止过期。
instance
Build & Test
构建与测试
Build the contract to WASM:
bash
stellar contract buildThis is a shortcut for . Output appears in .
cargo build --target wasm32v1-none --releasetarget/wasm32v1-none/release/Run tests:
bash
cargo testtestutils are automatically enabled for in-crate unit tests.soroban-sdk
将合约构建为WASM文件:
bash
stellar contract build这是的快捷命令。构建产物将输出到目录下。
cargo build --target wasm32v1-none --releasetarget/wasm32v1-none/release/运行测试:
bash
cargo test对于 crate 内的单元测试,的测试工具会自动启用。soroban-sdk