-
Notifications
You must be signed in to change notification settings - Fork 316
/
upgradeable.cairo
48 lines (42 loc) · 1.39 KB
/
upgradeable.cairo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts for Cairo v0.11.0 (upgrades/upgradeable.cairo)
/// # Upgradeable Component
///
/// The Upgradeable component provides a mechanism to make a contract upgradeable.
#[starknet::component]
mod UpgradeableComponent {
use starknet::ClassHash;
use starknet::SyscallResultTrait;
#[storage]
struct Storage {}
#[event]
#[derive(Drop, PartialEq, starknet::Event)]
enum Event {
Upgraded: Upgraded
}
/// Emitted when the contract is upgraded.
#[derive(Drop, PartialEq, starknet::Event)]
struct Upgraded {
class_hash: ClassHash
}
mod Errors {
const INVALID_CLASS: felt252 = 'Class hash cannot be zero';
}
#[generate_trait]
impl InternalImpl<
TContractState, +HasComponent<TContractState>
> of InternalTrait<TContractState> {
/// Replaces the contract's class hash with `new_class_hash`.
///
/// Requirements:
///
/// - `new_class_hash` is not the zero address.
///
/// Emits an `Upgraded` event.
fn _upgrade(ref self: ComponentState<TContractState>, new_class_hash: ClassHash) {
assert(!new_class_hash.is_zero(), Errors::INVALID_CLASS);
starknet::replace_class_syscall(new_class_hash).unwrap_syscall();
self.emit(Upgraded { class_hash: new_class_hash });
}
}
}