with_components
这个宏简化了向合约添加一组组件的语法。它:
-
将相应的组件导入到合约中。
-
添加相应的
component!
宏条目。 -
将每个组件的存储条目添加到 Storage 结构体中。
-
将每个组件的事件条目添加到 Event 结构体中,如果缺少该结构体则创建它。
-
将相应的内部实现引入作用域。
-
为每个特定组件提供一些诊断信息,以帮助开发者避免常见错误。
警告:由于宏不公开任何外部实现,开发者必须确保明确指定合约所需的实现。
安全性考虑
该宏旨在简单有效,同时又难以被滥用。因此,它提供的功能有限,可能导致合约以意想不到的方式运行的内容必须由开发者明确指定。它不指定外部实现,因此合约不会发现自己在未经开发者知情的情况下暴露了外部函数的情况。它将内部实现引入作用域,因此这些函数默认可用,但如果未使用它们,则不会对合约的行为产生任何影响。
用法
以下是使用该宏时具有多个组件的合约的外观。
#[with_components(Account, SRC5, SRC9, Upgradeable)]
#[starknet::contract(account)]
mod OutsideExecutionAccountUpgradeable {
use openzeppelin_upgrades::interface::IUpgradeable;
use starknet::{ClassHash, ContractAddress};
// External
#[abi(embed_v0)]
impl AccountMixinImpl = AccountComponent::AccountMixinImpl<ContractState>;
#[abi(embed_v0)]
impl OutsideExecutionV2Impl =
SRC9Component::OutsideExecutionV2Impl<ContractState>;
#[storage]
struct Storage {}
#[constructor]
fn constructor(ref self: ContractState, public_key: felt252) {
self.account.initializer(public_key);
self.src9.initializer();
}
#[abi(embed_v0)]
impl UpgradeableImpl of IUpgradeable<ContractState> {
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
self.account.assert_only_self();
self.upgradeable.upgrade(new_class_hash);
}
}
}
这是使用常规语法时,同一合约的外观。
#[starknet::contract(account)]
mod OutsideExecutionAccountUpgradeable {
use openzeppelin::account::AccountComponent;
use openzeppelin::account::extensions::SRC9Component;
use openzeppelin::introspection::src5::SRC5Component;
use openzeppelin::upgrades::UpgradeableComponent;
use openzeppelin::upgrades::interface::IUpgradeable;
use starknet::ClassHash;
component!(path: AccountComponent, storage: account, event: AccountEvent);
component!(path: SRC5Component, storage: src5, event: SRC5Event);
component!(path: SRC9Component, storage: src9, event: SRC9Event);
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);
// External
#[abi(embed_v0)]
impl AccountMixinImpl = AccountComponent::AccountMixinImpl<ContractState>;
#[abi(embed_v0)]
impl OutsideExecutionV2Impl =
SRC9Component::OutsideExecutionV2Impl<ContractState>;
// Internal
impl AccountInternalImpl = AccountComponent::InternalImpl<ContractState>;
impl OutsideExecutionInternalImpl = SRC9Component::InternalImpl<ContractState>;
impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;
#[storage]
struct Storage {
#[substorage(v0)]
account: AccountComponent::Storage,
#[substorage(v0)]
src5: SRC5Component::Storage,
#[substorage(v0)]
src9: SRC9Component::Storage,
#[substorage(v0)]
upgradeable: UpgradeableComponent::Storage,
}
#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
AccountEvent: AccountComponent::Event,
#[flat]
SRC5Event: SRC5Component::Event,
#[flat]
SRC9Event: SRC9Component::Event,
#[flat]
UpgradeableEvent: UpgradeableComponent::Event,
}
#[constructor]
fn constructor(ref self: ContractState, public_key: felt252) {
self.account.initializer(public_key);
self.src9.initializer();
}
#[abi(embed_v0)]
impl UpgradeableImpl of IUpgradeable<ContractState> {
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
self.account.assert_only_self();
self.upgradeable.upgrade(new_class_hash);
}
}
}