generated from keep-starknet-strange/alexandria
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add upgradable component (#431)
* feat: add upgradable component * dev: change updradable to updradeable + change updatable to upgradeable * dev: change upgratable to upgradeable * dev: minor changes on casing * dev: rename function parameter to `new_class_hash` from `class_hash` for `upgrade_contract` * dev: remove unused code + fix casing * dev: fix casing * dev: fix casing --------- Co-authored-by: Harsh Bajpai <[email protected]>
- Loading branch information
Showing
4 changed files
with
157 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,39 @@ | ||
// TODO | ||
use starknet::{replace_class_syscall, ClassHash}; | ||
|
||
#[starknet::interface] | ||
trait IUpgradeable<TContractState> { | ||
fn upgrade_contract(ref self: TContractState, new_class_hash: ClassHash); | ||
} | ||
|
||
|
||
#[starknet::component] | ||
mod upgradeable_component { | ||
use starknet::ClassHash; | ||
use starknet::info::get_caller_address; | ||
|
||
#[storage] | ||
struct Storage {} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
ContractUpgraded: ContractUpgraded | ||
} | ||
|
||
#[derive(Drop, starknet::Event)] | ||
struct ContractUpgraded { | ||
new_class_hash: ClassHash | ||
} | ||
|
||
#[embeddable_as(Upgradeable)] | ||
impl UpgradeableImpl< | ||
TContractState, +HasComponent<TContractState> | ||
> of super::IUpgradeable<ComponentState<TContractState>> { | ||
fn upgrade_contract( | ||
ref self: ComponentState<TContractState>, new_class_hash: starknet::ClassHash | ||
) { | ||
starknet::replace_class_syscall(new_class_hash); | ||
self.emit(ContractUpgraded { new_class_hash: new_class_hash }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,93 @@ | ||
// TODO | ||
use MockContractUpgradeableV0::HasComponentImpl_upgradeable_component; | ||
use contracts::components::upgradeable::{IUpgradeableDispatcher, IUpgradeableDispatcherTrait}; | ||
use contracts::components::upgradeable::{upgradeable_component}; | ||
use contracts::tests::utils; | ||
use debug::PrintTrait; | ||
use serde::Serde; | ||
use starknet::{deploy_syscall, ClassHash, ContractAddress, testing}; | ||
|
||
use upgradeable_component::{UpgradeableImpl}; | ||
|
||
#[starknet::interface] | ||
trait IMockContractUpgradeable<TContractState> { | ||
fn version(self: @TContractState) -> felt252; | ||
} | ||
|
||
#[starknet::contract] | ||
mod MockContractUpgradeableV0 { | ||
use contracts::components::upgradeable::{upgradeable_component}; | ||
use super::IMockContractUpgradeable; | ||
component!(path: upgradeable_component, storage: upgradeable, event: UpgradeableEvent); | ||
|
||
#[abi(embed_v0)] | ||
impl UpgradeableImpl = upgradeable_component::Upgradeable<ContractState>; | ||
|
||
#[storage] | ||
struct Storage { | ||
#[substorage(v0)] | ||
upgradeable: upgradeable_component::Storage | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
UpgradeableEvent: upgradeable_component::Event | ||
} | ||
|
||
#[external(v0)] | ||
impl MockContractUpgradeableImpl of IMockContractUpgradeable<ContractState> { | ||
fn version(self: @ContractState) -> felt252 { | ||
0 | ||
} | ||
} | ||
} | ||
|
||
#[starknet::contract] | ||
mod MockContractUpgradeableV1 { | ||
use contracts::components::upgradeable::{upgradeable_component}; | ||
use super::IMockContractUpgradeable; | ||
component!(path: upgradeable_component, storage: upgradeable, event: upgradeableEvent); | ||
|
||
#[storage] | ||
struct Storage { | ||
#[substorage(v0)] | ||
upgradeable: upgradeable_component::Storage | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
upgradeableEvent: upgradeable_component::Event | ||
} | ||
|
||
#[external(v0)] | ||
impl MockContractUpgradeableImpl of IMockContractUpgradeable<ContractState> { | ||
fn version(self: @ContractState) -> felt252 { | ||
1 | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
#[available_gas(500000)] | ||
fn test_upgradeable_update_contract() { | ||
let (contract_address, _) = deploy_syscall( | ||
MockContractUpgradeableV0::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false | ||
) | ||
.unwrap(); | ||
|
||
let version = IMockContractUpgradeableDispatcher { contract_address: contract_address } | ||
.version(); | ||
|
||
assert(version == 0, 'version is not 0'); | ||
|
||
let mut call_data: Array<felt252> = array![]; | ||
|
||
let new_class_hash: ClassHash = MockContractUpgradeableV1::TEST_CLASS_HASH.try_into().unwrap(); | ||
|
||
IUpgradeableDispatcher { contract_address: contract_address }.upgrade_contract(new_class_hash); | ||
|
||
let version = IMockContractUpgradeableDispatcher { contract_address: contract_address } | ||
.version(); | ||
assert(version == 1, 'version is not 1'); | ||
} |