setup-stylus-contracts
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseStylus 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-unknownInstall the Cargo Stylus CLI:
bash
cargo install --force cargo-stylusCreate a new Stylus project:
bash
cargo stylus new my_projectA Rust nightly toolchain is required. The project should include aspecifying the nightly channel,rust-toolchain.tomlcomponent, andrust-srctarget. Check the rust-contracts-stylus repo for the current recommended nightly date.wasm32-unknown-unknown
安装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工具链。项目应包含一个文件,指定nightly频道、rust-toolchain.toml组件以及rust-src目标环境。请查看rust-contracts-stylus仓库获取当前推荐的nightly版本日期。wasm32-unknown-unknown
Adding OpenZeppelin Dependencies
添加OpenZeppelin依赖
Look up the current version from crates.io/crates/openzeppelin-stylus before adding. Add to :
Cargo.tomltoml
[dependencies]
openzeppelin-stylus = "=<VERSION>"Enable the feature flag for ABI generation:
export-abitoml
[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.tomltoml
[dependencies]
openzeppelin-stylus = "=<VERSION>"启用特性标志以生成ABI:
export-abitoml
[features]
export-abi = ["openzeppelin-stylus/export-abi"]该crate必须同时编译为库文件和cdylib:
toml
[lib]
crate-type = ["lib", "cdylib"]Import Conventions
导入约定
Imports use (underscores) as the crate root:
openzeppelin_stylusrust
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 and on the main struct, embedding OpenZeppelin components as fields:
#[storage]#[entrypoint]rust
#[entrypoint]
#[storage]
struct MyToken {
erc20: Erc20,
ownable: Ownable,
}Public methods are exposed with and . The canonical pattern uses an empty impl block for dispatch registration, plus separate trait impl blocks:
#[public]#[implements(...)]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: , , , , .
accessfinanceproxytokenutils导入时使用(下划线形式)作为crate根路径:
openzeppelin_stylusrust
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;合约的主结构体需使用和属性,并将OpenZeppelin组件作为字段嵌入:
#[storage]#[entrypoint]rust
#[entrypoint]
#[storage]
struct MyToken {
erc20: Erc20,
ownable: Ownable,
}公共方法通过和属性暴露。标准写法是使用一个空的impl块进行调度注册,再加上单独的trait实现块:
#[public]#[implements(...)]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 ...
}顶级模块包括:、、、、。
accessfinanceproxytokenutilsBuild & Deploy Basics
构建与部署基础
Validate the contract compiles to valid Stylus WASM:
bash
cargo stylus checkExport the Solidity ABI:
bash
cargo stylus export-abiDeploy 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>"