技能标识:setup-stylus-contracts
Set up a Stylus smart contract project with OpenZeppelin Contracts for Stylus on Arbitrum. Use when users need to: (1) install Rust toolchain and WASM target for Stylus, (2) create a new Cargo Stylus project, (3) add OpenZeppelin Stylus dependencies to Cargo.toml, or (4) understand Stylus import conventions and storage patterns for OpenZeppelin.
安装 Rust 工具链和 WASM 目标:
bash
curl --proto =https --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup target add wasm32-unknown-unknown
安装 Cargo Stylus CLI:
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 日期。
在添加前,请从 crates.io/crates/openzeppelin-stylus 查询当前版本。添加到 Cargo.toml:
toml
[dependencies]
openzeppelin-stylus = =<版本号>
启用 export-abi 特性标志以生成 ABI:
toml
[features]
export-abi = [openzeppelin-stylus/export-abi]
该 crate 必须同时编译为库和 cdylib:
toml
[lib]
crate-type = [lib, cdylib]
导入使用 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 impl 块:
rust
#[public]
#[implements(IErc20
impl MyToken {}
#[public]
impl IErc20 for MyToken {
type Error = erc20::Error;
// 委托给 self.erc20 ...
}
顶层模块:access、finance、proxy、token、utils。
验证合约编译为有效的 Stylus WASM:
bash
cargo stylus check
导出 Solidity ABI:
bash
cargo stylus export-abi
部署到 Arbitrum Stylus 端点:
bash
cargo stylus deploy --endpoint=
私钥安全: 切勿在命令行中使用 --private-key 传入原始密钥——它会在 shell 历史记录和进程列表中可见。始终使用 --private-key-path 配合具有严格权限(chmod 600)的文件,或使用硬件钱包/密钥库。
以下为平台配置的接入选项,并非逐项实测通过。能否安装取决于客户端支持、技能来源和运行环境:
帮我安装 SkillHub 和 setup-stylus-contracts-1776078136 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 setup-stylus-contracts-1776078136 技能
skillhub install setup-stylus-contracts-1776078136
文件大小: 2.12 KB | 发布时间: 2026-4-15 14:20