Skip to content

Commit

Permalink
feat: make it upgradeable
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed Nov 1, 2024
1 parent 80dd830 commit a37b41a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2024_07"

[dependencies]
starknet = "2.8.4"
openzeppelin = "0.18.0"
utils = { git = "https://github.com/keep-starknet-strange/raito.git", rev = "02a13045b7074ae2b3247431cd91f1ad76263fb2" }

[dev-dependencies]
Expand All @@ -21,3 +22,6 @@ casm-add-pythonic-hints = true
[lib]
sierra = true
casm = false

[cairo]
sierra-replace-ids = true
49 changes: 44 additions & 5 deletions src/utu_relay.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,67 @@ pub mod UtuRelay {
utils::digest::DigestStore,
interfaces::{IUtuRelay, BlockStatus, HeightProof, BlockStatusTrait}
};
use starknet::storage::{
StorageMapReadAccess, StoragePointerReadAccess, StoragePointerWriteAccess, StoragePathEntry,
Map
use starknet::{
ClassHash, ContractAddress,
storage::{
StorageMapReadAccess, StoragePointerReadAccess, StoragePointerWriteAccess,
StoragePathEntry, Map
}
};
use utils::hash::Digest;
use core::num::traits::zero::Zero;
use openzeppelin::{access::ownable::OwnableComponent, upgrades::UpgradeableComponent,};
use openzeppelin_upgrades::interface::IUpgradeable;

component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);

// add an owner
#[abi(embed_v0)]
impl OwnableTwoStepMixinImpl =
OwnableComponent::OwnableMixinImpl<ContractState>;
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;

// make it upgradable
impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;

#[storage]
struct Storage {
// This is an acyclic graph which contains all blocks registered, including those from forks
blocks: Map<Digest, BlockStatus>,
// This is a mapping of each chain height to a block from the strongest chain registered
chain: Map<u64, Digest>,
// Components
#[substorage(v0)]
ownable: OwnableComponent::Storage,
#[substorage(v0)]
upgradeable: UpgradeableComponent::Storage
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {}
enum Event {
#[flat]
OwnableEvent: OwnableComponent::Event,
#[flat]
UpgradeableEvent: UpgradeableComponent::Event
}

#[constructor]
fn constructor(ref self: ContractState) {}
fn constructor(ref self: ContractState, owner: ContractAddress) {
self.ownable.initializer(owner);
}

#[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
self.ownable.assert_only_owner();

// Replace the class hash upgrading the contract
self.upgradeable.upgrade(new_class_hash);
}
}

#[abi(embed_v0)]
impl UtuRelayImpl of IUtuRelay<ContractState> {
Expand Down

0 comments on commit a37b41a

Please sign in to comment.