重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
upgrade-stylus-contracts by openzeppelin/openzeppelin-skills
npx skills add https://github.com/openzeppelin/openzeppelin-skills --skill upgrade-stylus-contractsStylus 合约作为 WebAssembly (WASM) 程序与 EVM 一同在 Arbitrum 上运行。它们与 Solidity 合约共享相同的状态树、存储模型和账户系统。因此,EVM 代理模式对 Stylus 完全适用——Solidity 代理可以委托给 Stylus 实现,反之亦然。
| Stylus | Solidity
---|---|---
代理机制 | 相同——通过 delegatecall 调用实现合约 | 通过 delegatecall 调用实现合约
存储布局 | #[storage] 字段映射到与等效 Solidity 结构体相同的 EVM 存储槽 | 根据 Solidity 规则顺序分配存储槽
EIP 标准 | ERC-1967 存储槽,ERC-1822 可代理 UUID | 相同
上下文检测 | 在唯一存储槽中使用 logic_flag 布尔值(不支持 immutable) | 将 address(this) 存储为
| 两步:构造函数设置 ,然后通过代理调用 | 构造函数 + 通过代理调用初始化器
| WASM 合约必须每 365 天或在 Stylus 协议升级后重新激活 | 不适用
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
immutablelogic_flagset_version()现有的 Solidity 合约可以通过代理模式升级到 Stylus (Rust) 实现。#[storage] 宏在 EVM 状态树中布局字段的方式与 Solidity 完全相同,因此当类型定义匹配时,存储槽是对齐的。
OpenZeppelin Contracts for Stylus 提供了三种代理模式:
| 模式 | 关键类型 | 最适合 |
|---|---|---|
| UUPS | UUPSUpgradeable, IErc1822Proxiable, Erc1967Proxy | 大多数项目——升级逻辑在实现合约中,代理更轻量 |
| Beacon | BeaconProxy, UpgradeableBeacon | 多个代理共享一个实现——更新信标可原子性地升级所有代理 |
| 基础代理 | Erc1967Proxy, Erc1967Utils | 用于自定义代理模式的底层构建块 |
实现合约在其 #[storage] 结构体中组合 UUPSUpgradeable 以及访问控制(例如 Ownable)。集成需要:
#[storage] 结构体中添加 UUPSUpgradeable(和访问控制)作为字段self.uups.constructor() 并初始化访问控制self.uups.set_version() 的 initialize 函数——在部署后通过代理调用IUUPSUpgradeable——由访问控制保护的 upgrade_to_and_call,以及委托给 self.uups 的 upgrade_interface_versionIErc1822Proxiable——委托给 self.uups 的 proxiable_uuid代理合约是一个轻量的 Erc1967Proxy,其构造函数接收实现地址和初始化数据,并有一个委托所有调用的 #[fallback] 处理程序。
使用 set_version 作为初始化调用数据来部署代理。使用 cargo stylus deploy 或部署者合约。初始化数据是 ABI 编码的 setVersion 调用:
let data = MyContractAbi::setVersionCall {}.abi_encode();
// 在部署时,将 `data` 作为代理构造函数的第二个参数传递。
多个 BeaconProxy 合约指向一个存储当前实现地址的 UpgradeableBeacon。更新信标可以在一个交易中升级所有代理。
Stylus 不支持 immutable 关键字。UUPSUpgradeable 使用唯一存储槽中的 logic_flag 布尔值,而不是存储 __self = address(this):
logic_flag = true。delegatecall)运行时,代理的存储不包含此标志,因此读取为 false。only_proxy() 检查此标志,以确保升级函数只能通过代理调用,而不能直接在实现合约上调用。only_proxy() 还会验证 ERC-1967 实现槽非零,并且代理存储的版本与实现合约的 VERSION_NUMBER 匹配。
示例: 请参阅 rust-contracts-stylus 仓库 的
examples/目录,查看 UUPS、Beacon 及相关模式的完整工作集成示例。
升级函数必须受到访问控制的保护。OpenZeppelin 的 Stylus 合约没有将访问控制嵌入到升级逻辑本身——您必须在 upgrade_to_and_call 中添加它:
fn upgrade_to_and_call(&mut self, new_implementation: Address, data: Bytes) -> Result<(), Vec<u8>> {
self.ownable.only_owner()?; // 或任何访问控制检查
self.uups.upgrade_to_and_call(new_implementation, data)?;
Ok(())
}
常见选项:
Stylus 的 #[storage] 字段在 EVM 状态树中的布局方式与 Solidity 完全相同。升级时适用相同的存储布局规则:
与 Solidity 的一个区别是:Stylus #[storage] 中的嵌套结构体(例如,将 Erc20、Ownable、UUPSUpgradeable 组合为字段)的布局方式是每个嵌套结构体从其自身确定的槽开始。这与 Solidity 中常规的结构体嵌套一致,但与 Solidity 基于继承的扁平布局(所有继承的变量共享一个连续的槽范围)不同。
logic_flag 和任何仅属于实现的状态。它在实现合约部署时运行一次。set_version() 必须通过代理调用(在部署期间或通过 upgrade_to_and_call),以将 VERSION_NUMBER 写入代理的存储。set_version()。UUPS 实现强制执行三项安全检查:
upgrade_to_and_call 的调用(例如 self.ownable.only_owner())delegatecall 进行的,only_proxy() 会回滚proxiable_uuid() 必须返回 ERC-1967 实现槽,以确认 UUPS 兼容性Stylus WASM 合约必须每年(365 天)或在任何 Stylus 协议升级后重新激活一次。可以使用 cargo-stylus 或 ArbWasm 预编译合约进行重新激活。如果合约未重新激活,它将变得不可调用。这与代理升级是正交的,但必须纳入维护计划中。
在升级生产环境合约之前:
upgrade_to_and_call 升级到 V2,并验证所有现有状态读取正确upgrade_to_and_callVERSION_NUMBER 已递增每周安装次数
67
仓库
GitHub 星标数
159
首次出现
2026年3月5日
安全审计
已安装于
opencode65
github-copilot64
codex64
cline64
kimi-cli64
gemini-cli64
Stylus contracts run on Arbitrum as WebAssembly (WASM) programs alongside the EVM. They share the same state trie, storage model, and account system as Solidity contracts. Because of this, EVM proxy patterns work identically for Stylus — a Solidity proxy can delegate to a Stylus implementation and vice versa.
| Stylus | Solidity
---|---|---
Proxy mechanism | Same — delegatecall to implementation contract | delegatecall to implementation contract
Storage layout | #[storage] fields map to the same EVM slots as equivalent Solidity structs | Sequential slot allocation per Solidity rules
EIP standards | ERC-1967 storage slots, ERC-1822 proxiable UUID | Same
Context detection | logic_flag boolean in a unique storage slot (no immutable support) | address(this) stored as immutable
Initialization | Two-step: constructor sets logic_flag, then set_version() via proxy | Constructor + initializer via proxy
Reactivation | WASM contracts must be reactivated every 365 days or after a Stylus protocol upgrade | Not applicable
Existing Solidity contracts can upgrade to a Stylus (Rust) implementation via proxy patterns. The #[storage] macro lays out fields in the EVM state trie identically to Solidity, so storage slots line up when type definitions match.
OpenZeppelin Contracts for Stylus provides three proxy patterns:
| Pattern | Key types | Best for |
|---|---|---|
| UUPS | UUPSUpgradeable, IErc1822Proxiable, Erc1967Proxy | Most projects — upgrade logic in the implementation, lighter proxy |
| Beacon | BeaconProxy, UpgradeableBeacon | Multiple proxies sharing one implementation — updating the beacon upgrades all proxies atomically |
| Basic Proxy | Erc1967Proxy, |
The implementation contract composes UUPSUpgradeable in its #[storage] struct alongside access control (e.g., Ownable). Integration requires:
UUPSUpgradeable (and access control) as fields in the #[storage] structself.uups.constructor() and initialize access control in the constructorinitialize calling self.uups.set_version() — invoked via proxy after deploymentIUUPSUpgradeable — upgrade_to_and_call guarded by access control, upgrade_interface_version delegating to self.uupsIErc1822Proxiable — delegating to The proxy contract is a thin Erc1967Proxy with a constructor that takes the implementation address and initialization data, and a #[fallback] handler that delegates all calls.
Deploy the proxy with set_version as the initialization call data. Use cargo stylus deploy or a deployer contract. The initialization data is the ABI-encoded setVersion call:
let data = MyContractAbi::setVersionCall {}.abi_encode();
// Pass `data` as the proxy constructor's second argument at deployment time.
Multiple BeaconProxy contracts point to a single UpgradeableBeacon that stores the current implementation address. Updating the beacon upgrades all proxies in one transaction.
Stylus does not support the immutable keyword. Instead of storing __self = address(this), UUPSUpgradeable uses a logic_flag boolean in a unique storage slot:
logic_flag = true in its own storage.delegatecall), the proxy's storage does not contain this flag, so it reads as false.only_proxy() checks this flag to ensure upgrade functions can only be called through the proxy, not directly on the implementation.only_proxy() also verifies that the ERC-1967 implementation slot is non-zero and that the proxy-stored version matches the implementation's VERSION_NUMBER.
Examples: See the
examples/directory of the rust-contracts-stylus repository for full working integration examples of UUPS, Beacon, and related patterns.
Upgrade functions must be guarded with access control. OpenZeppelin's Stylus contracts do not embed access control into the upgrade logic itself — you must add it in upgrade_to_and_call:
fn upgrade_to_and_call(&mut self, new_implementation: Address, data: Bytes) -> Result<(), Vec<u8>> {
self.ownable.only_owner()?; // or any access control check
self.uups.upgrade_to_and_call(new_implementation, data)?;
Ok(())
}
Common options:
Stylus #[storage] fields are laid out in the EVM state trie identically to Solidity. The same storage layout rules apply when upgrading:
One difference from Solidity: nested structs in Stylus #[storage] (e.g., composing Erc20, Ownable, UUPSUpgradeable as fields) are laid out with each nested struct starting at its own deterministic slot. This is consistent with regular struct nesting in Solidity, but not with Solidity's inheritance-based flat layout where all inherited variables share a single sequential slot range.
logic_flag and any implementation-only state. It runs once at implementation deployment.set_version() must be called via the proxy (during deployment or via upgrade_to_and_call) to write the VERSION_NUMBER into the proxy's storage.set_version() in it.The UUPS implementation enforces three safety checks:
upgrade_to_and_call (e.g., self.ownable.only_owner())only_proxy() reverts if the call is not via delegatecallproxiable_uuid() must return the ERC-1967 implementation slot, confirming UUPS compatibilityStylus WASM contracts must be reactivated once per year (365 days) or after any Stylus protocol upgrade. Reactivation can be done using cargo-stylus or the ArbWasm precompile. If a contract is not reactivated, it becomes uncallable. This is orthogonal to proxy upgrades but must be factored into maintenance planning.
Before upgrading a production contract:
upgrade_to_and_call, and verify that all existing state reads correctlyupgrade_to_and_callVERSION_NUMBER is incremented in the new implementationWeekly Installs
67
Repository
GitHub Stars
159
First Seen
Mar 5, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
opencode65
github-copilot64
codex64
cline64
kimi-cli64
gemini-cli64
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
122,000 周安装
Metabase 前端分析事件指南:Snowplow 事件追踪与 TypeScript 类型定义
286 周安装
DaleStudy技能创建指南:skill-creator使用教程与SEO优化
275 周安装
Android Gradle构建逻辑优化:使用约定插件与版本目录提升项目可维护性
278 周安装
数据库模式探索技能:自动分析表结构、关系与示例数据,提升SQL查询效率
287 周安装
使用 Vercel Geist 设计系统创建 Remotion 视频 - 深色主题动画制作指南
275 周安装
Seedance 2.0 视频提示词生成器 | AI视频生成 | 字节跳动即梦平台多模态创作工具
282 周安装
Erc1967Utils| Low-level building block for custom proxy patterns |
proxiable_uuidself.uups