Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add upgradeability #36

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/appchain.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ mod appchain {
OwnableComponent as ownable_cpt, OwnableComponent::InternalTrait as OwnableInternal,
interface::IOwnable
};
use openzeppelin::upgrades::{
UpgradeableComponent as upgradeable_cpt,
UpgradeableComponent::InternalTrait as UpgradeableInternal, interface::IUpgradeable
};
use openzeppelin::security::reentrancyguard::{
ReentrancyGuardComponent,
ReentrancyGuardComponent::InternalTrait as InternalReentrancyGuardImpl
Expand All @@ -37,13 +41,14 @@ mod appchain {
use piltover::snos_output;
use piltover::state::component::state_cpt::HasComponent;
use piltover::state::{state_cpt, state_cpt::InternalTrait as StateInternal, IState};
use starknet::ContractAddress;
use starknet::{ContractAddress, ClassHash};
use super::errors;

/// The default cancellation delay of 5 days.
const CANCELLATION_DELAY_SECS: u64 = 432000;

component!(path: ownable_cpt, storage: ownable, event: OwnableEvent);
component!(path: upgradeable_cpt, storage: upgradeable, event: UpgradeableEvent);
component!(path: config_cpt, storage: config, event: ConfigEvent);
component!(path: messaging_cpt, storage: messaging, event: MessagingEvent);
component!(path: state_cpt, storage: state, event: StateEvent);
Expand All @@ -63,6 +68,8 @@ mod appchain {
#[substorage(v0)]
ownable: ownable_cpt::Storage,
#[substorage(v0)]
upgradeable: upgradeable_cpt::Storage,
#[substorage(v0)]
config: config_cpt::Storage,
#[substorage(v0)]
messaging: messaging_cpt::Storage,
Expand All @@ -78,6 +85,8 @@ mod appchain {
#[flat]
OwnableEvent: ownable_cpt::Event,
#[flat]
UpgradeableEvent: upgradeable_cpt::Event,
#[flat]
ConfigEvent: config_cpt::Event,
#[flat]
MessagingEvent: messaging_cpt::Event,
Expand Down Expand Up @@ -197,4 +206,15 @@ mod appchain {
);
}
}

#[abi(embed_v0)]
impl UpgradeableImpl of IUpgradeable<ContractState> {
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
// This function can only be called by the owner
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment that could be removed as code is self-explanatory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

self.ownable.assert_only_owner();

// Replace the class hash upgrading the contract
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, comment could be removed as very self-explanatory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

self.upgradeable._upgrade(new_class_hash);
}
}
}