setup-stylus-contracts

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Stylus Setup

Stylus 项目搭建

Rust & Cargo Stylus Setup

Rust 与 Cargo Stylus 环境搭建

Install the Rust toolchain and WASM target:
bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup target add wasm32-unknown-unknown
Install the Cargo Stylus CLI:
bash
cargo install --force cargo-stylus
Create a new Stylus project:
bash
cargo stylus new my_project
A Rust nightly toolchain is required. The project should include a
rust-toolchain.toml
specifying the nightly channel,
rust-src
component, and
wasm32-unknown-unknown
target. Check the rust-contracts-stylus repo for the current recommended nightly date.
安装Rust工具链和WASM目标环境:
bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup target add wasm32-unknown-unknown
安装Cargo Stylus命令行工具:
bash
cargo install --force cargo-stylus
创建新的Stylus项目:
bash
cargo stylus new my_project
需要使用Rust nightly工具链。项目应包含一个
rust-toolchain.toml
文件,指定nightly频道、
rust-src
组件以及
wasm32-unknown-unknown
目标环境。请查看rust-contracts-stylus仓库获取当前推荐的nightly版本日期。

Adding OpenZeppelin Dependencies

添加OpenZeppelin依赖

Look up the current version from crates.io/crates/openzeppelin-stylus before adding. Add to
Cargo.toml
:
toml
[dependencies]
openzeppelin-stylus = "=<VERSION>"
Enable the
export-abi
feature flag for ABI generation:
toml
[features]
export-abi = ["openzeppelin-stylus/export-abi"]
The crate must be compiled as both a library and a cdylib:
toml
[lib]
crate-type = ["lib", "cdylib"]
添加前请从crates.io/crates/openzeppelin-stylus查询最新版本。将以下内容添加到
Cargo.toml
中:
toml
[dependencies]
openzeppelin-stylus = "=<VERSION>"
启用
export-abi
特性标志以生成ABI:
toml
[features]
export-abi = ["openzeppelin-stylus/export-abi"]
该crate必须同时编译为库文件和cdylib:
toml
[lib]
crate-type = ["lib", "cdylib"]

Import Conventions

导入约定

Imports use
openzeppelin_stylus
(underscores) as the crate root:
rust
use openzeppelin_stylus::token::erc20::{Erc20, IErc20};
use openzeppelin_stylus::access::ownable::{Ownable, IOwnable};
use openzeppelin_stylus::utils::pausable::{Pausable, IPausable};
use openzeppelin_stylus::utils::introspection::erc165::IErc165;
Contracts use
#[storage]
and
#[entrypoint]
on the main struct, embedding OpenZeppelin components as fields:
rust
#[entrypoint]
#[storage]
struct MyToken {
    erc20: Erc20,
    ownable: Ownable,
}
Public methods are exposed with
#[public]
and
#[implements(...)]
. The canonical pattern uses an empty impl block for dispatch registration, plus separate trait impl blocks:
rust
#[public]
#[implements(IErc20<Error = erc20::Error>, IOwnable<Error = ownable::Error>)]
impl MyToken {}

#[public]
impl IErc20 for MyToken {
    type Error = erc20::Error;
    // delegate to self.erc20 ...
}
Top-level modules:
access
,
finance
,
proxy
,
token
,
utils
.
导入时使用
openzeppelin_stylus
(下划线形式)作为crate根路径:
rust
use openzeppelin_stylus::token::erc20::{Erc20, IErc20};
use openzeppelin_stylus::access::ownable::{Ownable, IOwnable};
use openzeppelin_stylus::utils::pausable::{Pausable, IPausable};
use openzeppelin_stylus::utils::introspection::erc165::IErc165;
合约的主结构体需使用
#[storage]
#[entrypoint]
属性,并将OpenZeppelin组件作为字段嵌入:
rust
#[entrypoint]
#[storage]
struct MyToken {
    erc20: Erc20,
    ownable: Ownable,
}
公共方法通过
#[public]
#[implements(...)]
属性暴露。标准写法是使用一个空的impl块进行调度注册,再加上单独的trait实现块:
rust
#[public]
#[implements(IErc20<Error = erc20::Error>, IOwnable<Error = ownable::Error>)]
impl MyToken {}

#[public]
impl IErc20 for MyToken {
    type Error = erc20::Error;
    // delegate to self.erc20 ...
}
顶级模块包括:
access
finance
proxy
token
utils

Build & Deploy Basics

构建与部署基础

Validate the contract compiles to valid Stylus WASM:
bash
cargo stylus check
Export the Solidity ABI:
bash
cargo stylus export-abi
Deploy to an Arbitrum Stylus endpoint:
bash
cargo stylus deploy --endpoint="<RPC_URL>" --private-key-path="<KEY_FILE>"
验证合约是否可编译为有效的Stylus WASM:
bash
cargo stylus check
导出Solidity ABI:
bash
cargo stylus export-abi
部署到Arbitrum Stylus节点:
bash
cargo stylus deploy --endpoint="<RPC_URL>" --private-key-path="<KEY_FILE>"