在 Arbitrum 上使用 Stylus 智能合约项目

本文提供了在Arbitrum上使用OpenZeppelin Contracts for Stylus设置智能合约项目的指南。内容涵盖了Rust工具链和WASM目标的安装、Cargo Stylus CLI的配置、OpenZeppelin依赖的添加、导入规范以及合约的构建和部署基础步骤。

Stylus 设置

Rust 和 Cargo Stylus 设置

安装 Rust 工具链和 WASM 目标:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup target add wasm32-unknown-unknown

安装 Cargo Stylus CLI:

cargo install --force cargo-stylus

创建一个新的 Stylus 项目:

cargo stylus new my_project

需要 Rust nightly toolchain。项目应包含一个指定 nightly channel、rust-src 组件和 wasm32-unknown-unknown target 的 rust-toolchain.toml 文件。请查阅 rust-contracts-stylus repo 获取当前推荐的 nightly 日期。

添加 OpenZeppelin 依赖项

添加前请从 crates.io/crates/openzeppelin-stylus 查找当前版本。添加到 Cargo.toml

[dependencies]
openzeppelin-stylus = "=<VERSION>"

为 ABI 生成启用 export-abi feature flag:

[features]
export-abi = ["openzeppelin-stylus/export-abi"]

crate 必须同时编译为 library 和 cdylib:

[lib]
crate-type = ["lib", "cdylib"]

导入约定

导入使用 openzeppelin_stylus (下划线) 作为 crate root:

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;

合约在主 struct 上使用 #[storage]#[entrypoint],将 OpenZeppelin 组件嵌入为字段:

##[entrypoint]
##[storage]
struct MyToken {
    erc20: Erc20,
    ownable: Ownable,
}

公共方法通过 #[public]#[implements(...)] 暴露。规范模式使用一个空的 impl block 进行 dispatch registration,外加单独的 trait impl blocks:

##[public]
##[implements(IErc20<Error = erc20::Error>, IOwnable<Error = ownable::Error>)]
impl MyToken {}

##[public]
impl IErc20 for MyToken {
    type Error = erc20::Error;
    // 委托给 self.erc20 ...
}

顶级模块:accessfinanceproxytokenutils

构建和部署基础知识

验证合约是否编译为有效的 Stylus WASM:

cargo stylus check

导出 Solidity ABI:

cargo stylus export-abi

部署到 Arbitrum Stylus endpoint:

cargo stylus deploy --endpoint="<RPC_URL>" --private-key-path="<KEY_FILE>"
  • 原文链接: github.com/OpenZeppelin/...
  • 登链社区 AI 助手,为大家转译优秀英文文章,如有翻译不通的地方,还请包涵~
点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
OpenZeppelin
OpenZeppelin
江湖只有他的大名,没有他的介绍。