generated from keep-starknet-strange/alexandria
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Harsh Bajpai
authored and
Harsh Bajpai
committed
Oct 17, 2023
1 parent
772d2e0
commit 4879b20
Showing
6 changed files
with
167 additions
and
9 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,2 +1,2 @@ | ||
mod ownable; | ||
mod upgradeable; | ||
mod upgradable; |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use starknet::{replace_class_syscall, ClassHash}; | ||
|
||
#[starknet::interface] | ||
trait IUpgradable<TContractState> { | ||
fn upgrade_contract(ref self: TContractState, class_hash: ClassHash); | ||
} | ||
|
||
|
||
#[starknet::component] | ||
mod upgradable_component { | ||
use starknet::ClassHash; | ||
use starknet::info::get_caller_address; | ||
|
||
#[storage] | ||
struct Storage {} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
ContractUpgrated: ContractUpgrated | ||
} | ||
|
||
#[derive(Drop, starknet::Event)] | ||
struct ContractUpgrated { | ||
new_class_hash: ClassHash | ||
} | ||
|
||
#[embeddable_as(UpgradableImpl)] | ||
impl Upgradable< | ||
TContractState, +HasComponent<TContractState> | ||
> of super::IUpgradable<ComponentState<TContractState>> { | ||
fn upgrade_contract( | ||
ref self: ComponentState<TContractState>, class_hash: starknet::ClassHash | ||
) { | ||
starknet::replace_class_syscall(class_hash); | ||
self.emit(ContractUpgrated { new_class_hash: class_hash }); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,100 @@ | ||
// TODO | ||
use MockContractUpdatableV0::HasComponentImpl_upgradable_component; | ||
use contracts::components::upgradable::{IUpgradableDispatcher, IUpgradableDispatcherTrait}; | ||
use contracts::components::upgradable::{upgradable_component}; | ||
use contracts::tests::utils; | ||
use debug::PrintTrait; | ||
use serde::Serde; | ||
use starknet::{deploy_syscall, ClassHash, ContractAddress, testing}; | ||
|
||
use upgradable_component::{UpgradableImpl}; | ||
|
||
#[starknet::interface] | ||
trait IMockContractUpdatable<TContractState> { | ||
fn version(self: @TContractState) -> felt252; | ||
} | ||
|
||
#[starknet::contract] | ||
mod MockContractUpdatableV0 { | ||
use contracts::components::upgradable::{upgradable_component}; | ||
use super::IMockContractUpdatable; | ||
component!(path: upgradable_component, storage: upgradable, event: UpgradableEvent); | ||
|
||
#[abi(embed_v0)] | ||
impl UpgradableImpl = upgradable_component::UpgradableImpl<ContractState>; | ||
|
||
#[storage] | ||
struct Storage { | ||
#[substorage(v0)] | ||
upgradable: upgradable_component::Storage | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
UpgradableEvent: upgradable_component::Event | ||
} | ||
|
||
#[external(v0)] | ||
impl MockContractUpdatableImpl of IMockContractUpdatable<ContractState> { | ||
fn version(self: @ContractState) -> felt252 { | ||
0 | ||
} | ||
} | ||
} | ||
|
||
type TestingState = upgradable_component::ComponentState<MockContractUpdatableV0::ContractState>; | ||
|
||
impl TestingStateDefault of Default<TestingState> { | ||
fn default() -> TestingState { | ||
upgradable_component::component_state_for_testing() | ||
} | ||
} | ||
|
||
#[test] | ||
#[available_gas(500000)] | ||
fn test_upgradable_update_contract() { | ||
let (contract_address, _) = deploy_syscall( | ||
MockContractUpdatableV0::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false | ||
) | ||
.unwrap(); | ||
|
||
let version = IMockContractUpdatableDispatcher { contract_address: contract_address }.version(); | ||
|
||
assert(version == 0, 'version is not 0'); | ||
|
||
let mut call_data: Array<felt252> = array![]; | ||
|
||
let new_class_hash: ClassHash = MockContractUpdatableV1::TEST_CLASS_HASH.try_into().unwrap(); | ||
|
||
IUpgradableDispatcher { contract_address: contract_address }.upgrade_contract(new_class_hash); | ||
|
||
let version = IMockContractUpdatableDispatcher { contract_address: contract_address }.version(); | ||
assert(version == 1, 'version is not 1'); | ||
} | ||
|
||
|
||
#[starknet::contract] | ||
mod MockContractUpdatableV1 { | ||
use contracts::components::upgradable::{upgradable_component}; | ||
use super::IMockContractUpdatable; | ||
component!(path: upgradable_component, storage: upgradable, event: UpgradableEvent); | ||
|
||
#[storage] | ||
struct Storage { | ||
#[substorage(v0)] | ||
upgradable: upgradable_component::Storage | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
UpgradableEvent: upgradable_component::Event | ||
} | ||
|
||
#[external(v0)] | ||
impl MockContractUpdatableImpl of IMockContractUpdatable<ContractState> { | ||
fn version(self: @ContractState) -> felt252 { | ||
1 | ||
} | ||
} | ||
} |